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);
};
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() {
//destroy box2d world etc.
};
@ -128,5 +109,13 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
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;
});

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() {
var self = this;
var material = "Stones";
var texturePath = Settings.GRAPHICS_PATH
+ Settings.GRAPHICS_SUBPATH_TILES
+ material + '/'
+ this.options.m + '/'
+ this.options.s + ''
+ (this.options.r || 0) + '.gif';

View file

@ -63,11 +63,13 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings) {
if (options.spawnedPlayers) {
for(var i = 0; i < options.spawnedPlayers.length; 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();
}

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) {
var oldLookDirection = this.lookDirection;
@ -141,6 +121,26 @@ function (Parent, Settings, NotificationCenter, Exception) {
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;
});

View file

@ -104,16 +104,5 @@ function (DomController, Settings, Exception, NotificationCenter) {
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;
});

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

View file

@ -2,9 +2,10 @@ define([
"Game/Config/Settings",
"Lib/Vendor/Box2D",
"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
function Level (path, engine, gameObjects) {
@ -17,6 +18,7 @@ define([
Level.prototype.loadLevelInToEngine = function () {
this.loadLevelObjectFromPath(this.path);
this.createTiles();
this.createItems();
}
Level.prototype.unload = function () {
@ -34,10 +36,23 @@ define([
var tiles = this.levelObject.tiles;
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) {
// TODO: load JSON levelObject from path
@ -89,87 +104,182 @@ define([
// o o o
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: [
{s:1, x:1, y:1, r:0},
{s:1, x:3, y:18},
{s:1, x:37, y:27},
{s:1, x:20, y:24},
{s:1, x:24, y:27},
{s:1, x:37, y:26},
{s:1, x:9, y:18},
{s:2, x:32, y:25, r:1},
{s:1, x:23, y:27},
{s:3, x:34, y:24, r:1},
{s:1, x:35, y:28},
{s:4, x:17, y:21},
{s:2, x:21, y:24},
{s:2, x:42, y:23, r:3},
{s:3, x:30, y:24, r:3},
{s:2, x:22, y:25},
{s:1, x:40, y:25},
{s:1, x:38, y:26},
{s:1, x:8, y:18},
{s:1, x:38, y:25},
{s:1, x:28, y:28},
{s:1, x:36, y:27},
{s:1, x:7, y:18},
{s:2, x:20, y:23},
{s:2, x:43, y:23, r:1},
{s:6, x:31, y:24},
{s:1, x:16, y:21},
{s:1, x:1, y:18},
{s:1, x:31, y:29},
{s:2, x:30, y:25, r:2},
{s:4, x:11, y:18},
{s:1, x:28, y:27},
{s:1, x:28, y:26},
{s:1, x:28, y:29},
{s:1, x:19, y:23},
{s:5, x:12, y:18, r:1},
{s:1, x:42, y:24},
{s:6, x:33, y:24, r:2},
{s:1, x:39, y:25},
{s:1, x:33, y:29},
{s:1, x:29, y:29},
{s:1, x:21, y:25},
{s:1, x:27, y:27},
{s:5, x:16, y:20, r:1},
{s:1, x:5, y:18},
{s:5, x:18, y:21, r:1},
{s:4, x:13, y:19},
{s:1, x:14, y:20},
{s:1, x:30, y:29},
{s:1, x:4, y:18},
{s:1, x:6, y:18},
{s:1, x:2, y:18},
{s:1, x:32, y:24},
{s:1, x:34, y:29},
{s:1, x:32, y:29},
{s:2, x:1, y:16},
{s:1, x:10, y:18},
{s:1, x:42, y:25},
{s:2, x:28, y:25, r:3},
{s:2, x:0, y:16, r:2},
{s:1, x:22, y:27},
{s:1, x:25, y:27},
{s:1, x:31, y:25},
{s:5, x:14, y:19, r:1},
{s:1, x:41, y:25},
{s:1, x:36, y:28},
{s:4, x:15, y:20},
{s:2, x:19, y:22},
{s:3, x:26, y:26, r:3},
{s:1, x:26, y:27},
{s:1, x:18, y:22},
{s:6, x:27, y:26},
{s:1, x:22, y:26},
{s:1, x:1, y:17},
{s:1, x:35, y:29},
{s:1, x:12, y:19}
{s:1, x:1, y:1, r:0},
{s:1, x:3, y:18},
{s:1, x:37, y:27},
{s:1, x:20, y:24},
{s:1, x:24, y:27},
{s:1, x:37, y:26},
{s:1, x:9, y:18},
{s:2, x:32, y:25, r:1},
{s:1, x:23, y:27},
{s:3, x:34, y:24, r:1},
{s:1, x:35, y:28},
{s:4, x:17, y:21},
{s:2, x:21, y:24},
{s:2, x:42, y:23, r:3},
{s:3, x:30, y:24, r:3},
{s:2, x:22, y:25},
{s:1, x:40, y:25},
{s:1, x:38, y:26},
{s:1, x:8, y:18},
{s:1, x:38, y:25},
{s:1, x:28, y:28},
{s:1, x:36, y:27},
{s:1, x:7, y:18},
{s:2, x:20, y:23},
{s:2, x:43, y:23, r:1},
{s:6, x:31, y:24},
{s:1, x:16, y:21},
{s:1, x:1, y:18},
{s:1, x:31, y:29},
{s:2, x:30, y:25, r:2},
{s:4, x:11, y:18},
{s:1, x:28, y:27},
{s:1, x:28, y:26},
{s:1, x:28, y:29},
{s:1, x:19, y:23},
{s:5, x:12, y:18, r:1},
{s:1, x:42, y:24},
{s:6, x:33, y:24, r:2},
{s:1, x:39, y:25},
{s:1, x:33, y:29},
{s:1, x:29, y:29},
{s:1, x:21, y:25},
{s:1, x:27, y:27},
{s:5, x:16, y:20, r:1},
{s:1, x:5, y:18},
{s:5, x:18, y:21, r:1},
{s:4, x:13, y:19},
{s:1, x:14, y:20},
{s:1, x:30, y:29},
{s:1, x:4, y:18},
{s:1, x:6, y:18},
{s:1, x:2, y:18},
{s:1, x:32, y:24},
{s:1, x:34, y:29},
{s:1, x:32, y:29},
{s:2, x:1, y:16},
{s:1, x:10, y:18},
{s:1, x:42, y:25},
{s:2, x:28, y:25, r:3},
{s:2, x:0, y:16, r:2},
{s:1, x:22, y:27},
{s:1, x:25, y:27},
{s:1, x:31, y:25},
{s:5, x:14, y:19, r:1},
{s:1, x:41, y:25},
{s:1, x:36, y:28},
{s:4, x:15, y:20},
{s:2, x:19, y:22},
{s:3, x:26, y:26, r:3},
{s:1, x:26, y:27},
{s:1, x:18, y:22},
{s:6, x:27, y:26},
{s:1, x:22, y:26},
{s:1, x:1, y:17},
{s:1, x:35, y:29},
{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;
})

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) {
if (this.standing == isStanding) return;
this.standing = isStanding;

View file

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

View file

@ -71,12 +71,23 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
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 isUpdateNeeded = false;
var body = this.physicsEngine.world.GetBodyList();
do {
if(body.IsAwake()) {
if(getSleeping || body.IsAwake()) {
var userData = body.GetUserData();
if (userData instanceof GameObject) {
@ -93,28 +104,12 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
update[gameObject.uid].as = gameObject.getActionState();
update[gameObject.uid].laxy = gameObject.lookAtXY;
}
isUpdateNeeded = true;
}
}
} while (body = body.GetNext());
if(isUpdateNeeded) {
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;
return update;
};
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 GameController;
});

View file

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