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

@ -1,10 +1,11 @@
define([ define([
"Game/Core/Control/PlayerController", "Game/Core/Control/PlayerController",
"Lib/Utilities/NotificationCenter", "Lib/Utilities/NotificationCenter",
"Lib/Utilities/Protocol/Parser" "Lib/Utilities/Protocol/Parser",
"Game/Config/Settings"
], ],
function(Parent, Nc, Parser) { function(Parent, Nc, Parser, Settings) {
function PlayerController(player) { function PlayerController(player) {
@ -39,6 +40,27 @@ function(Parent, Nc, Parser) {
this.player.suicide(); this.player.suicide();
}; };
PlayerController.prototype.meStateUpdate = function(update) {
if(!this.player.doll) {
console.warn('me state update, even though doll does not exist');
return;
}
var difference = {
x: Math.abs(update.p.x - this.player.doll.body.GetPosition().x),
y: Math.abs(update.p.y - this.player.doll.body.GetPosition().y)
}
if(difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS
|| difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) {
this.player.doll.body.SetAwake(true);
this.player.doll.body.SetPosition(update.p);
} else {
// HARD UPDATE FOR SELF
}
};
return PlayerController; return PlayerController;
}); });

View file

@ -108,7 +108,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'worldUpdate', update); Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'worldUpdate', update);
} }
this.worldUpdateTimeout = setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); this.worldUpdateTimeout = setTimeout(this.updateWorld.bind(this), Settings.NETWORK_UPDATE_INTERVAL);
} }
GameController.prototype.getWorldUpdateObject = function(getSleeping) { GameController.prototype.getWorldUpdateObject = function(getSleeping) {

View file

@ -10,10 +10,11 @@ define([
"Game/Client/GameObjects/GameObject", "Game/Client/GameObjects/GameObject",
"Game/Client/GameObjects/Doll", "Game/Client/GameObjects/Doll",
"Game/Client/View/DomController", "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) { if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) { window.cancelAnimationFrame = function(id) {
@ -53,6 +54,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
if(this.me) { if(this.me) {
this.me.update(); this.me.update();
this.localMePositionUpdate();
} }
for (var i = 0; i < this.gameObjects.animated.length; i++) { for (var i = 0; i < this.gameObjects.animated.length; i++) {
@ -64,6 +66,12 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
DomController.statsEnd(); 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) { GameController.prototype.onClientReadyResponse = function(options) {
if (options.worldUpdate) { 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 this.clientIsReady = true; // needs to stay before onSpawnPlayer
@ -108,16 +116,21 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
var gameObject = userData; var gameObject = userData;
if(updateData[gameObject.uid]) { if(updateData[gameObject.uid]) {
var update = 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.SetAwake(true);
body.SetPosition(update.p); body.SetPosition(update.p);
body.SetAngle(update.a); body.SetAngle(update.a);
body.SetLinearVelocity(update.lv); body.SetLinearVelocity(update.lv);
body.SetAngularVelocity(update.av); 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) { GameController.prototype.createMe = function(user) {
this.me = this.players[playerId]; 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.me.setPlayerController(new PlayerController(this.me));
this.view.setMe(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,7 +61,8 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
Networker.prototype.onJoinSuccess = function (options) { Networker.prototype.onJoinSuccess = function (options) {
console.log("join success") console.log("join success")
this.onUserJoined(options.user); this.onUserJoined(options.user, true);
this.meUserId = options.user.id;
if (options.joinedUsers) { if (options.joinedUsers) {
for(var i = 0; i < options.joinedUsers.length; i++) { for(var i = 0; i < options.joinedUsers.length; i++) {
@ -78,9 +79,13 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
}; };
Networker.prototype.onLevelLoaded = function() { Networker.prototype.onLevelLoaded = function() {
this.gameController.createMe(this.users[this.meUserId]);
for (var userId in this.users) { for (var userId in this.users) {
if(this.meUserId != userId) {
this.gameController.createPlayer(this.users[userId]); this.gameController.createPlayer(this.users[userId]);
} }
}
this.sendGameCommand("clientReady"); this.sendGameCommand("clientReady");
}; };
@ -132,17 +137,18 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
// Commands from server // Commands from server
Networker.prototype.onUserJoined = function (options) { Networker.prototype.onUserJoined = function (options, isMe) {
var user = new User(options.id, options); var user = new User(options.id, options);
console.log(options.nickname) console.log(options.nickname)
this.users[user.id] = user; this.users[user.id] = user;
if (this.gameController if (!isMe
&& this.gameController
&& this.gameController.level && this.gameController.level
&& this.gameController.level.isLoaded) { && this.gameController.level.isLoaded) {
this.gameController.createPlayer(user); this.gameController.createPlayer(user);
}; }
} }
Networker.prototype.onUserLeft = function (userId) { Networker.prototype.onUserLeft = function (userId) {

View file

@ -359,7 +359,7 @@ define(function() {
"category": "kitchen", "category": "kitchen",
"image": "banana.gif", "image": "banana.gif",
// "type": "rube", // "type": "rube",
"weight": "1", "weight": "1",
"width": "5", "width": "5",
"height": "9", "height": "9",

View file

@ -61,9 +61,9 @@ define(function() {
USE_WEBGL: true, USE_WEBGL: true,
// NETWORKING // NETWORKING
WORLD_UPDATE_BROADCAST_INTERVAL: 70, NETWORK_UPDATE_INTERVAL: 70,
CHANNEL_DESTRUCTION_TIME: 30, CHANNEL_DESTRUCTION_TIME: 30,
NETWORK_LOG_INCOMING: true, NETWORK_LOG_INCOMING: false,
NETWORK_LOG_OUTGOING: false, NETWORK_LOG_OUTGOING: false,
NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'], NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'],
@ -71,7 +71,11 @@ define(function() {
CHANNEL_END_ROUND_TIME: 4, //10, CHANNEL_END_ROUND_TIME: 4, //10,
CHANNEL_DEFAULT_MAX_USERS: 40, CHANNEL_DEFAULT_MAX_USERS: 40,
CHANNEL_DEFAULT_SCORE_LIMIT: 10, CHANNEL_DEFAULT_SCORE_LIMIT: 10,
CHANNEL_DEFAULT_LEVELS: ['stones2', 'debug', 'stones2', 'debug'] CHANNEL_DEFAULT_LEVELS: ['stones2', 'debug', 'stones2', 'debug'],
// ME STATE
ME_STATE_MAX_DIFFERENCE_METERS: 1,
PUNKBUSTER_DIFFERENCE_METERS: 1
} }
Settings.TILE_RATIO = Settings.ORIGINAL_TILE_SIZE / Settings.TILE_SIZE; Settings.TILE_RATIO = Settings.ORIGINAL_TILE_SIZE / Settings.TILE_SIZE;