mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
added areaSensor to doll and not allowing client to update position when another player is nearby
This commit is contained in:
parent
e7f4b6043d
commit
b500ef436d
5 changed files with 78 additions and 18 deletions
|
|
@ -40,7 +40,7 @@ function(Parent, Nc, Parser, Settings) {
|
||||||
this.player.suicide();
|
this.player.suicide();
|
||||||
};
|
};
|
||||||
|
|
||||||
PlayerController.prototype.meStateUpdate = function(update) {
|
PlayerController.prototype.mePositionStateUpdate = function(update) {
|
||||||
|
|
||||||
if(!this.player.doll) {
|
if(!this.player.doll) {
|
||||||
console.warn('me state update, even though doll does not exist');
|
console.warn('me state update, even though doll does not exist');
|
||||||
|
|
@ -54,10 +54,10 @@ function(Parent, Nc, Parser, Settings) {
|
||||||
|
|
||||||
if(difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS
|
if(difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS
|
||||||
|| difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) {
|
|| difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) {
|
||||||
this.player.doll.body.SetAwake(true);
|
this.player.doll.updatePositionState(update.p);
|
||||||
this.player.doll.body.SetPosition(update.p);
|
|
||||||
} else {
|
} else {
|
||||||
// HARD UPDATE FOR SELF
|
// HARD UPDATE FOR SELF
|
||||||
|
console.log(this.player.user.options.nickname + ' is cheating.')
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,13 @@ function (Parent, Item, Box2D, Nc) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Doll.prototype.updatePositionState = function(positionState) {
|
||||||
|
if(!this.isAnotherPlayerNearby()) {
|
||||||
|
this.body.SetAwake(true);
|
||||||
|
this.body.SetPosition(positionState);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return Doll;
|
return Doll;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -54,7 +54,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
|
|
||||||
if(this.me) {
|
if(this.me) {
|
||||||
this.me.update();
|
this.me.update();
|
||||||
this.localMePositionUpdate();
|
this.mePositionStateUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
||||||
|
|
@ -66,9 +66,9 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
DomController.statsEnd();
|
DomController.statsEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController.prototype.localMePositionUpdate = function() {
|
GameController.prototype.mePositionStateUpdate = function() {
|
||||||
if(this.me.isStateUpdateNeeded()) {
|
if(this.me.isPositionStateUpdateNeeded()) {
|
||||||
Nc.trigger(Nc.ns.client.to.server.gameCommand.send, "meStateUpdate", this.me.getStateUpdate());
|
Nc.trigger(Nc.ns.client.to.server.gameCommand.send, "mePositionStateUpdate", this.me.getPositionStateUpdate());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -119,8 +119,10 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
|
|
||||||
if (gameObject instanceof Doll) {
|
if (gameObject instanceof Doll) {
|
||||||
if(gameObject === this.me.doll) {
|
if(gameObject === this.me.doll) {
|
||||||
this.me.setLastServerState(update);
|
this.me.setLastServerPositionState(update);
|
||||||
continue; // this is to ignore own doll updates from world update
|
if(!this.me.acceptPositionStateUpdateFromServer()) {
|
||||||
|
continue; // this is to ignore own doll updates from world update
|
||||||
|
}
|
||||||
}
|
}
|
||||||
gameObject.setActionState(update.as);
|
gameObject.setActionState(update.as);
|
||||||
gameObject.lookAt(update.laxy.x, update.laxy.y);
|
gameObject.lookAt(update.laxy.x, update.laxy.y);
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ function (Parent, Settings) {
|
||||||
function Me(id, physicsEngine, user) {
|
function Me(id, physicsEngine, user) {
|
||||||
Parent.call(this, id, physicsEngine, user);
|
Parent.call(this, id, physicsEngine, user);
|
||||||
|
|
||||||
this.lastServerState = {
|
this.lastServerPositionState = {
|
||||||
p: {
|
p: {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0
|
y: 0
|
||||||
|
|
@ -18,17 +18,23 @@ function (Parent, Settings) {
|
||||||
|
|
||||||
Me.prototype = Object.create(Parent.prototype);
|
Me.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
Me.prototype.setLastServerState = function(update) {
|
Me.prototype.setLastServerPositionState = function(update) {
|
||||||
this.lastServerState = update;
|
this.lastServerPositionState = update;
|
||||||
};
|
};
|
||||||
|
|
||||||
Me.prototype.isStateUpdateNeeded = function() {
|
Me.prototype.isPositionStateUpdateNeeded = function() {
|
||||||
|
|
||||||
if(!this.doll) return false;
|
if(!this.doll) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.doll.isAnotherPlayerNearby()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var difference = {
|
var difference = {
|
||||||
x: Math.abs(this.lastServerState.p.x - this.doll.body.GetPosition().x),
|
x: Math.abs(this.lastServerPositionState.p.x - this.doll.body.GetPosition().x),
|
||||||
y: Math.abs(this.lastServerState.p.y - this.doll.body.GetPosition().y)
|
y: Math.abs(this.lastServerPositionState.p.y - this.doll.body.GetPosition().y)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS
|
if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS
|
||||||
|
|
@ -39,13 +45,18 @@ function (Parent, Settings) {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
Me.prototype.getStateUpdate = function() {
|
Me.prototype.getPositionStateUpdate = function() {
|
||||||
return {
|
return {
|
||||||
p: this.doll.body.GetPosition().Copy(),
|
p: this.doll.body.GetPosition().Copy(),
|
||||||
lv: this.doll.body.GetLinearVelocity().Copy()
|
lv: this.doll.body.GetLinearVelocity().Copy()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Me.prototype.acceptPositionStateUpdateFromServer = function() {
|
||||||
|
// gamecontroller should accept me's doll update only when another players doll is nearby.
|
||||||
|
return this.doll.isAnotherPlayerNearby();
|
||||||
|
};
|
||||||
|
|
||||||
return Me;
|
return Me;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -17,6 +17,7 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
|
||||||
this.width = 9;
|
this.width = 9;
|
||||||
this.headHeight = 12;
|
this.headHeight = 12;
|
||||||
this.reachDistance = 20;
|
this.reachDistance = 20;
|
||||||
|
this.areaSize = 25;
|
||||||
|
|
||||||
Parent.call(this, physicsEngine, uid);
|
Parent.call(this, physicsEngine, uid);
|
||||||
|
|
||||||
|
|
@ -30,6 +31,7 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
|
||||||
left: [],
|
left: [],
|
||||||
right: []
|
right: []
|
||||||
};
|
};
|
||||||
|
this.nearbyDolls = [];
|
||||||
this.holdingJoint = null;
|
this.holdingJoint = null;
|
||||||
this.holdingItem = null;
|
this.holdingItem = null;
|
||||||
|
|
||||||
|
|
@ -141,6 +143,40 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.body.CreateFixture(fixtureDef);
|
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) {
|
Doll.prototype.setActionState = function(state) {
|
||||||
|
|
@ -323,6 +359,10 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) {
|
||||||
item.throw(x, y);
|
item.throw(x, y);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Doll.prototype.isAnotherPlayerNearby = function() {
|
||||||
|
return this.nearbyDolls.length > 0;
|
||||||
|
};
|
||||||
|
|
||||||
Doll.prototype.onFootSensorDetection = function(isColliding, fixture) {
|
Doll.prototype.onFootSensorDetection = function(isColliding, fixture) {
|
||||||
|
|
||||||
var hasJumpStartVelocity = this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED;
|
var hasJumpStartVelocity = this.body.GetLinearVelocity().y < -Settings.JUMP_SPEED;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue