fixed problems with first world update

This commit is contained in:
jeena 2013-12-25 04:35:51 +01:00
parent 283a1ef48b
commit 93e8133c89
13 changed files with 353 additions and 166 deletions

View file

@ -36,25 +36,6 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
document.body.appendChild(this.stats.domElement); document.body.appendChild(this.stats.domElement);
}; };
GameController.prototype.makeMouseJoint = function(p) {
var ground = this.physicsEngine.getGround();
var body = this.me.getBody();
var def = new Box2D.Dynamics.Joints.b2MouseJointDef();
def.bodyA = ground;
def.bodyB = body;
def.target = p;
def.collideConnected = false;
def.maxForce = 100;
def.dampingRatio = 0.99;
this.mouse_joint = this.physicsEngine.world.CreateJoint(def);
body.SetAwake(true);
}
GameController.prototype.destruct = function() { GameController.prototype.destruct = function() {
//destroy box2d world etc. //destroy box2d world etc.
}; };
@ -128,5 +109,13 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
Parent.prototype.loadLevel.call(this, path); Parent.prototype.loadLevel.call(this, path);
} }
GameController.prototype.userLeft = function(user) {
var doll = this.players[user.id].doll;
var i = this.gameObjects.animated.indexOf(doll);
if(i>=0) this.gameObjects.animated.splice(i, 1);
Parent.prototype.userLeft.call(this, user);
}
return GameController; return GameController;
}); });

View file

@ -0,0 +1,53 @@
define([
"Game/Core/GameObjects/Item",
"Game/Config/Settings",
"Game/Core/NotificationCenter"
],
function (Parent, Settings, NotificationCenter) {
function Item(physicsEngine, uid, options) {
Parent.call(this, physicsEngine, uid, options);
}
Item.prototype = Object.create(Parent.prototype);
Item.prototype.createMesh = function() {
var self = this;
var texturePath = Settings.GRAPHICS_PATH
+ Settings.GRAPHICS_SUBPATH_ITEMS
+ this.options.category + '/'
+ this.options.image;
var callback = function(mesh) {
self.mesh = mesh;
NotificationCenter.trigger("view/addMesh", mesh);
}
NotificationCenter.trigger("view/createMesh",
texturePath,
callback,
{
width: this.options.width,
height: this.options.height,
pivot: "mb"
}
);
};
Item.prototype.render = function() {
NotificationCenter.trigger("view/updateMesh",
this.mesh,
{
x: this.body.GetPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO,
rotation: this.body.GetAngle()
}
);
}
return Item;
});

View file

@ -15,10 +15,9 @@ function (Parent, Settings, NotificationCenter) {
Tile.prototype.createMesh = function() { Tile.prototype.createMesh = function() {
var self = this; var self = this;
var material = "Stones";
var texturePath = Settings.GRAPHICS_PATH var texturePath = Settings.GRAPHICS_PATH
+ Settings.GRAPHICS_SUBPATH_TILES + Settings.GRAPHICS_SUBPATH_TILES
+ material + '/' + this.options.m + '/'
+ this.options.s + '' + this.options.s + ''
+ (this.options.r || 0) + '.gif'; + (this.options.r || 0) + '.gif';

View file

@ -63,11 +63,13 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings) {
if (options.spawnedPlayers) { if (options.spawnedPlayers) {
for(var i = 0; i < options.spawnedPlayers.length; i++) { for(var i = 0; i < options.spawnedPlayers.length; i++) {
this.gameController.onSpawnPlayer(options.spawnedPlayers[i]); this.gameController.onSpawnPlayer(options.spawnedPlayers[i]);
console.log("already spawned player, options: ", options.spawnedPlayers[i])
} }
} }
if (options.worldUpdate) {
this.gameController.onWorldUpdate(options.worldUpdate);
}
this.initPing(); this.initPing();
} }

View file

@ -83,26 +83,6 @@ function (Parent, Settings, NotificationCenter, Exception) {
} }
Doll.prototype.render = function() {
if(this.actionState) {
NotificationCenter.trigger("view/updateMesh",
this.animatedMeshes[this.actionState],
{
x: this.body.GetPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO
}
);
NotificationCenter.trigger("view/updateMesh",
this.headMesh,
{
x: this.body.GetPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO - 31
}
)
}
}
Doll.prototype.lookAt = function(x, y) { Doll.prototype.lookAt = function(x, y) {
var oldLookDirection = this.lookDirection; var oldLookDirection = this.lookDirection;
@ -141,6 +121,26 @@ function (Parent, Settings, NotificationCenter, Exception) {
Parent.prototype.destroy.call(this); Parent.prototype.destroy.call(this);
} }
Doll.prototype.render = function() {
if(this.actionState) {
NotificationCenter.trigger("view/updateMesh",
this.animatedMeshes[this.actionState],
{
x: this.body.GetPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO
}
);
NotificationCenter.trigger("view/updateMesh",
this.headMesh,
{
x: this.body.GetPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO - 31
}
)
}
}
return Doll; return Doll;
}); });

View file

@ -104,16 +104,5 @@ function (DomController, Settings, Exception, NotificationCenter) {
throw new Exception('Abstract Function setCameraZoom not overwritten '); throw new Exception('Abstract Function setCameraZoom not overwritten ');
} }
// TODO Move to Level
AbstractView.prototype.tileAtPositionExists = function(objects, x, y) {
for (var i = 0; i < objects.length; i++) {
var o = objects[i];
if(o.x == x && o.y == y) return true;
}
return false;
};
return AbstractView; return AbstractView;
}); });

View file

@ -0,0 +1,46 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Vendor/Box2D",
"Game/Config/Settings"
],
function (Parent, Box2D, Settings) {
function Item(physicsEngine, uid, options) {
this.options = options;
Parent.call(this, physicsEngine, uid);
this.createFixture();
}
Item.prototype = Object.create(Parent.prototype);
Item.prototype.getBodyDef = function() {
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = this.options.x / Settings.RATIO;
bodyDef.position.y = this.options.y / Settings.RATIO;
bodyDef.angle = 0;
return bodyDef;
}
Item.prototype.createFixture = function () {
var itemShape = new Box2D.Collision.Shapes.b2PolygonShape();
var w = this.options.width / 2 / Settings.RATIO;
var h = this.options.height / 2 / Settings.RATIO;
itemShape.SetAsOrientedBox(w, h, new Box2D.Common.Math.b2Vec2(0, -h));
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.shape = itemShape;
fixtureDef.density = Settings.ITEM_DENSITY;
fixtureDef.friction = Settings.ITEM_FRICTION;
fixtureDef.restitution = Settings.ITEM_RESTITUTION;
fixtureDef.isSensor = false;
this.body.CreateFixture(fixtureDef);
}
return Item;
});

View file

@ -1,11 +1,10 @@
define([ define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject", "Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Vendor/Box2D", "Lib/Vendor/Box2D",
"Game/Config/Settings", "Game/Config/Settings"
"Game/" + GLOBALS.context + "/Collision/Detector"
], ],
function (Parent, Box2D, Settings, CollisionDetector) { function (Parent, Box2D, Settings) {
function Tile(physicsEngine, uid, options) { function Tile(physicsEngine, uid, options) {
this.options = options; this.options = options;
@ -37,7 +36,6 @@ function (Parent, Box2D, Settings, CollisionDetector) {
fixtureDef.friction = Settings.TILE_FRICTION; fixtureDef.friction = Settings.TILE_FRICTION;
fixtureDef.restitution = Settings.TILE_RESTITUTION; fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = CollisionDetector.IDENTIFIER.TILE;
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
} }

View file

@ -2,9 +2,10 @@ 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" "Game/" + GLOBALS.context + "/GameObjects/Tile",
"Game/" + GLOBALS.context + "/GameObjects/Item"
], function (Settings, Box2D, CollisionDetector, Tile) { ], function (Settings, Box2D, CollisionDetector, Tile, Item) {
// Public // Public
function Level (path, engine, gameObjects) { function Level (path, engine, gameObjects) {
@ -17,6 +18,7 @@ define([
Level.prototype.loadLevelInToEngine = function () { Level.prototype.loadLevelInToEngine = function () {
this.loadLevelObjectFromPath(this.path); this.loadLevelObjectFromPath(this.path);
this.createTiles(); this.createTiles();
this.createItems();
} }
Level.prototype.unload = function () { Level.prototype.unload = function () {
@ -34,10 +36,23 @@ define([
var tiles = this.levelObject.tiles; var tiles = this.levelObject.tiles;
for (var i = 0; i < tiles.length; i++) { for (var i = 0; i < tiles.length; i++) {
this.gameObjects.fixed.push(new Tile(this.engine, "tile-" + i, tiles[i])); var options = tiles[i];
options.m = this.tileAtPositionExists(options.x, options.y - 1) ? "Soil" : "GrassSoil";
this.gameObjects.fixed.push(new Tile(this.engine, "tile-" + i, options));
} }
} }
Level.prototype.createItems = function() {
var items = this.levelObject.items;
for (var i = 0; i < items.length; i++) {
var options = items[i];
this.gameObjects.animated.push(new Item(this.engine, "item-" + i, options));
};
};
Level.prototype.loadLevelObjectFromPath = function (path) { Level.prototype.loadLevelObjectFromPath = function (path) {
// TODO: load JSON levelObject from path // TODO: load JSON levelObject from path
@ -89,6 +104,92 @@ define([
// o o o // o o o
this.levelObject = { this.levelObject = {
items: [
{
name:'Banana',
image:'banana.gif',
shape:'rectangle',
category:'kitchen',
weight:0.2,
width:5,
height:9,
x:20,
y:0,
rotation: 0
},
{
name:'Large Cleaver',
image:'cleaver_large.gif',
shape:'rectangle',
category:'kitchen',
weight:1.1,
width:8,
height:22,
x:40,
y:0,
rotation: 0
},
{
name:'Small Cleaver',
image:'cleaver_small.gif',
shape:'rectangle',
category:'kitchen',
weight:0.8,
width:6,
height:17,
x:60,
y:0,
rotation: 0
},
{
name:'Coffeemachine',
image:'coffeemachine.gif',
shape:'rectangle',
category:'kitchen',
weight:3,
width:11,
height:14,
x:80,
y:0,
rotation: 0
},
{
name:'Microwave',
image:'microwave.gif',
shape:'rectangle',
category:'kitchen',
weight:7,
width:19,
height:12,
x:100,
y:0,
rotation: 0
},
{
name:'Refridgerator',
image:'fridge.gif',
shape:'rectangle',
category:'kitchen',
weight:5,
width:31,
height:53,
x:120,
y:200,
rotation: 0
},
{
name:'Knife',
image:'knife.gif',
shape:'rectangle',
category:'kitchen',
weight:0.3,
width:4,
height:15,
x:140,
y:0,
rotation: 0
}
],
tiles: [ tiles: [
{s:1, x:1, y:1, r:0}, {s:1, x:1, y:1, r:0},
{s:1, x:3, y:18}, {s:1, x:3, y:18},
@ -166,10 +267,19 @@ define([
{s:1, x:1, y:17}, {s:1, x:1, y:17},
{s:1, x:35, y:29}, {s:1, x:35, y:29},
{s:1, x:12, y:19} {s:1, x:12, y:19}
] ]
} }
} }
// TODO remove hack
Level.prototype.tileAtPositionExists = function(x, y) {
for (var i = 0; i < this.levelObject.tiles.length; i++) {
var o = this.levelObject.tiles[i];
if(o.x == x && o.y == y) return true;
}
return false;
};
return Level; return Level;
}) })

View file

@ -158,10 +158,6 @@ function (Parent, Box2D, Settings, CollisionDetector) {
} }
} }
Doll.prototype.destroy = function () {
Parent.prototype.destroy.call(this);
}
Doll.prototype.setStanding = function (isStanding) { Doll.prototype.setStanding = function (isStanding) {
if (this.standing == isStanding) return; if (this.standing == isStanding) return;
this.standing = isStanding; this.standing = isStanding;

View file

@ -40,6 +40,7 @@
var user = new User(userId, this); var user = new User(userId, this);
var joinedUsers = Object.keys(this.users); var joinedUsers = Object.keys(this.users);
var spawnedPlayers = this.gameController.getSpawnedPlayersAndTheirPositions(); var spawnedPlayers = this.gameController.getSpawnedPlayersAndTheirPositions();
var worldUpdate = this.gameController.getWorldUpdateObject(true);
this.users[user.id] = user; this.users[user.id] = user;
@ -47,7 +48,9 @@
userId: user.id, userId: user.id,
channelName: this.name, channelName: this.name,
joinedUsers: joinedUsers, joinedUsers: joinedUsers,
spawnedPlayers: spawnedPlayers spawnedPlayers: spawnedPlayers,
worldUpdate: worldUpdate
}; };
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options); NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options);

View file

@ -71,12 +71,23 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
GameController.prototype.updateWorld = function () { GameController.prototype.updateWorld = function () {
var update = this.getWorldUpdateObject(false);
if(Object.getOwnPropertyNames(update).length > 0) {
NotificationCenter.trigger("sendControlCommandToAllUsers", 'gameCommand', {worldUpdate:update});
}
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
}
GameController.prototype.getWorldUpdateObject = function(getSleeping) {
getSleeping = getSleeping || false;
var update = {}; var update = {};
var isUpdateNeeded = false;
var body = this.physicsEngine.world.GetBodyList(); var body = this.physicsEngine.world.GetBodyList();
do { do {
if(body.IsAwake()) { if(getSleeping || body.IsAwake()) {
var userData = body.GetUserData(); var userData = body.GetUserData();
if (userData instanceof GameObject) { if (userData instanceof GameObject) {
@ -93,28 +104,12 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
update[gameObject.uid].as = gameObject.getActionState(); update[gameObject.uid].as = gameObject.getActionState();
update[gameObject.uid].laxy = gameObject.lookAtXY; update[gameObject.uid].laxy = gameObject.lookAtXY;
} }
isUpdateNeeded = true;
} }
} }
} while (body = body.GetNext()); } while (body = body.GetNext());
if(isUpdateNeeded) { return update;
NotificationCenter.trigger("sendControlCommandToAllUsers", 'gameCommand', {worldUpdate:update});
}
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
}
GameController.prototype.getSpawnedPlayers = function() {
var spawnedPlayers = {};
for(player in this.players) {
if(player.isSpawned) {
spawnedPlayers[player.id] = player;
}
}
return spawnedPlayers;
}; };
GameController.prototype.getSpawnedPlayersAndTheirPositions = function() { GameController.prototype.getSpawnedPlayersAndTheirPositions = function() {
@ -130,11 +125,9 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
} }
} }
// FIXME: OR: use get Spawned Players and fetch them into a sort of transfer objects
// that contains only necessary data
return spawnedPlayers; return spawnedPlayers;
}; };
return GameController; return GameController;
}); });

View file

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