mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
refactored Server and Lobby
This commit is contained in:
parent
42475c9b38
commit
d83376d5c7
26 changed files with 29 additions and 29 deletions
122
app/Game/Channel/Channel.js
Executable file
122
app/Game/Channel/Channel.js
Executable file
|
|
@ -0,0 +1,122 @@
|
|||
define([
|
||||
"Game/Channel/GameController",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Game/Channel/User",
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/Options",
|
||||
"Game/Config/Settings"
|
||||
],
|
||||
|
||||
function (GameController, Nc, User, ProtocolHelper, Options, Settings) {
|
||||
|
||||
function Channel (pipeToServer, name, options) {
|
||||
|
||||
var self = this;
|
||||
|
||||
this.options = options = Options.merge(options, {
|
||||
levelUids: Settings.DEFAULT_LEVELS
|
||||
});
|
||||
|
||||
this.name = name;
|
||||
this.users = {};
|
||||
|
||||
this.pipeToServer = pipeToServer;
|
||||
|
||||
this.gameController = new GameController(this);
|
||||
|
||||
Nc.on('channel/controlCommand', function (message) {
|
||||
ProtocolHelper.applyCommand(message.data, self);
|
||||
});
|
||||
|
||||
Nc.on('broadcastControlCommand', this.broadcastControlCommand, this);
|
||||
Nc.on('broadcastControlCommandExcept', this.broadcastControlCommandExcept, this);
|
||||
|
||||
Nc.on('broadcastGameCommand', this.broadcastGameCommand, this);
|
||||
Nc.on('broadcastGameCommandExcept', this.broadcastGameCommandExcept, this);
|
||||
|
||||
console.checkpoint('channel ' + name + ' created');
|
||||
}
|
||||
|
||||
Channel.validateName = function (name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Channel command callbacks
|
||||
|
||||
Channel.prototype.onAddUser = function (userId) {
|
||||
var self = this;
|
||||
|
||||
if(!this.gameController.level || !this.gameController.level.isLoaded) {
|
||||
var token = Nc.on("game/level/loaded", function() {
|
||||
self.sendJoinSuccess(userId);
|
||||
Nc.off(token);
|
||||
});
|
||||
} else {
|
||||
self.sendJoinSuccess(userId);
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.sendJoinSuccess = function(userId) {
|
||||
var user = new User(userId, this);
|
||||
var joinedUsers = Object.keys(this.users);
|
||||
|
||||
var levelUid = null;
|
||||
if(this.gameController.level) {
|
||||
levelUid = this.gameController.level.uid;
|
||||
}
|
||||
|
||||
this.users[user.id] = user;
|
||||
|
||||
var options = {
|
||||
userId: user.id,
|
||||
channelName: this.name,
|
||||
joinedUsers: joinedUsers,
|
||||
levelUid: levelUid
|
||||
};
|
||||
|
||||
Nc.trigger('user/' + user.id + "/joinSuccess", options);
|
||||
Nc.trigger('user/joined', user);
|
||||
};
|
||||
|
||||
Channel.prototype.onReleaseUser = function (userId) {
|
||||
var user = this.users[userId];
|
||||
this.broadcastControlCommandExcept("userLeft", user.id, user);
|
||||
Nc.trigger('user/left', user);
|
||||
delete this.users[user.id];
|
||||
}
|
||||
|
||||
|
||||
// Sending commands
|
||||
|
||||
Channel.prototype.broadcastControlCommand = function (command, options) {
|
||||
for(var id in this.users) {
|
||||
this.users[id].sendControlCommand(command, options);
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.broadcastControlCommandExcept = function (command, options, exceptUser) {
|
||||
for(var id in this.users) {
|
||||
if (id != exceptUser.id) {
|
||||
this.users[id].sendControlCommand(command, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.broadcastGameCommand = function (command, options) {
|
||||
for(var id in this.users) {
|
||||
this.users[id].sendGameCommand(command, options);
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.broadcastGameCommandExcept = function (command, options, exceptUser) {
|
||||
for(var id in this.users) {
|
||||
if (id != exceptUser.id) {
|
||||
this.users[id].sendGameCommand(command, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Channel;
|
||||
|
||||
});
|
||||
15
app/Game/Channel/Collision/Detector.js
Executable file
15
app/Game/Channel/Collision/Detector.js
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
define([
|
||||
"Game/Core/Collision/Detector"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
|
||||
function Detector () {
|
||||
Parent.call(this);
|
||||
}
|
||||
|
||||
Detector.prototype = Object.create(Parent.prototype);
|
||||
|
||||
|
||||
return Detector;
|
||||
});
|
||||
44
app/Game/Channel/Control/PlayerController.js
Executable file
44
app/Game/Channel/Control/PlayerController.js
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
define([
|
||||
"Game/Core/Control/PlayerController",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Lib/Utilities/Protocol/Parser"
|
||||
],
|
||||
|
||||
function(Parent, Nc, Parser) {
|
||||
|
||||
function PlayerController(player) {
|
||||
|
||||
Parent.call(this, player);
|
||||
}
|
||||
|
||||
PlayerController.prototype = Object.create(Parent.prototype);
|
||||
|
||||
/*
|
||||
* retrieves move (and other) commands from client and executes them at the server
|
||||
*/
|
||||
PlayerController.prototype.applyCommand = function(options) {
|
||||
// FIXME: remove this function and use ProtocolHelper.applyCommand() instead
|
||||
// Don't forget to change the function names to on...
|
||||
var message;
|
||||
if (typeof options == "string") {
|
||||
message = Parser.decode(options);
|
||||
} else {
|
||||
message = options;
|
||||
}
|
||||
|
||||
for (var command in message) {
|
||||
this[command].call(this, message[command]);
|
||||
}
|
||||
};
|
||||
|
||||
PlayerController.prototype.handActionRequest = function(options) {
|
||||
if (options) this.player.handActionRequest(options.x, options.y);
|
||||
};
|
||||
|
||||
PlayerController.prototype.suicide = function() {
|
||||
this.player.suicide();
|
||||
};
|
||||
|
||||
return PlayerController;
|
||||
|
||||
});
|
||||
226
app/Game/Channel/GameController.js
Executable file
226
app/Game/Channel/GameController.js
Executable file
|
|
@ -0,0 +1,226 @@
|
|||
define([
|
||||
"Game/Core/GameController",
|
||||
"Game/Channel/Physics/Engine",
|
||||
"Game/Config/Settings",
|
||||
"Game/Channel/Control/PlayerController",
|
||||
"Lib/Utilities/RequestAnimFrame",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Lib/Vendor/Box2D",
|
||||
"Game/Channel/Player",
|
||||
"Game/Channel/GameObjects/GameObject",
|
||||
"Game/Channel/GameObjects/Doll",
|
||||
"Game/Channel/GameObjects/Items/RagDoll"
|
||||
],
|
||||
|
||||
function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, Nc, Box2D, Player, GameObject, Doll, RagDoll) {
|
||||
|
||||
function GameController (channel) {
|
||||
|
||||
this.channel = channel;
|
||||
|
||||
Parent.call(this);
|
||||
|
||||
Nc.on('user/joined', this.onUserJoined, this);
|
||||
Nc.on('user/left', this.onUserLeft, this); // FIXME: refactor this.userLeft -> this.onUserLeft, even in core and client
|
||||
Nc.on('user/resetLevel', this.onResetLevel, this);
|
||||
Nc.on('user/clientReady', this.onClientReady, this);
|
||||
Nc.on('player/killed', this.onPlayerKilled, this);
|
||||
|
||||
console.checkpoint('starting game controller for channel ' + channel.name);
|
||||
|
||||
var nextUid = this.getNextLevelUid();
|
||||
this.loadLevel(nextUid);
|
||||
}
|
||||
|
||||
GameController.prototype = Object.create(Parent.prototype);
|
||||
|
||||
GameController.prototype.update = function () {
|
||||
|
||||
Parent.prototype.update.call(this);
|
||||
|
||||
requestAnimFrame(this.update.bind(this));
|
||||
|
||||
this.physicsEngine.update();
|
||||
for(var id in this.players) {
|
||||
this.players[id].update();
|
||||
}
|
||||
}
|
||||
|
||||
GameController.prototype.onLevelLoaded = function() {
|
||||
Parent.prototype.onLevelLoaded.call(this);
|
||||
this.updateWorld();
|
||||
};
|
||||
|
||||
GameController.prototype.onUserJoined = function (user) {
|
||||
this.createPlayer(user);
|
||||
}
|
||||
|
||||
GameController.prototype.createPlayer = function(user) {
|
||||
var player = Parent.prototype.createPlayer.call(this, user);
|
||||
player.setPlayerController(new PlayerController(player))
|
||||
user.setPlayer(player);
|
||||
};
|
||||
|
||||
GameController.prototype.onPlayerKilled = function(player, respawnTime) {
|
||||
this.spawnPlayer(player, respawnTime);
|
||||
};
|
||||
|
||||
GameController.prototype.spawnPlayer = function(player, respawnTime) {
|
||||
var self = this;
|
||||
var spawnPoint = this.level.getRandomSpawnPoint();
|
||||
|
||||
respawnTime = typeof respawnTime == 'undefined'
|
||||
? Settings.RESPAWN_TIME
|
||||
: respawnTime;
|
||||
|
||||
setTimeout(function() {
|
||||
player.spawn(spawnPoint.x, spawnPoint.y);
|
||||
// put it into
|
||||
self.gameObjects.animated.push(player);
|
||||
|
||||
var options = {
|
||||
id: player.id,
|
||||
x: spawnPoint.x,
|
||||
y: spawnPoint.y
|
||||
};
|
||||
|
||||
Nc.trigger("broadcastGameCommand", "spawnPlayer", options);
|
||||
}, respawnTime * 1000);
|
||||
};
|
||||
|
||||
/*
|
||||
GameController.prototype.createPlayer = function(user) {
|
||||
var player = new Player(user.id, this.physicsEngine);
|
||||
player.setPlayerController(new PlayerController(player))
|
||||
|
||||
return player;
|
||||
};
|
||||
*/
|
||||
|
||||
GameController.prototype.updateWorld = function () {
|
||||
|
||||
var update = this.getWorldUpdateObject(false);
|
||||
|
||||
if(Object.getOwnPropertyNames(update).length > 0) {
|
||||
Nc.trigger("broadcastGameCommand", 'worldUpdate', update);
|
||||
}
|
||||
|
||||
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
|
||||
}
|
||||
|
||||
GameController.prototype.getWorldUpdateObject = function(getSleeping) {
|
||||
getSleeping = getSleeping || false;
|
||||
|
||||
var update = {};
|
||||
|
||||
var body = this.physicsEngine.world.GetBodyList();
|
||||
do {
|
||||
if((getSleeping || body.IsAwake()) && body.GetType() === Box2D.Dynamics.b2Body.b2_dynamicBody) {
|
||||
var userData = body.GetUserData();
|
||||
|
||||
if (userData instanceof GameObject) {
|
||||
var gameObject = userData;
|
||||
|
||||
update[gameObject.uid] = {
|
||||
p: body.GetPosition(),
|
||||
a: body.GetAngle(),
|
||||
lv: body.GetLinearVelocity(),
|
||||
av: body.GetAngularVelocity()
|
||||
};
|
||||
|
||||
if(gameObject instanceof Doll) {
|
||||
update[gameObject.uid].as = gameObject.getActionState();
|
||||
update[gameObject.uid].laxy = gameObject.lookAtXY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} while (body = body.GetNext());
|
||||
|
||||
return update;
|
||||
};
|
||||
|
||||
GameController.prototype.getSpawnedPlayersAndTheirPositions = function() {
|
||||
var spawnedPlayers = [];
|
||||
for(var id in this.players) {
|
||||
var player = this.players[id];
|
||||
if(player.isSpawned) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return spawnedPlayers;
|
||||
};
|
||||
|
||||
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: object.uid,
|
||||
options: object.options
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return objects;
|
||||
};
|
||||
|
||||
GameController.prototype.onClientReady = function(userId) {
|
||||
var player = this.players[userId];
|
||||
this.spawnPlayer(player, 0);
|
||||
|
||||
var options = {
|
||||
spawnedPlayers: this.getSpawnedPlayersAndTheirPositions(),
|
||||
worldUpdate: this.getWorldUpdateObject(true),
|
||||
runtimeItems: this.getRuntimeItems(),
|
||||
userId: userId
|
||||
}
|
||||
|
||||
Nc.trigger('user/' + userId + "/gameCommand", "clientReadyResponse", options);
|
||||
};
|
||||
|
||||
GameController.prototype.onResetLevel = function(userId) {
|
||||
Parent.prototype.onResetLevel.call(this);
|
||||
Nc.trigger("broadcastGameCommand", "resetLevel", true);
|
||||
for (var key in this.players) {
|
||||
this.spawnPlayer(this.players[key]);
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
});
|
||||
78
app/Game/Channel/GameObjects/Doll.js
Executable file
78
app/Game/Channel/GameObjects/Doll.js
Executable file
|
|
@ -0,0 +1,78 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/Doll",
|
||||
"Game/Channel/GameObjects/Item",
|
||||
"Lib/Vendor/Box2D",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent, Item, Box2D, Nc) {
|
||||
|
||||
function Doll(physicsEngine, uid, player) {
|
||||
Parent.call(this, physicsEngine, uid, player);
|
||||
}
|
||||
|
||||
Doll.prototype = Object.create(Parent.prototype);
|
||||
|
||||
Doll.prototype.findCloseItem = function(x, y) {
|
||||
|
||||
function findItem(array) {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
var item = array[i];
|
||||
if(item.isGrabbingAllowed(this.player)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (x < 0) { // looking left
|
||||
return findItem(this.reachableItems.left);
|
||||
} else {
|
||||
return findItem(this.reachableItems.right);
|
||||
}
|
||||
}
|
||||
|
||||
Doll.prototype.onImpact = function(isColliding, fixture) {
|
||||
var self = this;
|
||||
|
||||
Parent.prototype.onImpact.call(this, isColliding, fixture);
|
||||
|
||||
if(isColliding) {
|
||||
var otherBody = fixture.GetBody();
|
||||
if(otherBody) {
|
||||
var item = otherBody.GetUserData();
|
||||
if(item instanceof Item) {
|
||||
var itemVelocity = item.body.GetLinearVelocity();
|
||||
var itemMass = item.body.GetMass();
|
||||
|
||||
var ownVelocity = this.body.GetLinearVelocity();
|
||||
|
||||
var b2Math = Box2D.Common.Math.b2Math;
|
||||
|
||||
var absItemVelocity = b2Math.AbsV(itemVelocity)
|
||||
var max = 1;
|
||||
|
||||
if(absItemVelocity.x > max || absItemVelocity.y > max) {
|
||||
if(item.lastMoved && item.lastMoved.player != this.player) {
|
||||
var damage = b2Math.SubtractVV(itemVelocity, ownVelocity);
|
||||
damage.Abs();
|
||||
damage.Multiply(itemMass);
|
||||
|
||||
var player = item.lastMoved.player;
|
||||
|
||||
var callback = function() {
|
||||
self.player.addDamage(damage.Length() * 2, player);
|
||||
}
|
||||
|
||||
Nc.trigger("engine/addToWorldQueue", callback)
|
||||
}
|
||||
}
|
||||
|
||||
item.setLastMovedBy(this.player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Doll;
|
||||
|
||||
});
|
||||
9
app/Game/Channel/GameObjects/GameObject.js
Executable file
9
app/Game/Channel/GameObjects/GameObject.js
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/GameObject"
|
||||
],
|
||||
|
||||
function(Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
87
app/Game/Channel/GameObjects/Item.js
Executable file
87
app/Game/Channel/GameObjects/Item.js
Executable file
|
|
@ -0,0 +1,87 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/Item"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
|
||||
function Item(physicsEngine, uid, options) {
|
||||
Parent.call(this, physicsEngine, uid, options);
|
||||
this.heldByPlayers = [];
|
||||
this.lastMoved = null;
|
||||
}
|
||||
|
||||
Item.prototype = Object.create(Parent.prototype);
|
||||
|
||||
|
||||
Item.prototype.setLastMovedBy = function(player) {
|
||||
|
||||
if(player) {
|
||||
this.lastMoved = {
|
||||
player: player,
|
||||
timestamp: new Date()
|
||||
}
|
||||
} else {
|
||||
this.lastMoved = null;
|
||||
}
|
||||
};
|
||||
|
||||
Item.prototype.isGrabbingAllowed = function(player) {
|
||||
return this.heldByPlayers.length == 0;
|
||||
};
|
||||
|
||||
Item.prototype.beingGrabbed = function(player) {
|
||||
Parent.prototype.beingGrabbed.call(this, player);
|
||||
|
||||
if(this.isGrabbingAllowed(player)) {
|
||||
this.heldByPlayers.push(player);
|
||||
this.setLastMovedBy(null);
|
||||
}
|
||||
};
|
||||
|
||||
Item.prototype.isReleasingAllowed = function(player) {
|
||||
return true;
|
||||
};
|
||||
|
||||
Item.prototype.beingReleased = function(player) {
|
||||
Parent.prototype.beingReleased.call(this, player);
|
||||
|
||||
if(this.isReleasingAllowed(player)) {
|
||||
var pos = this.heldByPlayers.indexOf(player);
|
||||
if(pos >= 0) {
|
||||
this.heldByPlayers.splice(pos, 1);
|
||||
this.setLastMovedBy(player);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Item.prototype.onCollisionChange = function(isColliding, fixture) {
|
||||
|
||||
if(isColliding) {
|
||||
var otherBody = fixture.GetBody();
|
||||
if(otherBody) {
|
||||
var otherItem = otherBody.GetUserData();
|
||||
if(otherItem instanceof Item) {
|
||||
if(!this.lastMoved && !otherItem.lastMoved) return;
|
||||
|
||||
if(this.lastMoved && otherItem.lastMoved) {
|
||||
if(this.lastMoved.timestamp > otherItem.lastMoved.timestamp) {
|
||||
this.setLastMovedBy(otherItem.lastMoved.player);
|
||||
} else {
|
||||
otherItem.setLastMovedBy(this.lastMoved.player);
|
||||
}
|
||||
} else {
|
||||
if(!this.lastMoved) {
|
||||
this.setLastMovedBy(otherItem.lastMoved.player);
|
||||
} else {
|
||||
otherItem.setLastMovedBy(this.lastMoved.player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return Item;
|
||||
|
||||
});
|
||||
47
app/Game/Channel/GameObjects/Items/RagDoll.js
Normal file
47
app/Game/Channel/GameObjects/Items/RagDoll.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/Items/RagDoll",
|
||||
"Game/Config/Settings",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent, Settings, Nc) {
|
||||
|
||||
function RagDoll(physicsEngine, uid, options) {
|
||||
this.scheduledForDestruction = false;
|
||||
this.destructionTimeout = null;
|
||||
|
||||
Parent.call(this, physicsEngine, uid, options);
|
||||
}
|
||||
|
||||
RagDoll.prototype = Object.create(Parent.prototype);
|
||||
|
||||
RagDoll.prototype.beingGrabbed = function(player) {
|
||||
Parent.prototype.beingGrabbed.call(this, player);
|
||||
if(this.scheduledForDestruction) {
|
||||
clearTimeout(this.destructionTimeout);
|
||||
}
|
||||
};
|
||||
|
||||
RagDoll.prototype.beingReleased = function(player) {
|
||||
Parent.prototype.beingReleased.call(this, player);
|
||||
if(this.scheduledForDestruction) {
|
||||
this.delayedDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
RagDoll.prototype.delayedDestroy = function() {
|
||||
this.scheduledForDestruction = true;
|
||||
this.destructionTimeout = setTimeout(this.destroy.bind(this), Settings.RAGDOLL_DESTRUCTION_TIME * 1000);
|
||||
};
|
||||
|
||||
RagDoll.prototype.destroy = function() {
|
||||
Nc.trigger("broadcastGameCommand", 'removeGameObject', {
|
||||
type: 'animated',
|
||||
uid: this.uid
|
||||
});
|
||||
Parent.prototype.destroy.call(this);
|
||||
};
|
||||
|
||||
return RagDoll;
|
||||
|
||||
});
|
||||
9
app/Game/Channel/GameObjects/Items/Skateboard.js
Executable file
9
app/Game/Channel/GameObjects/Items/Skateboard.js
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/Items/Skateboard"
|
||||
],
|
||||
|
||||
function(Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
9
app/Game/Channel/GameObjects/SpectatorDoll.js
Normal file
9
app/Game/Channel/GameObjects/SpectatorDoll.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/SpectatorDoll"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
9
app/Game/Channel/GameObjects/Tile.js
Executable file
9
app/Game/Channel/GameObjects/Tile.js
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/GameObjects/Tile"
|
||||
],
|
||||
|
||||
function(Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
25
app/Game/Channel/Loader/Level.js
Executable file
25
app/Game/Channel/Loader/Level.js
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
define([
|
||||
"Game/Core/Loader/Level",
|
||||
"Game/Config/Settings",
|
||||
"fs"
|
||||
],
|
||||
|
||||
function (Parent, Settings, fs) {
|
||||
|
||||
function Level (uid, engine, gameObjects) {
|
||||
Parent.call(this, uid, engine, gameObjects);
|
||||
}
|
||||
|
||||
Level.prototype = Object.create(Parent.prototype);
|
||||
|
||||
Level.prototype.loadLevelDataFromPath = function (path, callback) {
|
||||
// overwriting parent
|
||||
|
||||
fs.readFile(path, "utf8", function (err, data) {
|
||||
if (err) throw err;
|
||||
callback(JSON.parse(data));
|
||||
});
|
||||
}
|
||||
|
||||
return Level;
|
||||
});
|
||||
9
app/Game/Channel/Loader/TiledLevel.js
Normal file
9
app/Game/Channel/Loader/TiledLevel.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/Loader/TiledLevel"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
9
app/Game/Channel/Physics/Engine.js
Executable file
9
app/Game/Channel/Physics/Engine.js
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/Physics/Engine"
|
||||
],
|
||||
|
||||
function(Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
46
app/Game/Channel/PipeToServer.js
Executable file
46
app/Game/Channel/PipeToServer.js
Executable file
|
|
@ -0,0 +1,46 @@
|
|||
define([
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Game/Channel/Channel"
|
||||
],
|
||||
|
||||
function (Nc, Channel) {
|
||||
|
||||
function PipeToServer (process) {
|
||||
|
||||
var self = this;
|
||||
|
||||
this.channel = null;
|
||||
this.process = process;
|
||||
|
||||
Nc.on('process/message', this.send, this);
|
||||
|
||||
process.on('message', function (message, handle) {
|
||||
|
||||
if(message.data.hasOwnProperty('CREATE')) {
|
||||
self.channel = new Channel(this, message.data['CREATE']);
|
||||
} else if (message.data.hasOwnProperty('KILL')) {
|
||||
self.channel.destroy();
|
||||
process.exit(0);
|
||||
} else {
|
||||
self.onMessage(message);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
PipeToServer.prototype.send = function (recipient, data) {
|
||||
var message = {
|
||||
recipient: recipient,
|
||||
data: data
|
||||
}
|
||||
|
||||
this.process.send(message);
|
||||
};
|
||||
|
||||
PipeToServer.prototype.onMessage = function (message) {
|
||||
Nc.trigger(message.recipient + '/controlCommand', message);
|
||||
}
|
||||
|
||||
return PipeToServer;
|
||||
|
||||
});
|
||||
115
app/Game/Channel/Player.js
Executable file
115
app/Game/Channel/Player.js
Executable file
|
|
@ -0,0 +1,115 @@
|
|||
define([
|
||||
"Game/Core/Player",
|
||||
"Lib/Utilities/NotificationCenter"
|
||||
],
|
||||
|
||||
function (Parent, Nc) {
|
||||
|
||||
function Player(id, physicsEngine) {
|
||||
Parent.call(this, id, physicsEngine);
|
||||
}
|
||||
|
||||
Player.prototype = Object.create(Parent.prototype);
|
||||
|
||||
Player.prototype.handActionRequest = function(x, y) {
|
||||
if(!this.doll) return false;
|
||||
|
||||
var item = null;
|
||||
var isHolding = !!this.holdingItem;
|
||||
|
||||
if (isHolding) {
|
||||
item = this.holdingItem;
|
||||
} else {
|
||||
item = this.doll.findCloseItem(x, y);
|
||||
}
|
||||
|
||||
if(item) {
|
||||
this.handAction(x, y, isHolding, item);
|
||||
}
|
||||
}
|
||||
|
||||
Player.prototype.handAction = function(x, y, isHolding, item) {
|
||||
|
||||
var options = {
|
||||
playerId: this.id,
|
||||
itemUid: item.uid
|
||||
}
|
||||
|
||||
if (isHolding) {
|
||||
// throw
|
||||
if(item.isReleasingAllowed()) {
|
||||
this.throw(x, y, item);
|
||||
|
||||
options.action = "throw";
|
||||
options.x = x;
|
||||
options.y = y;
|
||||
Nc.trigger("broadcastGameCommand", "handActionResponse", options);
|
||||
}
|
||||
} else {
|
||||
// grab
|
||||
if(item.isGrabbingAllowed()) {
|
||||
this.grab(item);
|
||||
|
||||
options.action = "grab";
|
||||
Nc.trigger("broadcastGameCommand", "handActionResponse", options);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Player.prototype.suicide = function() {
|
||||
this.addDamage(100, this);
|
||||
};
|
||||
|
||||
Player.prototype.addDamage = function(damage, enemy) {
|
||||
this.stats.health -= damage;
|
||||
|
||||
if(this.stats.health < 0) this.stats.health = 0;
|
||||
|
||||
if(this.stats.health <= 0) {
|
||||
if(enemy != this) enemy.score();
|
||||
this.kill(enemy);
|
||||
} else {
|
||||
this.broadcastStats();
|
||||
}
|
||||
};
|
||||
|
||||
Player.prototype.spawn = function(x, y) {
|
||||
Parent.prototype.spawn.call(this, x, y);
|
||||
this.stats.health = 100;
|
||||
this.broadcastStats();
|
||||
};
|
||||
|
||||
Player.prototype.kill = function(killedByPlayer) {
|
||||
this.stats.deaths++;
|
||||
var ragDollId = this.stats.deaths;
|
||||
Parent.prototype.kill.call(this, killedByPlayer, ragDollId);
|
||||
|
||||
this.broadcastStats();
|
||||
Nc.trigger("broadcastGameCommand", "playerKill", {
|
||||
playerId: this.id,
|
||||
killedByPlayerId: killedByPlayer.id,
|
||||
ragDollId: ragDollId
|
||||
});
|
||||
|
||||
if(this.ragDoll) {
|
||||
this.ragDoll.delayedDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
Player.prototype.score = function() {
|
||||
this.stats.score++;
|
||||
this.broadcastStats();
|
||||
};
|
||||
|
||||
Player.prototype.broadcastStats = function() {
|
||||
Nc.trigger("broadcastGameCommand", "updateStats", {
|
||||
playerId: this.id,
|
||||
stats: this.stats
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
return Player;
|
||||
|
||||
});
|
||||
83
app/Game/Channel/User.js
Executable file
83
app/Game/Channel/User.js
Executable file
|
|
@ -0,0 +1,83 @@
|
|||
define([
|
||||
"Game/Core/User",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/Protocol/Parser",
|
||||
],
|
||||
|
||||
function(Parent, Nc, ProtocolHelper, ProtocolParser) {
|
||||
|
||||
function User(id, channel) {
|
||||
Parent.call(this, id);
|
||||
|
||||
this.channel = channel;
|
||||
this.player = null;
|
||||
this.isReady = false;
|
||||
var self = this;
|
||||
|
||||
Nc.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead
|
||||
if(user.id != self.id) {
|
||||
self.sendControlCommand("userJoined", user.id);
|
||||
}
|
||||
});
|
||||
|
||||
Nc.on('user/' + this.id + "/joinSuccess", function(options) {
|
||||
self.sendControlCommand("joinSuccess", options);
|
||||
});
|
||||
|
||||
Nc.on('user/' + this.id + "/controlCommand", function(message) {
|
||||
ProtocolHelper.applyCommand(message.data, self);
|
||||
});
|
||||
|
||||
Nc.on('user/' + this.id + "/gameCommand", function(command, options) {
|
||||
self.sendGameCommand(command, options);
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype = Object.create(Parent.prototype);
|
||||
|
||||
User.prototype.setPlayer = function(player) {
|
||||
this.player = player;
|
||||
};
|
||||
|
||||
|
||||
// User command callbacks
|
||||
|
||||
User.prototype.onGameCommand = function(command) {
|
||||
|
||||
if (typeof command == "string") {
|
||||
command = ProtocolParser.decode(command);
|
||||
} // FIXME: move this to Protocol helper as a function
|
||||
|
||||
if(command.hasOwnProperty("resetLevel")) {
|
||||
Nc.trigger("user/resetLevel", this.id);
|
||||
} else if(command.hasOwnProperty("clientReady")) {
|
||||
this.isReady = true;
|
||||
Nc.trigger("user/clientReady", this.id);
|
||||
} else {
|
||||
this.player.playerController.applyCommand(command);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
// Sending commands
|
||||
|
||||
User.prototype.sendControlCommand = function(command, options) {
|
||||
var recipient = "user/" + this.id;
|
||||
var data = ProtocolHelper.encodeCommand(command, options);
|
||||
|
||||
Nc.trigger("process/message", recipient, data);
|
||||
};
|
||||
|
||||
User.prototype.sendGameCommand = function(command, options) {
|
||||
if(this.isReady) {
|
||||
var data = ProtocolHelper.encodeCommand(command, options);
|
||||
this.sendControlCommand("gameCommand", data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return User;
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue