refactored grabbing

This commit is contained in:
Jeena 2014-01-20 18:15:40 +01:00
parent 1c4336c7f7
commit 38b5023410
7 changed files with 124 additions and 62 deletions

View file

@ -105,7 +105,16 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
} }
}; };
player.handAction(options.x, options.y, options.isHolding, item); if(item) {
if(options.action == "throw") {
player.throw(options.x, options.y, item);
} else if(options.action == "grab") {
player.grab(item);
}
} else {
console.warn("Item for joint can not be found locally.")
}
}; };
GameController.prototype.loadLevel = function (path) { GameController.prototype.loadLevel = function (path) {

View file

@ -7,9 +7,9 @@ function (Box2D) {
function Detector () { // FIXME evtl.bind(this) ? function Detector () { // FIXME evtl.bind(this) ?
this.listener = new Box2D.Dynamics.b2ContactListener(); this.listener = new Box2D.Dynamics.b2ContactListener();
this.listener.chuckDetector = this; this.listener.chuckDetector = this;
this.listener.BeginContact = this.BeginContact; this.listener.BeginContact = this.beginContact;
//this.listener.PostSolve = this.PostSolve; //this.listener.PostSolve = this.postSolve;
this.listener.EndContact = this.EndContact; this.listener.EndContact = this.endContact;
} }
Detector.IDENTIFIER = { Detector.IDENTIFIER = {
@ -34,14 +34,14 @@ function (Box2D) {
/** Extension **/ /** Extension **/
Detector.prototype.BeginContact = function (point) { Detector.prototype.beginContact = function (point) {
this.chuckDetector.onCollisionChange(point, true); 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); this.chuckDetector.onCollisionChange(point, false);
} }

View file

@ -12,7 +12,6 @@ function (Parent, Box2D, Settings) {
this.createFixture(); this.createFixture();
this.body.ResetMassData(); this.body.ResetMassData();
this.flipDirection = 1; this.flipDirection = 1;
this.heldByPlayers = [];
} }
Item.prototype = Object.create(Parent.prototype); Item.prototype = Object.create(Parent.prototype);
@ -29,6 +28,7 @@ function (Parent, Box2D, Settings) {
} }
Item.prototype.createFixture = function () { Item.prototype.createFixture = function () {
var self = this;
var itemShape; var itemShape;
var w = this.options.width / Settings.RATIO; var w = this.options.width / Settings.RATIO;
@ -60,6 +60,14 @@ function (Parent, Box2D, Settings) {
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
/*
fixtureDef.userData = {
onCollisionChange: function(isColliding, fixture) {
self.onFixtureWithinReach(isColliding, "right", fixture);
}
}
*/
this.body.CreateFixture(fixtureDef); this.body.CreateFixture(fixtureDef);
} }
@ -69,29 +77,12 @@ function (Parent, Box2D, Settings) {
// FIXME: implement body flip if necessary // FIXME: implement body flip if necessary
}; };
Item.prototype.isGrabbingAllowed = function(player) {
return this.heldByPlayers.length == 0;
};
Item.prototype.beingGrabbed = function(player) { Item.prototype.beingGrabbed = function(player) {
if(this.isGrabbingAllowed(player)) { // overwrite if necessary
this.heldByPlayers.push(player);
return true;
}
return false;
};
Item.prototype.isReleasingAllowed = function(player) {
return true;
}; };
Item.prototype.beingReleased = function(player) { Item.prototype.beingReleased = function(player) {
if(this.isReleasingAllowed(player)) { // overwrite if necessary
var pos = this.heldByPlayers.indexOf(player);
if(pos >= 0) {
this.heldByPlayers.splice(pos, 1);
}
}
}; };
return Item; return Item;

View file

@ -52,23 +52,16 @@ function (Doll, Settings) {
if(this.doll) this.doll.lookAt(x, y); if(this.doll) this.doll.lookAt(x, y);
} }
Player.prototype.handAction = function(x, y, isHolding, item) { Player.prototype.grab = function(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); item.beingGrabbed(this);
this.doll.grab(item); this.doll.grab(item);
this.holdingItem = 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 () { Player.prototype.update = function () {

View file

@ -4,6 +4,56 @@ define([
function (Parent) { function (Parent) {
return Parent; function Item(physicsEngine, uid, options) {
Parent.call(this, physicsEngine, uid, options);
this.heldByPlayers = [];
this.lastMoved = null;
}
Item.prototype = Object.create(Parent.prototype);
Item.prototype.setLastMovedBy = function(player) {
if(player) {
this.lastMoved = {
player: player,
timestamp: new Date()
}
} else {
this.lastMoved = null;
}
};
Item.prototype.isGrabbingAllowed = function(player) {
return this.heldByPlayers.length == 0;
};
Item.prototype.beingGrabbed = function(player) {
Parent.prototype.beingGrabbed.call(this, player);
if(this.isGrabbingAllowed(player)) {
this.heldByPlayers.push(player);
this.setLastMovedBy(null);
}
};
Item.prototype.isReleasingAllowed = function(player) {
return true;
};
Item.prototype.beingReleased = function(player) {
Parent.prototype.beingReleased.call(this, player);
if(this.isReleasingAllowed(player)) {
var pos = this.heldByPlayers.indexOf(player);
if(pos >= 0) {
this.heldByPlayers.splice(pos, 1);
this.setLastMovedBy(player);
}
}
};
return Item;
}); });

View file

@ -23,22 +23,41 @@ function (Parent, NotificationCenter) {
} }
if(item) { if(item) {
this.handAction(x, y, isHolding, item); this.handAction(x, y, isHolding, item);
}
}
Player.prototype.handAction = function(x, y, isHolding, item) {
var options = {
playerId: this.id,
itemUid: item.uid
}
var message = { var message = {
handActionResponse: { handActionResponse: options
playerId: this.id,
isHolding: isHolding,
itemUid: item.uid,
x: x,
y: y
} }
};
if (isHolding) {
// throw
if(item.isReleasingAllowed()) {
this.throw(x, y, item);
options.action = "throw";
options.x = x;
options.y = y;
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
}
} else {
// grab
if(item.isGrabbingAllowed()) {
this.grab(item);
options.action = "grab";
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
} }
} }
};
return Player; return Player;