added getSpawnedPlayersAndTheirPositions in order to get already spawned players to spawn the moment you join and spawn yourself. fixes #26

This commit is contained in:
Logsol 2013-01-05 22:38:29 +01:00
parent a852bccae6
commit a1e94e1ed0
6 changed files with 55 additions and 8 deletions

View file

@ -72,8 +72,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
x = options.x, x = options.x,
y = options.y; y = options.y;
console.log(this.players, options);
var player = this.players[playerId]; var player = this.players[playerId];
player.spawn(x, y); player.spawn(x, y);

View file

@ -49,9 +49,17 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
this.onUserJoined(options.userId); this.onUserJoined(options.userId);
this.gameController.onJoinMe(options.userId); this.gameController.onJoinMe(options.userId);
if (options.others) { if (options.joinedUsers) {
for(var i = 0; i < options.others.length; i++) { for(var i = 0; i < options.joinedUsers.length; i++) {
this.onUserJoined(options.others[i]); this.onUserJoined(options.joinedUsers[i]);
}
}
if (options.spawnedPlayers) {
for(var i = 0; i < options.spawnedPlayers.length; i++) {
this.gameController.onSpawnPlayer(options.spawnedPlayers[i]);
console.log("already spawned player, options: ", options.spawnedPlayers[i])
} }
} }
} }

View file

@ -1,4 +1,10 @@
define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detector"], function (Box2D, Settings, CollisionDetector) { define([
"Lib/Vendor/Box2D",
"Game/Config/Settings",
"Game/Core/Collision/Detector"
],
function (Box2D, Settings, CollisionDetector) {
function Doll (physicsEngine, id) { function Doll (physicsEngine, id) {
this.id = id; this.id = id;

View file

@ -32,6 +32,10 @@ function (Doll, Settings) {
return this.doll.getBody(); return this.doll.getBody();
} }
Player.prototype.getPosition = function () {
return this.getBody().GetPosition();
}
Player.prototype.setStanding = function (isStanding) { Player.prototype.setStanding = function (isStanding) {
var resetStates = ['jump', 'jumploop']; var resetStates = ['jump', 'jumploop'];
if (resetStates.indexOf(this.currentAnimationState)>=0 && !this.standing && isStanding) { if (resetStates.indexOf(this.currentAnimationState)>=0 && !this.standing && isStanding) {

View file

@ -38,14 +38,16 @@
Channel.prototype.onAddUser = function (userId) { Channel.prototype.onAddUser = function (userId) {
var user = new User(userId, this); var user = new User(userId, this);
var others = Object.keys(this.users); var joinedUsers = Object.keys(this.users);
var spawnedPlayers = this.gameController.getSpawnedPlayersAndTheirPositions();
this.users[user.id] = user; this.users[user.id] = user;
var options = { var options = {
userId: user.id, userId: user.id,
channelName: this.name, channelName: this.name,
others: others joinedUsers: joinedUsers,
spawnedPlayers: spawnedPlayers
}; };
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options); NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options);

View file

@ -91,5 +91,34 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
} }
GameController.prototype.getSpawnedPlayers = function() {
var spawnedPlayers = {};
for(player in this.players) {
if(player.isSpawned) {
spawnedPlayers[player.id] = player;
}
}
return spawnedPlayers;
};
GameController.prototype.getSpawnedPlayersAndTheirPositions = function() {
var spawnedPlayers = [];
for(id in this.players) {
var player = this.players[id];
if(player.isSpawned) {
spawnedPlayers.push({
id: id,
x: player.getPosition().x * Settings.RATIO,
y: player.getPosition().y * Settings.RATIO
});
}
}
// FIXME: OR: use get Spawned Players and fetch them into a sort of transfer objects
// that contains only necessary data
return spawnedPlayers;
};
return GameController; return GameController;
}); });