mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
fixed problem when user disconnected
This commit is contained in:
parent
e751210345
commit
7d0df79ac8
8 changed files with 73 additions and 33 deletions
|
|
@ -2,20 +2,28 @@ define(["Chuck/Processors/ClientProcessor"], function(ClientProcessor) {
|
||||||
|
|
||||||
function ClientGame(networker, id) {
|
function ClientGame(networker, id) {
|
||||||
this.networker = networker;
|
this.networker = networker;
|
||||||
this.processor = new ClientProcessor(this);
|
this.clientProcessor = new ClientProcessor(this);
|
||||||
this.processor.spawnMeWithId(id);
|
this.clientProcessor.spawnMeWithId(id);
|
||||||
|
|
||||||
|
this.players = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientGame.prototype.loadLevel = function(path) {
|
ClientGame.prototype.loadLevel = function(path) {
|
||||||
this.processor.loadLevel(path);
|
this.clientProcessor.loadLevel(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientGame.prototype.userJoined = function(userId) {
|
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){
|
ClientGame.prototype.processGameCommand = function(command, options){
|
||||||
this.processor.processGameCommand(command, options);
|
this.clientProcessor.processGameCommand(command, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientGame.prototype.sendGameCommand = function(command, options) {
|
ClientGame.prototype.sendGameCommand = function(command, options) {
|
||||||
|
|
@ -23,7 +31,7 @@ define(["Chuck/Processors/ClientProcessor"], function(ClientProcessor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientGame.prototype.destruct = function(){
|
ClientGame.prototype.destruct = function(){
|
||||||
this.processor.destruct();
|
this.clientProcessor.destruct();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ClientGame;
|
return ClientGame;
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
|
||||||
|
|
||||||
function Doll (physicsEngine, id){
|
function Doll (physicsEngine, id){
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this._physicsEngine = physicsEngine;
|
this.physicsEngine = physicsEngine;
|
||||||
this._body;
|
this.body;
|
||||||
this._legs;
|
this.legs;
|
||||||
this._contactPoint;
|
this.contactPoint;
|
||||||
|
|
||||||
this.init(this._physicsEngine.getWorld());
|
this.init(this.physicsEngine.getWorld());
|
||||||
}
|
}
|
||||||
|
|
||||||
Doll.prototype.init = function (world) {
|
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.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
||||||
bodyDef.userData = 'player-' + this.id;
|
bodyDef.userData = 'player-' + this.id;
|
||||||
|
|
||||||
this._body = world.CreateBody(bodyDef);
|
this.body = world.CreateBody(bodyDef);
|
||||||
|
|
||||||
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
|
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
|
||||||
fixtureDef.density = Settings.PLAYER_DENSITY;
|
fixtureDef.density = Settings.PLAYER_DENSITY;
|
||||||
|
|
@ -33,14 +33,14 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
|
||||||
fixtureDef.shape = headShape;
|
fixtureDef.shape = headShape;
|
||||||
fixtureDef.isSensor = false;
|
fixtureDef.isSensor = false;
|
||||||
fixtureDef.userData = 'myHead-' + this.id;
|
fixtureDef.userData = 'myHead-' + this.id;
|
||||||
this._body.CreateFixture(fixtureDef);
|
this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
|
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));
|
bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO));
|
||||||
fixtureDef.shape = bodyShape;
|
fixtureDef.shape = bodyShape;
|
||||||
fixtureDef.isSensor = false;
|
fixtureDef.isSensor = false;
|
||||||
fixtureDef.userData = 'myBody-' + this.id;
|
fixtureDef.userData = 'myBody-' + this.id;
|
||||||
this._body.CreateFixture(fixtureDef);
|
this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
|
var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||||
legsShape.SetRadius(5 / Settings.RATIO);
|
legsShape.SetRadius(5 / Settings.RATIO);
|
||||||
|
|
@ -49,7 +49,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
|
||||||
fixtureDef.friction = Settings.PLAYER_FRICTION;
|
fixtureDef.friction = Settings.PLAYER_FRICTION;
|
||||||
fixtureDef.isSensor = false;
|
fixtureDef.isSensor = false;
|
||||||
fixtureDef.userData = 'myLegs-' + this.id;
|
fixtureDef.userData = 'myLegs-' + this.id;
|
||||||
this._legs = this._body.CreateFixture(fixtureDef);
|
this.legs = this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
var feetShape = new Box2D.Collision.Shapes.b2CircleShape();
|
var feetShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||||
feetShape.SetRadius(4 / Settings.RATIO);
|
feetShape.SetRadius(4 / Settings.RATIO);
|
||||||
|
|
@ -57,45 +57,45 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
|
||||||
fixtureDef.shape = feetShape;
|
fixtureDef.shape = feetShape;
|
||||||
fixtureDef.isSensor = true;
|
fixtureDef.isSensor = true;
|
||||||
fixtureDef.userData = 'myFeet-' + this.id;
|
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) {
|
Doll.prototype.spawn = function (x, y) {
|
||||||
this._body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
|
this.body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
|
||||||
this._body.SetActive(true);
|
this.body.SetActive(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Doll.prototype.getBody = function () {
|
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(!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) {
|
Doll.prototype.move = function (direction, speed) {
|
||||||
this._setFriction(Settings.PLAYER_MOTION_FRICTION);
|
this.setFriction(Settings.PLAYER_MOTION_FRICTION);
|
||||||
this._body.SetAwake(true);
|
this.body.SetAwake(true);
|
||||||
var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this._body.GetLinearVelocity().y);
|
var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y);
|
||||||
this._body.SetLinearVelocity(vector);
|
this.body.SetLinearVelocity(vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
Doll.prototype.stop = function () {
|
Doll.prototype.stop = function () {
|
||||||
this._setFriction(Settings.PLAYER_FRICTION);
|
this.setFriction(Settings.PLAYER_FRICTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
Doll.prototype.jump = function () {
|
Doll.prototype.jump = function () {
|
||||||
this._body.SetAwake(true);
|
this.body.SetAwake(true);
|
||||||
|
|
||||||
var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED);
|
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?
|
// maybe change to a constant force instead of applying of force?
|
||||||
// to prevent higher jumping running uphill, etc.
|
// to prevent higher jumping running uphill, etc.
|
||||||
|
|
@ -103,7 +103,11 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
|
||||||
|
|
||||||
Doll.prototype.jumping = function () {
|
Doll.prototype.jumping = function () {
|
||||||
var vector = new Box2D.Common.Math.b2Vec2(0, -0.05);
|
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;
|
return Doll;
|
||||||
|
|
|
||||||
|
|
@ -180,5 +180,9 @@ define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Player.prototype.destroy = function() {
|
||||||
|
this.doll.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
return Player;
|
return Player;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,10 @@ define(requires,
|
||||||
this.inputControlUnit = new InputControlUnit(this.me, this);
|
this.inputControlUnit = new InputControlUnit(this.me, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ClientProcessor.prototype.userIdLeft = function(userId) {
|
||||||
|
// body...
|
||||||
|
};
|
||||||
|
|
||||||
ClientProcessor.prototype.sendGameCommand = function(command, options) {
|
ClientProcessor.prototype.sendGameCommand = function(command, options) {
|
||||||
this.clientGame.sendGameCommand(command, options);
|
this.clientGame.sendGameCommand(command, options);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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() {
|
ServerProcessor.prototype.updateWorld = function() {
|
||||||
|
|
||||||
var update = {};
|
var update = {};
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,10 @@ define(["Chuck/Processors/ServerProcessor"], function(ServerProcessor) {
|
||||||
this.serverProcessor.createPlayerWithId(user.id);
|
this.serverProcessor.createPlayerWithId(user.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServerGame.prototype.userIdLeft = function(userId) {
|
||||||
|
this.serverProcessor.userIdLeft(userId);
|
||||||
|
};
|
||||||
|
|
||||||
ServerGame.prototype.updateClientsWorld = function(update_world) {
|
ServerGame.prototype.updateClientsWorld = function(update_world) {
|
||||||
this.channel.sendCommandToAllUsers('gameCommand', {worldUpdate: update_world});
|
this.channel.sendCommandToAllUsers('gameCommand', {worldUpdate: update_world});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,10 @@ define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientG
|
||||||
this.sendCommand('gameCommand', message);
|
this.sendCommand('gameCommand', message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Networker.prototype.onUserLeft = function(userId) {
|
||||||
|
this.clientGame.userLeft(userId);
|
||||||
|
}
|
||||||
|
|
||||||
Networker.prototype.processControlCommand = function(command, options) {
|
Networker.prototype.processControlCommand = function(command, options) {
|
||||||
switch(command) {
|
switch(command) {
|
||||||
case 'joinSuccess':
|
case 'joinSuccess':
|
||||||
|
|
@ -88,6 +92,10 @@ define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientG
|
||||||
this.onUserJoined(options);
|
this.onUserJoined(options);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'userLeft':
|
||||||
|
this.onUserLeft(options);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,10 @@ define(["Chuck/ServerGame"], function(ServerGame) {
|
||||||
this.serverGame.createPlayerForUser(user)
|
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];
|
delete this.users[user.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue