This commit is contained in:
Jeena 2014-02-24 19:26:31 +01:00
parent 695008afd8
commit aa4535cb0c
10 changed files with 73 additions and 32 deletions

View file

@ -68,6 +68,25 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
this.onWorldUpdate(options.worldUpdate);
}
if (options.runtimeItems) {
for (var i = 0; i < options.runtimeItems.length; i++) {
var itemDef = options.runtimeItems[i];
var alreadyExists = false;
for (var i = 0; i < this.gameObjects.animated.length; i++) {
if(this.gameObjects.animated[i].uid == itemDef.uid) {
alreadyExists = true;
break;
}
};
if(!alreadyExists) {
var item = this.level.createItem(itemDef.uid, itemDef.options);
this.onGameObjectAdd("animated", item);
}
};
}
this.createMe(options.userId);
};

View file

@ -159,10 +159,6 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
this.setActionState("fall");
}
Doll.prototype.getPosition = function() {
return this.body.GetPosition().Copy();
};
Doll.prototype.getHeadPosition = function() {
var pos = this.body.GetPosition();
return {

View file

@ -28,6 +28,10 @@ function (Box2D, Exception) {
GameObject.prototype.getBody = function() {
return this.body;
};
GameObject.prototype.getPosition = function() {
return this.body.GetPosition().Copy();
};
return GameObject;

View file

@ -345,8 +345,8 @@ function (Parent, Box2D, Settings, NotificationCenter) {
body.SetAwake(true);
var vector = new Box2D.Common.Math.b2Vec2(
x * Settings.MAX_THROW_FORCE * limbDampingFactor,
-y * Settings.MAX_THROW_FORCE * 1.5 *limbDampingFactor // 1.5 is to throw higher then far
x * Settings.MAX_THROW_FORCE * limbDampingFactor / this.options.weight,
-y * Settings.MAX_THROW_FORCE * limbDampingFactor / this.options.weight
);
body.SetLinearVelocity(vector);
// body.SetAngularVelocity(Settings.MAX_THROW_ANGULAR_VELOCITY * x);

View file

@ -85,6 +85,21 @@ function (Parent, Box2D, Settings) {
// FIXME: implement body flip if necessary
};
Skateboard.prototype.throw = function(x, y) {
Parent.prototype.throw.call(this, x, y);
for (var i = 0; i < this.wheels.length; i++) {
var body = this.wheels[i];
body.SetAwake(true);
var vector = new Box2D.Common.Math.b2Vec2(
x * Settings.MAX_THROW_FORCE / this.options.weight,
-y * Settings.MAX_THROW_FORCE / this.options.weight
);
body.SetLinearVelocity(vector);
}
};
Skateboard.prototype.destroy = function() {
for (var i = 0; i < this.wheels.length; i++) {
this.body.GetWorld().DestroyBody(this.wheels[i]);

View file

@ -67,7 +67,7 @@ define([
var options = items[i];
var uid = "item-" + i;
var item = this.createItem(uid, options);
this.gameObjects.animated.push(item);
this.gameObjects.animated.push(item); // FIXME: use NotificationCenter
};
};

View file

@ -116,7 +116,6 @@ function (Doll, Settings, NotificationCenter, Exception, SpectatorDoll, RagDoll)
var ragDoll = new RagDoll(this.physicsEngine, "ragDoll-" + this.id + "-" + ragDollId, options);
ragDoll.setVelocities(this.doll.getVelocities());
console.log(ragDoll.uid)
this.isSpawned = false;

View file

@ -104,11 +104,17 @@
}
Channel.prototype.broadcastGameCommand = function (command, options) {
this.broadcastControlCommand("gameCommand", ProtocolHelper.encodeCommand(command, options));
for(var id in this.users) {
this.users[id].sendGameCommand(command, options);
}
}
Channel.prototype.broadcastGameCommandExcept = function (command, options, exceptUser) {
this.broadcastControlCommandExcept("gameCommand", ProtocolHelper.encodeCommand(command, options), exceptUser);
for(var id in this.users) {
if (id != exceptUser.id) {
this.users[id].sendGameCommand(command, options);
}
}
}
return Channel;

View file

@ -8,10 +8,11 @@ define([
"Lib/Vendor/Box2D",
"Game/Server/Player",
"Game/Server/GameObjects/GameObject",
"Game/Server/GameObjects/Doll"
"Game/Server/GameObjects/Doll",
"Game/Server/GameObjects/Items/RagDoll"
],
function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Box2D, Player, GameObject, Doll) {
function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Box2D, Player, GameObject, Doll, RagDoll) {
function GameController (channel) {
@ -77,15 +78,13 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
// put it into
self.gameObjects.animated.push(player);
var message = {
spawnPlayer: {
id: player.id,
x: spawnPoint.x,
y: spawnPoint.y
}
var options = {
id: player.id,
x: spawnPoint.x,
y: spawnPoint.y
};
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", message);
NotificationCenter.trigger("broadcastGameCommand", "spawnPlayer", options);
}, respawnTime * 1000);
};
@ -103,7 +102,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var update = this.getWorldUpdateObject(false);
if(Object.getOwnPropertyNames(update).length > 0) {
NotificationCenter.trigger("broadcastControlCommand", 'gameCommand', {worldUpdate:update});
NotificationCenter.trigger("broadcastGameCommand", 'worldUpdate', update);
}
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
@ -143,7 +142,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
GameController.prototype.getSpawnedPlayersAndTheirPositions = function() {
var spawnedPlayers = [];
for(id in this.players) {
for(var id in this.players) {
var player = this.players[id];
if(player.isSpawned) {
@ -164,23 +163,21 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
return spawnedPlayers;
};
GameController.prototype.getAdditionalObjects = function() {
GameController.prototype.getRuntimeItems = function() {
var objects = []
for (var i = 0; i < this.gameObjects.animated.length; i++) {
if(this.gameObjects.animated[i] instanceof RagDoll) {
var object = this.gameObjects.animated[i];
var options = object.options;
options.x = object.getPosition().x;
options.y = object.getPosition().y;
objects.push({
uid: objects.uid,
options: object.options,
x: object.getPosition().x,
y: object.getPosition().y
uid: object.uid,
options: object.options
});
}
};
var object = {
// FIXME: finish it!
}
return objects;
};
@ -192,6 +189,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var options = {
spawnedPlayers: this.getSpawnedPlayersAndTheirPositions(),
worldUpdate: this.getWorldUpdateObject(true),
runtimeItems: this.getRuntimeItems(),
userId: userId
}
@ -200,7 +198,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
GameController.prototype.onResetLevel = function(userId) {
Parent.prototype.onResetLevel.call(this);
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", {resetLevel:true});
NotificationCenter.trigger("broadcastGameCommand", "resetLevel", true);
for (var key in this.players) {
this.spawnPlayer(this.players[key]);
}

View file

@ -12,6 +12,7 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
this.channel = channel;
this.player = null;
this.isReady = false;
var self = this;
NotificationCenter.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead
@ -51,6 +52,7 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
if(command.hasOwnProperty("resetLevel")) {
NotificationCenter.trigger("user/resetLevel", this.id);
} else if(command.hasOwnProperty("clientReady")) {
this.isReady = true;
NotificationCenter.trigger("user/clientReady", this.id);
} else {
this.player.playerController.applyCommand(command);
@ -69,8 +71,10 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
};
User.prototype.sendGameCommand = function(command, options) {
var data = ProtocolHelper.encodeCommand(command, options);
this.sendControlCommand("gameCommand", data);
if(this.isReady) {
var data = ProtocolHelper.encodeCommand(command, options);
this.sendControlCommand("gameCommand", data);
}
};