diff --git a/lib/Chuck/ClientGame.js b/lib/Chuck/ClientGame.js index 08ce95a..91c0f16 100644 --- a/lib/Chuck/ClientGame.js +++ b/lib/Chuck/ClientGame.js @@ -2,20 +2,28 @@ define(["Chuck/Processors/ClientProcessor"], function(ClientProcessor) { function ClientGame(networker, id) { this.networker = networker; - this.processor = new ClientProcessor(this); - this.processor.spawnMeWithId(id); + this.clientProcessor = new ClientProcessor(this); + this.clientProcessor.spawnMeWithId(id); + + this.players = {}; } ClientGame.prototype.loadLevel = function(path) { - this.processor.loadLevel(path); + this.clientProcessor.loadLevel(path); } ClientGame.prototype.userJoined = function(userId) { - this.processor.spawnNewPlayerWithId(userId); - }; + this.players[userId] = this.clientProcessor.spawnNewPlayerWithId(userId); + } + + ClientGame.prototype.userLeft = function(userId) { + var player = this.players[userId]; + player.destroy(); + delete this.players[userId]; + } ClientGame.prototype.processGameCommand = function(command, options){ - this.processor.processGameCommand(command, options); + this.clientProcessor.processGameCommand(command, options); } ClientGame.prototype.sendGameCommand = function(command, options) { @@ -23,7 +31,7 @@ define(["Chuck/Processors/ClientProcessor"], function(ClientProcessor) { } ClientGame.prototype.destruct = function(){ - this.processor.destruct(); + this.clientProcessor.destruct(); } return ClientGame; diff --git a/lib/Chuck/Physics/Doll.js b/lib/Chuck/Physics/Doll.js index 84e959b..a9455ff 100644 --- a/lib/Chuck/Physics/Doll.js +++ b/lib/Chuck/Physics/Doll.js @@ -2,12 +2,12 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ function Doll (physicsEngine, id){ this.id = id; - this._physicsEngine = physicsEngine; - this._body; - this._legs; - this._contactPoint; + this.physicsEngine = physicsEngine; + this.body; + this.legs; + this.contactPoint; - this.init(this._physicsEngine.getWorld()); + this.init(this.physicsEngine.getWorld()); } Doll.prototype.init = function (world) { @@ -20,7 +20,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.userData = 'player-' + this.id; - this._body = world.CreateBody(bodyDef); + this.body = world.CreateBody(bodyDef); var fixtureDef = new Box2D.Dynamics.b2FixtureDef(); fixtureDef.density = Settings.PLAYER_DENSITY; @@ -33,14 +33,14 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ fixtureDef.shape = headShape; fixtureDef.isSensor = false; fixtureDef.userData = 'myHead-' + this.id; - this._body.CreateFixture(fixtureDef); + this.body.CreateFixture(fixtureDef); 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)); fixtureDef.shape = bodyShape; fixtureDef.isSensor = false; fixtureDef.userData = 'myBody-' + this.id; - this._body.CreateFixture(fixtureDef); + this.body.CreateFixture(fixtureDef); var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); legsShape.SetRadius(5 / Settings.RATIO); @@ -49,7 +49,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.isSensor = false; fixtureDef.userData = 'myLegs-' + this.id; - this._legs = this._body.CreateFixture(fixtureDef); + this.legs = this.body.CreateFixture(fixtureDef); var feetShape = new Box2D.Collision.Shapes.b2CircleShape(); feetShape.SetRadius(4 / Settings.RATIO); @@ -57,45 +57,45 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ fixtureDef.shape = feetShape; fixtureDef.isSensor = true; fixtureDef.userData = 'myFeet-' + this.id; - this._body.CreateFixture(fixtureDef); + this.body.CreateFixture(fixtureDef); - this._body.SetActive(false); + this.body.SetActive(false); } Doll.prototype.spawn = function (x, y) { - this._body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO)); - this._body.SetActive(true); + this.body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO)); + this.body.SetActive(true); } Doll.prototype.getBody = function () { - return this._body; + return this.body; } - Doll.prototype._setFriction = function (friction) { + Doll.prototype.setFriction = function (friction) { if(!friction) friction = -1; - if (this._legs.GetFriction() != friction) + if (this.legs.GetFriction() != friction) { - this._legs.SetFriction(friction); + this.legs.SetFriction(friction); } } Doll.prototype.move = function (direction, speed) { - this._setFriction(Settings.PLAYER_MOTION_FRICTION); - this._body.SetAwake(true); - var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this._body.GetLinearVelocity().y); - this._body.SetLinearVelocity(vector); + this.setFriction(Settings.PLAYER_MOTION_FRICTION); + this.body.SetAwake(true); + var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y); + this.body.SetLinearVelocity(vector); } Doll.prototype.stop = function () { - this._setFriction(Settings.PLAYER_FRICTION); + this.setFriction(Settings.PLAYER_FRICTION); } Doll.prototype.jump = function () { - this._body.SetAwake(true); + this.body.SetAwake(true); var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED); - this._body.ApplyImpulse(vector, this._body.GetPosition()); + this.body.ApplyImpulse(vector, this.body.GetPosition()); // maybe change to a constant force instead of applying of force? // to prevent higher jumping running uphill, etc. @@ -103,7 +103,11 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ Doll.prototype.jumping = function () { var vector = new Box2D.Common.Math.b2Vec2(0, -0.05); - this._body.ApplyImpulse(vector, this._body.GetPosition()); + this.body.ApplyImpulse(vector, this.body.GetPosition()); + } + + Doll.prototype.destroy = function() { + this.body.GetWorld().DestroyBody(this.body); } return Doll; diff --git a/lib/Chuck/Player.js b/lib/Chuck/Player.js index c30ec60..4d54cf3 100644 --- a/lib/Chuck/Player.js +++ b/lib/Chuck/Player.js @@ -180,5 +180,9 @@ define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){ } } + Player.prototype.destroy = function() { + this.doll.destroy(); + } + return Player; }); diff --git a/lib/Chuck/Processors/ClientProcessor.js b/lib/Chuck/Processors/ClientProcessor.js index 1fecad0..a39a7e6 100755 --- a/lib/Chuck/Processors/ClientProcessor.js +++ b/lib/Chuck/Processors/ClientProcessor.js @@ -70,6 +70,10 @@ define(requires, this.inputControlUnit = new InputControlUnit(this.me, this); } + ClientProcessor.prototype.userIdLeft = function(userId) { + // body... + }; + ClientProcessor.prototype.sendGameCommand = function(command, options) { this.clientGame.sendGameCommand(command, options); } diff --git a/lib/Chuck/Processors/ServerProcessor.js b/lib/Chuck/Processors/ServerProcessor.js index cf7aef7..03e0d7e 100755 --- a/lib/Chuck/Processors/ServerProcessor.js +++ b/lib/Chuck/Processors/ServerProcessor.js @@ -67,6 +67,11 @@ define(requires, function(PhysicsEngine, Player, Box2D, Level, InputController, } } + ServerProcessor.prototype.userIdLeft = function(id) { + var player = this.players[id].player; + player.destroy(); + } + ServerProcessor.prototype.updateWorld = function() { var update = {}; diff --git a/lib/Chuck/ServerGame.js b/lib/Chuck/ServerGame.js index 76d7bf5..b2f14b1 100644 --- a/lib/Chuck/ServerGame.js +++ b/lib/Chuck/ServerGame.js @@ -21,6 +21,10 @@ define(["Chuck/Processors/ServerProcessor"], function(ServerProcessor) { this.serverProcessor.createPlayerWithId(user.id); } + ServerGame.prototype.userIdLeft = function(userId) { + this.serverProcessor.userIdLeft(userId); + }; + ServerGame.prototype.updateClientsWorld = function(update_world) { this.channel.sendCommandToAllUsers('gameCommand', {worldUpdate: update_world}); } diff --git a/lib/Client/Networker.js b/lib/Client/Networker.js index 583b7d2..efb3270 100644 --- a/lib/Client/Networker.js +++ b/lib/Client/Networker.js @@ -72,6 +72,10 @@ define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientG this.sendCommand('gameCommand', message); } + Networker.prototype.onUserLeft = function(userId) { + this.clientGame.userLeft(userId); + } + Networker.prototype.processControlCommand = function(command, options) { switch(command) { case 'joinSuccess': @@ -88,6 +92,10 @@ define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientG this.onUserJoined(options); break; + case 'userLeft': + this.onUserLeft(options); + break; + default: break; } diff --git a/lib/Server/Channel.js b/lib/Server/Channel.js index a6898ed..8dfe7e6 100644 --- a/lib/Server/Channel.js +++ b/lib/Server/Channel.js @@ -22,7 +22,10 @@ define(["Chuck/ServerGame"], function(ServerGame) { this.serverGame.createPlayerForUser(user) } - Channel.prototype.releaseUser = function(user){ + Channel.prototype.releaseUser = function(user) { + this.serverGame.userIdLeft(user.id); + + this.sendCommandToAllUsersExcept("userLeft", user.id, user); delete this.users[user.id]; }