mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
implemented level load, more to do see #1
This commit is contained in:
parent
b02036a019
commit
953159e6bd
12 changed files with 333 additions and 409 deletions
|
|
@ -2,22 +2,26 @@
|
|||
"Game/Server/GameController",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Game/Server/User",
|
||||
"Lib/Utilities/Protocol/Helper"
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/Options"
|
||||
],
|
||||
|
||||
function (GameController, NotificationCenter, User, ProtocolHelper) {
|
||||
function (GameController, NotificationCenter, User, ProtocolHelper, Options) {
|
||||
|
||||
function Channel (pipeToLobby, name) {
|
||||
function Channel (pipeToLobby, name, options) {
|
||||
|
||||
var self = this;
|
||||
|
||||
this.options = options = Options.merge(options, {
|
||||
levelUids: ["dungeon"]
|
||||
});
|
||||
|
||||
this.name = name;
|
||||
this.users = {};
|
||||
|
||||
this.pipeToLobby = pipeToLobby;
|
||||
|
||||
this.gameController = new GameController(this);
|
||||
this.gameController.loadLevel("default.json");
|
||||
|
||||
NotificationCenter.on('channel/controlCommand', function (message) {
|
||||
ProtocolHelper.applyCommand(message.data, self);
|
||||
|
|
@ -41,6 +45,11 @@
|
|||
var joinedUsers = Object.keys(this.users);
|
||||
var spawnedPlayers = this.gameController.getSpawnedPlayersAndTheirPositions();
|
||||
var worldUpdate = this.gameController.getWorldUpdateObject(true);
|
||||
|
||||
var levelUid = null;
|
||||
if(this.gameController.level) {
|
||||
levelUid = this.gameController.level.uid;
|
||||
}
|
||||
|
||||
this.users[user.id] = user;
|
||||
|
||||
|
|
@ -49,8 +58,8 @@
|
|||
channelName: this.name,
|
||||
joinedUsers: joinedUsers,
|
||||
spawnedPlayers: spawnedPlayers,
|
||||
worldUpdate: worldUpdate
|
||||
|
||||
worldUpdate: worldUpdate,
|
||||
levelUid: levelUid
|
||||
};
|
||||
|
||||
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options);
|
||||
|
|
|
|||
|
|
@ -18,14 +18,16 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
|
||||
Parent.call(this);
|
||||
|
||||
this.updateWorld();
|
||||
|
||||
NotificationCenter.on('user/joined', this.userJoined, this);
|
||||
NotificationCenter.on('user/left', this.userLeft, this); // FIXME: refactor this.userLeft -> this.onUserLeft, even in core and client
|
||||
NotificationCenter.on('user/resetLevel', this.onResetLevel, this);
|
||||
NotificationCenter.on('player/killed', this.spawnPlayer, this);
|
||||
NotificationCenter.on('game/level/loaded', this.onLevelLoaded, this);
|
||||
|
||||
console.checkpoint('starting game controller for channel ' + channel.name);
|
||||
|
||||
var nextUid = this.getNextLevelUid();
|
||||
this.loadLevel(nextUid);
|
||||
}
|
||||
|
||||
GameController.prototype = Object.create(Parent.prototype);
|
||||
|
|
@ -40,6 +42,10 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
}
|
||||
}
|
||||
|
||||
GameController.prototype.onLevelLoaded = function() {
|
||||
this.updateWorld();
|
||||
};
|
||||
|
||||
GameController.prototype.userJoined = function (user) {
|
||||
Parent.prototype.userJoined.call(this, user);
|
||||
var player = this.players[user.id];
|
||||
|
|
@ -137,6 +143,23 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
}
|
||||
};
|
||||
|
||||
GameController.prototype.getNextLevelUid = function() {
|
||||
if(!this.level) return this.channel.options.levelUids[0];
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
var next = i + 1;
|
||||
|
||||
return this.channel.options.levelUids[next % levelCount];
|
||||
};
|
||||
|
||||
|
||||
return GameController;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
define([
|
||||
"Game/Core/Loader/Level",
|
||||
"Game/Config/Settings"
|
||||
"Game/Config/Settings",
|
||||
"fs"
|
||||
],
|
||||
|
||||
function (Parent, Fs) {
|
||||
function (Parent, Settings, fs) {
|
||||
|
||||
function Level () {
|
||||
Parent.call(this);
|
||||
function Level (uid, engine, gameObjects) {
|
||||
Parent.call(this, uid, engine, gameObjects);
|
||||
}
|
||||
|
||||
Level.prototype = Object.create(Parent.prototype);
|
||||
|
||||
Level.prototype.loadLevelObjectFromPath = function (path, callback) {
|
||||
Level.prototype.loadLevelDataFromPath = function (path, callback) {
|
||||
// overwriting parent
|
||||
|
||||
fs.readFile( + path, function (err, data) {
|
||||
fs.readFile(path, "utf8", function (err, data) {
|
||||
if (err) throw err;
|
||||
callback(data);
|
||||
callback(JSON.parse(data));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue