Added spawning gameCommand logic

This commit is contained in:
Logsol 2013-01-05 08:19:49 +01:00
parent 371592f167
commit fb5de522e5
8 changed files with 84 additions and 59 deletions

View file

@ -38,10 +38,10 @@
Channel.prototype.onAddUser = function (userId) {
var user = new User(userId, this);
var others = Object.keys(this.users);
var othersJoined = Object.keys(this.users);
this.users[user.id] = user;
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name, others: others});
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name, others: othersJoined});
NotificationCenter.trigger('user/joined', user);
}

View file

@ -4,15 +4,15 @@ define([
"Game/Config/Settings",
"Game/Server/Control/InputController",
"Lib/Utilities/RequestAnimFrame",
"Game/Core/NotificationCenter"
"Game/Core/NotificationCenter",
"Game/Server/Player"
],
function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) {
function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter, Player) {
function GameController (channel) {
Parent.call(this, new PhysicsEngine());
this.inputControllers = {};
this.channel = channel;
this.update();
@ -38,26 +38,31 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No
GameController.prototype.userJoined = function (user) {
Parent.prototype.userJoined.call(this, user);
var id = user.id;
var player = this.players[id];
var player = this.players[user.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
this.spawnPlayer(player);
}
GameController.prototype.userLeft = function (user) {
Parent.prototype.userLeft.call(this, user);
delete this.inputControllers[user.id];
}
GameController.prototype.spawnPlayer = function(player) {
var x = 150,
y = 50;
player.spawn(x, y);
GameController.prototype.progressGameCommandFromUser = function (command, options, user) {
var inputController = this.inputControllers[user.id];
if (typeof inputController[command] == 'function') {
inputController[command](options);
}
}
var message = {
spawnPlayer: {
id: player.id,
x: x,
y: y
}
};
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
};
GameController.prototype.createPlayer = function(user) {
var player = new Player(user.id, this.physicsEngine);
player.setInputController(new InputController(player))
return player;
};
GameController.prototype.updateWorld = function () {

21
app/Game/Server/Player.js Normal file
View file

@ -0,0 +1,21 @@
define([
"Game/Core/Player"
],
function(Parent) {
function Player(id, physicsEngine) {
Parent.call(this, id, physicsEngine);
this.inputController = null;
}
Player.prototype = Object.create(Parent.prototype);
Player.prototype.setInputController = function(inputController) {
this.inputController = inputController;
}
return Player;
});

View file

@ -13,7 +13,7 @@ function(Parent, NotificationCenter, ProtocolHelper) {
this.player = null;
var self = this;
NotificationCenter.on('user/joined', function(user) {
NotificationCenter.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead
if(user.id != self.id) {
self.sendControlCommand("userJoined", user.id);
}
@ -23,7 +23,7 @@ function(Parent, NotificationCenter, ProtocolHelper) {
self.sendControlCommand("joinSuccess", options);
});
NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) { // FIXME: right now only game commands?
NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) {
ProtocolHelper.applyCommand(message.data, self);
});
}