fixes #12 and added health

This commit is contained in:
Jeena 2014-02-10 14:21:26 +01:00
parent 6c78c750f0
commit d84fb20f90
7 changed files with 83 additions and 18 deletions

View file

@ -117,6 +117,16 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
};
GameController.prototype.onUpdateStats = function(options) {
var player = this.players[options.playerId];
player.stats = options.stats;
// FIXME: move to canvas later
if(player == this.me) {
DomController.setHealth(player.stats.health);
}
};
GameController.prototype.loadLevel = function (path) {
Parent.prototype.loadLevel.call(this, path);
}

View file

@ -77,6 +77,13 @@ function (Settings, NotificationCenter, Stats, Screenfull) {
label.appendChild(checkbox);
label.appendChild(document.createTextNode("Debug"));
this.devToolsContainer.appendChild(label);
// create health
this.health = document.createElement("span");
this.health.innerHTML = "Health: 100";
p = document.createElement("p");
p.appendChild(this.health);
this.devToolsContainer.appendChild(p);
};
DomController.prototype.statsBegin = function() {
@ -133,6 +140,10 @@ function (Settings, NotificationCenter, Stats, Screenfull) {
return this.debugCanvas;
}
DomController.prototype.setHealth = function(health) {
this.health.innerHTML = "Health: " + parseInt(health, 10);
};
return new DomController();

View file

@ -11,7 +11,7 @@ function (Doll, Settings, NotificationCenter) {
this.stats = {
health: 100,
deaths: 0,
kills: 0
score: 0
}
this.physicsEngine = physicsEngine;

View file

@ -28,8 +28,11 @@
ProtocolHelper.applyCommand(message.data, self);
});
NotificationCenter.on('sendControlCommandToAllUsers', this.sendControlCommandToAllUsers, this);
NotificationCenter.on('sendControlCommandToAllUsersExcept', this.sendControlCommandToAllUsersExcept, this);
NotificationCenter.on('broadcastControlCommand', this.broadcastControlCommand, this);
NotificationCenter.on('broadcastControlCommandExcept', this.broadcastControlCommandExcept, this);
NotificationCenter.on('broadcastGameCommand', this.broadcastGameCommand, this);
NotificationCenter.on('broadcastGameCommandExcept', this.broadcastGameCommandExcept, this);
console.checkpoint('channel ' + name + ' created');
}
@ -69,7 +72,7 @@
Channel.prototype.onReleaseUser = function (userId) {
var user = this.users[userId];
this.sendControlCommandToAllUsersExcept("userLeft", user.id, user);
this.broadcastControlCommandExcept("userLeft", user.id, user);
NotificationCenter.trigger('user/left', user);
delete this.users[user.id];
}
@ -77,13 +80,13 @@
// Sending commands
Channel.prototype.sendControlCommandToAllUsers = function (command, options) {
Channel.prototype.broadcastControlCommand = function (command, options) {
for(var id in this.users) {
this.users[id].sendControlCommand(command, options);
}
}
Channel.prototype.sendControlCommandToAllUsersExcept = function (command, options, exceptUser) {
Channel.prototype.broadcastControlCommandExcept = function (command, options, exceptUser) {
for(var id in this.users) {
if (id != exceptUser.id) {
this.users[id].sendControlCommand(command, options);
@ -91,6 +94,14 @@
}
}
Channel.prototype.broadcastGameCommand = function (command, options) {
this.broadcastControlCommand("gameCommand", ProtocolHelper.encodeCommand(command, options));
}
Channel.prototype.broadcastGameCommandExcept = function (command, options, exceptUser) {
this.broadcastControlCommandExcept("gameCommand", ProtocolHelper.encodeCommand(command, options), exceptUser);
}
return Channel;
});

View file

@ -67,7 +67,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
y: spawnPoint.y
}
};
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", message);
};
GameController.prototype.createPlayer = function(user) {
@ -82,7 +82,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var update = this.getWorldUpdateObject(false);
if(Object.getOwnPropertyNames(update).length > 0) {
NotificationCenter.trigger("sendControlCommandToAllUsers", 'gameCommand', {worldUpdate:update});
NotificationCenter.trigger("broadcastControlCommand", 'gameCommand', {worldUpdate:update});
}
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
@ -138,7 +138,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
GameController.prototype.onResetLevel = function(userId) {
Parent.prototype.onResetLevel.call(this);
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", {resetLevel:true});
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", {resetLevel:true});
for (var key in this.players) {
this.spawnPlayer(this.players[key]);
}

View file

@ -34,10 +34,6 @@ function (Parent, NotificationCenter) {
itemUid: item.uid
}
var message = {
handActionResponse: options
}
if (isHolding) {
// throw
if(item.isReleasingAllowed()) {
@ -46,7 +42,7 @@ function (Parent, NotificationCenter) {
options.action = "throw";
options.x = x;
options.y = y;
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
NotificationCenter.trigger("broadcastGameCommand", "handActionResponse", options);
}
} else {
// grab
@ -54,20 +50,53 @@ function (Parent, NotificationCenter) {
this.grab(item);
options.action = "grab";
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
NotificationCenter.trigger("broadcastGameCommand", "handActionResponse", options);
}
}
};
Player.prototype.addDamage = function(damage, enemy) {
this.stats.health -= damage;
this.updateHealth(this.stats.health - damage);
if(this.stats.health <= 0) {
this.stats.deaths++;
enemy.stats.kills++;
enemy.score();
this.kill();
}
};
Player.prototype.spawn = function(x, y) {
Parent.prototype.spawn.call(this, x, y);
this.updateHealth(100);
};
Player.prototype.updateHealth = function(health) {
this.stats.health = health;
NotificationCenter.trigger("user/" + this.id + "/gameCommand", "updateStats", {
playerId: this.id,
stats: this.stats
});
};
Player.prototype.kill = function() {
Parent.prototype.kill.call(this);
this.stats.deaths++;
this.broadcastStats();
};
Player.prototype.score = function() {
this.stats.score++;
this.broadcastStats();
};
Player.prototype.broadcastStats = function() {
NotificationCenter.trigger("broadcastGameCommand", "updateStats", {
playerId: this.id,
stats: this.stats
});
};
return Player;
});

View file

@ -27,6 +27,10 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) {
ProtocolHelper.applyCommand(message.data, self);
});
NotificationCenter.on('user/' + this.id + "/gameCommand", function(command, options) {
self.sendGameCommand(command, options);
});
}
User.prototype = Object.create(Parent.prototype);