mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
refactored grabbing
This commit is contained in:
parent
1c4336c7f7
commit
38b5023410
7 changed files with 124 additions and 62 deletions
|
|
@ -7,9 +7,9 @@ function (Box2D) {
|
|||
function Detector () { // FIXME evtl.bind(this) ?
|
||||
this.listener = new Box2D.Dynamics.b2ContactListener();
|
||||
this.listener.chuckDetector = this;
|
||||
this.listener.BeginContact = this.BeginContact;
|
||||
//this.listener.PostSolve = this.PostSolve;
|
||||
this.listener.EndContact = this.EndContact;
|
||||
this.listener.BeginContact = this.beginContact;
|
||||
//this.listener.PostSolve = this.postSolve;
|
||||
this.listener.EndContact = this.endContact;
|
||||
}
|
||||
|
||||
Detector.IDENTIFIER = {
|
||||
|
|
@ -34,14 +34,14 @@ function (Box2D) {
|
|||
|
||||
/** Extension **/
|
||||
|
||||
Detector.prototype.BeginContact = function (point) {
|
||||
Detector.prototype.beginContact = function (point) {
|
||||
this.chuckDetector.onCollisionChange(point, true);
|
||||
}
|
||||
|
||||
Detector.prototype.PostSolve = function (point, impulse) {
|
||||
Detector.prototype.postSolve = function (point, impulse) {
|
||||
}
|
||||
|
||||
Detector.prototype.EndContact = function (point) {
|
||||
Detector.prototype.endContact = function (point) {
|
||||
this.chuckDetector.onCollisionChange(point, false);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ function (Parent, Box2D, Settings) {
|
|||
this.createFixture();
|
||||
this.body.ResetMassData();
|
||||
this.flipDirection = 1;
|
||||
this.heldByPlayers = [];
|
||||
}
|
||||
|
||||
Item.prototype = Object.create(Parent.prototype);
|
||||
|
|
@ -29,6 +28,7 @@ function (Parent, Box2D, Settings) {
|
|||
}
|
||||
|
||||
Item.prototype.createFixture = function () {
|
||||
var self = this;
|
||||
|
||||
var itemShape;
|
||||
var w = this.options.width / Settings.RATIO;
|
||||
|
|
@ -60,6 +60,14 @@ function (Parent, Box2D, Settings) {
|
|||
|
||||
fixtureDef.isSensor = false;
|
||||
|
||||
/*
|
||||
fixtureDef.userData = {
|
||||
onCollisionChange: function(isColliding, fixture) {
|
||||
self.onFixtureWithinReach(isColliding, "right", fixture);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
this.body.CreateFixture(fixtureDef);
|
||||
}
|
||||
|
||||
|
|
@ -69,29 +77,12 @@ function (Parent, Box2D, Settings) {
|
|||
// FIXME: implement body flip if necessary
|
||||
};
|
||||
|
||||
Item.prototype.isGrabbingAllowed = function(player) {
|
||||
return this.heldByPlayers.length == 0;
|
||||
};
|
||||
|
||||
Item.prototype.beingGrabbed = function(player) {
|
||||
if(this.isGrabbingAllowed(player)) {
|
||||
this.heldByPlayers.push(player);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
Item.prototype.isReleasingAllowed = function(player) {
|
||||
return true;
|
||||
// overwrite if necessary
|
||||
};
|
||||
|
||||
Item.prototype.beingReleased = function(player) {
|
||||
if(this.isReleasingAllowed(player)) {
|
||||
var pos = this.heldByPlayers.indexOf(player);
|
||||
if(pos >= 0) {
|
||||
this.heldByPlayers.splice(pos, 1);
|
||||
}
|
||||
}
|
||||
// overwrite if necessary
|
||||
};
|
||||
|
||||
return Item;
|
||||
|
|
|
|||
|
|
@ -52,23 +52,16 @@ function (Doll, Settings) {
|
|||
if(this.doll) this.doll.lookAt(x, y);
|
||||
}
|
||||
|
||||
Player.prototype.handAction = function(x, y, isHolding, item) {
|
||||
|
||||
if (isHolding) {
|
||||
// throw
|
||||
if(item.isReleasingAllowed()) {
|
||||
item.beingReleased(this);
|
||||
this.doll.throw(item, x, y);
|
||||
this.holdingItem = null;
|
||||
}
|
||||
} else {
|
||||
// take
|
||||
if(item.isGrabbingAllowed()) {
|
||||
item.beingGrabbed(this);
|
||||
this.doll.grab(item);
|
||||
this.holdingItem = item;
|
||||
}
|
||||
}
|
||||
Player.prototype.grab = function(item) {
|
||||
item.beingGrabbed(this);
|
||||
this.doll.grab(item);
|
||||
this.holdingItem = item;
|
||||
};
|
||||
|
||||
Player.prototype.throw = function(x, y, item) {
|
||||
item.beingReleased(this);
|
||||
this.doll.throw(item, x, y);
|
||||
this.holdingItem = null;
|
||||
};
|
||||
|
||||
Player.prototype.update = function () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue