mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
working towards usable new structure
This commit is contained in:
parent
3afc2fa66e
commit
815c63009f
15 changed files with 223 additions and 84 deletions
33
app/Bootstrap/Channel.js
Executable file
33
app/Bootstrap/Channel.js
Executable file
|
|
@ -0,0 +1,33 @@
|
|||
define([
|
||||
"Game/Server/Channel"
|
||||
],
|
||||
|
||||
function(Channel) {
|
||||
|
||||
function ChannelBootstrap(process) {
|
||||
|
||||
var coordinatorLink = new CoordinatorLink(process);
|
||||
var channel = null;
|
||||
|
||||
process.on('message', function(message) {
|
||||
|
||||
switch(message){
|
||||
case 'CREATE':
|
||||
channel = new Channel(coordinatorLink);
|
||||
break;
|
||||
|
||||
case 'KILL':
|
||||
channel.destroy();
|
||||
process.exit(0);
|
||||
break;
|
||||
|
||||
default:
|
||||
coordinatorLink.receive(message);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return ChannelBootstrap;
|
||||
|
||||
});
|
||||
|
|
@ -3,24 +3,28 @@ define([
|
|||
"Game/Client/Physics/Engine",
|
||||
"Game/Client/View/ViewController",
|
||||
"Game/Client/Control/KeyboardController",
|
||||
"Game/Core/NotificationCenter",
|
||||
"Lib/Utilities/RequestAnimFrame"
|
||||
],
|
||||
|
||||
function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimFrame) {
|
||||
function(Parent, PhysicsEngine, ViewController, KeyboardController, NotificationCenter, requestAnimFrame) {
|
||||
|
||||
function GameController () {
|
||||
this.viewController = new ViewController();
|
||||
|
||||
Parent.call(this, new PhysicsEngine());
|
||||
|
||||
this.me = null;
|
||||
this.keyboardController = null;
|
||||
|
||||
this.viewController = new ViewController();
|
||||
this.update();
|
||||
//NotificationCenter.on('me/joined', this.meJoined, this)
|
||||
|
||||
//this.update();
|
||||
}
|
||||
|
||||
GameController.prototype = Object.create(Parent.prototype);
|
||||
|
||||
|
||||
/*
|
||||
GameController.prototype.getMe = function() {
|
||||
return this.me;
|
||||
}
|
||||
|
|
@ -40,6 +44,7 @@ function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimF
|
|||
|
||||
GameController.prototype.meJoined = function(user) {
|
||||
this.me = this.userJoined(user);
|
||||
|
||||
this.keyboardController = new KeyboardController(this.me, this);
|
||||
}
|
||||
|
||||
|
|
@ -66,6 +71,6 @@ function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimF
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
return GameController;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ function(ProtocolHelper, GameController) {
|
|||
this.socketLink.on('connect', function() {
|
||||
self.onConnect();
|
||||
});
|
||||
|
||||
/*
|
||||
this.socketLink.on('message', function(message) {
|
||||
self.onMessage(message);
|
||||
});
|
||||
|
|
@ -27,12 +27,13 @@ function(ProtocolHelper, GameController) {
|
|||
this.socketLink.on('disconnect', function() {
|
||||
self.onDisconnect();
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
Networker.prototype.onConnect = function() {
|
||||
this.join('dungeon');
|
||||
}
|
||||
|
||||
/*
|
||||
Networker.prototype.onMessage = function(message) {
|
||||
var self = this;
|
||||
ProtocolHelper.runCommands(message, function(command, options) {
|
||||
|
|
@ -44,7 +45,7 @@ function(ProtocolHelper, GameController) {
|
|||
if(this.gameController) this.gameController.destruct();
|
||||
this.gameController = null;
|
||||
}
|
||||
|
||||
*/
|
||||
Networker.prototype.join = function(channelName){
|
||||
this.sendCommand('join', channelName);
|
||||
}
|
||||
|
|
@ -53,9 +54,9 @@ function(ProtocolHelper, GameController) {
|
|||
var message = ProtocolHelper.encodeCommand(command, options);
|
||||
this.socketLink.send(message);
|
||||
}
|
||||
|
||||
/*
|
||||
Networker.prototype.onJoinSuccess = function(options) {
|
||||
this.gameController = new GameController(this, options.id);
|
||||
this.gameController = new GameController(options.id);
|
||||
this.gameController.loadLevel("default.json")
|
||||
console.log("Joined " + options.channelName);
|
||||
|
||||
|
|
@ -103,7 +104,7 @@ function(ProtocolHelper, GameController) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
return Networker;
|
||||
|
||||
});
|
||||
2
app/Game/Client/Physics/Engine.js
Normal file → Executable file
2
app/Game/Client/Physics/Engine.js
Normal file → Executable file
|
|
@ -17,7 +17,7 @@ function(Parent, Settings, DomController, Box2D) {
|
|||
|
||||
Engine.prototype.setupDebugDraw = function() {
|
||||
//var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE;
|
||||
var debugSprite = Dom.getDebugCanvas().getContext("2d");
|
||||
var debugSprite = DomController.getDebugCanvas().getContext("2d");
|
||||
|
||||
// set debug draw
|
||||
var debugDraw = new Box2D.Dynamics.b2DebugDraw();
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ define([
|
|||
function(Engine, Level, Player) {
|
||||
|
||||
function GameController(physicsEngine) {
|
||||
console.log('constructor called');
|
||||
this.players = {};
|
||||
|
||||
if (! physicsEngine instanceof Engine) {
|
||||
|
|
@ -30,12 +29,16 @@ function(Engine, Level, Player) {
|
|||
this.level.loadLevelInToEngine();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
GameController.prototype.destroy = function() {
|
||||
for(var player in this.players) {
|
||||
this.players[player].destroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
GameController.prototype.userJoined = function(user) {
|
||||
var player = new Player(user.id, this.physicsEngine);
|
||||
this.players[user.id] = player;
|
||||
|
|
@ -48,5 +51,7 @@ function(Engine, Level, Player) {
|
|||
delete this.players[user.id];
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
return GameController;
|
||||
});
|
||||
|
|
|
|||
8
app/Game/Core/User.js
Executable file
8
app/Game/Core/User.js
Executable file
|
|
@ -0,0 +1,8 @@
|
|||
define(function() {
|
||||
|
||||
function User(id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
return User;
|
||||
});
|
||||
|
|
@ -1,26 +1,32 @@
|
|||
define([
|
||||
"Game/Server/GameController",
|
||||
"Game/Server/NotificationCenter"
|
||||
"Game/Core/NotificationCenter"
|
||||
],
|
||||
|
||||
function(GameController, NotificationCenter) {
|
||||
|
||||
function Channel(name) {
|
||||
this.name = name;
|
||||
function Channel(coordinatorLink) {
|
||||
|
||||
this.coordinatorLink = coordinatorLink;
|
||||
|
||||
console.log('A CHANNEL WAS CREATED');
|
||||
/*
|
||||
this.users = {};
|
||||
this.gameController = new GameController();
|
||||
this.gameController.loadLevel("default.json");
|
||||
|
||||
*/
|
||||
/*
|
||||
var self = this;
|
||||
NotificationCenter.on("processGameCommandFromUser", function(topic, args) {
|
||||
self.processGameCommandFromUser.apply(self, args);
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
Channel.validateName = function(name){
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Channel.prototype.addUser = function(user){
|
||||
var userIds = Object.keys(this.users);
|
||||
|
||||
|
|
@ -56,7 +62,7 @@ function(GameController, NotificationCenter) {
|
|||
Channel.prototype.processGameCommandFromUser = function(command, options, user) {
|
||||
this.gameController.progressGameCommandFromUser(command, options, user);
|
||||
}
|
||||
|
||||
*/
|
||||
return Channel;
|
||||
|
||||
});
|
||||
20
app/Game/Server/CoordinatorLink.js
Executable file
20
app/Game/Server/CoordinatorLink.js
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
define([
|
||||
],
|
||||
|
||||
function() {
|
||||
|
||||
function CoordinatorLink(process) {
|
||||
this.process = process;
|
||||
}
|
||||
|
||||
CoordinatorLink.prototype.send = function(message) {
|
||||
this.process.send(message);
|
||||
};
|
||||
|
||||
CoordinatorLink.prototype.receive = function(message) {
|
||||
throw 'This method is abstract and must be overwritten by Channel';
|
||||
};
|
||||
|
||||
return CoordinatorLink;
|
||||
|
||||
});
|
||||
|
|
@ -4,7 +4,7 @@ define([
|
|||
"Game/Config/Settings",
|
||||
"Game/Core/Control/InputController",
|
||||
"Lib/Utilities/RequestAnimFrame",
|
||||
"Game/Server/NotificationCenter"
|
||||
"Game/Core/NotificationCenter"
|
||||
],
|
||||
|
||||
function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) {
|
||||
|
|
@ -14,15 +14,15 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not
|
|||
|
||||
this.inputControllers = {};
|
||||
|
||||
this.update();
|
||||
this.updateWorld();
|
||||
//this.update();
|
||||
//this.updateWorld();
|
||||
|
||||
NotificationCenter.on('user/joined', this.userJoined, this);
|
||||
NotificationCenter.on('user/left', this.userLeft, this);
|
||||
//NotificationCenter.on('user/joined', this.userJoined, this);
|
||||
//NotificationCenter.on('user/left', this.userLeft, this);
|
||||
}
|
||||
|
||||
GameController.prototype = Object.create(Parent.prototype);
|
||||
|
||||
/*
|
||||
GameController.prototype.update = function() {
|
||||
|
||||
requestAnimFrame(this.update.bind(this));
|
||||
|
|
@ -79,6 +79,6 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not
|
|||
|
||||
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
|
||||
}
|
||||
|
||||
*/
|
||||
return GameController;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
define([
|
||||
"Game/Core/User",
|
||||
"Game/Core/Protocol/Helper",
|
||||
"Game/Server/NotificationCenter"
|
||||
"Game/Core/NotificationCenter"
|
||||
],
|
||||
|
||||
function(ProtocolHelper, NotificationCenter) {
|
||||
|
||||
function User(socketLink, coordinator) {
|
||||
function(Parent, ProtocolHelper, NotificationCenter) {
|
||||
|
||||
function User(id, coordinator) {
|
||||
Parent.call(this, id);
|
||||
this.id = socketLink.id;
|
||||
this.socketLink = socketLink;
|
||||
this.coordinator = coordinator;
|
||||
|
|
@ -15,19 +16,14 @@ function(ProtocolHelper, NotificationCenter) {
|
|||
this.init(socketLink);
|
||||
}
|
||||
|
||||
User.prototype = Object.create(Parent.prototype);
|
||||
|
||||
User.prototype.init = function(socketLink){
|
||||
|
||||
var self = this;
|
||||
|
||||
socketLink.on('message', function(message){
|
||||
self.onMessage(message);
|
||||
});
|
||||
|
||||
socketLink.on('disconnect', function(){
|
||||
self.onDisconnect();
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
User.prototype.setChannel = function(channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
|
@ -38,44 +34,12 @@ function(ProtocolHelper, NotificationCenter) {
|
|||
this.socketLink.send(message);
|
||||
}
|
||||
|
||||
User.prototype.onMessage = function(message){
|
||||
var self = this;
|
||||
ProtocolHelper.runCommands(message, function(command, options){
|
||||
self.processControlCommand(command, options);
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype.onDisconnect = function(){
|
||||
this.coordinator.removeUser(this);
|
||||
}
|
||||
|
||||
User.prototype.processControlCommand = function(command, options){
|
||||
switch(command) {
|
||||
|
||||
case 'join':
|
||||
this.coordinator.assignUserToChannel(this, options);
|
||||
break;
|
||||
|
||||
case 'leave':
|
||||
this.coordinator.assignUserToLobby(this);
|
||||
break;
|
||||
|
||||
case 'gameCommand':
|
||||
for(var gameCommand in options) {
|
||||
//NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]);
|
||||
//this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
User.prototype.toString = function() {
|
||||
return "[User " + this.id + "]";
|
||||
};
|
||||
|
||||
*/
|
||||
return User;
|
||||
|
||||
});
|
||||
|
|
@ -1,9 +1,12 @@
|
|||
define([
|
||||
"Game/Server/User",
|
||||
"Game/Server/Channel"
|
||||
"Lobby/User",
|
||||
"Game/Server/Channel",
|
||||
"node-fork"
|
||||
],
|
||||
|
||||
function(User, Channel) {
|
||||
function(User, Channel, nodeFork) {
|
||||
|
||||
var fork = nodeFork.fork;
|
||||
|
||||
function Coordinator() {
|
||||
this.channels = {};
|
||||
|
|
@ -16,16 +19,16 @@ function(User, Channel) {
|
|||
}
|
||||
|
||||
Coordinator.prototype.assignUserToLobby = function(user){
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
if(user.channelProcess) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
this.lobbyUsers[user.id] = user;
|
||||
}
|
||||
|
||||
Coordinator.prototype.assignUserToChannel = function(user, channelName){
|
||||
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
if(user.channelProcess) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
|
||||
if(!Channel.validateName(channelName)){
|
||||
|
|
@ -35,20 +38,29 @@ function(User, Channel) {
|
|||
|
||||
var channel = this.channels[channelName];
|
||||
if(!channel) {
|
||||
channel = new Channel(channelName);
|
||||
|
||||
try {
|
||||
console.log('try');
|
||||
channel = fork('app/Bootstrap/Channel.js');
|
||||
channel.send('CREATE');
|
||||
channel.send('{setName:"' + channelName + '"}');
|
||||
} catch (err) {
|
||||
throw 'Failed to fork channel ' + channelName + '! (' + err + ')';
|
||||
}
|
||||
|
||||
this.channels[channelName] = channel;
|
||||
}
|
||||
|
||||
channel.addUser(user);
|
||||
user.setChannel(channel);
|
||||
//channel.addUser(user);
|
||||
//user.setChannel(channel);
|
||||
|
||||
delete this.lobbyUsers[user.id];
|
||||
}
|
||||
|
||||
Coordinator.prototype.removeUser = function(user){
|
||||
delete this.lobbyUsers[user.id];
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
if(user.channelProcess) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
66
app/Lobby/User.js
Executable file
66
app/Lobby/User.js
Executable file
|
|
@ -0,0 +1,66 @@
|
|||
define([
|
||||
"Game/Core/User",
|
||||
"Game/Core/Protocol/Helper"
|
||||
],
|
||||
|
||||
function(Parent, ProtocolHelper) {
|
||||
|
||||
function User(socketLink, coordinator) {
|
||||
Parent.call(this, socketLink.id);
|
||||
|
||||
this.coordinator = coordinator;
|
||||
this.channelProcess = null;
|
||||
|
||||
var self = this;
|
||||
|
||||
socketLink.on('message', function(message){
|
||||
self.onMessage(message);
|
||||
});
|
||||
socketLink.on('disconnect', function(){
|
||||
self.onDisconnect();
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype = Object.create(Parent.prototype);
|
||||
|
||||
User.prototype.setChannelProcess = function(channelProcess) {
|
||||
this.channelProcess = channelProcess;
|
||||
}
|
||||
|
||||
User.prototype.onMessage = function(message){
|
||||
var self = this;
|
||||
ProtocolHelper.runCommands(message, function(command, options){
|
||||
self.processControlCommand(command, options);
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype.onDisconnect = function(){
|
||||
this.coordinator.removeUser(this);
|
||||
}
|
||||
|
||||
User.prototype.processControlCommand = function(command, options){
|
||||
switch(command) {
|
||||
|
||||
case 'join':
|
||||
this.coordinator.assignUserToChannel(this, options);
|
||||
break;
|
||||
|
||||
case 'leave':
|
||||
this.coordinator.assignUserToLobby(this);
|
||||
break;
|
||||
|
||||
case 'gameCommand':
|
||||
for(var gameCommand in options) {
|
||||
//NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]);
|
||||
//this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return User;
|
||||
|
||||
});
|
||||
18
channel.js
Executable file
18
channel.js
Executable file
|
|
@ -0,0 +1,18 @@
|
|||
console.log(requirejs);
|
||||
|
||||
|
||||
var ree = require('requirejs');
|
||||
|
||||
console.log(ree);
|
||||
|
||||
requirejs.config({
|
||||
baseUrl: 'app'
|
||||
});
|
||||
|
||||
var inspector = {};
|
||||
|
||||
requirejs(["Bootstrap/Channel"], function(ChannelBootstrap) {
|
||||
|
||||
var channelBootstrap = new ChannelBootstrap(process);
|
||||
inspector.channelBootstrap = channelBootstrap;
|
||||
});
|
||||
|
|
@ -17,7 +17,8 @@
|
|||
"dependencies": {
|
||||
"socket.io": ">= 0.9.6",
|
||||
"node-static": ">= 0.6.0",
|
||||
"requirejs": ">= 2.0.2"
|
||||
"requirejs": ">= 2.0.2",
|
||||
"node-fork": ">= 0.4.2"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"optionalDependencies": {},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue