mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
refactoring
This commit is contained in:
parent
39af52dd14
commit
c256f8a729
8 changed files with 87 additions and 160 deletions
|
|
@ -50,37 +50,30 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
|
||||||
|
|
||||||
GameController.prototype.userJoined = function (user) {
|
GameController.prototype.userJoined = function (user) {
|
||||||
var player = Parent.prototype.userJoined.call(this, user);
|
var player = Parent.prototype.userJoined.call(this, user);
|
||||||
player.spawn(50, 50);
|
//player.spawn(50, 50);
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
GameController.prototype.userLeft = function (user) {
|
GameController.prototype.onWorldUpdate = function (updateData) {
|
||||||
Parent.prototype.userLeft.call(user);
|
|
||||||
|
var body = this.physicsEngine.world.GetBodyList();
|
||||||
|
do {
|
||||||
|
var bodyName = body.GetUserData();
|
||||||
|
if(bodyName && updateData[bodyName]) {
|
||||||
|
var update = updateData[bodyName];
|
||||||
|
body.SetAwake(true);
|
||||||
|
body.SetPosition(update.p);
|
||||||
|
body.SetAngle(update.a);
|
||||||
|
body.SetLinearVelocity(update.lv);
|
||||||
|
body.SetAngularVelocity(update.av);
|
||||||
|
}
|
||||||
|
} while (body = body.GetNext());
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
GameController.prototype.processGameCommand = function (command, options) {
|
GameController.prototype.onPlayerSpawn = function(user) {
|
||||||
|
|
||||||
if (command == "worldUpdate") {
|
};
|
||||||
|
|
||||||
var body = this.physicsEngine.world.GetBodyList();
|
|
||||||
do {
|
|
||||||
var userData = body.GetUserData();
|
|
||||||
if(userData && options[userData]) {
|
|
||||||
var update = options[userData];
|
|
||||||
|
|
||||||
//console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y);
|
|
||||||
|
|
||||||
body.SetAwake(true);
|
|
||||||
body.SetPosition(update.p);
|
|
||||||
body.SetAngle(update.a);
|
|
||||||
body.SetLinearVelocity(update.lv);
|
|
||||||
body.SetAngularVelocity(update.av);
|
|
||||||
}
|
|
||||||
} while (body = body.GetNext());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return GameController;
|
return GameController;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -10,32 +10,29 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
|
||||||
function Networker (socketLink) {
|
function Networker (socketLink) {
|
||||||
this.socketLink = socketLink;
|
this.socketLink = socketLink;
|
||||||
this.gameController = null;
|
this.gameController = null;
|
||||||
|
this.users = {};
|
||||||
|
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
Networker.prototype.init = function () {
|
Networker.prototype.init = function () {
|
||||||
|
|
||||||
|
this.socketLink.on('connect', this.onConnect.bind(this));
|
||||||
|
this.socketLink.on('disconnect', this.onDisconnect.bind(this));
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.socketLink.on('connect', function () {
|
|
||||||
self.onConnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
this.socketLink.on('message', function (message) {
|
this.socketLink.on('message', function (message) {
|
||||||
self.onMessage(message);
|
ProtocolHelper.applyCommand(message, self);
|
||||||
});
|
|
||||||
|
|
||||||
this.socketLink.on('disconnect', function () {
|
|
||||||
self.onDisconnect();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
NotificationCenter.on("sendGameCommand", this.sendGameCommand, this);
|
NotificationCenter.on("sendGameCommand", this.sendGameCommand, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Socket callbacks
|
||||||
|
|
||||||
Networker.prototype.onConnect = function () {
|
Networker.prototype.onConnect = function () {
|
||||||
console.log('connected.')
|
console.log('connected.')
|
||||||
this.join('dungeon');
|
this.sendCommand('join', 'dungeon');
|
||||||
}
|
}
|
||||||
|
|
||||||
Networker.prototype.onDisconnect = function () {
|
Networker.prototype.onDisconnect = function () {
|
||||||
|
|
@ -45,30 +42,24 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
|
||||||
document.body.style.backgroundColor = '#aaaaaa';
|
document.body.style.backgroundColor = '#aaaaaa';
|
||||||
}
|
}
|
||||||
|
|
||||||
Networker.prototype.join = function (channelName) {
|
|
||||||
this.sendCommand('join', channelName);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Networker.prototype.onJoinSuccess = function (options) {
|
Networker.prototype.onJoinSuccess = function (options) {
|
||||||
this.gameController = new GameController();
|
this.gameController = new GameController();
|
||||||
this.gameController.loadLevel("default.json");
|
this.gameController.loadLevel("default.json");
|
||||||
|
|
||||||
|
|
||||||
var user = new User(options.userId);
|
var user = new User(options.userId);
|
||||||
this.gameController.meJoined(user);
|
this.gameController.meJoined(user);
|
||||||
|
|
||||||
console.log("Joined ", options);
|
console.log("Joined Success", options);
|
||||||
|
|
||||||
// -> replace with decent command
|
if (options.others) {
|
||||||
if (options.others && options.others.length > 0) {
|
|
||||||
for(var i = 0; i < options.others.length; i++) {
|
for(var i = 0; i < options.others.length; i++) {
|
||||||
var user = {id: options.others[i]};
|
this.users[userId] = new User(options.others[i]);
|
||||||
this.gameController.userJoined(user);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sending commands
|
||||||
|
|
||||||
Networker.prototype.sendCommand = function (command, options) {
|
Networker.prototype.sendCommand = function (command, options) {
|
||||||
var message = ProtocolHelper.encodeCommand(command, options);
|
var message = ProtocolHelper.encodeCommand(command, options);
|
||||||
this.socketLink.send(message);
|
this.socketLink.send(message);
|
||||||
|
|
@ -79,53 +70,18 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
|
||||||
this.sendCommand('gameCommand', message);
|
this.sendCommand('gameCommand', message);
|
||||||
}
|
}
|
||||||
|
|
||||||
Networker.prototype.onMessage = function (message) {
|
// Commands from server
|
||||||
var self = this;
|
|
||||||
|
|
||||||
ProtocolHelper.runCommands(message, function (command, options) {
|
|
||||||
self.processControlCommand(command, options);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Networker.prototype.onUserJoined = function (userId) {
|
Networker.prototype.onUserJoined = function (userId) {
|
||||||
// -> replace with game command
|
this.users[userId] = new User(userId);
|
||||||
var user = {id: userId};
|
|
||||||
this.gameController.userJoined(user);
|
|
||||||
console.log("User " + userId + " joined");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Networker.prototype.onUserLeft = function (userId) {
|
Networker.prototype.onUserLeft = function (userId) {
|
||||||
// -> replace with game command
|
delete this.users[userId];
|
||||||
var user = {id: userId};
|
|
||||||
this.gameController.userLeft(user);
|
|
||||||
console.log("User " + userId + " left");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Networker.prototype.processControlCommand = function (command, options) {
|
Networker.prototype.onGameCommand = function(message) {
|
||||||
|
ProtocolHelper.applyCommand(message, this.gameController);
|
||||||
switch(command) {
|
|
||||||
case 'joinSuccess':
|
|
||||||
this.onJoinSuccess(options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'gameCommand':
|
|
||||||
for(var gameCommand in options) {
|
|
||||||
this.gameController.processGameCommand(gameCommand, options[gameCommand]);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'userJoined':
|
|
||||||
this.onUserJoined(options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'userLeft':
|
|
||||||
this.onUserLeft(options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Networker;
|
return Networker;
|
||||||
|
|
|
||||||
|
|
@ -18,23 +18,13 @@
|
||||||
|
|
||||||
this.gameController = new GameController(this);
|
this.gameController = new GameController(this);
|
||||||
this.gameController.loadLevel("default.json");
|
this.gameController.loadLevel("default.json");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var self = this;
|
NotificationCenter.on('channel/controlCommand', function (message) {
|
||||||
NotificationCenter.on("processGameCommandFromUser", function (topic, args) {
|
ProtocolHelper.applyCommand(message.data, self);
|
||||||
self.processGameCommandFromUser.apply(self, args);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
NotificationCenter.on('channel/message', function (message) {
|
|
||||||
ProtocolHelper.runCommands(message.data, function (command, options) {
|
|
||||||
self[command].call(self, options);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
NotificationCenter.on('sendControlCommandToAllUsers', this.sendControlCommandToAllUsers, this);
|
NotificationCenter.on('sendControlCommandToAllUsers', this.sendControlCommandToAllUsers, this);
|
||||||
NotificationCenter.on('channel/users/all/except', this.sendControlCommandToAllUsersExcept, this);
|
NotificationCenter.on('sendControlCommandToAllUsersExcept', this.sendControlCommandToAllUsersExcept, this);
|
||||||
|
|
||||||
console.checkpoint('channel ' + name + ' created');
|
console.checkpoint('channel ' + name + ' created');
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +33,10 @@
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Channel.prototype.addUser = function (userId) {
|
|
||||||
|
// Channel command callbacks
|
||||||
|
|
||||||
|
Channel.prototype.onAddUser = function (userId) {
|
||||||
var user = new User(userId, this);
|
var user = new User(userId, this);
|
||||||
var others = Object.keys(this.users);
|
var others = Object.keys(this.users);
|
||||||
|
|
||||||
|
|
@ -52,15 +45,15 @@
|
||||||
NotificationCenter.trigger('user/joined', user);
|
NotificationCenter.trigger('user/joined', user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Channel.prototype.onReleaseUser = function (userId) {
|
||||||
Channel.prototype.releaseUser = function (userId) {
|
|
||||||
var user = this.users[userId];
|
var user = this.users[userId];
|
||||||
//this.gameController.userIdLeft(user.id);
|
|
||||||
|
|
||||||
this.sendControlCommandToAllUsersExcept("userLeft", user.id, user);
|
this.sendControlCommandToAllUsersExcept("userLeft", user.id, user);
|
||||||
delete this.users[user.id];
|
delete this.users[user.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Sending commands
|
||||||
|
|
||||||
Channel.prototype.sendControlCommandToAllUsers = function (command, options) {
|
Channel.prototype.sendControlCommandToAllUsers = function (command, options) {
|
||||||
for(var id in this.users) {
|
for(var id in this.users) {
|
||||||
this.users[id].sendControlCommand(command, options);
|
this.users[id].sendControlCommand(command, options);
|
||||||
|
|
@ -74,11 +67,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
Channel.prototype.processGameCommandFromUser = function (command, options, user) {
|
|
||||||
this.gameController.progressGameCommandFromUser(command, options, user);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return Channel;
|
return Channel;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -2,7 +2,7 @@ define([
|
||||||
"Game/Core/GameController",
|
"Game/Core/GameController",
|
||||||
"Game/Core/Physics/Engine",
|
"Game/Core/Physics/Engine",
|
||||||
"Game/Config/Settings",
|
"Game/Config/Settings",
|
||||||
"Game/Core/Control/InputController",
|
"Game/Server/Control/InputController",
|
||||||
"Lib/Utilities/RequestAnimFrame",
|
"Lib/Utilities/RequestAnimFrame",
|
||||||
"Game/Core/NotificationCenter"
|
"Game/Core/NotificationCenter"
|
||||||
],
|
],
|
||||||
|
|
@ -68,9 +68,6 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No
|
||||||
do {
|
do {
|
||||||
var userData = body.GetUserData();
|
var userData = body.GetUserData();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(userData && body.IsAwake()) {
|
if(userData && body.IsAwake()) {
|
||||||
update[userData] = {
|
update[userData] = {
|
||||||
p: body.GetPosition(),
|
p: body.GetPosition(),
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ function (NotificationCenter, Channel) {
|
||||||
};
|
};
|
||||||
|
|
||||||
PipeToLobby.prototype.onMessage = function (message) {
|
PipeToLobby.prototype.onMessage = function (message) {
|
||||||
NotificationCenter.trigger(message.recipient + '/message', message);
|
NotificationCenter.trigger(message.recipient + '/controlCommand', message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return PipeToLobby;
|
return PipeToLobby;
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,27 @@ function(Parent, NotificationCenter, ProtocolHelper) {
|
||||||
self.sendControlCommand("joinSuccess", options);
|
self.sendControlCommand("joinSuccess", options);
|
||||||
});
|
});
|
||||||
|
|
||||||
NotificationCenter.on('user/' + this.id + "/message", function(message) { // FIXME: right now only game commands?
|
NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) { // FIXME: right now only game commands?
|
||||||
ProtocolHelper.runCommands(message.data, function (command, options) {
|
ProtocolHelper.applyCommand(message.data, self);
|
||||||
self.gameCommand(command, options);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype = Object.create(Parent.prototype);
|
User.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
|
User.prototype.setPlayer = function(player) {
|
||||||
|
this.player = player;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// User command callbacks
|
||||||
|
|
||||||
|
User.prototype.onGameCommand = function(command) {
|
||||||
|
this.player.inputController.applyCommand(command);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Sending commands
|
||||||
|
|
||||||
User.prototype.sendControlCommand = function(command, options) {
|
User.prototype.sendControlCommand = function(command, options) {
|
||||||
var recipient = "user/" + this.id;
|
var recipient = "user/" + this.id;
|
||||||
var data = ProtocolHelper.encodeCommand(command, options);
|
var data = ProtocolHelper.encodeCommand(command, options);
|
||||||
|
|
@ -44,13 +56,6 @@ function(Parent, NotificationCenter, ProtocolHelper) {
|
||||||
this.sendControlCommand("gameCommand", data);
|
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;
|
return User;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ function (User, Channel, PipeToChannel, NotificationCenter) {
|
||||||
channelPipe.send('channel', { releaseUser: user.id });
|
channelPipe.send('channel', { releaseUser: user.id });
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
NotificationCenter.on('user/gameCommand', function (userId, data) {
|
NotificationCenter.on('user/controlCommand', function (userId, data) {
|
||||||
channelPipe.sendToUser(userId, data);
|
channelPipe.sendToUser(userId, data);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,16 +13,10 @@ function (Parent, ProtocolHelper, NotificationCenter) {
|
||||||
this.channelProcess = null;
|
this.channelProcess = null;
|
||||||
this.socketLink = socketLink;
|
this.socketLink = socketLink;
|
||||||
|
|
||||||
var self = this;
|
socketLink.on('message', this.onMessage.bind(this));
|
||||||
|
socketLink.on('disconnect', this.onDisconnect.bind(this));
|
||||||
|
|
||||||
socketLink.on('message', function (message) {
|
NotificationCenter.on("user/" + this.socketLink.id + "/message", this.socketLink.send, this.socketLink);
|
||||||
self.onMessage(message);
|
|
||||||
});
|
|
||||||
socketLink.on('disconnect', function () {
|
|
||||||
self.onDisconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
NotificationCenter.on("user/" + this.socketLink.id + "/message", this.onChannelMessage, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype = Object.create(Parent.prototype);
|
User.prototype = Object.create(Parent.prototype);
|
||||||
|
|
@ -31,39 +25,32 @@ function (Parent, ProtocolHelper, NotificationCenter) {
|
||||||
this.channelProcess = channelProcess;
|
this.channelProcess = channelProcess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Socket callbacks
|
||||||
|
|
||||||
User.prototype.onMessage = function (message) {
|
User.prototype.onMessage = function (message) {
|
||||||
var self = this;
|
ProtocolHelper.applyCommand(message, this);
|
||||||
ProtocolHelper.runCommands(message, function (command, options) {
|
|
||||||
self.processControlCommand(command, options);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype.onDisconnect = function () {
|
User.prototype.onDisconnect = function () {
|
||||||
this.coordinator.removeUser(this);
|
this.coordinator.removeUser(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
User.prototype.processControlCommand = function (command, options) {
|
|
||||||
switch(command) {
|
|
||||||
|
|
||||||
case 'join':
|
// User command callbacks
|
||||||
this.coordinator.assignUserToChannel(this, options);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'leave':
|
User.prototype.onJoin = function(options) {
|
||||||
this.coordinator.assignUserToLobby(this);
|
this.coordinator.assignUserToChannel(this, options);
|
||||||
break;
|
};
|
||||||
|
|
||||||
case 'gameCommand':
|
User.prototype.onLeave = function(options) {
|
||||||
NotificationCenter.trigger("user/gameCommand", this.id, options);
|
this.coordinator.assignUserToLobby(this);
|
||||||
break;
|
};
|
||||||
|
|
||||||
default:
|
User.prototype.onGameCommand = function(options) {
|
||||||
break;
|
// repacking for transport via pipe
|
||||||
}
|
var message = ProtocolHelper.encodeCommand("gameCommand", options);
|
||||||
}
|
NotificationCenter.trigger("user/controlCommand", this.id, message);
|
||||||
|
|
||||||
User.prototype.onChannelMessage = function(message) {
|
|
||||||
this.socketLink.send(message);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return User;
|
return User;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue