improve input packets
This commit is contained in:
parent
a2a93882f0
commit
d6daed2e9b
5 changed files with 36 additions and 37 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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])];
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -28,7 +28,7 @@ class World {
|
|||
|
||||
this.bounds = {
|
||||
left: 0,
|
||||
right: 10,
|
||||
right: 150,
|
||||
top: 0,
|
||||
bottom: 30
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue