ground work for different users on server and players on client

This commit is contained in:
Jeena Paradies 2012-07-13 21:10:08 +02:00
parent e37d1eeb2e
commit 2cb401bbd3
9 changed files with 122 additions and 54 deletions

View file

@ -1,14 +1,19 @@
define(["Chuck/Processors/ClientProcessor"], function(ClientProcessor) { define(["Chuck/Processors/ClientProcessor"], function(ClientProcessor) {
function ClientGame(networker){ function ClientGame(networker, id) {
this.networker = networker; this.networker = networker;
this.processor = new ClientProcessor(); this.processor = new ClientProcessor();
this.processor.spawnMeWithId(id);
} }
ClientGame.prototype.loadLevel = function(path) { ClientGame.prototype.loadLevel = function(path) {
this.processor.loadLevel(path); this.processor.loadLevel(path);
} }
ClientGame.prototype.userJoined = function(userId) {
this.processor.spawnNewPlayerWithId(userId);
};
ClientGame.prototype.processGameCommand = function(command, options){ ClientGame.prototype.processGameCommand = function(command, options){
console.log('(not implemented) processGameCommand:', command, options); console.log('(not implemented) processGameCommand:', command, options);
} }

View file

@ -1,6 +1,7 @@
define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
function Doll (physicsEngine){ function Doll (physicsEngine, id){
this.id = id;
this._physicsEngine = physicsEngine; this._physicsEngine = physicsEngine;
this._body; this._body;
this._legs; this._legs;
@ -30,14 +31,14 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO)); headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO));
fixtureDef.shape = headShape; fixtureDef.shape = headShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'myHead'; fixtureDef.userData = 'myHead' + this.id;
this._body.CreateFixture(fixtureDef); this._body.CreateFixture(fixtureDef);
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape(); var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO)); bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO));
fixtureDef.shape = bodyShape; fixtureDef.shape = bodyShape;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'myBody'; fixtureDef.userData = 'myBody' + this.id;
this._body.CreateFixture(fixtureDef); this._body.CreateFixture(fixtureDef);
var legsShape = new Box2D.Collision.Shapes.b2CircleShape(); var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
@ -46,7 +47,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
fixtureDef.shape = legsShape; fixtureDef.shape = legsShape;
fixtureDef.friction = Settings.PLAYER_FRICTION; fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'myLegs'; fixtureDef.userData = 'myLegs' + this.id;
this._legs = this._body.CreateFixture(fixtureDef); this._legs = this._body.CreateFixture(fixtureDef);
var feetShape = new Box2D.Collision.Shapes.b2CircleShape(); var feetShape = new Box2D.Collision.Shapes.b2CircleShape();
@ -54,7 +55,7 @@ define(["Vendor/Box2D", "Chuck/Settings"], function(Box2D, Settings){
feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO)); feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO));
fixtureDef.shape = feetShape; fixtureDef.shape = feetShape;
fixtureDef.isSensor = true; fixtureDef.isSensor = true;
fixtureDef.userData = 'myFeet'; fixtureDef.userData = 'myFeet' + this.id;
this._body.CreateFixture(fixtureDef); this._body.CreateFixture(fixtureDef);
this._body.SetActive(false); this._body.SetActive(false);

View file

@ -1,7 +1,8 @@
define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){ define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
function Player (physicsEngine, repository) { function Player (physicsEngine, id, repository) {
this._physicsEngine = physicsEngine; this._physicsEngine = physicsEngine;
this.id = id;
this._repository = repository; this._repository = repository;
this._standing = false; this._standing = false;
this._doll; this._doll;
@ -10,11 +11,11 @@ define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
this._lookDirection = 1; this._lookDirection = 1;
this._moveDirection = 0; this._moveDirection = 0;
this.init(); this.init(id);
} }
Player.prototype.init = function() { Player.prototype.init = function(id) {
this._doll = new Doll(this._physicsEngine); this._doll = new Doll(this._physicsEngine, id);
//this._mc = EmbedHandler.load(EmbedHandler.CHUCK); //this._mc = EmbedHandler.load(EmbedHandler.CHUCK);
//this._mc.stop(); //this._mc.stop();
//var mclp = new MovieClipLabelParser(); //var mclp = new MovieClipLabelParser();

View file

@ -12,22 +12,18 @@ var requires = [
define(requires, define(requires,
function(ViewController, PhysicsEngine, Player, InputControlUnit, Settings, Box2D, Level, requestAnimFrame) { function(ViewController, PhysicsEngine, Player, InputControlUnit, Settings, Box2D, Level, requestAnimFrame) {
function ClientGame () { function ClientProcessor () {
this.init(); this.init();
}; };
ClientGame.prototype.init = function() { ClientProcessor.prototype.init = function() {
this.viewController = new ViewController(); this.viewController = new ViewController();
this.physicsEngine = new PhysicsEngine(); this.physicsEngine = new PhysicsEngine();
this.me = new Player(this.physicsEngine, null);
this.me.spawn(100, 0);
this.inputControlUnit = new InputControlUnit(this.me);
this.physicsEngine.setCollisionDetector(this.me);
this.update(); this.update();
} }
ClientGame.prototype.loadLevel = function(path) { ClientProcessor.prototype.loadLevel = function(path) {
if (this.level) { if (this.level) {
this.level.unload(); this.level.unload();
} }
@ -36,27 +32,41 @@ define(requires,
this.level.loadLevelInToEngine(); this.level.loadLevelInToEngine();
} }
ClientGame.prototype.getPhysicsEngine = function() { ClientProcessor.prototype.getPhysicsEngine = function() {
return this.physicsEngine; return this.physicsEngine;
} }
ClientGame.prototype.getMe = function() { ClientProcessor.prototype.getMe = function() {
return this.me; return this.me;
} }
ClientGame.prototype.update = function() { ClientProcessor.prototype.update = function() {
requestAnimFrame(this.update.bind(this)); requestAnimFrame(this.update.bind(this));
this.physicsEngine.update(); this.physicsEngine.update();
this.viewController.update(); this.viewController.update();
if(this.me) {
this.inputControlUnit.update(); this.inputControlUnit.update();
this.me.update(); this.me.update();
} }
}
ClientGame.prototype.destruct = function() { ClientProcessor.prototype.destruct = function() {
} }
return ClientGame; ClientProcessor.prototype.spawnNewPlayerWithId = function(id) {
var player = new Player(this.physicsEngine, id, null);
player.spawn(100, 0);
this.physicsEngine.setCollisionDetector(player);
return player;
}
ClientProcessor.prototype.spawnMeWithId = function(id) {
this.me = this.spawnNewPlayerWithId(id);
this.inputControlUnit = new InputControlUnit(this.me);
}
return ClientProcessor;
}); });

View file

@ -8,39 +8,77 @@ var requires = [
define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame){ define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame){
function ServerGame () { function ServerProcessor (ServerProcessor) {
this.ServerProcessor = ServerProcessor;
this.players = {};
this.init(); this.init();
}; };
ServerGame.prototype.init = function() { ServerProcessor.prototype.init = function() {
this.physicsEngine = new PhysicsEngine(); this.physicsEngine = new PhysicsEngine();
this.update(); this.update();
} }
ServerGame.prototype.loadLevel = function(path) { ServerProcessor.prototype.loadLevel = function(path) {
if (this.level) { if (this.level) {
this.level.unload(); this.level.unload();
} }
this.level = new Level(path, this.physicsEngine); this.level = new Level(path, this.physicsEngine);
this.level.loadLevelInToEngine(); this.level.loadLevelInToEngine();
console.log(this.level);
} }
ServerGame.prototype.getPhysicsEngine = function() { ServerProcessor.prototype.getPhysicsEngine = function() {
return this.physicsEngine; return this.physicsEngine;
} }
ServerGame.prototype.update = function() { ServerProcessor.prototype.update = function() {
requestAnimFrame(this.update.bind(this)); requestAnimFrame(this.update.bind(this));
this.physicsEngine.update(); this.physicsEngine.update();
for(var id in this.players) {
this.players[id].update();
}
} }
ServerGame.prototype.destruct = function() { ServerProcessor.prototype.destruct = function() {
} }
return ServerGame; ServerProcessor.prototype.createPlayerWithId = function(id) {
var player = new Player(this.physicsEngine, id, null);
this.players[id] = player;
player.spawn(100, 0);
this.physicsEngine.setCollisionDetector(player);
}
ServerProcessor.prototype.updateWorld = function() {
var update = {};
var isUpdateNeeded = false;
var body = world.GetBodyList();
do {
var userData = body.GetUserData();
if(userData && userData.bodyId && body.IsAwake()){
update[userData.bodyId] = {
p: body.GetPosition(),
a: body.GetAngle(),
lv: body.GetLinearVelocity(),
av: body.GetAngularVelocity()
};
isUpdateNeeded = true;
}
} while (body = body.GetNext());
if(isUpdateNeeded) {
this.ServerProcessor.updateClientsWorld(update);
}
}
return ServerProcessor;
}); });

View file

@ -1,12 +1,12 @@
define(["Chuck/Processors/ServerProcessor"], function(ServerProcessor) { define(["Chuck/Processors/ServerProcessor"], function(ServerProcessor) {
function ServerGame(chanel) { function ServerGame(channel) {
this.chanel = chanel; this.channel = channel;
this.processor = new ServerProcessor(); this.serverProcessor = new ServerProcessor(this);
} }
ServerGame.prototype.loadLevel = function(path) { ServerGame.prototype.loadLevel = function(path) {
this.processor.loadLevel(path); this.serverProcessor.loadLevel(path);
} }
ServerGame.prototype.processGameCommand = function(command, options) { ServerGame.prototype.processGameCommand = function(command, options) {
@ -14,7 +14,15 @@ define(["Chuck/Processors/ServerProcessor"], function(ServerProcessor) {
} }
ServerGame.prototype.destruct = function() { ServerGame.prototype.destruct = function() {
this.processor.destruct(); this.serverProcessor.destruct();
}
ServerGame.prototype.createPlayerForUser = function(user) {
this.serverProcessor.createPlayerWithId(user.id);
}
ServerGame.prototype.updateClientsWorld = function(update_world) {
this.channel.sendCommandToAllUsers('gameCommand', update_world);
} }
return ServerGame; return ServerGame;

View file

@ -49,14 +49,14 @@ define(["Protocol/Helper", "Chuck/ClientGame"], function(ProtocolHelper, ClientG
this.socketLink.send(message); this.socketLink.send(message);
} }
Networker.prototype.onJoinSuccess = function(channelName) { Networker.prototype.onJoinSuccess = function(options) {
this.clientGame = new ClientGame(this); this.clientGame = new ClientGame(this, options.id);
this.clientGame.loadLevel("default.json") this.clientGame.loadLevel("default.json")
console.log("Joined " + channelName); console.log("Joined " + options.channelName);
} }
Networker.prototype.onUserJoined = function(userId) { Networker.prototype.onUserJoined = function(userId) {
//this.clientGame.userJoined(userId); this.clientGame.userJoined(userId);
console.log("User " + userId + " joined"); console.log("User " + userId + " joined");
}; };

View file

@ -13,8 +13,10 @@ define(["Chuck/ServerGame"], function(ServerGame) {
Channel.prototype.addUser = function(user){ Channel.prototype.addUser = function(user){
this.users[user.id] = user; this.users[user.id] = user;
user.sendCommand('joinSuccess', this.name); user.sendCommand('joinSuccess', {chanelName: this.name, id: user.id});
this.sendCommandToAllUsersBut('userJoined', user.id, user); this.sendCommandToAllUsersExcept('userJoined', user.id, user);
this.serverGame.createPlayerForUser(user)
} }
Channel.prototype.releaseUser = function(user){ Channel.prototype.releaseUser = function(user){
@ -27,14 +29,18 @@ define(["Chuck/ServerGame"], function(ServerGame) {
} }
} }
Channel.prototype.sendCommandToAllUsersBut = function(command, options, user) { Channel.prototype.sendCommandToAllUsersExcept = function(command, options, except_user) {
for(var id in this.users) { for(var id in this.users) {
if (id != user.id) { if (id != except_user.id) {
this.users[id].sendCommand(command, options); this.users[id].sendCommand(command, options);
} }
} }
} }
Channel.prototype.processGameCommandFromUser = function(command, user) {
console.log("not implemented processGameCommandFromUser: " + command + user);
}
return Channel; return Channel;
}); });

View file

@ -25,7 +25,6 @@ define(["Protocol/Helper"], function(ProtocolHelper) {
User.prototype.setChannel = function(channel){ User.prototype.setChannel = function(channel){
this.channel = channel; this.channel = channel;
this.sendCommand('joinSuccess', channel.name);
} }
User.prototype.sendCommand = function(command, options) { User.prototype.sendCommand = function(command, options) {
@ -56,7 +55,7 @@ define(["Protocol/Helper"], function(ProtocolHelper) {
break; break;
case 'gameCommand': case 'gameCommand':
//this.channel.game.processGameCommand(options); this.channel.processGameCommandFromUser(options, this);
break; break;
default: default: