This commit is contained in:
jeena 2013-12-23 22:36:42 +01:00
commit caa3945869
19 changed files with 490 additions and 397 deletions

View file

@ -72,6 +72,10 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
this.me.update(); this.me.update();
} }
for (var i = 0; i < this.gameObjects.animated.length; i++) {
this.gameObjects.animated[i].render();
}
this.view.render(); this.view.render();
this.stats.end(); this.stats.end();
@ -123,7 +127,6 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
} }
GameController.prototype.onJoinMe = function (playerId) { GameController.prototype.onJoinMe = function (playerId) {
//this.onSpawnPlayer(options);
this.me = this.players[playerId]; this.me = this.players[playerId];
this.me.setPlayerController(new PlayerController(this.me)); this.me.setPlayerController(new PlayerController(this.me));
this.view.setMe(this.me); this.view.setMe(this.me);
@ -136,15 +139,11 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
var player = this.players[playerId]; var player = this.players[playerId];
player.spawn(x, y); player.spawn(x, y);
this.gameObjects.animated.push(player.getDoll());
// add to view controller
this.view.addPlayer(player);
} }
GameController.prototype.loadLevel = function (path) { GameController.prototype.loadLevel = function (path) {
Parent.prototype.loadLevel.call(this, path); Parent.prototype.loadLevel.call(this, path);
var tiles = this.level.levelObject.tiles;
this.view.loadMeshes(tiles);
} }
return GameController; return GameController;

View file

@ -0,0 +1,31 @@
define([
"Game/Core/GameObjects/GameObject",
"Lib/Utilities/Exception"
],
function (Parent, Exception) {
function GameObject(physicsEngine) {
Parent.call(this, physicsEngine);
this.createMesh();
this.render();
}
GameObject.prototype = Object.create(Parent.prototype);
GameObject.prototype.destroy = function() {
// view ...
Parent.prototype.destroy.call(this);
};
GameObject.prototype.render = function() {
throw new Exception('Abstract method GameObject.render not overwritten');
}
GameObject.prototype.createMesh = function() {
throw new Exception('Abstract method GameObject.createMesh not overwritten');
};
return GameObject;
});

View file

@ -0,0 +1,53 @@
define([
"Game/Core/GameObjects/Tile",
"Game/Config/Settings",
"Game/Core/NotificationCenter"
],
function (Parent, Settings, NotificationCenter) {
function Tile(physicsEngine, options) {
Parent.call(this, physicsEngine, options);
}
Tile.prototype = Object.create(Parent.prototype);
Tile.prototype.createMesh = function() {
var self = this;
var material = "Stones";
var imgPath = Settings.GRAPHICS_PATH
+ Settings.GRAPHICS_SUBPATH_TILES
+ material + '/'
+ this.options.s + ''
+ (this.options.r || 0) + '.gif';
var callback = function(mesh) {
self.mesh = mesh;
NotificationCenter.trigger("view/addMesh", mesh);
}
NotificationCenter.trigger("view/createMesh",
Settings.TILE_SIZE,
Settings.TILE_SIZE,
0,
0,
imgPath,
callback
);
};
Tile.prototype.render = function() {
NotificationCenter.trigger("view/updateMesh",
this.mesh,
{
x: this.options.x * Settings.TILE_SIZE,
y: this.options.y * Settings.TILE_SIZE,
}
);
}
return Tile;
});

View file

@ -1,9 +1,52 @@
define([ define([
"Game/Core/Physics/Doll" "Game/Core/Physics/Doll",
"Game/Config/Settings",
"Game/Core/NotificationCenter"
], ],
function(Parent) { function (Parent, Settings, NotificationCenter) {
return Parent; function Doll(physicsEngine, playerId) {
Parent.call(this, physicsEngine, playerId);
this.height = 36;
}
Doll.prototype = Object.create(Parent.prototype);
Doll.prototype.createMesh = function() {
var self = this;
var imgPath = Settings.GRAPHICS_PATH
+ Settings.GRAPHICS_SUBPATH_CHARACTERS
+ 'Chuck' + '/'
+ 'chuck.png';
var callback = function(mesh) {
self.mesh = mesh;
NotificationCenter.trigger("view/addMesh", mesh);
}
NotificationCenter.trigger("view/createMesh",
10,
36,
0,
0,
imgPath,
callback
);
};
Doll.prototype.render = function() {
NotificationCenter.trigger("view/updateMesh",
this.mesh,
{
x: this.body.GetPosition().x * Settings.RATIO + 4,
y: (this.body.GetPosition().y * Settings.RATIO) - 29
}
);
}
return Doll;
}); });

View file

@ -1,14 +1,19 @@
define([ define([
"Game/Client/View/DomController", "Game/Client/View/DomController",
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Utilities/Exception" "Lib/Utilities/Exception",
"Game/Core/NotificationCenter"
], ],
function (DomController, Settings, Exception) { function (DomController, Settings, Exception, NotificationCenter) {
function AbstractView () { function AbstractView () {
this.me = null; this.me = null;
this.canvas = null; this.canvas = null;
NotificationCenter.on("view/createMesh", this.createMesh, this);
NotificationCenter.on("view/addMesh", this.addMesh, this);
NotificationCenter.on("view/updateMesh", this.updateMesh, this);
} }
AbstractView.prototype.isWebGlEnabled = function () { AbstractView.prototype.isWebGlEnabled = function () {
@ -45,6 +50,14 @@ function (DomController, Settings, Exception) {
throw new Exception('Abstract Function createMesh not overwritten '); throw new Exception('Abstract Function createMesh not overwritten ');
} }
AbstractView.prototype.addMesh = function(mesh) {
throw new Exception('Abstract Function addMesh not overwritten ');
};
AbstractView.prototype.updateMesh = function(mesh, options) {
throw new Exception('Abstract Function updateMesh not overwritten ');
};
AbstractView.prototype.setMe = function(player) { AbstractView.prototype.setMe = function(player) {
this.me = player; this.me = player;
}; };

View file

@ -35,81 +35,40 @@ function (Parent, DomController, PIXI, Settings) {
this.setCanvas(this.renderer.view); this.setCanvas(this.renderer.view);
} }
PixiView.prototype.loadMeshes = function(objects) {
var self = this;
for (var i = 0; i < objects.length; i++) {
(function() {
var o = objects[i];
var x = o.x * Settings.TILE_SIZE;
var y = o.y * Settings.TILE_SIZE;
var r = o.r ? o.r : 0;
var rad = 0.5 * Math.PI * -r;
var material = self.tileAtPositionExists(objects, o.x, o.y -1) ? "Soil" : "GrassSoil";
var callback = function(mesh) {
self.container.addChild(mesh);
//console.log("img height:", mesh.material.map.image.height);
//mesh.rotation.z = rad;
};
self.createMesh(Settings.TILE_SIZE, Settings.TILE_SIZE, x, y, 'static/img/Tiles/' + material + '/' + o.s + '' + o.r + '.gif', callback, true);
})();
};
};
PixiView.prototype.render = function () { PixiView.prototype.render = function () {
if(this.me) { if(this.me) {
var pos = this.calculateCameraPosition(); var pos = this.calculateCameraPosition();
this.setCameraPosition(pos.x, pos.y); this.setCameraPosition(pos.x, pos.y);
} }
for (var i = 0; i < this.movableObjects.length; i++) {
var obj = this.movableObjects[i];
var pos = obj.player.getPosition();
obj.mesh.position.x = pos.x * Settings.RATIO + 4 ;
obj.mesh.position.y = pos.y * Settings.RATIO - 34; // weirdly a different magic number as for three
}
this.renderer.render(this.stage); this.renderer.render(this.stage);
} }
PixiView.prototype.createMesh = function (width, height, x, y, imgPath, callback, isTile) { PixiView.prototype.addMesh = function(mesh) {
this.container.addChild(mesh);
};
PixiView.prototype.createMesh = function (width, height, x, y, imgPath, callback) {
var texture = PIXI.Texture.fromImage(imgPath); var texture = PIXI.Texture.fromImage(imgPath);
var mesh;
//if(isTile) { var mesh = new PIXI.Sprite(texture);
// mesh = new PIXI.TilingSprite(texture, width, height);
//} else {
mesh = new PIXI.Sprite(texture);
mesh.width = width; mesh.width = width;
mesh.height = height; mesh.height = height;
//}
mesh.position.x = x; mesh.position.x = x;
mesh.position.y = y; mesh.position.y = y;
callback(mesh); callback(mesh);
} }
PixiView.prototype.addPlayer = function(player) { PixiView.prototype.updateMesh = function(mesh, options) {
var self = this; if (options.x) mesh.position.x = options.x;
var mesh = null; if (options.y) mesh.position.y = options.y;
var pos = player.getPosition(); if (options.rotation) mesh.rotation = options.rotation;
var size = {w:10, h:42}; if (options.xScale) mesh.scale.x = options.xScale;
var callback = function(mesh) { if (options.yScale) mesh.scale.y = options.yScale;
self.container.addChild(mesh); if (options.width) mesh.width = options.width;
self.movableObjects.push({ if (options.height) mesh.height = options.height;
player: player,
mesh: mesh
});
}
this.createMesh(size.w, size.h, pos.x, pos.y, "static/img/Characters/Chuck/chuck.png", callback, false);
};
PixiView.prototype.removPlayer = function(player) {
// nothing
}; };
PixiView.prototype.initCamera = function () { PixiView.prototype.initCamera = function () {

View file

@ -61,7 +61,7 @@ function (Parent, DomController, Three, Settings) {
var material = self.tileAtPositionExists(objects, o.x, o.y -1) ? "Soil" : "GrassSoil"; var material = self.tileAtPositionExists(objects, o.x, o.y -1) ? "Soil" : "GrassSoil";
self.createMesh(Settings.TILE_SIZE, Settings.TILE_SIZE, x, y, 'static/img/Tiles/' + material + '/' + o.s + '' + o.r + '.gif', function(mesh) { self.createMesh(Settings.TILE_SIZE, Settings.TILE_SIZE, x, y, 'static/img/Tiles/' + material + '/' + o.s + '' + o.r + '.gif', function(mesh) {
self.scene.add(mesh); self.addMesh(mesh);
//console.log("img height:", mesh.material.map.image.height); //console.log("img height:", mesh.material.map.image.height);
//mesh.rotation.z = rad; //mesh.rotation.z = rad;
}); });
@ -86,6 +86,10 @@ function (Parent, DomController, Three, Settings) {
this.renderer.render(this.scene, this.camera); this.renderer.render(this.scene, this.camera);
} }
ThreeView.prototype.addMesh = function(mesh) {
this.scene.add(mesh);
};
ThreeView.prototype.createMesh = function (width, height, x, y, imgPath, callback) { ThreeView.prototype.createMesh = function (width, height, x, y, imgPath, callback) {
var mesh; var mesh;
var material = new Three.MeshLambertMaterial({ var material = new Three.MeshLambertMaterial({
@ -101,13 +105,24 @@ function (Parent, DomController, Three, Settings) {
mesh.position.y = y; mesh.position.y = y;
} }
ThreeView.prototype.updateMesh = function(mesh, options) {
if (options.x) mesh.position.x = options.x;
if (options.y) mesh.position.y = options.y;
if (options.rotation) mesh.rotation.z = options.rotation * (Math.PI / 180);
if (options.xScale) mesh.width *= options.xScale;
if (options.yScale) mesh.height *= options.yScale;
if (options.width) mesh.width = options.width;
if (options.height) mesh.height = options.height;
};
ThreeView.prototype.addPlayer = function(player) { ThreeView.prototype.addPlayer = function(player) {
var self = this; var self = this;
var mesh = null; var mesh = null;
var pos = player.getPosition(); var pos = player.getPosition();
var size = {w:10, h:42}; var size = {w:10, h:42};
var callback = function(mesh) { var callback = function(mesh) {
self.scene.add(mesh); self.addMesh(mesh);
self.movableObjects.push({ self.movableObjects.push({
player: player, player: player,
mesh: mesh mesh: mesh

View file

@ -12,8 +12,9 @@ define({
// GRAPHIC PATHS // GRAPHIC PATHS
GRAPHICS_PATH: 'static/img/', GRAPHICS_PATH: 'static/img/',
GRAPHICS_SUBPATH_ITEMS: 'items/', GRAPHICS_SUBPATH_ITEMS: 'Items/',
GRAPHICS_SUBPATH_CHARACTERS: 'characters/', GRAPHICS_SUBPATH_CHARACTERS: 'Characters/',
GRAPHICS_SUBPATH_TILES: 'Tiles/',
RATIO: 35, RATIO: 35,
TILE_SIZE: 15, //15 TILE_SIZE: 15, //15
@ -46,7 +47,7 @@ define({
IS_BROWSER_ENVIRONMENT: typeof window !== 'undefined', IS_BROWSER_ENVIRONMENT: typeof window !== 'undefined',
USE_WEGBL: true, USE_WEGBL: true,
DEBUG_MODE: 0, DEBUG_MODE: 1,
// NETWORKING // NETWORKING
WORLD_UPDATE_BROADCAST_INTERVAL: 70, WORLD_UPDATE_BROADCAST_INTERVAL: 70,

View file

@ -14,38 +14,35 @@ function (Box2D) {
Detector.IDENTIFIER = { Detector.IDENTIFIER = {
TILE: "tile", TILE: "tile",
PLAYER: "player", PLAYER: "player"
PLAYER_HEAD: 'head',
PLAYER_CHEST: 'chest',
PLAYER_LEGS: 'legs',
PLAYER_FOOT_SENSOR: 'footsensor'
} }
Detector.prototype.getListener = function () { Detector.prototype.getListener = function () {
return this.listener; return this.listener;
} }
Detector.prototype.handleStand = function (point, isColliding) { Detector.prototype.onCollisionChange = function (point, isColliding) {
var userDataA = point.GetFixtureA().GetUserData();
var userDataB = point.GetFixtureB().GetUserData();
if (point.GetFixtureA().GetUserData().identifier == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR) { if (userDataA && userDataA.onCollisionChange) {
point.GetFixtureA().GetUserData().player.onFootSensorDetection(isColliding); userDataA.onCollisionChange(isColliding);
} else if (point.GetFixtureB().GetUserData().identifier == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR) { } else if (userDataB && userDataB.onCollisionChange) {
point.GetFixtureB().GetUserData().player.onFootSensorDetection(isColliding); userDataB.onCollisionChange(isColliding);
} }
} }
/** Extension **/ /** Extension **/
Detector.prototype.BeginContact = function (point) { Detector.prototype.BeginContact = function (point) {
this.chuckDetector.handleStand(point, true); this.chuckDetector.onCollisionChange(point, true);
} }
Detector.prototype.PostSolve = function (point, impulse) { Detector.prototype.PostSolve = function (point, impulse) {
//this.chuckDetector.handleStand(point, true);
} }
Detector.prototype.EndContact = function (point) { Detector.prototype.EndContact = function (point) {
this.chuckDetector.handleStand(point, false); this.chuckDetector.onCollisionChange(point, false);
} }
return Detector; return Detector;

View file

@ -8,6 +8,10 @@ function (Engine, Level, Player) {
function GameController (physicsEngine) { function GameController (physicsEngine) {
this.players = {}; this.players = {};
this.gameObjects = {
animated: [],
fixed: []
};
if (! physicsEngine instanceof Engine) { if (! physicsEngine instanceof Engine) {
throw physicsEngine + " is not of type Engine"; throw physicsEngine + " is not of type Engine";
@ -25,7 +29,7 @@ function (Engine, Level, Player) {
this.level.unload(); this.level.unload();
} }
this.level = new Level(path, this.physicsEngine); this.level = new Level(path, this.physicsEngine, this.gameObjects);
this.level.loadLevelInToEngine(); this.level.loadLevelInToEngine();
} }

View file

@ -1,20 +0,0 @@
define([
],
function() {
function GameObject() {
this.body = null;
}
GameObject.prototype.render = function() {
return null;
}
GameObject.prototype.getBody = function() {
return this.body;
};
return GameObject;
});

View file

@ -0,0 +1,29 @@
define([
"Lib/Vendor/Box2D",
"Lib/Utilities/Exception"
],
function (Box2D, Exception) {
function GameObject(physicsEngine) {
var def = this.getBodyDef();
this.body = physicsEngine.getWorld().CreateBody(def);
}
GameObject.prototype.getBodyDef = function() {
throw new Exception('Abstract method GameObject.getBodyDef not overwritten');
};
GameObject.prototype.destroy = function() {
if(this.body instanceof Box2D.Dynamics.b2Body) {
this.body.GetWorld().DestroyBody(this.body);
}
};
GameObject.prototype.getBody = function() {
return this.body;
};
return GameObject;
});

View file

@ -0,0 +1,118 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Vendor/Box2D",
"Game/Config/Settings",
"Game/" + GLOBALS.context + "/Collision/Detector"
],
function (Parent, Box2D, Settings, CollisionDetector) {
function Tile(physicsEngine, options) {
this.options = options;
Parent.call(this, physicsEngine);
this.createPhysicTile(this.options);
}
Tile.prototype = Object.create(Parent.prototype);
Tile.prototype.getBodyDef = function() {
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
bodyDef.position.x = this.options.x * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.position.y = this.options.y * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.angle = (this.options.r || 0) * 90 * Math.PI / 180;
return bodyDef;
}
Tile.prototype.createPhysicTile = function (tile) {
var vertices = this.createVertices(tile);
var tileShape = new Box2D.Collision.Shapes.b2PolygonShape();
tileShape.SetAsArray(vertices, vertices.length);
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.shape = tileShape;
fixtureDef.density = 0;
fixtureDef.friction = Settings.TILE_FRICTION;
fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE;
this.body.CreateFixture(fixtureDef);
}
Tile.prototype.createVertices = function (tile) {
var vs = [];
switch(tile.s) {
case 1:
this.addVec(vs, -1, -1); // o o o
this.addVec(vs, 1, -1); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 2:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 1); // o o
this.addVec(vs, -1, 1); // o o o
break;
case 3:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 4:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 5:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o
this.addVec(vs, 0, 1); // o o
break;
case 6:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1); // o o o
this.addVec(vs, -1, 0);
break;
case 7:
this.addVec(vs, -1, 0); //
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 8:
this.addVec(vs, -1, -1); // o o
this.addVec(vs, 0, -1); // o o o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1);
this.addVec(vs, -1, 1);
break;
default:
break;
}
return vs;
}
Tile.prototype.mkArg = function (multiplier) {
return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier;
}
Tile.prototype.addVec = function (vs, m1, m2) {
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2)));
}
return Tile;
});

View file

@ -1,20 +1,22 @@
define([ define([
"Game/Config/Settings", "Game/Config/Settings",
"Lib/Vendor/Box2D", "Lib/Vendor/Box2D",
"Game/" + GLOBALS.context + "/Collision/Detector" "Game/" + GLOBALS.context + "/Collision/Detector",
"Game/" + GLOBALS.context + "/GameObjects/Tile"
], function (Settings, Box2D, CollisionDetector) { ], function (Settings, Box2D, CollisionDetector, Tile) {
// Public // Public
function Level (path, engine) { function Level (path, engine, gameObjects) {
this.path = path; this.path = path;
this.engine = engine; this.engine = engine;
this.levelObject = null; this.levelObject = null;
this.gameObjects = gameObjects;
} }
Level.prototype.loadLevelInToEngine = function () { Level.prototype.loadLevelInToEngine = function () {
this.loadLevelObjectFromPath(this.path); this.loadLevelObjectFromPath(this.path);
this.createPhysicTiles(); this.createTiles();
} }
Level.prototype.unload = function () { Level.prototype.unload = function () {
@ -24,114 +26,18 @@ define([
// Private // Private
Level.prototype.createPhysicTiles = function () { Level.prototype.createTiles = function () {
if (!this.levelObject || !this.levelObject.tiles || this.levelObject.tiles.length < 1) { if (!this.levelObject || !this.levelObject.tiles || this.levelObject.tiles.length < 1) {
throw "Level: Can't create physic tiles, no tiles found"; throw "Level: Can't create physic tiles, no tiles found";
} }
var tiles = this.levelObject.tiles; var tiles = this.levelObject.tiles;
for (var i = tiles.length - 1; i >= 0; i--) {
this.createPhysicTile(tiles[i]); for (var i = 0; i < tiles.length; i++) {
this.gameObjects.fixed.push(new Tile(this.engine, tiles[i]));
} }
} }
Level.prototype.createPhysicTile = function (tile) {
tile.r = tile.r || 0;
var vertices = this.createVertices(tile);
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
bodyDef.position.x = tile.x * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.position.y = tile.y * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.angle = tile.r * 90 * Math.PI / 180;
var tileShape = new Box2D.Collision.Shapes.b2PolygonShape();
tileShape.SetAsArray(vertices, vertices.length);
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.shape = tileShape;
fixtureDef.density = 0;
fixtureDef.friction = Settings.TILE_FRICTION;
fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE;
this.engine.createBody(bodyDef).CreateFixture(fixtureDef);
}
Level.prototype.createVertices = function (tile) {
var vs = [];
switch(tile.s) {
case 1:
this.addVec(vs, -1, -1); // o o o
this.addVec(vs, 1, -1); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 2:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 1); // o o
this.addVec(vs, -1, 1); // o o o
break;
case 3:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 4:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 5:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o
this.addVec(vs, 0, 1); // o o
break;
case 6:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1); // o o o
this.addVec(vs, -1, 0);
break;
case 7:
this.addVec(vs, -1, 0); //
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 8:
this.addVec(vs, -1, -1); // o o
this.addVec(vs, 0, -1); // o o o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1);
this.addVec(vs, -1, 1);
break;
default:
break;
}
return vs;
}
Level.prototype.mkArg = function (multiplier) {
return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier;
}
Level.prototype.addVec = function (vs, m1, m2) {
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2)));
}
Level.prototype.loadLevelObjectFromPath = function (path) { Level.prototype.loadLevelObjectFromPath = function (path) {
// TODO: load JSON levelObject from path // TODO: load JSON levelObject from path

View file

@ -1,32 +1,42 @@
define([ define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Vendor/Box2D", "Lib/Vendor/Box2D",
"Game/Config/Settings", "Game/Config/Settings",
"Game/" + GLOBALS.context + "/Collision/Detector" "Game/" + GLOBALS.context + "/Collision/Detector"
], ],
function (Box2D, Settings, CollisionDetector) { function (Parent, Box2D, Settings, CollisionDetector) {
function Doll (physicsEngine, player) { function Doll (physicsEngine, playerId) {
this.player = player;
this.physicsEngine = physicsEngine; Parent.call(this, physicsEngine);
this.body;
this.playerId = playerId;
this.standing = false;
this.moveDirection = 0;
this.lookDirection = 0;
this.legs; this.legs;
this.contactPoint;
this.init(this.physicsEngine.getWorld()); this.createFixtures();
this.body.SetActive(false);
} }
Doll.prototype.init = function (world) { Doll.prototype = Object.create(Parent.prototype);
Doll.prototype.getBodyDef = function() {
var bodyDef = new Box2D.Dynamics.b2BodyDef(); var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.position.x = 220 / Settings.RATIO; bodyDef.position.x = 220 / Settings.RATIO;
bodyDef.position.y = 0 / Settings.RATIO; bodyDef.position.y = 0 / Settings.RATIO;
bodyDef.fixedRotation = true; bodyDef.fixedRotation = true;
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING; bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.userData = CollisionDetector.IDENTIFIER.PLAYER + '-' + this.player.id; bodyDef.userData = CollisionDetector.IDENTIFIER.PLAYER + '-' + this.playerId;
this.body = world.CreateBody(bodyDef); return bodyDef;
};
Doll.prototype.createFixtures = function () {
var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.density = Settings.PLAYER_DENSITY; fixtureDef.density = Settings.PLAYER_DENSITY;
@ -38,14 +48,12 @@ function (Box2D, Settings, CollisionDetector) {
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO)); headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO));
fixtureDef.shape = headShape; fixtureDef.shape = headShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.PLAYER_HEAD;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape(); var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO)); bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO));
fixtureDef.shape = bodyShape; fixtureDef.shape = bodyShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.PLAYER_CHEST;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
@ -54,7 +62,6 @@ function (Box2D, Settings, CollisionDetector) {
fixtureDef.shape = legsShape; fixtureDef.shape = legsShape;
fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.PLAYER_LEGS;
this.legs = this.body.CreateFixture(fixtureDef); this.legs = this.body.CreateFixture(fixtureDef);
@ -65,13 +72,10 @@ function (Box2D, Settings, CollisionDetector) {
fixtureDef.isSensor = true; fixtureDef.isSensor = true;
fixtureDef.userData = { fixtureDef.userData = {
identifier: CollisionDetector.IDENTIFIER.PLAYER_FOOT_SENSOR, onCollisionChange: this.onFootSensorDetection.bind(this)
player: this.player
} }
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
this.body.SetActive(false);
} }
Doll.prototype.spawn = function (x, y) { Doll.prototype.spawn = function (x, y) {
@ -79,9 +83,9 @@ function (Box2D, Settings, CollisionDetector) {
this.body.SetActive(true); this.body.SetActive(true);
} }
Doll.prototype.getBody = function () { Doll.prototype.getPosition = function() {
return this.body; return this.body.GetPosition();
} };
Doll.prototype.setFriction = function (friction) { Doll.prototype.setFriction = function (friction) {
if(!friction) friction = -1; if(!friction) friction = -1;
@ -91,7 +95,25 @@ function (Box2D, Settings, CollisionDetector) {
} }
} }
Doll.prototype.move = function (direction, speed) { Doll.prototype.move = function (direction) {
this.moveDirection = direction;
var speed;
switch(true) {
case direction == this.lookDirection && this.isStanding():
speed = Settings.RUN_SPEED;
break;
case !this.isStanding():
speed = Settings.FLY_SPEED;
break;
default:
speed = Settings.WALK_SPEED;
break;
}
this.setFriction(Settings.PLAYER_MOTION_FRICTION); this.setFriction(Settings.PLAYER_MOTION_FRICTION);
this.body.SetAwake(true); this.body.SetAwake(true);
var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y); var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y);
@ -99,29 +121,73 @@ function (Box2D, Settings, CollisionDetector) {
} }
Doll.prototype.stop = function () { Doll.prototype.stop = function () {
this.moveDirection = 0;
this.setFriction(Settings.PLAYER_FRICTION); this.setFriction(Settings.PLAYER_FRICTION);
} }
Doll.prototype.jump = function () { Doll.prototype.jump = function () {
this.body.SetAwake(true); if (this.isStanding()) {
var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED); this.body.SetAwake(true);
this.body.ApplyImpulse(vector, this.body.GetPosition()); var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED);
this.body.ApplyImpulse(vector, this.body.GetPosition());
// maybe change to a constant force instead of applying of force? this.setStanding(false);
// to prevent higher jumping running uphill, etc. }
} }
/*
Doll.prototype.jumping = function () {
var vector = new Box2D.Common.Math.b2Vec2(0, -0.05);
this.body.ApplyImpulse(vector, this.body.GetPosition());
}
*/
Doll.prototype.destroy = function () { Doll.prototype.destroy = function () {
this.body.GetWorld().DestroyBody(this.body); this.body.GetWorld().DestroyBody(this.body);
} }
Doll.prototype.setStanding = function (isStanding) {
this.standing = isStanding;
}
Doll.prototype.isStanding = function () {
return this.standing;
}
Doll.prototype.lookAt = function(x, y) {
var lastLookDirection = this.lookDirection;
/*
var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
if (x < Settings.STAGE_WIDTH / 2) {
this.mc.scaleX = -1;
this.lookDirection = -1;
degree = (-45 + degree / 2);
this.mc.head.rotation = degree;
} else if (x >= Settings.STAGE_WIDTH / 2) {
this.mc.scaleX = 1;
this.lookDirection = 1;
degree = (45 + -degree / 2) - 90;
this.mc.head.rotation = degree;
}
*/
if(x < 0) {
this.lookDirection = -1;
} else {
this.lookDirection = 1;
}
};
Doll.prototype.onFootSensorDetection = function(isColliding) {
if(isColliding && !(this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding())) {
this.setStanding(true);
}
};
Doll.prototype.update = function() {
if (this.body.GetLinearVelocity().x == 0) {
this.stop();
}
if (!this.body.IsAwake() && !this.isStanding()) {
this.setStanding(true);
}
};
return Doll; return Doll;
}); });

View file

@ -13,7 +13,6 @@ function (Settings, Box2D, CollisionDetector) {
); );
this.ground = null; this.ground = null;
this.lastStep = Date.now(); this.lastStep = Date.now();
console.log(Settings.BOX2D_TIME_STEP)
} }
Engine.prototype.getWorld = function () { Engine.prototype.getWorld = function () {

View file

@ -8,185 +8,47 @@ function (Doll, Settings) {
function Player (id, physicsEngine) { function Player (id, physicsEngine) {
this.physicsEngine = physicsEngine; this.physicsEngine = physicsEngine;
this.playerController = null; this.playerController = null;
this.doll;
this.id = id; this.id = id;
this.standing = false;
this.doll;
this.currentAnimationState = 'stand';
this.moveDirection = 0;
this.lookDirection = 0;
this.isSpawned = false; this.isSpawned = false;
} }
Player.prototype.getDoll = function() {
return this.doll;
};
Player.prototype.spawn = function (x, y) { Player.prototype.spawn = function (x, y) {
this.doll = new Doll(this.physicsEngine, this); this.doll = new Doll(this.physicsEngine, this.id);
this.doll.spawn(x, y); this.doll.spawn(x, y);
this.isSpawned = true; this.isSpawned = true;
} }
Player.prototype.getDoll = function () {
return this.doll;
}
Player.prototype.getBody = function () {
return this.doll.getBody();
}
Player.prototype.getPosition = function () { Player.prototype.getPosition = function () {
return this.getBody().GetPosition(); if(!this.doll) return false;
} return this.doll.getPosition();
Player.prototype.setStanding = function (isStanding) {
var resetStates = ['jump', 'jumploop'];
if (resetStates.indexOf(this.currentAnimationState)>=0 && !this.standing && isStanding) {
this.animate('stand');
}
this.standing = isStanding;
}
Player.prototype.isStanding = function () {
return this.standing;
} }
Player.prototype.move = function (direction) { Player.prototype.move = function (direction) {
this.moveDirection = direction; this.doll.move(direction);
switch(true) {
case direction == this.lookDirection && this.isStanding():
this.doll.move(direction, Settings.RUN_SPEED);
break;
case !this.isStanding():
this.doll.move(direction, Settings.FLY_SPEED);
break;
default:
this.doll.move(direction, Settings.WALK_SPEED);
break;
}
if (this.isStanding()) {
this.animate(this.calculateWalkAnimation());
}
} }
Player.prototype.stop = function () { Player.prototype.stop = function () {
this.moveDirection = 0;
this.doll.stop(); this.doll.stop();
if (this.isWalking() || this.standing) {
this.animate('stand');
}
} }
Player.prototype.jump = function () { Player.prototype.jump = function () {
if (this.isStanding()) { this.doll.jump();
this.doll.jump();
this.animate('jump');
this.setStanding(false);
}
}
Player.prototype.jumping = function () {
if (!this.isStanding()) {
this.doll.jumping();
}
}
Player.prototype.duck = function () {
if (this.standing && !this.isWalking()) {
this.animate('duck');
}
}
Player.prototype.standUp = function () {
if (this.standing) {
this.animate('standup');
}
}
Player.prototype.animate = function (type) {
if (type == this.currentAnimationState) {
return;
}
this.currentAnimationState = type;
}
Player.prototype.calculateWalkAnimation = function () {
// FIXME dont know if this is right
if (this.moveDirection == (this.lookDirection > 0)) {
return 'run';
}
return 'walkback';
} }
Player.prototype.lookAt = function (x, y) { Player.prototype.lookAt = function (x, y) {
//console.log("player looking at ", x, y); if(this.doll) this.doll.lookAt(x, y);
var lastLookDirection = this.lookDirection;
/*
var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
if (x < Settings.STAGE_WIDTH / 2) {
this.mc.scaleX = -1;
this.lookDirection = -1;
degree = (-45 + degree / 2);
this.mc.head.rotation = degree;
} else if (x >= Settings.STAGE_WIDTH / 2) {
this.mc.scaleX = 1;
this.lookDirection = 1;
degree = (45 + -degree / 2) - 90;
this.mc.head.rotation = degree;
}
*/
if(x < 0) {
this.lookDirection = -1;
} else {
this.lookDirection = 1;
}
if (this.lookDirection != lastLookDirection && this.isWalking()) {
this.animate(this.calculateWalkAnimation());
}
}
Player.prototype.isWalking = function () {
var states = ['walk', 'walkback', 'run'];
if (states.indexOf(this.currentAnimationState) >= 0) {
return true;
}
return false;
}
// called by CollisionDetection
Player.prototype.onFootSensorDetection = function (isColliding) {
if(isColliding) {
if(this.doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) {
return;
}
this.setStanding(true);
} else {
// TODO This needs some more thought to it.
// maybe take a look at collision groups for collision detection,
// to group all tiles together
//this.setStanding(false);
//this.animate('jumploop');
}
} }
Player.prototype.update = function () { Player.prototype.update = function () {
//this.mc.head.y = this.mc.head_posmask.y;
if (this.doll.getBody().GetLinearVelocity().x == 0 && this.isWalking()) { if(this.doll) {
this.stop(); this.doll.update();
}
if (!this.doll.getBody().IsAwake() && !this.isStanding()) {
this.setStanding(true);
} }
if(this.playerController) { if(this.playerController) {

View file

@ -0,0 +1,9 @@
define([
"Game/Core/GameObjects/GameObject"
],
function(Parent) {
return Parent;
});

View file

@ -0,0 +1,9 @@
define([
"Game/Core/GameObjects/Tile"
],
function(Parent) {
return Parent;
});