added first damage and killing

This commit is contained in:
Jeena 2014-01-21 03:14:50 +01:00
parent 2a4327c5cf
commit f22e0dd53d
9 changed files with 124 additions and 52 deletions

View file

@ -4,17 +4,11 @@ define([
function (Box2D) {
function Detector () { // FIXME evtl.bind(this) ?
function Detector () {
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;
}
Detector.IDENTIFIER = {
TILE: "tile",
PLAYER: "player"
this.listener.BeginContact = this.beginContact.bind(this);
//this.listener.PostSolve = this.postSolve.bind(this);
this.listener.EndContact = this.endContact.bind(this);
}
Detector.prototype.getListener = function () {
@ -27,7 +21,9 @@ function (Box2D) {
if (userDataA && userDataA.onCollisionChange) {
userDataA.onCollisionChange(isColliding, point.GetFixtureB());
} else if (userDataB && userDataB.onCollisionChange) {
}
if (userDataB && userDataB.onCollisionChange) {
userDataB.onCollisionChange(isColliding, point.GetFixtureA());
}
}
@ -35,14 +31,24 @@ function (Box2D) {
/** Extension **/
Detector.prototype.beginContact = function (point) {
this.chuckDetector.onCollisionChange(point, true);
this.onCollisionChange(point, true);
}
/*
Detector.prototype.postSolve = function (point, impulse) {
var userDataA = point.GetFixtureA().GetUserData();
var userDataB = point.GetFixtureB().GetUserData();
if (userDataA && userDataA.onImpulse) {
userDataA.onImpulse(impulse, point.GetFixtureB());
} else if (userDataB && userDataB.onImpulse) {
userDataB.onImpulse(impulse, point.GetFixtureA());
}
}
*/
Detector.prototype.endContact = function (point) {
this.chuckDetector.onCollisionChange(point, false);
this.onCollisionChange(point, false);
}
return Detector;

View file

@ -60,6 +60,10 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, -(this.height - (this.width / 2)) / Settings.RATIO));
fixtureDef.shape = headShape;
fixtureDef.isSensor = false;
fixtureDef.userData = {
onCollisionChange: this.onImpact.bind(this)
}
this.body.CreateFixture(fixtureDef);
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
@ -270,30 +274,8 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
};
Doll.prototype.grab = function(item) {
this.holdingItem = item;
this.positionHoldingItem();
/*
var item = null;
if (this.lookDirection == -1) {
item = this.reachableItems.left.shift();
} else {
item = this.reachableItems.right.shift();
}
if(item) {
this.holdingItem = item;
this.positionHoldingItem();
}
return item;
*/
};
Doll.prototype.positionHoldingItem = function() {
@ -346,6 +328,10 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
}
}
Doll.prototype.onImpact = function(isColliding, fixture) {
// overwrite if necessary
};
Doll.prototype.onFixtureWithinReach = function(isColliding, side, fixture) {
var item = fixture.GetBody().GetUserData();
if (!(item instanceof Item)) return;

View file

@ -1,10 +1,11 @@
define([
"Game/Config/Settings",
"Lib/Vendor/Box2D",
"Game/" + GLOBALS.context + "/Collision/Detector"
"Game/" + GLOBALS.context + "/Collision/Detector",
"Lib/Utilities/NotificationCenter"
],
function (Settings, Box2D, CollisionDetector) {
function (Settings, Box2D, CollisionDetector, NotificationCenter) {
function Engine () {
this.world = new Box2D.Dynamics.b2World(
@ -14,6 +15,9 @@ function (Settings, Box2D, CollisionDetector) {
this.world.SetWarmStarting(true);
this.ground = null;
this.lastStep = Date.now();
this.worldQueue = [];
NotificationCenter.on("engine/addToWorldQueue", this.addToWorldQueue, this);
}
Engine.prototype.getWorld = function () {
@ -24,9 +28,9 @@ function (Settings, Box2D, CollisionDetector) {
return this.ground;
}
Engine.prototype.setCollisionDetector = function (player) {
Engine.prototype.setCollisionDetector = function () {
var detector = new CollisionDetector(player);
var detector = new CollisionDetector();
this.world.SetContactListener(detector.getListener());
}
@ -36,12 +40,26 @@ function (Settings, Box2D, CollisionDetector) {
return body;
}
Engine.prototype.addToWorldQueue = function(callback) {
this.worldQueue.push(callback);
};
Engine.prototype.processWorldQueue = function() {
for (var i = 0; i < this.worldQueue.length; i++) {
this.worldQueue[i]();
};
this.worldQueue = [];
};
Engine.prototype.update = function () {
var stepLength = (Date.now() - this.lastStep) / 1000;
this.world.Step(stepLength, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
this.lastStep = Date.now();
this.world.ClearForces();
this.processWorldQueue();
}
return Engine;
});

View file

@ -1,12 +1,19 @@
define([
"Game/" + GLOBALS.context + "/GameObjects/Doll",
"Game/Config/Settings"
"Game/Config/Settings",
"Lib/Utilities/NotificationCenter"
],
function (Doll, Settings) {
function (Doll, Settings, NotificationCenter) {
function Player (id, physicsEngine) {
this.stats = {
health: 100,
deaths: 0,
kills: 0
}
this.physicsEngine = physicsEngine;
this.playerController = null;
this.doll;
@ -20,6 +27,9 @@ function (Doll, Settings) {
};
Player.prototype.spawn = function (x, y) {
if(this.doll) {
this.doll.destroy();
}
this.doll = new Doll(this.physicsEngine, "doll-" + this.id, this);
this.doll.spawn(x, y);
this.isSpawned = true;
@ -64,6 +74,10 @@ function (Doll, Settings) {
this.holdingItem = null;
};
Player.prototype.kill = function(killedBy) {
NotificationCenter.trigger("player/killed", this);
};
Player.prototype.update = function () {
if(this.doll) {