mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
This commit is contained in:
parent
039213cf50
commit
55256ada95
23 changed files with 409 additions and 110 deletions
|
|
@ -13,30 +13,27 @@
|
|||
|
||||
var self = this;
|
||||
|
||||
this.options = options = Options.merge(options, {
|
||||
levelUids: Settings.DEFAULT_LEVELS
|
||||
});
|
||||
|
||||
this.name = options.channelName;
|
||||
this.users = {};
|
||||
|
||||
this.pipeToServer = pipeToServer;
|
||||
this.levelListIndex = -1;
|
||||
|
||||
this.gameController = new GameController(this);
|
||||
|
||||
|
||||
this.options = options = Options.merge(options, {
|
||||
levelUids: Settings.CHANNEL_DEFAULT_LEVELS
|
||||
});
|
||||
|
||||
// Notification Center
|
||||
Nc.on(Nc.ns.channel.events.round.end, this.onEndRound, this);
|
||||
Nc.on(Nc.ns.channel.events.controlCommand.channel, function (message) {
|
||||
ProtocolHelper.applyCommand(message.data, self);
|
||||
});
|
||||
|
||||
|
||||
Nc.on(Nc.ns.channel.to.client.gameCommand.broadcast, this.broadcastGameCommand, this);
|
||||
|
||||
// prepared - not triggered yet
|
||||
Nc.on(Nc.ns.channel.to.client.controlCommand.broadcast, this.broadcastControlCommand, this);
|
||||
//Nc.on(Nc.ns.channel.to.client.gameCommand.broadcastExcept, this.broadcastGameCommandExcept, this);
|
||||
//Nc.on(Nc.ns.channel.to.client.controlCommand.broadcast, this.broadcastControlCommand, this);
|
||||
//Nc.on(Nc.ns.channel.to.client.controlCommand.broadcastExcept, this.broadcastControlCommandExcept, this);
|
||||
|
||||
this.beginRound();
|
||||
|
||||
console.checkpoint('channel ' + this.name + ' created');
|
||||
|
||||
setTimeout(function() {
|
||||
|
|
@ -46,19 +43,68 @@
|
|||
}, Settings.CHANNEL_DESTRUCTION_TIME * 1000);
|
||||
}
|
||||
|
||||
Channel.prototype.getNextLevelUid = function() {
|
||||
this.levelListIndex = (this.levelListIndex + 1) % this.options.levelUids.length;
|
||||
return this.options.levelUids[this.levelListIndex];
|
||||
};
|
||||
|
||||
|
||||
Channel.prototype.beginRound = function() {
|
||||
|
||||
if(this.gameController) {
|
||||
this.gameController.destroy();
|
||||
delete this.gameController;
|
||||
}
|
||||
|
||||
var gameControllerOptions = {
|
||||
channelName: this.name,
|
||||
scoreLimit: this.options.scoreLimit,
|
||||
levelUid: this.getNextLevelUid()
|
||||
};
|
||||
|
||||
console.log(gameControllerOptions)
|
||||
|
||||
this.gameController = new GameController(gameControllerOptions);
|
||||
|
||||
for(var userId in this.users) {
|
||||
this.gameController.createPlayer(this.users[userId]);
|
||||
}
|
||||
|
||||
var clientGameControllerOptions = {
|
||||
levelUid: gameControllerOptions.levelUid
|
||||
};
|
||||
|
||||
this.broadcastControlCommand("beginRound", clientGameControllerOptions);
|
||||
};
|
||||
|
||||
Channel.prototype.onEndRound = function() {
|
||||
var self = this;
|
||||
this.broadcastControlCommand("endRound", true);
|
||||
|
||||
setTimeout(function() {
|
||||
self.beginRound();
|
||||
}, Settings.CHANNEL_END_ROUND_TIME * 1000);
|
||||
};
|
||||
|
||||
|
||||
// Channel command callbacks
|
||||
|
||||
Channel.prototype.onAddUser = function (options) {
|
||||
var self = this;
|
||||
|
||||
var clientGameControllerOptions = {
|
||||
levelUid: this.gameController.options.levelUid
|
||||
};
|
||||
|
||||
if(!this.gameController.level || !this.gameController.level.isLoaded) {
|
||||
var token = Nc.on(Nc.ns.core.game.events.level.loaded, function() {
|
||||
self.sendJoinSuccess(options);
|
||||
this.users[options.id].sendControlCommand("beginRound", clientGameControllerOptions);
|
||||
Nc.off(token);
|
||||
});
|
||||
} else {
|
||||
self.sendJoinSuccess(options);
|
||||
this.users[options.id].sendControlCommand("beginRound", clientGameControllerOptions);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -91,15 +137,24 @@
|
|||
};
|
||||
|
||||
Channel.prototype.onReleaseUser = function (userId) {
|
||||
var self = this;
|
||||
var user = this.users[userId];
|
||||
Nc.trigger(Nc.ns.channel.events.user.left, userId);
|
||||
delete this.users[userId];
|
||||
|
||||
this.broadcastControlCommand("userLeft", userId);
|
||||
|
||||
// FIXME: if this was the last user terminate forked process
|
||||
if(Object.keys(this.users).length < 1) {
|
||||
this.destroy();
|
||||
|
||||
console.checkpoint("channel (" + this.name + ") destruction scheduled. t - " + Settings.CHANNEL_DESTRUCTION_TIME + " seconds");
|
||||
|
||||
setTimeout(function() {
|
||||
if(Object.keys(self.users).length < 1) {
|
||||
self.destroy();
|
||||
} else {
|
||||
console.checkpoint("channel (" + self.name + ") destruction aborted (a user joined).");
|
||||
}
|
||||
}, Settings.CHANNEL_DESTRUCTION_TIME * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,32 +14,34 @@ define([
|
|||
|
||||
function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, Nc, Box2D, Player, GameObject, Doll, RagDoll) {
|
||||
|
||||
function GameController (channel) {
|
||||
|
||||
this.channel = channel;
|
||||
function GameController (options) {
|
||||
|
||||
Parent.call(this);
|
||||
this.animationTimeout = null;
|
||||
this.worldUpdateTimeout = null;
|
||||
this.spawnTimeouts = [];
|
||||
|
||||
Nc.on(Nc.ns.channel.events.user.joined, this.onUserJoined, this);
|
||||
Nc.on(Nc.ns.channel.events.user.left, this.onUserLeft, this);
|
||||
Nc.on(Nc.ns.channel.events.user.level.reset, this.onResetLevel, this);
|
||||
Nc.on(Nc.ns.channel.events.user.client.ready, this.onClientReady, this);
|
||||
Parent.call(this, options);
|
||||
|
||||
Nc.on(Nc.ns.core.game.player.killed, this.onPlayerKilled, this);
|
||||
this.ncTokens = this.ncTokens.concat([
|
||||
Nc.on(Nc.ns.channel.events.user.joined, this.onUserJoined, this),
|
||||
Nc.on(Nc.ns.channel.events.user.left, this.onUserLeft, this),
|
||||
Nc.on(Nc.ns.channel.events.user.level.reset, this.onResetLevel, this),
|
||||
Nc.on(Nc.ns.channel.events.user.client.ready, this.onClientReady, this),
|
||||
Nc.on(Nc.ns.core.game.events.level.loaded, this.onLevelLoaded, this),
|
||||
Nc.on(Nc.ns.core.game.player.killed, this.onPlayerKilled, this), // FIXME: move to events
|
||||
]);
|
||||
|
||||
console.checkpoint('starting game controller for channel ' + channel.name);
|
||||
|
||||
var nextUid = this.getNextLevelUid();
|
||||
this.loadLevel(nextUid);
|
||||
console.checkpoint('starting game controller for channel (' + options.channelName + ')');
|
||||
}
|
||||
|
||||
|
||||
GameController.prototype = Object.create(Parent.prototype);
|
||||
|
||||
GameController.prototype.update = function () {
|
||||
|
||||
Parent.prototype.update.call(this);
|
||||
|
||||
requestAnimFrame(this.update.bind(this));
|
||||
this.animationTimeout = requestAnimFrame(this.update.bind(this));
|
||||
|
||||
this.physicsEngine.update();
|
||||
for(var id in this.players) {
|
||||
|
|
@ -48,7 +50,6 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
}
|
||||
|
||||
GameController.prototype.onLevelLoaded = function() {
|
||||
Parent.prototype.onLevelLoaded.call(this);
|
||||
this.updateWorld();
|
||||
};
|
||||
|
||||
|
|
@ -62,8 +63,11 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
user.setPlayer(player);
|
||||
};
|
||||
|
||||
GameController.prototype.onPlayerKilled = function(player, respawnTime) {
|
||||
this.spawnPlayer(player, respawnTime);
|
||||
GameController.prototype.onPlayerKilled = function(player, killedByPlayer) {
|
||||
if(killedByPlayer.stats.score >= this.options.scoreLimit) {
|
||||
Nc.trigger(Nc.ns.channel.events.round.end);
|
||||
}
|
||||
this.spawnPlayer(player, Settings.RESPAWN_TIME);
|
||||
};
|
||||
|
||||
GameController.prototype.spawnPlayer = function(player, respawnTime) {
|
||||
|
|
@ -74,7 +78,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
? Settings.RESPAWN_TIME
|
||||
: respawnTime;
|
||||
|
||||
setTimeout(function() {
|
||||
var spawnTimeout = setTimeout(function() {
|
||||
player.spawn(spawnPoint.x, spawnPoint.y);
|
||||
// put it into
|
||||
self.gameObjects.animated.push(player);
|
||||
|
|
@ -86,7 +90,13 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
};
|
||||
|
||||
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "spawnPlayer", options);
|
||||
|
||||
var i = self.spawnTimeouts.indexOf(spawnTimeout);
|
||||
self.spawnTimeouts.splice(i, 1);
|
||||
|
||||
}, respawnTime * 1000);
|
||||
|
||||
this.spawnTimeouts.push(spawnTimeout);
|
||||
};
|
||||
|
||||
GameController.prototype.updateWorld = function () {
|
||||
|
|
@ -97,7 +107,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'worldUpdate', update);
|
||||
}
|
||||
|
||||
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
|
||||
this.worldUpdateTimeout = setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
|
||||
}
|
||||
|
||||
GameController.prototype.getWorldUpdateObject = function(getSleeping) {
|
||||
|
|
@ -196,23 +206,16 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
}
|
||||
};
|
||||
|
||||
GameController.prototype.getNextLevelUid = function() {
|
||||
if(!this.level) return this.channel.options.levelUids[0];
|
||||
GameController.prototype.destroy = function() {
|
||||
clearTimeout(this.animationTimeout);
|
||||
clearTimeout(this.worldUpdateTimeout);
|
||||
|
||||
var levelCount = this.channel.options.levelUids.length;
|
||||
|
||||
for (var i = 0; i < levelCount; i++) {
|
||||
var uid = this.channel.options.levelUids[i];
|
||||
if(uid == this.level.uid) {
|
||||
break;
|
||||
}
|
||||
for (var i = 0; i < this.spawnTimeouts.length; i++) {
|
||||
clearTimeout(this.spawnTimeouts[i]);
|
||||
};
|
||||
|
||||
var next = i + 1;
|
||||
|
||||
return this.channel.options.levelUids[next % levelCount];
|
||||
Parent.prototype.destroy.call(this);
|
||||
};
|
||||
|
||||
|
||||
return GameController;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -30,15 +30,21 @@ function (Parent, Settings, Nc) {
|
|||
};
|
||||
|
||||
RagDoll.prototype.delayedDestroy = function() {
|
||||
var self = this;
|
||||
this.scheduledForDestruction = true;
|
||||
this.destructionTimeout = setTimeout(this.destroy.bind(this), Settings.RAGDOLL_DESTRUCTION_TIME * 1000);
|
||||
this.destructionTimeout = setTimeout(function() {
|
||||
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'removeGameObject', {
|
||||
type: 'animated',
|
||||
uid: self.uid
|
||||
});
|
||||
self.destroy.bind(self);
|
||||
}, Settings.RAGDOLL_DESTRUCTION_TIME * 1000);
|
||||
};
|
||||
|
||||
RagDoll.prototype.destroy = function() {
|
||||
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'removeGameObject', {
|
||||
type: 'animated',
|
||||
uid: this.uid
|
||||
});
|
||||
if(this.scheduledForDestruction) {
|
||||
clearTimeout(this.destructionTimeout);
|
||||
}
|
||||
Parent.prototype.destroy.call(this);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue