mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
moved files towards new structure. fixes #22
This commit is contained in:
parent
8cf8f057bb
commit
9de3147406
40 changed files with 0 additions and 110 deletions
106
app/game/Server/GameController.js
Executable file
106
app/game/Server/GameController.js
Executable file
|
|
@ -0,0 +1,106 @@
|
|||
var requires = [
|
||||
"Chuck/Physics/Engine",
|
||||
"Chuck/Settings",
|
||||
"Chuck/Player",
|
||||
"Vendor/Box2D",
|
||||
"Chuck/Loader/Level",
|
||||
"Chuck/Control/InputController",
|
||||
"RequestAnimationFrame"
|
||||
];
|
||||
|
||||
define(requires, function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, requestAnimFrame){
|
||||
|
||||
function ServerProcessor (serverGame) {
|
||||
this.serverGame = serverGame;
|
||||
this.players = {};
|
||||
this.init();
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.init = function() {
|
||||
this.physicsEngine = this.factory.new(PhysicsEngine);
|
||||
|
||||
this.update();
|
||||
this.updateWorld();
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.loadLevel = function(path) {
|
||||
if (this.level) {
|
||||
this.level.unload();
|
||||
}
|
||||
|
||||
this.level = new Level(path, this.physicsEngine);
|
||||
this.level.loadLevelInToEngine();
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.getPhysicsEngine = function() {
|
||||
return this.physicsEngine;
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.update = function() {
|
||||
|
||||
requestAnimFrame(this.update.bind(this));
|
||||
|
||||
this.physicsEngine.update();
|
||||
for(var id in this.players) {
|
||||
this.players[id].player.update();
|
||||
}
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.destruct = function() {
|
||||
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.createPlayerWithId = function(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);
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.progressGameCommandFromId = function(command, options, id) {
|
||||
var inputController = this.players[id].inputController;
|
||||
if (typeof inputController[command] == 'function') {
|
||||
inputController[command](options);
|
||||
}
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.userIdLeft = function(id) {
|
||||
var player = this.players[id].player;
|
||||
player.destroy();
|
||||
delete this.players[id];
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.updateWorld = function() {
|
||||
|
||||
var update = {};
|
||||
var isUpdateNeeded = false;
|
||||
|
||||
var body = this.physicsEngine.world.GetBodyList();
|
||||
do {
|
||||
var userData = body.GetUserData();
|
||||
|
||||
if(userData && body.IsAwake()){
|
||||
update[userData] = {
|
||||
p: body.GetPosition(),
|
||||
a: body.GetAngle(),
|
||||
lv: body.GetLinearVelocity(),
|
||||
av: body.GetAngularVelocity()
|
||||
};
|
||||
isUpdateNeeded = true;
|
||||
}
|
||||
} while (body = body.GetNext());
|
||||
|
||||
if(isUpdateNeeded) {
|
||||
//this.serverGame.updateClientsWorld(update);
|
||||
this.notificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]);
|
||||
}
|
||||
|
||||
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
|
||||
}
|
||||
|
||||
return ServerProcessor;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue