add fixture autofire
This commit is contained in:
parent
d9e8e217c6
commit
8e0c4b02e4
9 changed files with 40 additions and 8 deletions
|
@ -15,7 +15,7 @@ class Player {
|
||||||
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 = [1, 1, 3].map(k => +input.mouse.pressed[k] || 0);
|
packet.fire = [1, 1, 3].map(k => +input.mouse.held[k] || 0);
|
||||||
packet.aim = [
|
packet.aim = [
|
||||||
+input.mouse.wx.toFixed(2),
|
+input.mouse.wx.toFixed(2),
|
||||||
+input.mouse.wy.toFixed(2)
|
+input.mouse.wy.toFixed(2)
|
||||||
|
|
|
@ -113,6 +113,8 @@ class Body {
|
||||||
if(pos.y < bounds.top) this.applyForce(0, 0.03);
|
if(pos.y < bounds.top) this.applyForce(0, 0.03);
|
||||||
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
|
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
|
||||||
|
|
||||||
|
this.mounts.forEach(m => m.tick());
|
||||||
|
|
||||||
this.sleepTime++;
|
this.sleepTime++;
|
||||||
|
|
||||||
this.tickType();
|
this.tickType();
|
||||||
|
|
|
@ -61,7 +61,9 @@ class Ship extends Body {
|
||||||
this.thrust.left = packet.thrust[1];
|
this.thrust.left = packet.thrust[1];
|
||||||
this.thrust.right = packet.thrust[2];
|
this.thrust.right = packet.thrust[2];
|
||||||
|
|
||||||
packet.fire.forEach((m, i) => m ? this.mounts[i].fire(m) : 0);
|
packet.fire.forEach((m, i) => {
|
||||||
|
m ? this.mounts[i].fire(m) : this.mounts[i].rest();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
launchMissile() {
|
launchMissile() {
|
||||||
|
|
|
@ -8,7 +8,7 @@ class Blaster extends Fixture {
|
||||||
super(mount, data);
|
super(mount, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fire() {
|
fireType() {
|
||||||
this.body.world.spawner.spawnLaser(this);
|
this.body.world.spawner.spawnLaser(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,11 @@ class Fixture {
|
||||||
constructor(mount, data) {
|
constructor(mount, data) {
|
||||||
this.type = data.type;
|
this.type = data.type;
|
||||||
this.id = data.id;
|
this.id = data.id;
|
||||||
this.rof = data.rateOfFire;
|
this.rof = 60 / data.rateOfFire | 0;
|
||||||
|
this.autofire = data.autofire || false;
|
||||||
|
|
||||||
|
this.fired = false;
|
||||||
|
this.cooldown = 0;
|
||||||
this.state = 0;
|
this.state = 0;
|
||||||
this.projectiles = new Set();
|
this.projectiles = new Set();
|
||||||
this._angle = mount.traversal ? mount.traversal.cw : 0;
|
this._angle = mount.traversal ? mount.traversal.cw : 0;
|
||||||
|
@ -16,10 +19,25 @@ class Fixture {
|
||||||
|
|
||||||
destruct() {
|
destruct() {
|
||||||
this.projectiles.forEach(p => p.world.removeBody(p));
|
this.projectiles.forEach(p => p.world.removeBody(p));
|
||||||
|
if (this.grapple) this.grapple.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
fire() {
|
fire(value) {
|
||||||
|
if (this.cooldown > 0) return;
|
||||||
|
if (this.fired && !this.autofire) return;
|
||||||
|
|
||||||
|
this.fireType(value);
|
||||||
|
this.cooldown = this.rof;
|
||||||
|
this.fired = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
rest() {
|
||||||
|
this.fired = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
if (this.cooldown > 0)
|
||||||
|
this.cooldown--;
|
||||||
}
|
}
|
||||||
|
|
||||||
packFull() {
|
packFull() {
|
||||||
|
|
|
@ -9,7 +9,7 @@ class Grapple extends Fixture {
|
||||||
this.grapple = false;
|
this.grapple = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fire(value) {
|
fireType(value) {
|
||||||
if (this.state == 1) {
|
if (this.state == 1) {
|
||||||
this.grapple.release();
|
this.grapple.release();
|
||||||
this.state = 0;
|
this.state = 0;
|
||||||
|
|
|
@ -38,6 +38,11 @@ class Mount {
|
||||||
this.fixture.fire();
|
this.fixture.fire();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rest() {
|
||||||
|
if (!this.fixture) return;
|
||||||
|
this.fixture.rest();
|
||||||
|
}
|
||||||
|
|
||||||
setFixture(fixture) {
|
setFixture(fixture) {
|
||||||
if (!(fixture in traits)) return;
|
if (!(fixture in traits)) return;
|
||||||
|
|
||||||
|
@ -53,6 +58,10 @@ class Mount {
|
||||||
this.fixture = new fixtureClass(this, data);
|
this.fixture = new fixtureClass(this, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
this.fixture.tick();
|
||||||
|
}
|
||||||
|
|
||||||
packDelta() {
|
packDelta() {
|
||||||
return [this.angle || 0, this.fixture.state || 0];
|
return [this.angle || 0, this.fixture.state || 0];
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ class World {
|
||||||
|
|
||||||
this.bounds = {
|
this.bounds = {
|
||||||
left: 0,
|
left: 0,
|
||||||
right: 150,
|
right: 60,
|
||||||
top: 0,
|
top: 0,
|
||||||
bottom: 30
|
bottom: 30
|
||||||
}
|
}
|
||||||
|
@ -115,7 +115,7 @@ class World {
|
||||||
}
|
}
|
||||||
|
|
||||||
populate() {
|
populate() {
|
||||||
for (var i = 0; i < 50; i++) {
|
for (var i = 0; i < 30; i++) {
|
||||||
let pos = {
|
let pos = {
|
||||||
x: Math.random() * this.bounds.right,
|
x: Math.random() * this.bounds.right,
|
||||||
y: Math.random() * this.bounds.bottom
|
y: Math.random() * this.bounds.bottom
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
"type": "grapple",
|
"type": "grapple",
|
||||||
"speed": 0.1,
|
"speed": 0.1,
|
||||||
"range": 8,
|
"range": 8,
|
||||||
|
"autofire": false,
|
||||||
"cost": {
|
"cost": {
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue