This commit is contained in:
Jeena 2014-02-10 16:11:01 +01:00
parent 7a59e175e0
commit 89c5e4a5d8
6 changed files with 64 additions and 25 deletions

View file

@ -73,6 +73,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
}
}
}
} while (body = body.GetNext());
}
@ -91,6 +92,14 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
var player = this.players[playerId];
player.spawn(x, y);
this.gameObjects.animated.push(player.getDoll());
if(options.holdingItemUid) {
this.onHandActionResponse({
itemUid: options.holdingItemUid,
action: "grab",
playerId: playerId
});
}
}
GameController.prototype.onHandActionResponse = function(options) {
@ -112,7 +121,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
player.grab(item);
}
} else {
console.warn("Item for joint can not be found locally.")
console.warn("Item for joint can not be found locally. " + options.itemUid)
}
};

View file

@ -14,11 +14,6 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
this.gameController = null;
this.users = {};
this.init();
}
Networker.prototype.init = function () {
this.socketLink.on('connect', this.onConnect.bind(this));
this.socketLink.on('disconnect', this.onDisconnect.bind(this));
@ -33,7 +28,6 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
NotificationCenter.on("sendGameCommand", this.sendGameCommand, this);
}
// Socket callbacks
Networker.prototype.onConnect = function () {
@ -49,6 +43,10 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
}
Networker.prototype.onJoinSuccess = function (options) {
NotificationCenter.on("game/level/loaded", function() {
this.onLevelLoaded(options);
}, this);
this.gameController = new GameController();
this.gameController.loadLevel(options.levelUid);
@ -61,6 +59,10 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
}
}
this.initPing();
}
Networker.prototype.onLevelLoaded = function(options) {
if (options.spawnedPlayers) {
for(var i = 0; i < options.spawnedPlayers.length; i++) {
this.gameController.onSpawnPlayer(options.spawnedPlayers[i]);
@ -70,9 +72,7 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
if (options.worldUpdate) {
this.gameController.onWorldUpdate(options.worldUpdate);
}
this.initPing();
}
};
Networker.prototype.initPing = function() {

View file

@ -1,10 +1,11 @@
define([
"Game/" + GLOBALS.context + "/Physics/Engine",
"Game/" + GLOBALS.context + "/Loader/TiledLevel",
"Game/" + GLOBALS.context + "/Player"
"Game/" + GLOBALS.context + "/Player",
"Lib/Utilities/NotificationCenter"
],
function (PhysicsEngine, TiledLevel, Player) {
function (PhysicsEngine, TiledLevel, Player, NotificationCenter) {
function GameController () {
this.players = {};
@ -17,7 +18,7 @@ function (PhysicsEngine, TiledLevel, Player) {
this.physicsEngine = new PhysicsEngine();
this.physicsEngine.setCollisionDetector();
this.update();
NotificationCenter.on("game/level/loaded", this.onLevelLoaded, this);
}
GameController.prototype.update = function() {
@ -45,6 +46,10 @@ function (PhysicsEngine, TiledLevel, Player) {
this.loadLevel(this.level.uid);
};
GameController.prototype.onLevelLoaded = function() {
this.update();
};
GameController.prototype.destroy = function () {
for(var player in this.players) {

View file

@ -2,18 +2,20 @@ define([
"Game/Config/Settings",
"Lib/Vendor/Box2D",
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Exception",
"Game/" + GLOBALS.context + "/Collision/Detector",
"Game/" + GLOBALS.context + "/GameObjects/Tile",
"Game/" + GLOBALS.context + "/GameObjects/Item",
"Game/" + GLOBALS.context + "/GameObjects/Items/Skateboard"
], function (Settings, Box2D, NotificationCenter, CollisionDetector, Tile, Item, Skateboard) {
], function (Settings, Box2D, NotificationCenter, Exception, CollisionDetector, Tile, Item, Skateboard) {
function Level (uid, engine, gameObjects) {
this.uid = uid;
this.engine = engine;
this.levelObject = null;
this.gameObjects = gameObjects;
this.isLoaded = false;
this.load(this.uid);
}
@ -24,6 +26,7 @@ define([
self.levelData = levelData;
self.createTiles();
self.createItems();
self.isLoaded = true;
NotificationCenter.trigger("game/level/loaded");
});
}
@ -33,8 +36,8 @@ define([
for (var i = 0; i < this.gameObjects[key].length; i++) {
this.gameObjects[key][i].destroy();
}
//this.gameObjects[key] = [];
}
this.isLoaded = false;
}
Level.prototype.createTiles = function () {
@ -77,9 +80,10 @@ define([
};
Level.prototype.getRandomSpawnPoint = function() {
throw new Error("Level not loaded.");
return {
x: 150 + Math.random() * 300,
y: 0
y: -500
};
};

View file

@ -45,6 +45,19 @@
// Channel command callbacks
Channel.prototype.onAddUser = function (userId) {
var self = this;
if(!this.gameController.level || !this.gameController.level.isLoaded) {
var token = NotificationCenter.on("game/level/loaded", function() {
self.sendJoinSuccess(userId);
NotificationCenter.off(token);
});
} else {
self.sendJoinSuccess(userId);
}
}
Channel.prototype.sendJoinSuccess = function(userId) {
var user = new User(userId, this);
var joinedUsers = Object.keys(this.users);
var spawnedPlayers = this.gameController.getSpawnedPlayersAndTheirPositions();
@ -64,11 +77,11 @@
spawnedPlayers: spawnedPlayers,
worldUpdate: worldUpdate,
levelUid: levelUid
};
};
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options);
NotificationCenter.trigger('user/joined', user);
}
NotificationCenter.trigger('user/joined', user);
};
Channel.prototype.onReleaseUser = function (userId) {
var user = this.users[userId];

View file

@ -5,12 +5,13 @@ define([
"Game/Server/Control/PlayerController",
"Lib/Utilities/RequestAnimFrame",
"Lib/Utilities/NotificationCenter",
"Lib/Vendor/Box2D",
"Game/Server/Player",
"Game/Server/GameObjects/GameObject",
"Game/Server/GameObjects/Doll"
],
function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Player, GameObject, Doll) {
function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Box2D, Player, GameObject, Doll) {
function GameController (channel) {
@ -22,7 +23,6 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
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);
@ -43,6 +43,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
}
GameController.prototype.onLevelLoaded = function() {
Parent.prototype.onLevelLoaded.call(this);
this.updateWorld();
};
@ -95,7 +96,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var body = this.physicsEngine.world.GetBodyList();
do {
if(getSleeping || body.IsAwake()) {
if((getSleeping || body.IsAwake()) && body.GetType() === Box2D.Dynamics.b2Body.b2_dynamicBody) {
var userData = body.GetUserData();
if (userData instanceof GameObject) {
@ -125,11 +126,18 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
for(id in this.players) {
var player = this.players[id];
if(player.isSpawned) {
spawnedPlayers.push({
var options = {
id: id,
x: player.getPosition().x * Settings.RATIO,
y: player.getPosition().y * Settings.RATIO
});
};
if(player.holdingItem) {
options.holdingItemUid = player.holdingItem.uid;
}
spawnedPlayers.push(options);
}
}