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

@ -2,8 +2,58 @@ define([
"Game/Core/GameObjects/Item"
],
function(Parent) {
return Parent;
function (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) {
this.handAction(x, y, isHolding, item);
var message = {
handActionResponse: {
playerId: this.id,
isHolding: isHolding,
itemUid: item.uid,
x: x,
y: y
}
};
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
}
}
Player.prototype.handAction = function(x, y, isHolding, item) {
var options = {
playerId: this.id,
itemUid: item.uid
}
var message = {
handActionResponse: options
}
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);
}
}
};
return Player;