mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
Added spawning gameCommand logic
This commit is contained in:
parent
371592f167
commit
fb5de522e5
8 changed files with 84 additions and 59 deletions
|
|
@ -43,17 +43,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController.prototype.meJoined = function (user) {
|
|
||||||
this.me = this.userJoined(user);
|
|
||||||
this.keyboardController = new KeyboardController(this.me, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
GameController.prototype.userJoined = function (user) {
|
|
||||||
var player = Parent.prototype.userJoined.call(this, user);
|
|
||||||
//player.spawn(50, 50);
|
|
||||||
return player;
|
|
||||||
}
|
|
||||||
|
|
||||||
GameController.prototype.onWorldUpdate = function (updateData) {
|
GameController.prototype.onWorldUpdate = function (updateData) {
|
||||||
|
|
||||||
var body = this.physicsEngine.world.GetBodyList();
|
var body = this.physicsEngine.world.GetBodyList();
|
||||||
|
|
@ -71,8 +60,24 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController.prototype.onPlayerSpawn = function(user) {
|
|
||||||
|
GameController.prototype.onJoinMe = function (playerId) {
|
||||||
|
//this.onSpawnPlayer(options);
|
||||||
|
this.me = this.players[playerId];
|
||||||
|
this.keyboardController = new KeyboardController(this.me, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
GameController.prototype.onSpawnPlayer = function(options) {
|
||||||
|
var playerId = options.id,
|
||||||
|
x = options.x,
|
||||||
|
y = options.y;
|
||||||
|
|
||||||
|
console.log(this.players, options);
|
||||||
|
|
||||||
|
var player = this.players[playerId];
|
||||||
|
player.spawn(x, y);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return GameController;
|
return GameController;
|
||||||
|
|
|
||||||
|
|
@ -46,14 +46,12 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
|
||||||
this.gameController = new GameController();
|
this.gameController = new GameController();
|
||||||
this.gameController.loadLevel("default.json");
|
this.gameController.loadLevel("default.json");
|
||||||
|
|
||||||
var user = new User(options.userId);
|
this.onUserJoined(options.userId);
|
||||||
this.gameController.meJoined(user);
|
this.gameController.onJoinMe(options.userId);
|
||||||
|
|
||||||
console.log("Joined Success", options);
|
|
||||||
|
|
||||||
if (options.others) {
|
if (options.others) {
|
||||||
for(var i = 0; i < options.others.length; i++) {
|
for(var i = 0; i < options.others.length; i++) {
|
||||||
this.users[userId] = new User(options.others[i]);
|
this.onUserJoined(options.others[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,10 +71,14 @@ function (ProtocolHelper, GameController, User, NotificationCenter) {
|
||||||
// Commands from server
|
// Commands from server
|
||||||
|
|
||||||
Networker.prototype.onUserJoined = function (userId) {
|
Networker.prototype.onUserJoined = function (userId) {
|
||||||
this.users[userId] = new User(userId);
|
var user = new User(userId);
|
||||||
|
this.users[userId] = user;
|
||||||
|
this.gameController.userJoined(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
Networker.prototype.onUserLeft = function (userId) {
|
Networker.prototype.onUserLeft = function (userId) {
|
||||||
|
var user = this.users[userId];
|
||||||
|
this.gameController.userLeft(user);
|
||||||
delete this.users[userId];
|
delete this.users[userId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,9 +36,7 @@ function (Engine, Level, Player) {
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController.prototype.userJoined = function (user) {
|
GameController.prototype.userJoined = function (user) {
|
||||||
var player = new Player(user.id, this.physicsEngine);
|
this.players[user.id] = this.createPlayer(user);
|
||||||
this.players[user.id] = player;
|
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController.prototype.userLeft = function (user) {
|
GameController.prototype.userLeft = function (user) {
|
||||||
|
|
@ -47,6 +45,10 @@ function (Engine, Level, Player) {
|
||||||
delete this.players[user.id];
|
delete this.players[user.id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GameController.prototype.createPlayer = function(user) {
|
||||||
|
return new Player(user.id, this.physicsEngine);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return GameController;
|
return GameController;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,31 +5,23 @@ define([
|
||||||
|
|
||||||
function (Doll, Settings) {
|
function (Doll, Settings) {
|
||||||
|
|
||||||
function Player (id, physicsEngine, repository) {
|
function Player (id, physicsEngine) {
|
||||||
this.physicsEngine = physicsEngine;
|
this.physicsEngine = physicsEngine;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.repository = repository;
|
|
||||||
this.standing = false;
|
this.standing = false;
|
||||||
this.doll;
|
this.doll;
|
||||||
this.mc;
|
this.mc;
|
||||||
this.currentAnimationState = 'stand';
|
this.currentAnimationState = 'stand';
|
||||||
this.lookDirection = 1;
|
this.lookDirection = 1;
|
||||||
this.moveDirection = 0;
|
this.moveDirection = 0;
|
||||||
|
this.isSpawned = false;
|
||||||
|
|
||||||
this.init(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
Player.prototype.init = function (id) {
|
|
||||||
this.doll = new Doll(this.physicsEngine, id);
|
|
||||||
//this.mc = EmbedHandler.load(EmbedHandler.CHUCK);
|
|
||||||
//this.mc.stop();
|
|
||||||
//var mclp = new MovieClipLabelParser();
|
|
||||||
//mclp.parse(this.mc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Player.prototype.spawn = function (x, y) {
|
Player.prototype.spawn = function (x, y) {
|
||||||
//this.repository.createModel(this.mc, this.doll.getBody());
|
this.doll = new Doll(this.physicsEngine, this.id);
|
||||||
this.doll.spawn(x, y);
|
this.doll.spawn(x, y);
|
||||||
|
this.isSpawned = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player.prototype.getDoll = function () {
|
Player.prototype.getDoll = function () {
|
||||||
|
|
@ -113,8 +105,6 @@ function (Doll, Settings) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//this.mc.gotoAndPlay(type);
|
|
||||||
|
|
||||||
this.currentAnimationState = type;
|
this.currentAnimationState = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,10 +38,10 @@
|
||||||
|
|
||||||
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 othersJoined = Object.keys(this.users);
|
||||||
|
|
||||||
this.users[user.id] = user;
|
this.users[user.id] = user;
|
||||||
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name, others: others});
|
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name, others: othersJoined});
|
||||||
NotificationCenter.trigger('user/joined', user);
|
NotificationCenter.trigger('user/joined', user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,15 @@ define([
|
||||||
"Game/Config/Settings",
|
"Game/Config/Settings",
|
||||||
"Game/Server/Control/InputController",
|
"Game/Server/Control/InputController",
|
||||||
"Lib/Utilities/RequestAnimFrame",
|
"Lib/Utilities/RequestAnimFrame",
|
||||||
"Game/Core/NotificationCenter"
|
"Game/Core/NotificationCenter",
|
||||||
|
"Game/Server/Player"
|
||||||
],
|
],
|
||||||
|
|
||||||
function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) {
|
function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter, Player) {
|
||||||
|
|
||||||
function GameController (channel) {
|
function GameController (channel) {
|
||||||
Parent.call(this, new PhysicsEngine());
|
Parent.call(this, new PhysicsEngine());
|
||||||
|
|
||||||
this.inputControllers = {};
|
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
|
|
||||||
this.update();
|
this.update();
|
||||||
|
|
@ -38,26 +38,31 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No
|
||||||
|
|
||||||
GameController.prototype.userJoined = function (user) {
|
GameController.prototype.userJoined = function (user) {
|
||||||
Parent.prototype.userJoined.call(this, user);
|
Parent.prototype.userJoined.call(this, user);
|
||||||
|
var player = this.players[user.id];
|
||||||
var id = user.id;
|
|
||||||
var player = this.players[id];
|
|
||||||
user.setPlayer(player);
|
user.setPlayer(player);
|
||||||
player.spawn(50, 50);
|
this.spawnPlayer(player);
|
||||||
this.inputControllers[id] = new InputController(player);
|
|
||||||
player.inputController = this.inputControllers[id]; // FIXME move this to Server/Player
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController.prototype.userLeft = function (user) {
|
GameController.prototype.spawnPlayer = function(player) {
|
||||||
Parent.prototype.userLeft.call(this, user);
|
var x = 150,
|
||||||
delete this.inputControllers[user.id];
|
y = 50;
|
||||||
}
|
player.spawn(x, y);
|
||||||
|
|
||||||
GameController.prototype.progressGameCommandFromUser = function (command, options, user) {
|
var message = {
|
||||||
var inputController = this.inputControllers[user.id];
|
spawnPlayer: {
|
||||||
if (typeof inputController[command] == 'function') {
|
id: player.id,
|
||||||
inputController[command](options);
|
x: x,
|
||||||
}
|
y: y
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message);
|
||||||
|
};
|
||||||
|
|
||||||
|
GameController.prototype.createPlayer = function(user) {
|
||||||
|
var player = new Player(user.id, this.physicsEngine);
|
||||||
|
player.setInputController(new InputController(player))
|
||||||
|
return player;
|
||||||
|
};
|
||||||
|
|
||||||
GameController.prototype.updateWorld = function () {
|
GameController.prototype.updateWorld = function () {
|
||||||
|
|
||||||
|
|
|
||||||
21
app/Game/Server/Player.js
Normal file
21
app/Game/Server/Player.js
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
define([
|
||||||
|
"Game/Core/Player"
|
||||||
|
],
|
||||||
|
|
||||||
|
function(Parent) {
|
||||||
|
|
||||||
|
function Player(id, physicsEngine) {
|
||||||
|
Parent.call(this, id, physicsEngine);
|
||||||
|
|
||||||
|
this.inputController = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Player.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
|
Player.prototype.setInputController = function(inputController) {
|
||||||
|
this.inputController = inputController;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Player;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
@ -13,7 +13,7 @@ function(Parent, NotificationCenter, ProtocolHelper) {
|
||||||
this.player = null;
|
this.player = null;
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
NotificationCenter.on('user/joined', function(user) {
|
NotificationCenter.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead
|
||||||
if(user.id != self.id) {
|
if(user.id != self.id) {
|
||||||
self.sendControlCommand("userJoined", user.id);
|
self.sendControlCommand("userJoined", user.id);
|
||||||
}
|
}
|
||||||
|
|
@ -23,7 +23,7 @@ function(Parent, NotificationCenter, ProtocolHelper) {
|
||||||
self.sendControlCommand("joinSuccess", options);
|
self.sendControlCommand("joinSuccess", options);
|
||||||
});
|
});
|
||||||
|
|
||||||
NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) { // FIXME: right now only game commands?
|
NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) {
|
||||||
ProtocolHelper.applyCommand(message.data, self);
|
ProtocolHelper.applyCommand(message.data, self);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue