gameCommand implementation

This commit is contained in:
Logsol 2013-01-04 03:02:05 +01:00
parent 6219a82fbd
commit c4c48ab821
10 changed files with 62 additions and 39 deletions

View file

@ -20,37 +20,17 @@
this.gameController.loadLevel("default.json");
/*
var self = this;
NotificationCenter.on("processGameCommandFromUser", function (topic, args) {
self.processGameCommandFromUser.apply(self, args);
});
*/
NotificationCenter.on('channel/message', function (message) {
switch(message.recipient) {
case 'user':
console.log(message);
var user = self.users[message.id];
ProtocolHelper.runCommands(message.data, function (command, options) {
user[command].call(user, options);
});
break;
case 'id': // Do nothing, it is needed by the user
break;
case 'channel':
ProtocolHelper.runCommands(message.data, function (command, options) {
self[command].call(self, options);
});
break;
default:
throw 'unknown recipient';
break;
}
ProtocolHelper.runCommands(message.data, function (command, options) {
self[command].call(self, options);
});
});
NotificationCenter.on('sendControlCommandToAllUsers', this.sendControlCommandToAllUsers, this);

View file

@ -41,8 +41,10 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No
var id = user.id;
var player = this.players[id];
user.setPlayer(player);
player.spawn(50, 50);
this.inputControllers[id] = new InputController(player);
player.inputController = this.inputControllers[id]; // FIXME move this to Server/Player
}
GameController.prototype.userLeft = function (user) {

View file

@ -45,7 +45,7 @@ function (NotificationCenter, Channel) {
};
PipeToLobby.prototype.onMessage = function (message) {
NotificationCenter.trigger(message.recipient + '/message', message);
NotificationCenter.trigger(message.recipient + '/message', message);
}
return PipeToLobby;

View file

@ -10,6 +10,7 @@ function(Parent, NotificationCenter, ProtocolHelper) {
Parent.call(this, id);
this.channel = channel;
this.player = null;
var self = this;
NotificationCenter.on('user/joined', function(user) {
@ -21,6 +22,12 @@ function(Parent, NotificationCenter, ProtocolHelper) {
NotificationCenter.on('user/' + this.id + "/joinSuccess", function(options) {
self.sendControlCommand("joinSuccess", options);
});
NotificationCenter.on('user/' + this.id + "/message", function(message) { // FIXME: right now only game commands?
ProtocolHelper.runCommands(message.data, function (command, options) {
self.gameCommand(command, options);
});
});
}
User.prototype = Object.create(Parent.prototype);
@ -37,6 +44,14 @@ function(Parent, NotificationCenter, ProtocolHelper) {
this.sendControlCommand("gameCommand", data);
};
User.prototype.gameCommand = function(command, options) {
this.player.inputController[command].call(this.player.inputController);
};
User.prototype.setPlayer = function(player) {
this.player = player;
};
return User;
});