Merge branch 'restructuring' of github.com:logsol/chuck.js into restructuring

This commit is contained in:
logsol 2012-07-22 02:20:52 +02:00
commit e4b41ebf4f
3 changed files with 47 additions and 79 deletions

View file

@ -9,10 +9,11 @@ define([
function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimFrame) { function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimFrame) {
function GameController () { function GameController () {
this.me;
this.keyboardController;
Parent.apply(this, new PhysicsEngine()); Parent.apply(this, new PhysicsEngine());
this.me = null;
this.keyboardController = null;
this.viewController = new ViewController(); this.viewController = new ViewController();
this.update(); this.update();
} }

View file

@ -1,41 +1,23 @@
define([ define([
"Chuck/Physics/Engine", "Game/Core/GameController",
"Chuck/Settings", "Game/Server/Physics/Engine",
"Chuck/Player", "Game/Config/Settings",
"Vendor/Box2D",
"Chuck/Loader/Level",
"Chuck/Control/InputController", "Chuck/Control/InputController",
"RequestAnimationFrame" "RequestAnimationFrame",
"NotificationCenter"
], ],
function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, requestAnimFrame) { function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) {
function GameController (channel) { function GameController () {
this.channel = channel; Parent.apply(this, new PhysicsEngine());
this.players = {};
this.init();
}
GameController.prototype.init = function() { this.inputControllers = {};
this.physicsEngine = this.factory.new(PhysicsEngine);
this.update(); this.update();
this.updateWorld(); this.updateWorld();
} }
GameController.prototype.loadLevel = function(path) {
if (this.level) {
this.level.unload();
}
this.level = new Level(path, this.physicsEngine);
this.level.loadLevelInToEngine();
}
GameController.prototype.getPhysicsEngine = function() {
return this.physicsEngine;
}
GameController.prototype.update = function() { GameController.prototype.update = function() {
requestAnimFrame(this.update.bind(this)); requestAnimFrame(this.update.bind(this));
@ -46,40 +28,23 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request
} }
} }
GameController.prototype.destruct = function() { GameController.prototype.userJoined = function(user) {
var player = Parent.prototype.userJoined.call(this, user);
this.inputControllers[player.id] = new InputController(player);
} }
GameController.prototype.createPlayerForUser = function(user) { GameController.prototype.userLeft = function(user) {
var id = user.id; Parent.prototype.userLeft.call(this, user);
delete this.inputControllers[user.id];
var player = new Player(this.physicsEngine, id, null);
this.players[id] = {
player: player,
inputController: new InputController(player)
};
player.spawn(100, 0);
this.physicsEngine.setCollisionDetector(player);
} }
GameController.prototype.progressGameCommandFromId = function(command, options, id) { GameController.prototype.progressGameCommandFromUser = function(command, options, user) {
var inputController = this.players[id].inputController; var inputController = this.inputControllers[user.id];
if (typeof inputController[command] == 'function') { if (typeof inputController[command] == 'function') {
inputController[command](options); inputController[command](options);
} }
} }
GameController.prototype.userIdLeft = function(id) {
var player = this.players[id].player;
player.destroy();
delete this.players[id];
}
GameController.prototype.updateClientsWorld = function(update_world) {
this.channel.sendCommandToAllUsers('gameCommand', {worldUpdate: update_world});
}
GameController.prototype.updateWorld = function() { GameController.prototype.updateWorld = function() {
var update = {}; var update = {};
@ -101,8 +66,7 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request
} while (body = body.GetNext()); } while (body = body.GetNext());
if(isUpdateNeeded) { if(isUpdateNeeded) {
//this.serverGame.updateClientsWorld(update); NotificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]);
this.notificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]);
} }
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);

41
app/Game/Server/NotificationCenter.js Executable file → Normal file
View file

@ -1,30 +1,32 @@
define( define(function() {
var NotificationCenter = { function NotificationCenter() {
topics: {}, this.topics = {};
subUid: -1 this.subUid = -1;
}; }
NotificationCenter.trigger = function(topic, args) { NotificationCenter.prototype.trigger = function(topic, args) {
if (!NotificationCenter.topics[topic]) { if (!this.topics[topic]) {
throw "No such topic " + topic + ". Could not trigger."; throw "No such topic " + topic + ". Could not trigger.";
} }
var subscribers = NotificationCenter.topics[topic]; var subscribers = this.topics[topic];
var len = subscribers ? subscribers.length : 0; var len = subscribers ? subscribers.length : 0;
while (len--) { while (len--) {
subscribers[len].func(topic, args); subscribers[len].func(topic, args);
} }
return this;
} }
NotificationCenter.on = function(topic, func) { NotificationCenter.prototype.on = function(topic, func) {
if (!NotificationCenter.topics[topic]) { if (!this.topics[topic]) {
NotificationCenter.topics[topic] = []; this.topics[topic] = [];
} }
var token = ( ++NotificationCenter.subUid ).toString(); var token = ( ++this.subUid ).toString();
NotificationCenter.topics[topic].push({ this.topics[topic].push({
token: token, token: token,
func: func func: func
}); });
@ -32,18 +34,19 @@ define(
return token; return token;
} }
NotificationCenter.off = function(token) { NotificationCenter.prototype.off = function(token) {
for(var m in NotificationCenter.topics) { for(var m in this.topics) {
if (NotificationCenter.topics[m]) { if (this.topics[m]) {
for(var i = 0, j = NotificationCenter.topics[m].length; i < j; i++) { for(var i = 0, j = this.topics[m].length; i < j; i++) {
if (NotificationCenter.topics[m][i].token === token) { if (this.topics[m][i].token === token) {
NotificationCenter.topics[m].splice(i, 1); this.topics[m].splice(i, 1);
return token; return token;
} }
} }
} }
} }
return this;
} }
return NotificationCenter; return NotificationCenter;