fixed problem when user disconnected

This commit is contained in:
Jeena Paradies 2012-07-14 00:04:41 +02:00
parent e751210345
commit 7d0df79ac8
8 changed files with 73 additions and 33 deletions

View file

@ -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;

View file

@ -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;

View file

@ -180,5 +180,9 @@ define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
}
}
Player.prototype.destroy = function() {
this.doll.destroy();
}
return Player;
});

View file

@ -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);
}

View file

@ -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 = {};

View file

@ -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});
}

View file

@ -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;
}

View file

@ -23,6 +23,9 @@ define(["Chuck/ServerGame"], function(ServerGame) {
}
Channel.prototype.releaseUser = function(user) {
this.serverGame.userIdLeft(user.id);
this.sendCommandToAllUsersExcept("userLeft", user.id, user);
delete this.users[user.id];
}