replaced killed doll with ragdoll - fixes #50

This commit is contained in:
logsol 2014-02-18 01:15:24 +01:00
parent aa6fdaa2df
commit 413254bfa4
19 changed files with 330 additions and 88 deletions

View file

@ -29,6 +29,10 @@ define(function () {
this.player.jump();
}
PlayerController.prototype.jumpStop = function () {
this.player.jumpStop();
}
PlayerController.prototype.lookAt = function (options) {
if(options) this.player.lookAt(options.x, options.y);
}

View file

@ -10,19 +10,36 @@ function (PhysicsEngine, TiledLevel, Player, NotificationCenter) {
function GameController () {
this.players = {};
this.level = null;
this.gameObjects = {
animated: [],
fixed: []
};
this.gameObjects = null;
this.resetGameObjects();
this.physicsEngine = new PhysicsEngine();
this.physicsEngine.setCollisionDetector();
NotificationCenter.on("game/level/loaded", this.onLevelLoaded, this);
NotificationCenter.on("game/object/add", this.onGameObjectAdd, this);
NotificationCenter.on("game/object/remove", this.onGameObjectRemove, this);
}
GameController.prototype.update = function() {
// extend for both sides if necessary
};
GameController.prototype.resetGameObjects = function() {
this.gameObjects = {
fixed: [],
animated: []
};
};
GameController.prototype.onGameObjectAdd = function(type, object) {
this.gameObjects[type].push(object);
};
GameController.prototype.onGameObjectRemove = function(type, object) {
var i = this.gameObjects[type].indexOf(object);
if(i>=0) this.gameObjects[type].splice(i, 1);
};
GameController.prototype.getPhysicsEngine = function () {
@ -33,10 +50,7 @@ function (PhysicsEngine, TiledLevel, Player, NotificationCenter) {
if (this.level) {
this.level.destroy();
this.gameObjects = {
animated: [],
fixed: []
};
this.resetGameObjects();
}
this.level = new TiledLevel(levelUid, this.physicsEngine, this.gameObjects);
@ -63,6 +77,10 @@ function (PhysicsEngine, TiledLevel, Player, NotificationCenter) {
GameController.prototype.userLeft = function (user) {
var player = this.players[user.id];
var i = this.gameObjects.animated.indexOf(player);
if(i>=0) this.gameObjects.animated.splice(i, 1);
player.destroy();
delete this.players[user.id];
}

View file

@ -158,16 +158,8 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
this.setActionState("fall");
}
Doll.prototype.kill = function() {
this.body.SetFixedRotation(false);
};
Doll.prototype.getPosition = function() {
var pos = this.body.GetPosition();
return {
x: pos.x,
y: pos.y
};
return this.body.GetPosition().Copy();
};
Doll.prototype.getHeadPosition = function() {
@ -235,7 +227,13 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
Doll.prototype.stop = function () {
this.moveDirection = 0;
this.setFriction(Settings.PLAYER_FRICTION);
if(this.isStanding()) this.setActionState("stand");
if(this.isStanding()) {
this.setActionState("stand");
} else {
var vector = this.body.GetLinearVelocity().Copy();
vector.x *= Settings.JUMP_STOP_DAMPING_FACTOR;
this.body.SetLinearVelocity(vector);
}
}
Doll.prototype.jump = function () {
@ -251,6 +249,17 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
}
}
Doll.prototype.jumpStop = function () {
if (!this.isStanding() ) {
this.body.SetAwake(true);
var vector = this.body.GetLinearVelocity().Copy();
if(vector.y < 0) {
vector.y *= Settings.JUMP_STOP_DAMPING_FACTOR;
this.body.SetLinearVelocity(vector);
}
}
}
Doll.prototype.setStanding = function (isStanding) {
if (this.standing == isStanding) return;
this.standing = isStanding;
@ -344,6 +353,13 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
}
}
Doll.prototype.getVelocities = function() {
return {
linearVelocity: this.body.GetLinearVelocity(),
angularVelocity: this.body.GetAngularVelocity()
};
};
Doll.prototype.update = function() {
if (this.body.GetLinearVelocity().x == 0 && this.isWalking()) {

View file

@ -1,10 +1,11 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/Item",
"Lib/Vendor/Box2D",
"Game/Config/Settings"
"Game/Config/Settings",
"Lib/Utilities/NotificationCenter"
],
function (Parent, Box2D, Settings) {
function (Parent, Box2D, Settings, NotificationCenter) {
function RagDoll(physicsEngine, uid, options) {
@ -160,6 +161,9 @@ function (Parent, Box2D, Settings) {
0,
options.limbs.upperRightArm.height / 2
);
NotificationCenter.trigger("game/object/add", 'animated', this);
}
RagDoll.prototype = Object.create(Parent.prototype);
@ -168,6 +172,14 @@ function (Parent, Box2D, Settings) {
return 55; //parseInt(this.uid.split("-")[1], 10);
};
RagDoll.prototype.getPosition = function() {
return this.body.GetPosition().Copy();
};
RagDoll.prototype.getHeadPosition = function() {
return this.limbs.head.GetPosition().Copy();
};
RagDoll.prototype.getBodyDef = function() {
var bodyDef = Parent.prototype.getBodyDef.call(this);
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
@ -212,11 +224,6 @@ function (Parent, Box2D, Settings) {
this.body.CreateFixture(fixtureDef);
};
RagDoll.prototype.destroy = function() {
Parent.prototype.destroy.call(this);
// remove head!!11
};
RagDoll.prototype.addHead = function() {
var x = this.options.x + this.options.limbs.head.x,
y = this.options.y + this.options.limbs.head.y;
@ -341,11 +348,29 @@ function (Parent, Box2D, Settings) {
x * Settings.MAX_THROW_FORCE * limbDampingFactor,
-y * Settings.MAX_THROW_FORCE * 1.5 *limbDampingFactor // 1.5 is to throw higher then far
);
this.body.SetLinearVelocity(vector);
body.SetLinearVelocity(vector);
// body.SetAngularVelocity(Settings.MAX_THROW_ANGULAR_VELOCITY * x);
}
};
RagDoll.prototype.setVelocities = function(options) {
this.body.SetLinearVelocity(options.linearVelocity);
this.body.SetAngularVelocity(options.angularVelocity);
for(var name in this.limbs) {
this.limbs[name].SetLinearVelocity(options.linearVelocity);
}
};
RagDoll.prototype.destroy = function() {
NotificationCenter.trigger("game/object/remove", 'animated', this);
var world = this.body.GetWorld();
Parent.prototype.destroy.call(this);
for (var name in this.limbs) {
world.DestroyBody(this.limbs[name]);
}
};
return RagDoll;
});

View file

@ -0,0 +1,37 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Vendor/Box2D"
],
function (Parent, Box2D) {
function SpectatorDoll(physicsEngine, uid, player) {
Parent.call(this, physicsEngine, uid);
}
SpectatorDoll.prototype = Object.create(Parent.prototype);
SpectatorDoll.prototype.getBodyDef = function() {
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
bodyDef.position.x = this.getPosition().x;
bodyDef.position.y = this.getPosition().y;
bodyDef.angle = 0;
return bodyDef;
};
SpectatorDoll.prototype.getPosition = function() {
return {x: 10, y: 10};
}
SpectatorDoll.prototype.getHeadPosition = function() {
return {x: 10, y: 10};
}
SpectatorDoll.prototype.update = function() {
};
return SpectatorDoll;
});

View file

@ -1,11 +1,14 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/Doll",
"Game/Config/Settings",
"Lib/Utilities/NotificationCenter"
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Exception",
"Game/" + GLOBALS.context + "/GameObjects/SpectatorDoll",
"Game/" + GLOBALS.context + "/GameObjects/Items/RagDoll"
],
function (Doll, Settings, NotificationCenter) {
function (Doll, Settings, NotificationCenter, Exception, SpectatorDoll, RagDoll) {
function Player (id, physicsEngine) {
this.stats = {
@ -20,28 +23,36 @@ function (Doll, Settings, NotificationCenter) {
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.spawn = function (x, y) {
if(this.doll) {
this.doll.destroy();
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.doll.getPosition();
return this.getActiveDoll().getPosition();
}
Player.prototype.getHeadPosition = function () {
if(!this.isSpawned) return false;
return this.doll.getHeadPosition();
return this.getActiveDoll().getHeadPosition();
}
@ -60,8 +71,14 @@ function (Doll, Settings, NotificationCenter) {
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);
}
@ -86,8 +103,35 @@ function (Doll, Settings, NotificationCenter) {
if(this.holdingItem) {
this.throw(0, 0, this.holdingItem)
}
this.doll.kill();
// 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);
};
@ -106,7 +150,10 @@ function (Doll, Settings, NotificationCenter) {
if(this.holdingItem) {
this.throw(0, 0, this.holdingItem);
}
this.doll.destroy();
this.spectatorDoll.destroy();
if(this.doll) this.doll.destroy();
if(this.ragDoll) this.ragDoll.destroy();
}
Player.prototype.setPlayerController = function(playerController) {