From e7f4b6043d2ca7069c8ca3dd3cb130d4923be953 Mon Sep 17 00:00:00 2001 From: logsol Date: Thu, 29 May 2014 18:29:17 +0200 Subject: [PATCH] Implemented local positioning as master - included punkbuster instead of webrtc for now. #49 --- app/Game/Channel/Control/PlayerController.js | 26 +++++++++- app/Game/Channel/GameController.js | 2 +- app/Game/Client/GameController.js | 37 ++++++++++---- app/Game/Client/Me.js | 51 ++++++++++++++++++++ app/Game/Client/Networker.js | 20 +++++--- app/Game/Config/ItemSettings.js | 2 +- app/Game/Config/Settings.js | 10 ++-- 7 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 app/Game/Client/Me.js diff --git a/app/Game/Channel/Control/PlayerController.js b/app/Game/Channel/Control/PlayerController.js index e62a74d..fa596bf 100755 --- a/app/Game/Channel/Control/PlayerController.js +++ b/app/Game/Channel/Control/PlayerController.js @@ -1,10 +1,11 @@ define([ "Game/Core/Control/PlayerController", "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) { @@ -39,6 +40,27 @@ function(Parent, Nc, Parser) { 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; }); \ No newline at end of file diff --git a/app/Game/Channel/GameController.js b/app/Game/Channel/GameController.js index 8547514..eb7e013 100755 --- a/app/Game/Channel/GameController.js +++ b/app/Game/Channel/GameController.js @@ -108,7 +108,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N 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) { diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index f1c853c..990370a 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -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); } diff --git a/app/Game/Client/Me.js b/app/Game/Client/Me.js new file mode 100644 index 0000000..d49bf08 --- /dev/null +++ b/app/Game/Client/Me.js @@ -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; + +}); \ No newline at end of file diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index 07faa7e..c45448d 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -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) { diff --git a/app/Game/Config/ItemSettings.js b/app/Game/Config/ItemSettings.js index 1bd68e4..e01e953 100644 --- a/app/Game/Config/ItemSettings.js +++ b/app/Game/Config/ItemSettings.js @@ -359,7 +359,7 @@ define(function() { "category": "kitchen", "image": "banana.gif", -// "type": "rube", + // "type": "rube", "weight": "1", "width": "5", "height": "9", diff --git a/app/Game/Config/Settings.js b/app/Game/Config/Settings.js index ce6cc82..dbaca0d 100755 --- a/app/Game/Config/Settings.js +++ b/app/Game/Config/Settings.js @@ -61,9 +61,9 @@ define(function() { USE_WEBGL: true, // NETWORKING - WORLD_UPDATE_BROADCAST_INTERVAL: 70, + NETWORK_UPDATE_INTERVAL: 70, CHANNEL_DESTRUCTION_TIME: 30, - NETWORK_LOG_INCOMING: true, + NETWORK_LOG_INCOMING: false, NETWORK_LOG_OUTGOING: false, NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'], @@ -71,7 +71,11 @@ define(function() { CHANNEL_END_ROUND_TIME: 4, //10, CHANNEL_DEFAULT_MAX_USERS: 40, 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;