From d6daed2e9b08aceeea8a5faec9edf100a995d31e Mon Sep 17 00:00:00 2001 From: Asraelite Date: Wed, 30 Mar 2016 11:15:32 +0100 Subject: [PATCH] improve input packets --- public/static/js/wingbase/player.js | 21 ++++++------- server/game/player.js | 7 ++++- server/game/room/world/body/asteroid.js | 2 +- server/game/room/world/body/ship.js | 41 +++++++++++-------------- server/game/room/world/index.js | 2 +- 5 files changed, 36 insertions(+), 37 deletions(-) diff --git a/public/static/js/wingbase/player.js b/public/static/js/wingbase/player.js index 749f3e2..4b6fd44 100644 --- a/public/static/js/wingbase/player.js +++ b/public/static/js/wingbase/player.js @@ -4,7 +4,7 @@ class Player { this.team = team; this.ship = ship; - this.lastInputs = {}; + this.lastDelta = []; this.inputInterface = []; } @@ -14,19 +14,18 @@ class Player { let input = game.input; let packet = {}; - packet.thrust = ['w', 'a', 'd', 's'].map(k => input.keys.held[k] || 0); - packet.fire = input.mouse.pressed[3] ? [1, 1] : [0, 0]; - packet.aim = { - x: input.mouse.wx, - y: input.mouse.wy - }; + packet.thrust = ['w', 'a', 'd', 's'].map(k => +input.keys.held[k] || 0); + packet.fire = [1, 3].map(k => +input.mouse.pressed[k] || 0); + packet.aim = [ + +input.mouse.wx.toFixed(2), + +input.mouse.wy.toFixed(2) + ]; packet.missile = input.keys.pressed['Spacebar']; packet = this.inputInterface.map(i => packet[i]); - window.q = packet; - let delta = this.lastInputs == packet ? false : packet; - this.lastInputs = packet; - return delta; + let noDelta = JSON.stringify(this.lastDelta) == JSON.stringify(packet); + this.lastDelta = packet; + return noDelta ? false : packet; } } diff --git a/server/game/player.js b/server/game/player.js index 223d8cb..86e2912 100644 --- a/server/game/player.js +++ b/server/game/player.js @@ -31,7 +31,12 @@ class Player { } updateInputs(data) { - this.ship.updateInputs(data); + let input = {}; + let sanitize = v => { + return v.length ? v.map(a => sanitize(a)) : +v || 0; + } + data.forEach((v, i) => input[this.inputInterface[i]] = sanitize(v)); + this.ship.updateInputs(input); this.lastAction = Date.now(); } diff --git a/server/game/room/world/body/asteroid.js b/server/game/room/world/body/asteroid.js index db6de62..040ecd9 100644 --- a/server/game/room/world/body/asteroid.js +++ b/server/game/room/world/body/asteroid.js @@ -28,7 +28,7 @@ class Asteroid extends Body { let modded = angles.map(a => a % Math.PI); var max = modded.reduce((a, b) => Math.max(a, b)); var min = modded.reduce((a, b) => Math.min(a, b)); - } while (max - min < 1) + } while (max - min < 2) return [angles.sort().map(a => [Math.cos(a) * s, Math.sin(a) * s])]; } diff --git a/server/game/room/world/body/ship.js b/server/game/room/world/body/ship.js index 9e8b046..f65d12a 100644 --- a/server/game/room/world/body/ship.js +++ b/server/game/room/world/body/ship.js @@ -41,37 +41,32 @@ class Ship extends Body { forward: 0, left: 0, right: 0 - } - } - - updateInputs(data) { - this.inputs = { - forward: data[0], - left: data[1], - right: data[2], - missile: data[3], - mx: data[4], - my: data[5], - grapple: data[6], - release: data[7] }; - this.thrust.forward = +this.inputs.forward; - this.thrust.left = +this.inputs.left; - this.thrust.right = +this.inputs.right; + this.aim = { + x: 0, + y: 0 + }; + } - if (this.inputs.missile) this.launchMissile(); - if (this.inputs.grapple) { + updateInputs(packet) { + this.aim.x = packet.aim[0]; + this.aim.y = packet.aim[1]; + + this.thrust.forward = packet.thrust[0]; + this.thrust.left = packet.thrust[1]; + this.thrust.right = packet.thrust[2]; + + if (packet.fire[1]) this.launchMissile(); + if (packet.fire[0] && this.grapple) { + this.grapple.release(); + } else if (packet.fire[0] && !this.grapple) { if (this.grapple) { this.grapple.retract(); } else { - let s = this.world.scale; - this.launchGrapple(this.inputs.mx, this.inputs.my); + this.launchGrapple(this.aim.x, this.aim.y); } } - if (this.inputs.release && this.grapple) { - this.grapple.release(); - } } launchMissile() { diff --git a/server/game/room/world/index.js b/server/game/room/world/index.js index 9c2a41e..9850e69 100644 --- a/server/game/room/world/index.js +++ b/server/game/room/world/index.js @@ -28,7 +28,7 @@ class World { this.bounds = { left: 0, - right: 10, + right: 150, top: 0, bottom: 30 }