mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
added first damage and killing
This commit is contained in:
parent
2a4327c5cf
commit
f22e0dd53d
9 changed files with 124 additions and 52 deletions
|
|
@ -4,14 +4,12 @@ define([
|
|||
|
||||
function (Parent) {
|
||||
|
||||
function Detector (player) {
|
||||
Parent.call(this, player);
|
||||
function Detector () {
|
||||
Parent.call(this);
|
||||
}
|
||||
|
||||
Detector.prototype = Object.create(Parent.prototype);
|
||||
|
||||
Detector.IDENTIFIER = Parent.IDENTIFIER; // Needed because otherwise it will not be
|
||||
// inherited because it is not in prototype
|
||||
|
||||
return Detector;
|
||||
});
|
||||
|
|
@ -23,6 +23,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
NotificationCenter.on('user/joined', this.userJoined, this);
|
||||
NotificationCenter.on('user/left', this.userLeft, this); // FIXME: refactor this.userLeft -> this.onUserLeft, even in core and client
|
||||
NotificationCenter.on('user/resetLevel', this.onResetLevel, this);
|
||||
NotificationCenter.on('player/killed', this.spawnPlayer, this);
|
||||
|
||||
console.checkpoint('starting game controller for channel ' + channel.name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/Doll"
|
||||
"Game/Core/GameObjects/Doll",
|
||||
"Game/Server/GameObjects/Item",
|
||||
"Lib/Vendor/Box2D",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
function (Parent, Item, Box2D, NotificationCenter) {
|
||||
|
||||
function Doll(physicsEngine, uid, player) {
|
||||
Parent.call(this, physicsEngine, uid, player);
|
||||
|
|
@ -27,6 +30,46 @@ function (Parent) {
|
|||
return findItem(this.reachableItems.right);
|
||||
}
|
||||
}
|
||||
|
||||
Doll.prototype.onImpact = function(isColliding, fixture) {
|
||||
var self = this;
|
||||
|
||||
Parent.prototype.onImpact.call(this, isColliding, fixture);
|
||||
|
||||
if(isColliding) {
|
||||
var otherBody = fixture.GetBody();
|
||||
if(otherBody) {
|
||||
var item = otherBody.GetUserData();
|
||||
if(item instanceof Item) {
|
||||
var itemVelocity = item.body.GetLinearVelocity();
|
||||
var itemMass = item.body.GetMass();
|
||||
|
||||
var ownVelocity = this.body.GetLinearVelocity();
|
||||
|
||||
var b2Math = Box2D.Common.Math.b2Math;
|
||||
|
||||
var absItemVelocity = b2Math.AbsV(itemVelocity)
|
||||
var max = 1;
|
||||
|
||||
if(absItemVelocity.x > max || absItemVelocity.y > max) {
|
||||
if(item.lastMoved && item.lastMoved.player != this.player) {
|
||||
var damage = b2Math.SubtractVV(itemVelocity, ownVelocity);
|
||||
damage.Abs();
|
||||
damage.Multiply(itemMass);
|
||||
|
||||
var callback = function() {
|
||||
self.player.addDamage(damage.Length() * 2, item.lastMoved.player);
|
||||
}
|
||||
|
||||
NotificationCenter.trigger("engine/addToWorldQueue", callback)
|
||||
}
|
||||
}
|
||||
|
||||
item.setLastMovedBy(this.player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Doll;
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,15 @@ function (Parent, NotificationCenter) {
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
Player.prototype.addDamage = function(damage, enemy) {
|
||||
this.stats.health -= damage;
|
||||
if(this.stats.health <= 0) {
|
||||
this.stats.deaths++;
|
||||
enemy.stats.kills++;
|
||||
this.kill();
|
||||
}
|
||||
};
|
||||
|
||||
return Player;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue