mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
fixed #53
This commit is contained in:
parent
695008afd8
commit
aa4535cb0c
10 changed files with 73 additions and 32 deletions
|
|
@ -68,6 +68,25 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
|
||||||
this.onWorldUpdate(options.worldUpdate);
|
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);
|
this.createMe(options.userId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -159,10 +159,6 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
|
||||||
this.setActionState("fall");
|
this.setActionState("fall");
|
||||||
}
|
}
|
||||||
|
|
||||||
Doll.prototype.getPosition = function() {
|
|
||||||
return this.body.GetPosition().Copy();
|
|
||||||
};
|
|
||||||
|
|
||||||
Doll.prototype.getHeadPosition = function() {
|
Doll.prototype.getHeadPosition = function() {
|
||||||
var pos = this.body.GetPosition();
|
var pos = this.body.GetPosition();
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,10 @@ function (Box2D, Exception) {
|
||||||
GameObject.prototype.getBody = function() {
|
GameObject.prototype.getBody = function() {
|
||||||
return this.body;
|
return this.body;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GameObject.prototype.getPosition = function() {
|
||||||
|
return this.body.GetPosition().Copy();
|
||||||
|
};
|
||||||
|
|
||||||
return GameObject;
|
return GameObject;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -345,8 +345,8 @@ function (Parent, Box2D, Settings, NotificationCenter) {
|
||||||
body.SetAwake(true);
|
body.SetAwake(true);
|
||||||
|
|
||||||
var vector = new Box2D.Common.Math.b2Vec2(
|
var vector = new Box2D.Common.Math.b2Vec2(
|
||||||
x * Settings.MAX_THROW_FORCE * limbDampingFactor,
|
x * Settings.MAX_THROW_FORCE * limbDampingFactor / this.options.weight,
|
||||||
-y * Settings.MAX_THROW_FORCE * 1.5 *limbDampingFactor // 1.5 is to throw higher then far
|
-y * Settings.MAX_THROW_FORCE * limbDampingFactor / this.options.weight
|
||||||
);
|
);
|
||||||
body.SetLinearVelocity(vector);
|
body.SetLinearVelocity(vector);
|
||||||
// body.SetAngularVelocity(Settings.MAX_THROW_ANGULAR_VELOCITY * x);
|
// body.SetAngularVelocity(Settings.MAX_THROW_ANGULAR_VELOCITY * x);
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,21 @@ function (Parent, Box2D, Settings) {
|
||||||
// FIXME: implement body flip if necessary
|
// 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() {
|
Skateboard.prototype.destroy = function() {
|
||||||
for (var i = 0; i < this.wheels.length; i++) {
|
for (var i = 0; i < this.wheels.length; i++) {
|
||||||
this.body.GetWorld().DestroyBody(this.wheels[i]);
|
this.body.GetWorld().DestroyBody(this.wheels[i]);
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ define([
|
||||||
var options = items[i];
|
var options = items[i];
|
||||||
var uid = "item-" + i;
|
var uid = "item-" + i;
|
||||||
var item = this.createItem(uid, options);
|
var item = this.createItem(uid, options);
|
||||||
this.gameObjects.animated.push(item);
|
this.gameObjects.animated.push(item); // FIXME: use NotificationCenter
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,6 @@ function (Doll, Settings, NotificationCenter, Exception, SpectatorDoll, RagDoll)
|
||||||
|
|
||||||
var ragDoll = new RagDoll(this.physicsEngine, "ragDoll-" + this.id + "-" + ragDollId, options);
|
var ragDoll = new RagDoll(this.physicsEngine, "ragDoll-" + this.id + "-" + ragDollId, options);
|
||||||
ragDoll.setVelocities(this.doll.getVelocities());
|
ragDoll.setVelocities(this.doll.getVelocities());
|
||||||
console.log(ragDoll.uid)
|
|
||||||
|
|
||||||
this.isSpawned = false;
|
this.isSpawned = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,11 +104,17 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
Channel.prototype.broadcastGameCommand = function (command, options) {
|
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) {
|
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;
|
return Channel;
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,11 @@ define([
|
||||||
"Lib/Vendor/Box2D",
|
"Lib/Vendor/Box2D",
|
||||||
"Game/Server/Player",
|
"Game/Server/Player",
|
||||||
"Game/Server/GameObjects/GameObject",
|
"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) {
|
function GameController (channel) {
|
||||||
|
|
||||||
|
|
@ -77,15 +78,13 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
// put it into
|
// put it into
|
||||||
self.gameObjects.animated.push(player);
|
self.gameObjects.animated.push(player);
|
||||||
|
|
||||||
var message = {
|
var options = {
|
||||||
spawnPlayer: {
|
id: player.id,
|
||||||
id: player.id,
|
x: spawnPoint.x,
|
||||||
x: spawnPoint.x,
|
y: spawnPoint.y
|
||||||
y: spawnPoint.y
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", message);
|
NotificationCenter.trigger("broadcastGameCommand", "spawnPlayer", options);
|
||||||
}, respawnTime * 1000);
|
}, respawnTime * 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -103,7 +102,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
var update = this.getWorldUpdateObject(false);
|
var update = this.getWorldUpdateObject(false);
|
||||||
|
|
||||||
if(Object.getOwnPropertyNames(update).length > 0) {
|
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);
|
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() {
|
GameController.prototype.getSpawnedPlayersAndTheirPositions = function() {
|
||||||
var spawnedPlayers = [];
|
var spawnedPlayers = [];
|
||||||
for(id in this.players) {
|
for(var id in this.players) {
|
||||||
var player = this.players[id];
|
var player = this.players[id];
|
||||||
if(player.isSpawned) {
|
if(player.isSpawned) {
|
||||||
|
|
||||||
|
|
@ -164,23 +163,21 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
return spawnedPlayers;
|
return spawnedPlayers;
|
||||||
};
|
};
|
||||||
|
|
||||||
GameController.prototype.getAdditionalObjects = function() {
|
GameController.prototype.getRuntimeItems = function() {
|
||||||
var objects = []
|
var objects = []
|
||||||
|
|
||||||
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
||||||
if(this.gameObjects.animated[i] instanceof RagDoll) {
|
if(this.gameObjects.animated[i] instanceof RagDoll) {
|
||||||
var object = this.gameObjects.animated[i];
|
var object = this.gameObjects.animated[i];
|
||||||
|
var options = object.options;
|
||||||
|
options.x = object.getPosition().x;
|
||||||
|
options.y = object.getPosition().y;
|
||||||
objects.push({
|
objects.push({
|
||||||
uid: objects.uid,
|
uid: object.uid,
|
||||||
options: object.options,
|
options: object.options
|
||||||
x: object.getPosition().x,
|
|
||||||
y: object.getPosition().y
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var object = {
|
|
||||||
// FIXME: finish it!
|
|
||||||
}
|
|
||||||
|
|
||||||
return objects;
|
return objects;
|
||||||
};
|
};
|
||||||
|
|
@ -192,6 +189,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
var options = {
|
var options = {
|
||||||
spawnedPlayers: this.getSpawnedPlayersAndTheirPositions(),
|
spawnedPlayers: this.getSpawnedPlayersAndTheirPositions(),
|
||||||
worldUpdate: this.getWorldUpdateObject(true),
|
worldUpdate: this.getWorldUpdateObject(true),
|
||||||
|
runtimeItems: this.getRuntimeItems(),
|
||||||
userId: userId
|
userId: userId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -200,7 +198,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
|
|
||||||
GameController.prototype.onResetLevel = function(userId) {
|
GameController.prototype.onResetLevel = function(userId) {
|
||||||
Parent.prototype.onResetLevel.call(this);
|
Parent.prototype.onResetLevel.call(this);
|
||||||
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", {resetLevel:true});
|
NotificationCenter.trigger("broadcastGameCommand", "resetLevel", true);
|
||||||
for (var key in this.players) {
|
for (var key in this.players) {
|
||||||
this.spawnPlayer(this.players[key]);
|
this.spawnPlayer(this.players[key]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
|
||||||
|
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
this.player = null;
|
this.player = null;
|
||||||
|
this.isReady = false;
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
NotificationCenter.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead
|
NotificationCenter.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead
|
||||||
|
|
@ -51,6 +52,7 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
|
||||||
if(command.hasOwnProperty("resetLevel")) {
|
if(command.hasOwnProperty("resetLevel")) {
|
||||||
NotificationCenter.trigger("user/resetLevel", this.id);
|
NotificationCenter.trigger("user/resetLevel", this.id);
|
||||||
} else if(command.hasOwnProperty("clientReady")) {
|
} else if(command.hasOwnProperty("clientReady")) {
|
||||||
|
this.isReady = true;
|
||||||
NotificationCenter.trigger("user/clientReady", this.id);
|
NotificationCenter.trigger("user/clientReady", this.id);
|
||||||
} else {
|
} else {
|
||||||
this.player.playerController.applyCommand(command);
|
this.player.playerController.applyCommand(command);
|
||||||
|
|
@ -69,8 +71,10 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) {
|
||||||
};
|
};
|
||||||
|
|
||||||
User.prototype.sendGameCommand = function(command, options) {
|
User.prototype.sendGameCommand = function(command, options) {
|
||||||
var data = ProtocolHelper.encodeCommand(command, options);
|
if(this.isReady) {
|
||||||
this.sendControlCommand("gameCommand", data);
|
var data = ProtocolHelper.encodeCommand(command, options);
|
||||||
|
this.sendControlCommand("gameCommand", data);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue