mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
new system for synchronizing game objects. fixes #74
This commit is contained in:
parent
3ef3a6abf9
commit
07dad646cf
19 changed files with 194 additions and 162 deletions
|
|
@ -83,8 +83,6 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
|
|
||||||
var spawnTimeout = setTimeout(function() {
|
var spawnTimeout = setTimeout(function() {
|
||||||
player.spawn(spawnPoint.x, spawnPoint.y);
|
player.spawn(spawnPoint.x, spawnPoint.y);
|
||||||
// put it into
|
|
||||||
self.gameObjects.animated.push(player);
|
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
id: player.id,
|
id: player.id,
|
||||||
|
|
@ -118,6 +116,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
|
|
||||||
var update = {};
|
var update = {};
|
||||||
|
|
||||||
|
/*
|
||||||
var body = this.physicsEngine.world.GetBodyList();
|
var body = this.physicsEngine.world.GetBodyList();
|
||||||
do {
|
do {
|
||||||
if((getSleeping || body.IsAwake()) && body.GetType() === Box2D.Dynamics.b2Body.b2_dynamicBody) {
|
if((getSleeping || body.IsAwake()) && body.GetType() === Box2D.Dynamics.b2Body.b2_dynamicBody) {
|
||||||
|
|
@ -125,22 +124,33 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
|
|
||||||
if (userData instanceof GameObject) {
|
if (userData instanceof GameObject) {
|
||||||
var gameObject = userData;
|
var gameObject = userData;
|
||||||
|
var updateData = gameObject.getUpdateData();
|
||||||
|
|
||||||
update[gameObject.uid] = {
|
if (updateData) {
|
||||||
p: body.GetPosition(),
|
update[gameObject.uid] = updateData;
|
||||||
a: body.GetAngle(),
|
|
||||||
lv: body.GetLinearVelocity(),
|
|
||||||
av: body.GetAngularVelocity()
|
|
||||||
};
|
|
||||||
|
|
||||||
if(gameObject instanceof Doll) {
|
|
||||||
update[gameObject.uid].as = gameObject.getActionState();
|
|
||||||
update[gameObject.uid].laxy = gameObject.lookAtXY;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (body = body.GetNext());
|
} while (body = body.GetNext());
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (var uid in this.worldUpdateObjects) {
|
||||||
|
|
||||||
|
var gameObject = this.worldUpdateObjects[uid];
|
||||||
|
|
||||||
|
if (!(gameObject instanceof GameObject)) {
|
||||||
|
console.warn('Cant find object ' + uid + ' in worldUpdateObjects pool (channel side), here is the object:');
|
||||||
|
console.log(gameObject);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var updateData = gameObject.getUpdateData(getSleeping);
|
||||||
|
|
||||||
|
if (updateData) {
|
||||||
|
update[gameObject.uid] = updateData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return update;
|
return update;
|
||||||
};
|
};
|
||||||
|
|
@ -171,9 +181,11 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
GameController.prototype.getRuntimeItems = function() {
|
GameController.prototype.getRuntimeItems = function() {
|
||||||
var objects = [];
|
var objects = [];
|
||||||
|
|
||||||
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
// This is using the level.createItem mechanism to
|
||||||
if(this.gameObjects.animated[i] instanceof RubeDoll) {
|
// create the RubeDoll from its ItemSettings
|
||||||
var object = this.gameObjects.animated[i];
|
for (var uid in this.worldUpdateObjects) {
|
||||||
|
if(this.worldUpdateObjects[uid] instanceof RubeDoll) {
|
||||||
|
var object = this.worldUpdateObjects[uid];
|
||||||
var options = object.options;
|
var options = object.options;
|
||||||
options.x = object.getPosition().x;
|
options.x = object.getPosition().x;
|
||||||
options.y = object.getPosition().y;
|
options.y = object.getPosition().y;
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,18 @@ function (Parent, Item, Box2D, Nc, Assert) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Doll.prototype.getUpdateData = function(getSleeping) {
|
||||||
|
|
||||||
|
var updateData = Parent.prototype.getUpdateData.call(this, getSleeping);
|
||||||
|
|
||||||
|
if(updateData) {
|
||||||
|
updateData.as = this.getActionState();
|
||||||
|
updateData.laxy = this.lookAtXY;
|
||||||
|
}
|
||||||
|
|
||||||
|
return updateData;
|
||||||
|
};
|
||||||
|
|
||||||
return Doll;
|
return Doll;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -1,9 +1,39 @@
|
||||||
define([
|
define([
|
||||||
"Game/Core/GameObjects/GameObject"
|
"Game/Core/GameObjects/GameObject",
|
||||||
|
"Lib/Vendor/Box2D"
|
||||||
],
|
],
|
||||||
|
|
||||||
function(Parent) {
|
function (Parent, Box2D) {
|
||||||
|
|
||||||
return Parent;
|
"use strict";
|
||||||
|
|
||||||
|
function GameObject(physicsEngine, uid) {
|
||||||
|
Parent.call(this, physicsEngine, uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
GameObject.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
|
GameObject.prototype.getUpdateData = function(getSleeping) {
|
||||||
|
|
||||||
|
if (!this.body) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.body.GetType() === Box2D.Dynamics.b2Body.b2_staticBody) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!getSleeping && !this.body.IsAwake()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
p: this.body.GetPosition(),
|
||||||
|
a: this.body.GetAngle(),
|
||||||
|
lv: this.body.GetLinearVelocity(),
|
||||||
|
av: this.body.GetAngularVelocity()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return GameObject;
|
||||||
});
|
});
|
||||||
|
|
@ -8,8 +8,8 @@ function (Parent, Settings, FileSystem) {
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
function Level (uid, engine, gameObjects) {
|
function Level (uid, engine) {
|
||||||
Parent.call(this, uid, engine, gameObjects);
|
Parent.call(this, uid, engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
Level.prototype = Object.create(Parent.prototype);
|
Level.prototype = Object.create(Parent.prototype);
|
||||||
|
|
|
||||||
|
|
@ -55,9 +55,11 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
this.mePositionStateUpdate();
|
this.mePositionStateUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
//for (var uid in this.gameObjects.animated) {
|
||||||
this.gameObjects.animated[i].render();
|
// this.gameObjects.animated[uid].render();
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
Nc.trigger(Nc.ns.client.game.events.render);
|
||||||
|
|
||||||
this.view.render();
|
this.view.render();
|
||||||
DomController.fpsStep();
|
DomController.fpsStep();
|
||||||
|
|
@ -78,17 +80,10 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
|
|
||||||
if (options.runtimeItems) {
|
if (options.runtimeItems) {
|
||||||
for (i = 0; i < options.runtimeItems.length; i++) {
|
for (i = 0; i < options.runtimeItems.length; i++) {
|
||||||
|
|
||||||
var itemDef = options.runtimeItems[i];
|
var itemDef = options.runtimeItems[i];
|
||||||
|
|
||||||
var alreadyExists = false;
|
if(!this.getItemByUid(itemDef.uid)) {
|
||||||
for (var j = 0; j < this.gameObjects.animated.length; j++) {
|
|
||||||
if(this.gameObjects.animated[j].uid == itemDef.uid) {
|
|
||||||
alreadyExists = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!alreadyExists) {
|
|
||||||
// When creating from synchronization we need to bring it into level format (px)
|
// When creating from synchronization we need to bring it into level format (px)
|
||||||
itemDef.options.x *= Settings.RATIO;
|
itemDef.options.x *= Settings.RATIO;
|
||||||
itemDef.options.y *= Settings.RATIO;
|
itemDef.options.y *= Settings.RATIO;
|
||||||
|
|
@ -112,6 +107,13 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
//this.audioPlayer.play();
|
//this.audioPlayer.play();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
TODO :
|
||||||
|
- remove this
|
||||||
|
- overwrite setUpdateData inside client / Me with an empty function
|
||||||
|
|
||||||
GameController.prototype.onWorldUpdateGameObject = function(body, gameObject, update) {
|
GameController.prototype.onWorldUpdateGameObject = function(body, gameObject, update) {
|
||||||
if(gameObject === this.me.doll) {
|
if(gameObject === this.me.doll) {
|
||||||
this.me.setLastServerPositionState(update);
|
this.me.setLastServerPositionState(update);
|
||||||
|
|
@ -122,6 +124,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
|
|
||||||
Parent.prototype.onWorldUpdateGameObject.call(this, body, gameObject, update);
|
Parent.prototype.onWorldUpdateGameObject.call(this, body, gameObject, update);
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
GameController.prototype.createMe = function(user) {
|
GameController.prototype.createMe = function(user) {
|
||||||
this.me = new Me(user.id, this.physicsEngine, user);
|
this.me = new Me(user.id, this.physicsEngine, user);
|
||||||
|
|
@ -161,15 +164,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
|
|
||||||
GameController.prototype.onHandActionResponse = function(options) {
|
GameController.prototype.onHandActionResponse = function(options) {
|
||||||
var player = this.players[options.playerId];
|
var player = this.players[options.playerId];
|
||||||
|
var item = this.getItemByUid(options.itemUid);
|
||||||
var item = null;
|
|
||||||
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
|
||||||
var currentItem = this.gameObjects.animated[i];
|
|
||||||
if(currentItem.uid == options.itemUid) {
|
|
||||||
item = currentItem;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(item) {
|
if(item) {
|
||||||
if(options.action == "throw") {
|
if(options.action == "throw") {
|
||||||
|
|
@ -230,22 +225,6 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
this.me.resetPositionState(options);
|
this.me.resetPositionState(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
GameController.prototype.onRemoveGameObject = function(options) {
|
|
||||||
var object = null;
|
|
||||||
for (var i = 0; i < this.gameObjects[options.type].length; i++) {
|
|
||||||
if(this.gameObjects[options.type][i].uid == options.uid) {
|
|
||||||
object = this.gameObjects[options.type][i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(object) {
|
|
||||||
//this.onGameObjectRemove(options.type, object);
|
|
||||||
object.destroy();
|
|
||||||
} else {
|
|
||||||
console.warn("GameObject for removal can not be found locally. " + options.uid);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
GameController.prototype.loadLevel = function (path) {
|
GameController.prototype.loadLevel = function (path) {
|
||||||
Parent.prototype.loadLevel.call(this, path);
|
Parent.prototype.loadLevel.call(this, path);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ function (Parent, Settings, Nc, Layer) {
|
||||||
function Item(physicsEngine, uid, options) {
|
function Item(physicsEngine, uid, options) {
|
||||||
this.layerId = Layer.ID.ITEM;
|
this.layerId = Layer.ID.ITEM;
|
||||||
Parent.call(this, physicsEngine, uid, options);
|
Parent.call(this, physicsEngine, uid, options);
|
||||||
|
|
||||||
|
Nc.on(Nc.ns.client.game.events.render, this.render, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Item.prototype = Object.create(Parent.prototype);
|
Item.prototype = Object.create(Parent.prototype);
|
||||||
|
|
|
||||||
|
|
@ -189,8 +189,6 @@ function (Parent, Layer, Settings, Nc) {
|
||||||
RubeDoll.prototype.flip = function(direction) {
|
RubeDoll.prototype.flip = function(direction) {
|
||||||
Parent.prototype.flip.call(this, direction);
|
Parent.prototype.flip.call(this, direction);
|
||||||
|
|
||||||
console.log("last", this.lastFlipDirection, "now", direction);
|
|
||||||
|
|
||||||
// flipping depth of right body side arm/leg images with left
|
// flipping depth of right body side arm/leg images with left
|
||||||
if (this.lastFlipDirection != direction) { // FIXME : this is a bit broken.
|
if (this.lastFlipDirection != direction) { // FIXME : this is a bit broken.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ function (Parent, Settings, Nc) {
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
function TiledLevel(uid, engine, gameObjects) {
|
function TiledLevel(uid, engine) {
|
||||||
this.layerId = "background";
|
this.layerId = "background";
|
||||||
Parent.call(this, uid, engine, gameObjects);
|
Parent.call(this, uid, engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
TiledLevel.prototype = Object.create(Parent.prototype);
|
TiledLevel.prototype = Object.create(Parent.prototype);
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@ function (Parent, Nc, Settings) {
|
||||||
this.healthBarViewVisibleTimeout = null;
|
this.healthBarViewVisibleTimeout = null;
|
||||||
this.healthBarViewVisible = false;
|
this.healthBarViewVisible = false;
|
||||||
this.initHealthBar();
|
this.initHealthBar();
|
||||||
|
|
||||||
|
Nc.on(Nc.ns.client.game.events.render, this.render, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Player.prototype = Object.create(Parent.prototype);
|
Player.prototype = Object.create(Parent.prototype);
|
||||||
|
|
|
||||||
|
|
@ -17,15 +17,14 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll, GameObject, Assert) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.players = {};
|
this.players = {};
|
||||||
this.level = null;
|
this.level = null;
|
||||||
this.gameObjects = null;
|
this.worldUpdateObjects = {};
|
||||||
this.resetGameObjects();
|
|
||||||
|
|
||||||
this.physicsEngine = new PhysicsEngine();
|
this.physicsEngine = new PhysicsEngine();
|
||||||
this.physicsEngine.setCollisionDetector();
|
this.physicsEngine.setCollisionDetector();
|
||||||
|
|
||||||
this.ncTokens = [
|
this.ncTokens = [
|
||||||
Nc.on(Nc.ns.core.game.gameObject.add, this.onGameObjectAdd, this),
|
Nc.on(Nc.ns.core.game.worldUpdateObjects.add, this.onWorldUpdateObjectAdd, this),
|
||||||
Nc.on(Nc.ns.core.game.gameObject.remove, this.onGameObjectRemove, this)
|
Nc.on(Nc.ns.core.game.worldUpdateObjects.remove, this.onWorldUpdateObjectRemove, this)
|
||||||
];
|
];
|
||||||
|
|
||||||
this.loadLevel(options.levelUid);
|
this.loadLevel(options.levelUid);
|
||||||
|
|
@ -37,78 +36,57 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll, GameObject, Assert) {
|
||||||
// extend for both sides if necessary
|
// extend for both sides if necessary
|
||||||
};
|
};
|
||||||
|
|
||||||
GameController.prototype.resetGameObjects = function() {
|
GameController.prototype.onWorldUpdateObjectAdd = function(object) {
|
||||||
this.gameObjects = {
|
this.worldUpdateObjects[object.uid] = object;
|
||||||
fixed: [],
|
|
||||||
animated: []
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GameController.prototype.onGameObjectAdd = function(type, object) {
|
GameController.prototype.onWorldUpdateObjectRemove = function(object) {
|
||||||
this.gameObjects[type].push(object);
|
delete this.worldUpdateObjects[object.uid];
|
||||||
};
|
|
||||||
|
|
||||||
GameController.prototype.onGameObjectRemove = function(type, object) {
|
|
||||||
var i = this.gameObjects[type].indexOf(object);
|
|
||||||
if(i>=0) this.gameObjects[type].splice(i, 1);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GameController.prototype.getPhysicsEngine = function () {
|
GameController.prototype.getPhysicsEngine = function () {
|
||||||
return this.physicsEngine;
|
return this.physicsEngine;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameController.prototype.getItemByUid = function(uid) {
|
||||||
|
// FIXME : maybe divide this into a dedicated item pool?
|
||||||
|
return this.worldUpdateObjects[uid];
|
||||||
|
};
|
||||||
|
|
||||||
GameController.prototype.loadLevel = function (levelUid) {
|
GameController.prototype.loadLevel = function (levelUid) {
|
||||||
|
|
||||||
if (this.level) {
|
if (this.level) {
|
||||||
this.level.destroy();
|
this.level.destroy();
|
||||||
this.resetGameObjects();
|
this.worldUpdateObjects = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
this.level = new TiledLevel(levelUid, this.physicsEngine, this.gameObjects);
|
this.level = new TiledLevel(levelUid, this.physicsEngine);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is now in core, because the recorder/player
|
||||||
|
* uses the world update mechanism on the channel side
|
||||||
|
*/
|
||||||
GameController.prototype.onWorldUpdate = function (updateData) {
|
GameController.prototype.onWorldUpdate = function (updateData) {
|
||||||
|
|
||||||
var body = this.physicsEngine.world.GetBodyList();
|
for (var uid in updateData) {
|
||||||
do {
|
|
||||||
var userData = body.GetUserData();
|
var gameObject = this.worldUpdateObjects[uid];
|
||||||
if (userData instanceof GameObject) {
|
|
||||||
var gameObject = userData;
|
if (!(gameObject instanceof GameObject)) {
|
||||||
if(updateData[gameObject.uid]) {
|
console.warn('Cant find object ' + uid + ' in worldUpdateObjects pool');
|
||||||
var update = updateData[gameObject.uid];
|
continue;
|
||||||
this.onWorldUpdateGameObject(body, gameObject, update);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (body = body.GetNext());
|
gameObject.setUpdateData(updateData[uid]);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
GameController.prototype.onWorldUpdateGameObject = function(body, gameObject, update) {
|
|
||||||
if (gameObject instanceof Doll) {
|
|
||||||
/*
|
/*
|
||||||
if(gameObject === this.me.doll) {
|
GameController.prototype.onWorldUpdateGameObject = function(body, gameObject, update) {
|
||||||
this.me.setLastServerPositionState(update);
|
FIXME : call gameObject.setUpdateData(updateData[uid]);
|
||||||
if(!this.me.acceptPositionStateUpdateFromServer()) {
|
|
||||||
return; // this is to ignore own doll updates from world update
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
gameObject.setActionState(update.as);
|
|
||||||
gameObject.lookAt(update.laxy.x, update.laxy.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.number(update.p.x, update.p.y);
|
|
||||||
Assert.number(update.a);
|
|
||||||
Assert.number(update.lv.x, update.lv.y);
|
|
||||||
Assert.number(update.av);
|
|
||||||
|
|
||||||
body.SetAwake(true);
|
|
||||||
body.SetPosition(update.p);
|
|
||||||
body.SetAngle(update.a);
|
|
||||||
body.SetLinearVelocity(update.lv);
|
|
||||||
body.SetAngularVelocity(update.av);
|
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
GameController.prototype.onResetLevel = function() {
|
GameController.prototype.onResetLevel = function() {
|
||||||
this.loadLevel(this.level.uid);
|
this.loadLevel(this.level.uid);
|
||||||
|
|
@ -140,39 +118,23 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll, GameObject, Assert) {
|
||||||
GameController.prototype.destroy = function () {
|
GameController.prototype.destroy = function () {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
|
|
||||||
/*
|
|
||||||
for(var player in this.players) {
|
for(var player in this.players) {
|
||||||
// this.players[player].destroy();
|
this.players[player].destroy();
|
||||||
|
|
||||||
// FIXME:
|
|
||||||
// commented out for now, because players are in gameObjects array.
|
|
||||||
// try using a real gameobject for the health bar
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
|
||||||
for (i = 0; i < this.ncTokens.length; i++) {
|
|
||||||
Nc.off(this.ncTokens[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
Nc.trigger(Nc.ns.client.game.events.destroy);
|
||||||
* Contents of gameObject: Players, Items, Tiles, RagDolls
|
|
||||||
* No Dolls.
|
|
||||||
*/
|
|
||||||
|
|
||||||
for (var key in this.gameObjects) {
|
// Testing after destroy if worldUpdateObjects is empty
|
||||||
for (i = 0; i < this.gameObjects[key].length; i++) {
|
// events.game.destroy -> gameobjects.destroy() -> Nc.trigger(worldUpdateObjects.remove)
|
||||||
var gameObject = this.gameObjects[key][i];
|
if(Object.keys(this.worldUpdateObjects).length > 0) {
|
||||||
|
console.warn('Not all worldUpdateObjects have been removed... ', Object.keys(this.worldUpdateObjects));
|
||||||
gameObject.destroy();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this.gameObjects = {
|
|
||||||
fixed: [],
|
|
||||||
animated: []
|
|
||||||
};
|
|
||||||
|
|
||||||
this.physicsEngine.destroy();
|
this.physicsEngine.destroy();
|
||||||
|
this.worldUpdateObjects = null;
|
||||||
|
|
||||||
|
Nc.off(this.ncTokens);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return GameController;
|
return GameController;
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,7 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc, Asser
|
||||||
this.createFixtures();
|
this.createFixtures();
|
||||||
this.body.SetActive(false);
|
this.body.SetActive(false);
|
||||||
|
|
||||||
|
Nc.trigger(Nc.ns.core.game.worldUpdateObjects.add, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Doll.prototype = Object.create(Parent.prototype);
|
Doll.prototype = Object.create(Parent.prototype);
|
||||||
|
|
@ -471,7 +472,16 @@ function (Parent, Exception, Box2D, Settings, CollisionDetector, Item, Nc, Asser
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Doll.prototype.setUpdateData = function(update) {
|
||||||
|
|
||||||
|
Parent.prototype.setUpdateData.call(this, update);
|
||||||
|
|
||||||
|
this.setActionState(update.as);
|
||||||
|
this.lookAt(update.laxy.x, update.laxy.y);
|
||||||
|
};
|
||||||
|
|
||||||
Doll.prototype.destroy = function() {
|
Doll.prototype.destroy = function() {
|
||||||
|
Nc.trigger(Nc.ns.core.game.worldUpdateObjects.remove, this);
|
||||||
Parent.prototype.destroy.call(this);
|
Parent.prototype.destroy.call(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
define([
|
define([
|
||||||
"Lib/Vendor/Box2D",
|
"Lib/Vendor/Box2D",
|
||||||
"Lib/Utilities/Exception"
|
"Lib/Utilities/Exception",
|
||||||
|
"Lib/Utilities/Assert",
|
||||||
|
"Lib/Utilities/NotificationCenter"
|
||||||
],
|
],
|
||||||
|
|
||||||
function (Box2D, Exception) {
|
function (Box2D, Exception, Assert, Nc) {
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
|
@ -13,6 +15,10 @@ function (Box2D, Exception) {
|
||||||
var def = this.getBodyDef();
|
var def = this.getBodyDef();
|
||||||
def.userData = this;
|
def.userData = this;
|
||||||
this.body = physicsEngine.getWorld().CreateBody(def);
|
this.body = physicsEngine.getWorld().CreateBody(def);
|
||||||
|
|
||||||
|
this.ncTokens = (this.ncTokens || []).concat([
|
||||||
|
Nc.on(Nc.ns.client.game.events.destroy, this.destroy, this)
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObject.prototype.getBodyDef = function() {
|
GameObject.prototype.getBodyDef = function() {
|
||||||
|
|
@ -20,11 +26,14 @@ function (Box2D, Exception) {
|
||||||
};
|
};
|
||||||
|
|
||||||
GameObject.prototype.destroy = function() {
|
GameObject.prototype.destroy = function() {
|
||||||
|
|
||||||
if(this.body instanceof Box2D.Dynamics.b2Body) {
|
if(this.body instanceof Box2D.Dynamics.b2Body) {
|
||||||
this.body.GetWorld().DestroyBody(this.body);
|
this.body.GetWorld().DestroyBody(this.body);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("can not destroy body");
|
throw new Exception("can not destroy body");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Nc.off(this.ncTokens);
|
||||||
};
|
};
|
||||||
|
|
||||||
GameObject.prototype.getBody = function() {
|
GameObject.prototype.getBody = function() {
|
||||||
|
|
@ -35,6 +44,20 @@ function (Box2D, Exception) {
|
||||||
return this.body.GetPosition().Copy();
|
return this.body.GetPosition().Copy();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameObject.prototype.setUpdateData = function(update) {
|
||||||
|
|
||||||
|
Assert.number(update.p.x, update.p.y);
|
||||||
|
Assert.number(update.a);
|
||||||
|
Assert.number(update.lv.x, update.lv.y);
|
||||||
|
Assert.number(update.av);
|
||||||
|
|
||||||
|
this.body.SetAwake(true);
|
||||||
|
this.body.SetPosition(update.p);
|
||||||
|
this.body.SetAngle(update.a);
|
||||||
|
this.body.SetLinearVelocity(update.lv);
|
||||||
|
this.body.SetAngularVelocity(update.av);
|
||||||
|
};
|
||||||
|
|
||||||
return GameObject;
|
return GameObject;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -41,7 +41,7 @@ function (Parent, Box2D, Options, Settings, Exception, Nc, Assert) {
|
||||||
this.body.SetBullet(true);
|
this.body.SetBullet(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.add, "animated", this);
|
Nc.trigger(Nc.ns.core.game.worldUpdateObjects.add, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Item.prototype = Object.create(Parent.prototype);
|
Item.prototype = Object.create(Parent.prototype);
|
||||||
|
|
@ -158,7 +158,7 @@ function (Parent, Box2D, Options, Settings, Exception, Nc, Assert) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Item.prototype.destroy = function() {
|
Item.prototype.destroy = function() {
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.remove, "animated", this);
|
Nc.trigger(Nc.ns.core.game.worldUpdateObjects.remove, this);
|
||||||
Parent.prototype.destroy.call(this);
|
Parent.prototype.destroy.call(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -378,7 +378,6 @@ function (Parent, Box2D, Settings, Nc, Assert, Options, ItemSettings) {
|
||||||
|
|
||||||
RagDoll.prototype.destroy = function() {
|
RagDoll.prototype.destroy = function() {
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.remove, "animated", this);
|
|
||||||
var world = this.body.GetWorld();
|
var world = this.body.GetWorld();
|
||||||
|
|
||||||
for (var name in this.limbs) {
|
for (var name in this.limbs) {
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,6 @@ function (Parent, RubeLoader, Box2D, Settings, Assert, Nc, RubeDollJson) {
|
||||||
|
|
||||||
RubeDoll.prototype.destroy = function() {
|
RubeDoll.prototype.destroy = function() {
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.remove, "animated", this);
|
|
||||||
var world = this.body.GetWorld();
|
var world = this.body.GetWorld();
|
||||||
|
|
||||||
for (var name in this.limbs) {
|
for (var name in this.limbs) {
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,6 @@ function (Parent, Box2D, Settings, Exception, Nc, Assert) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
Parent.call(this, physicsEngine, uid);
|
Parent.call(this, physicsEngine, uid);
|
||||||
this.createPhysicTile(this.options);
|
this.createPhysicTile(this.options);
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.add, "fixed", this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Tile.prototype = Object.create(Parent.prototype);
|
Tile.prototype = Object.create(Parent.prototype);
|
||||||
|
|
@ -120,10 +118,6 @@ function (Parent, Box2D, Settings, Exception, Nc, Assert) {
|
||||||
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2)));
|
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2)));
|
||||||
};
|
};
|
||||||
|
|
||||||
Tile.prototype.destroy = function() {
|
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.remove, "fixed", this);
|
|
||||||
};
|
|
||||||
|
|
||||||
return Tile;
|
return Tile;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -27,8 +27,6 @@ function (Doll, Settings, Nc, Exception, ColorConverter, SpectatorDoll, RubeDoll
|
||||||
this.spawned = false;
|
this.spawned = false;
|
||||||
this.holdingItem = null;
|
this.holdingItem = null;
|
||||||
this.spectatorDoll = new SpectatorDoll(this.physicsEngine, "spectatorDoll-" + this.id, this);
|
this.spectatorDoll = new SpectatorDoll(this.physicsEngine, "spectatorDoll-" + this.id, this);
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.add, 'animated', this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Player.prototype.getNickname = function() {
|
Player.prototype.getNickname = function() {
|
||||||
|
|
@ -161,7 +159,7 @@ function (Doll, Settings, Nc, Exception, ColorConverter, SpectatorDoll, RubeDoll
|
||||||
|
|
||||||
Player.prototype.destroy = function () {
|
Player.prototype.destroy = function () {
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.core.game.gameObject.remove, 'animated', this);
|
// FIXME add destroy nc hook
|
||||||
|
|
||||||
if(this.holdingItem) {
|
if(this.holdingItem) {
|
||||||
var options = {
|
var options = {
|
||||||
|
|
@ -174,6 +172,8 @@ function (Doll, Settings, Nc, Exception, ColorConverter, SpectatorDoll, RubeDoll
|
||||||
|
|
||||||
this.spectatorDoll.destroy();
|
this.spectatorDoll.destroy();
|
||||||
|
|
||||||
|
// doll destoys itself at the end cause its a gameobject
|
||||||
|
// but on userLeft, the player has to destroy it.
|
||||||
if(this.doll) {
|
if(this.doll) {
|
||||||
this.doll.destroy();
|
this.doll.destroy();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,10 @@ function (Exception) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
game: {
|
game: {
|
||||||
|
events: {
|
||||||
|
render: null,
|
||||||
|
destroy: null
|
||||||
|
},
|
||||||
gameStats: {
|
gameStats: {
|
||||||
toggle: null
|
toggle: null
|
||||||
},
|
},
|
||||||
|
|
@ -102,9 +106,9 @@ function (Exception) {
|
||||||
},
|
},
|
||||||
core: {
|
core: {
|
||||||
game: {
|
game: {
|
||||||
gameObject: {
|
worldUpdateObjects: {
|
||||||
add: null,
|
add: null,
|
||||||
remove: null
|
remove: null,
|
||||||
},
|
},
|
||||||
events: {
|
events: {
|
||||||
level: {
|
level: {
|
||||||
|
|
@ -241,18 +245,24 @@ function (Exception) {
|
||||||
|
|
||||||
NotificationCenter.prototype.off = function (token) {
|
NotificationCenter.prototype.off = function (token) {
|
||||||
|
|
||||||
|
if(token && token.constructor === Array) {
|
||||||
|
this.offAll(token);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for(var m in this.topics) {
|
for(var m in this.topics) {
|
||||||
if (this.topics[m]) {
|
if (this.topics[m]) {
|
||||||
for(var i = 0, j = this.topics[m].length; i < j; i++) {
|
for(var i = 0, j = this.topics[m].length; i < j; i++) {
|
||||||
if (this.topics[m][i].token === token) {
|
if (this.topics[m][i].token === token) {
|
||||||
this.topics[m].splice(i, 1);
|
this.topics[m].splice(i, 1);
|
||||||
return token;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// should be treated as a private function - use Nc.off(Array);
|
||||||
NotificationCenter.prototype.offAll = function (tokens) {
|
NotificationCenter.prototype.offAll = function (tokens) {
|
||||||
for (var i = 0; i < tokens.length; i++) {
|
for (var i = 0; i < tokens.length; i++) {
|
||||||
this.off(tokens[i]);
|
this.off(tokens[i]);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue