Implemented local positioning as master - included punkbuster instead of webrtc for now. #49

This commit is contained in:
logsol 2014-05-29 18:29:17 +02:00
parent efe4c76cc2
commit e7f4b6043d
7 changed files with 124 additions and 24 deletions

View file

@ -10,10 +10,11 @@ define([
"Game/Client/GameObjects/GameObject",
"Game/Client/GameObjects/Doll",
"Game/Client/View/DomController",
"Lib/Utilities/Protocol/Helper"
"Lib/Utilities/Protocol/Helper",
"Game/Client/Me"
],
function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController, ProtocolHelper) {
function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController, ProtocolHelper, Me) {
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
@ -53,6 +54,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
if(this.me) {
this.me.update();
this.localMePositionUpdate();
}
for (var i = 0; i < this.gameObjects.animated.length; i++) {
@ -64,6 +66,12 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
DomController.statsEnd();
}
GameController.prototype.localMePositionUpdate = function() {
if(this.me.isStateUpdateNeeded()) {
Nc.trigger(Nc.ns.client.to.server.gameCommand.send, "meStateUpdate", this.me.getStateUpdate());
}
};
GameController.prototype.onClientReadyResponse = function(options) {
if (options.worldUpdate) {
@ -88,7 +96,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
};
}
this.createMe(options.userId);
this.setMe();
this.clientIsReady = true; // needs to stay before onSpawnPlayer
@ -108,16 +116,21 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
var gameObject = userData;
if(updateData[gameObject.uid]) {
var update = updateData[gameObject.uid];
if (gameObject instanceof Doll) {
if(gameObject === this.me.doll) {
this.me.setLastServerState(update);
continue; // this is to ignore own doll updates from world update
}
gameObject.setActionState(update.as);
gameObject.lookAt(update.laxy.x, update.laxy.y);
}
body.SetAwake(true);
body.SetPosition(update.p);
body.SetAngle(update.a);
body.SetLinearVelocity(update.lv);
body.SetAngularVelocity(update.av);
if (gameObject instanceof Doll) {
gameObject.setActionState(update.as);
gameObject.lookAt(update.laxy.x, update.laxy.y);
}
}
}
@ -125,8 +138,12 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
}
GameController.prototype.createMe = function (playerId) {
this.me = this.players[playerId];
GameController.prototype.createMe = function(user) {
this.me = new Me(user.id, this.physicsEngine, user);
this.players[user.id] = this.me;
};
GameController.prototype.setMe = function() {
this.me.setPlayerController(new PlayerController(this.me));
this.view.setMe(this.me);
}

51
app/Game/Client/Me.js Normal file
View file

@ -0,0 +1,51 @@
define([
"Game/Client/Player",
"Game/Config/Settings"
],
function (Parent, Settings) {
function Me(id, physicsEngine, user) {
Parent.call(this, id, physicsEngine, user);
this.lastServerState = {
p: {
x: 0,
y: 0
}
};
}
Me.prototype = Object.create(Parent.prototype);
Me.prototype.setLastServerState = function(update) {
this.lastServerState = update;
};
Me.prototype.isStateUpdateNeeded = function() {
if(!this.doll) return false;
var difference = {
x: Math.abs(this.lastServerState.p.x - this.doll.body.GetPosition().x),
y: Math.abs(this.lastServerState.p.y - this.doll.body.GetPosition().y)
}
if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS
|| difference.y > Settings.ME_STATE_MAX_DIFFERENCE_METERS) {
console.log('AAAAHHHH');
return true;
}
return false;
};
Me.prototype.getStateUpdate = function() {
return {
p: this.doll.body.GetPosition().Copy(),
lv: this.doll.body.GetLinearVelocity().Copy()
}
};
return Me;
});

View file

@ -61,8 +61,9 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
Networker.prototype.onJoinSuccess = function (options) {
console.log("join success")
this.onUserJoined(options.user);
this.onUserJoined(options.user, true);
this.meUserId = options.user.id;
if (options.joinedUsers) {
for(var i = 0; i < options.joinedUsers.length; i++) {
this.onUserJoined(options.joinedUsers[i]);
@ -77,9 +78,13 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
window.location.href = "/";
};
Networker.prototype.onLevelLoaded = function() {
Networker.prototype.onLevelLoaded = function() {
this.gameController.createMe(this.users[this.meUserId]);
for (var userId in this.users) {
this.gameController.createPlayer(this.users[userId]);
if(this.meUserId != userId) {
this.gameController.createPlayer(this.users[userId]);
}
}
this.sendGameCommand("clientReady");
@ -132,17 +137,18 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
// Commands from server
Networker.prototype.onUserJoined = function (options) {
Networker.prototype.onUserJoined = function (options, isMe) {
var user = new User(options.id, options);
console.log(options.nickname)
this.users[user.id] = user;
if (this.gameController
if (!isMe
&& this.gameController
&& this.gameController.level
&& this.gameController.level.isLoaded) {
this.gameController.createPlayer(user);
};
}
}
Networker.prototype.onUserLeft = function (userId) {