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.team = team;
|
||||||
this.ship = ship;
|
this.ship = ship;
|
||||||
|
|
||||||
this.lastInputs = {};
|
this.lastDelta = [];
|
||||||
|
|
||||||
this.inputInterface = [];
|
this.inputInterface = [];
|
||||||
}
|
}
|
||||||
|
@ -14,19 +14,18 @@ class Player {
|
||||||
let input = game.input;
|
let input = game.input;
|
||||||
let packet = {};
|
let packet = {};
|
||||||
|
|
||||||
packet.thrust = ['w', 'a', 'd', 's'].map(k => input.keys.held[k] || 0);
|
packet.thrust = ['w', 'a', 'd', 's'].map(k => +input.keys.held[k] || 0);
|
||||||
packet.fire = input.mouse.pressed[3] ? [1, 1] : [0, 0];
|
packet.fire = [1, 3].map(k => +input.mouse.pressed[k] || 0);
|
||||||
packet.aim = {
|
packet.aim = [
|
||||||
x: input.mouse.wx,
|
+input.mouse.wx.toFixed(2),
|
||||||
y: input.mouse.wy
|
+input.mouse.wy.toFixed(2)
|
||||||
};
|
];
|
||||||
packet.missile = input.keys.pressed['Spacebar'];
|
packet.missile = input.keys.pressed['Spacebar'];
|
||||||
|
|
||||||
packet = this.inputInterface.map(i => packet[i]);
|
packet = this.inputInterface.map(i => packet[i]);
|
||||||
window.q = packet;
|
|
||||||
|
|
||||||
let delta = this.lastInputs == packet ? false : packet;
|
let noDelta = JSON.stringify(this.lastDelta) == JSON.stringify(packet);
|
||||||
this.lastInputs = packet;
|
this.lastDelta = packet;
|
||||||
return delta;
|
return noDelta ? false : packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,12 @@ class Player {
|
||||||
}
|
}
|
||||||
|
|
||||||
updateInputs(data) {
|
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();
|
this.lastAction = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ class Asteroid extends Body {
|
||||||
let modded = angles.map(a => a % Math.PI);
|
let modded = angles.map(a => a % Math.PI);
|
||||||
var max = modded.reduce((a, b) => Math.max(a, b));
|
var max = modded.reduce((a, b) => Math.max(a, b));
|
||||||
var min = modded.reduce((a, b) => Math.min(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])];
|
return [angles.sort().map(a => [Math.cos(a) * s, Math.sin(a) * s])];
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,37 +41,32 @@ class Ship extends Body {
|
||||||
forward: 0,
|
forward: 0,
|
||||||
left: 0,
|
left: 0,
|
||||||
right: 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.aim = {
|
||||||
this.thrust.left = +this.inputs.left;
|
x: 0,
|
||||||
this.thrust.right = +this.inputs.right;
|
y: 0
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (this.inputs.missile) this.launchMissile();
|
updateInputs(packet) {
|
||||||
if (this.inputs.grapple) {
|
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) {
|
if (this.grapple) {
|
||||||
this.grapple.retract();
|
this.grapple.retract();
|
||||||
} else {
|
} else {
|
||||||
let s = this.world.scale;
|
this.launchGrapple(this.aim.x, this.aim.y);
|
||||||
this.launchGrapple(this.inputs.mx, this.inputs.my);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.inputs.release && this.grapple) {
|
|
||||||
this.grapple.release();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
launchMissile() {
|
launchMissile() {
|
||||||
|
|
|
@ -28,7 +28,7 @@ class World {
|
||||||
|
|
||||||
this.bounds = {
|
this.bounds = {
|
||||||
left: 0,
|
left: 0,
|
||||||
right: 10,
|
right: 150,
|
||||||
top: 0,
|
top: 0,
|
||||||
bottom: 30
|
bottom: 30
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue