added areaSensor to doll and not allowing client to update position when another player is nearby

This commit is contained in:
logsol 2014-05-29 20:27:41 +02:00
parent e7f4b6043d
commit b500ef436d
5 changed files with 78 additions and 18 deletions

View file

@ -17,6 +17,7 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
this.width = 9;
this.headHeight = 12;
this.reachDistance = 20;
this.areaSize = 25;
Parent.call(this, physicsEngine, uid);
@ -30,6 +31,7 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
left: [],
right: []
};
this.nearbyDolls = [];
this.holdingJoint = null;
this.holdingItem = null;
@ -141,6 +143,40 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
}
this.body.CreateFixture(fixtureDef);
// Area Sensor
var areaSensorShape = new Box2D.Collision.Shapes.b2PolygonShape();
areaSensorShape.SetAsOrientedBox(
(this.width + this.areaSize) / 2 / Settings.RATIO,
(this.height + this.areaSize) / 2 / Settings.RATIO,
new Box2D.Common.Math.b2Vec2(
0,
-this.height / 2 / Settings.RATIO
)
);
fixtureDef.shape = areaSensorShape;
fixtureDef.isSensor = true;
fixtureDef.userData = {
onCollisionChange: function(isColliding, fixture) {
var userData = fixture.GetBody().GetUserData()
if(userData instanceof Doll) {
var doll = userData;
var i = this.nearbyDolls.indexOf(doll);
if(isColliding) {
if(i === -1) {
this.nearbyDolls.push(doll);
}
} else {
if(i !== -1) {
this.nearbyDolls.slice(i, 1);
}
}
}
}
}
this.body.CreateFixture(fixtureDef);
}
Doll.prototype.setActionState = function(state) {
@ -323,6 +359,10 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
item.throw(x, y);
};
Doll.prototype.isAnotherPlayerNearby = function() {
return this.nearbyDolls.length > 0;
};
Doll.prototype.onFootSensorDetection = function(isColliding, fixture) {
var hasJumpStartVelocity = this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED;