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,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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue