add fixture autofire

This commit is contained in:
Asraelite 2016-03-30 20:40:05 +01:00
parent d9e8e217c6
commit 8e0c4b02e4
9 changed files with 40 additions and 8 deletions

View file

@ -113,6 +113,8 @@ class Body {
if(pos.y < bounds.top) this.applyForce(0, 0.03);
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
this.mounts.forEach(m => m.tick());
this.sleepTime++;
this.tickType();

View file

@ -61,7 +61,9 @@ class Ship extends Body {
this.thrust.left = packet.thrust[1];
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() {

View file

@ -8,7 +8,7 @@ class Blaster extends Fixture {
super(mount, data);
}
fire() {
fireType() {
this.body.world.spawner.spawnLaser(this);
}
}

View file

@ -4,8 +4,11 @@ class Fixture {
constructor(mount, data) {
this.type = data.type;
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.projectiles = new Set();
this._angle = mount.traversal ? mount.traversal.cw : 0;
@ -16,10 +19,25 @@ class Fixture {
destruct() {
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() {

View file

@ -9,7 +9,7 @@ class Grapple extends Fixture {
this.grapple = false;
}
fire(value) {
fireType(value) {
if (this.state == 1) {
this.grapple.release();
this.state = 0;

View file

@ -38,6 +38,11 @@ class Mount {
this.fixture.fire();
}
rest() {
if (!this.fixture) return;
this.fixture.rest();
}
setFixture(fixture) {
if (!(fixture in traits)) return;
@ -53,6 +58,10 @@ class Mount {
this.fixture = new fixtureClass(this, data);
}
tick() {
this.fixture.tick();
}
packDelta() {
return [this.angle || 0, this.fixture.state || 0];
}

View file

@ -29,7 +29,7 @@ class World {
this.bounds = {
left: 0,
right: 150,
right: 60,
top: 0,
bottom: 30
}
@ -115,7 +115,7 @@ class World {
}
populate() {
for (var i = 0; i < 50; i++) {
for (var i = 0; i < 30; i++) {
let pos = {
x: Math.random() * this.bounds.right,
y: Math.random() * this.bounds.bottom

View file

@ -32,6 +32,7 @@
"type": "grapple",
"speed": 0.1,
"range": 8,
"autofire": false,
"cost": {
},