chuck.js/app/Game/Core/Player.js

164 lines
4.4 KiB
JavaScript
Executable file

define([
"Game/" + GLOBALS.context + "/GameObjects/Doll",
"Game/Config/Settings",
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Exception",
"Game/" + GLOBALS.context + "/GameObjects/SpectatorDoll",
"Game/" + GLOBALS.context + "/GameObjects/Items/RagDoll"
],
function (Doll, Settings, NotificationCenter, Exception, SpectatorDoll, RagDoll) {
function Player (id, physicsEngine) {
this.stats = {
health: 100,
deaths: 0,
score: 0
}
this.physicsEngine = physicsEngine;
this.playerController = null;
this.doll;
this.id = id;
this.isSpawned = false;
this.holdingItem = null;
this.spectatorDoll = new SpectatorDoll(this.physicsEngine, "spectatorDoll-" + this.id, this);
}
Player.prototype.getDoll = function() {
throw new Exception('-- PLEASE REMOVE getDoll Calls --');
return this.doll;
};
Player.prototype.getActiveDoll = function() {
if(this.isSpawned) {
return this.doll;
} else if (this.ragDoll) {
return this.ragDoll;
}
return this.spectatorDoll;
};
Player.prototype.spawn = function (x, y) {
this.doll = new Doll(this.physicsEngine, "doll-" + this.id, this);
this.doll.spawn(x, y);
this.isSpawned = true;
NotificationCenter.trigger("game/object/add", 'animated', this.doll);
}
Player.prototype.getPosition = function () {
return this.getActiveDoll().getPosition();
}
Player.prototype.getHeadPosition = function () {
return this.getActiveDoll().getHeadPosition();
}
Player.prototype.move = function (direction) {
if(!this.isSpawned) return false;
this.doll.move(direction);
}
Player.prototype.stop = function () {
if(!this.isSpawned) return false;
this.doll.stop();
}
Player.prototype.jump = function () {
if(!this.isSpawned) return false;
this.doll.jump();
}
Player.prototype.jumpStop = function () {
if(!this.isSpawned) return false;
this.doll.jumpStop();
}
Player.prototype.lookAt = function (x, y) {
if(!this.isSpawned) return false;
// FIXME implement spectator movement here
this.doll.lookAt(x, y);
}
Player.prototype.grab = function(item) {
if(!this.isSpawned) return false;
this.doll.grab(item);
item.beingGrabbed(this);
this.holdingItem = item;
};
Player.prototype.throw = function(x, y, item) {
if(!this.isSpawned) return false;
this.doll.throw(item, x, y);
item.beingReleased(this);
this.holdingItem = null;
};
Player.prototype.kill = function(killedByPlayer) {
if(!this.isSpawned) return false;
// FIXME: do something better then just respawn in GameController
if(this.holdingItem) {
this.throw(0, 0, this.holdingItem)
}
// get forces
var options = {
x: this.getPosition().x * Settings.RATIO,
y: this.getPosition().y * Settings.RATIO,
category: "graveyard",
grabAngle: -0.3,
image: "chest.png",
name: "RagDoll",
rotation: 0,
type: "ragdoll",
weight: 3,
width: 5,
height: 12
};
var ragDoll = new RagDoll(this.physicsEngine, "ragDoll-" + this.id, options);
ragDoll.setVelocities(this.doll.getVelocities());
this.isSpawned = false;
NotificationCenter.trigger("game/object/remove", 'animated', this.doll);
this.doll.destroy();
this.doll = null;
this.ragDoll = ragDoll;
NotificationCenter.trigger("player/killed", this);
};
Player.prototype.update = function () {
if(this.doll) {
this.doll.update();
}
if(this.playerController) {
this.playerController.update();
}
}
Player.prototype.destroy = function () {
if(this.holdingItem) {
this.throw(0, 0, this.holdingItem);
}
this.spectatorDoll.destroy();
if(this.doll) this.doll.destroy();
if(this.ragDoll) this.ragDoll.destroy();
}
Player.prototype.setPlayerController = function(playerController) {
this.playerController = playerController;
}
return Player;
});