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

@ -43,17 +43,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
}
}
GameController.prototype.meJoined = function (user) {
this.me = this.userJoined(user);
this.keyboardController = new KeyboardController(this.me, this);
}
GameController.prototype.userJoined = function (user) {
var player = Parent.prototype.userJoined.call(this, user);
//player.spawn(50, 50);
return player;
}
GameController.prototype.onWorldUpdate = function (updateData) {
var body = this.physicsEngine.world.GetBodyList();
@ -71,7 +60,23 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
}
GameController.prototype.onPlayerSpawn = function(user) {
GameController.prototype.onJoinMe = function (playerId) {
//this.onSpawnPlayer(options);
this.me = this.players[playerId];
this.keyboardController = new KeyboardController(this.me, this);
}
GameController.prototype.onSpawnPlayer = function(options) {
var playerId = options.id,
x = options.x,
y = options.y;
console.log(this.players, options);
var player = this.players[playerId];
player.spawn(x, y);
};

View file

@ -46,14 +46,12 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
this.gameController = new GameController();
this.gameController.loadLevel("default.json");
var user = new User(options.userId);
this.gameController.meJoined(user);
console.log("Joined Success", options);
this.onUserJoined(options.userId);
this.gameController.onJoinMe(options.userId);
if (options.others) {
for(var i = 0; i < options.others.length; i++) {
this.users[userId] = new User(options.others[i]);
this.onUserJoined(options.others[i]);
}
}
}
@ -73,10 +71,14 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
// Commands from server
Networker.prototype.onUserJoined = function (userId) {
this.users[userId] = new User(userId);
var user = new User(userId);
this.users[userId] = user;
this.gameController.userJoined(user);
}
Networker.prototype.onUserLeft = function (userId) {
var user = this.users[userId];
this.gameController.userLeft(user);
delete this.users[userId];
}

View file

@ -36,9 +36,7 @@ function (Engine, Level, Player) {
}
GameController.prototype.userJoined = function (user) {
var player = new Player(user.id, this.physicsEngine);
this.players[user.id] = player;
return player;
this.players[user.id] = this.createPlayer(user);
}
GameController.prototype.userLeft = function (user) {
@ -47,6 +45,10 @@ function (Engine, Level, Player) {
delete this.players[user.id];
}
GameController.prototype.createPlayer = function(user) {
return new Player(user.id, this.physicsEngine);
};
return GameController;
});

View file

@ -5,31 +5,23 @@ define([
function (Doll, Settings) {
function Player (id, physicsEngine, repository) {
function Player (id, physicsEngine) {
this.physicsEngine = physicsEngine;
this.id = id;
this.repository = repository;
this.standing = false;
this.doll;
this.mc;
this.currentAnimationState = 'stand';
this.lookDirection = 1;
this.moveDirection = 0;
this.isSpawned = false;
this.init(id);
}
Player.prototype.init = function (id) {
this.doll = new Doll(this.physicsEngine, id);
//this.mc = EmbedHandler.load(EmbedHandler.CHUCK);
//this.mc.stop();
//var mclp = new MovieClipLabelParser();
//mclp.parse(this.mc);
}
Player.prototype.spawn = function (x, y) {
//this.repository.createModel(this.mc, this.doll.getBody());
this.doll = new Doll(this.physicsEngine, this.id);
this.doll.spawn(x, y);
this.isSpawned = true;
}
Player.prototype.getDoll = function () {
@ -113,8 +105,6 @@ function (Doll, Settings) {
return;
}
//this.mc.gotoAndPlay(type);
this.currentAnimationState = type;
}

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);
});
}