reorder and rename hardpoints and particles

This commit is contained in:
asraelite 2016-10-18 17:35:14 +01:00
parent d302bd4149
commit ea533015ba
16 changed files with 65 additions and 64 deletions

View file

@ -0,0 +1,16 @@
'use strict';
const Fixture = require('./fixture.js');
const Laser = require('../../particle/laser.js');
class Blaster extends Fixture {
constructor(mount, data) {
super(mount, data);
}
fireType() {
this.body.world.spawner.spawnLaser(this);
}
}
module.exports = Blaster;

View file

@ -0,0 +1,65 @@
'use strict';
class Fixture {
constructor(mount, data) {
this.type = data.type;
this.id = data.id;
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;
this.mount = mount;
this.body = this.mount.body;
}
destruct() {
this.projectiles.forEach(p => p.world.removeBody(p));
if (this.grapple) this.grapple.release();
}
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() {
return {
traversal: this.traversal,
angle: this.angle
}
}
packDelta() {
}
get angle() {
return this._angle;
}
set angle(angle) {
// TODO: Check if within traversal limit if on mount.
if (this.mount.type == 'fixed') return;
this._angle = angle;
}
}
module.exports = Fixture;

View file

@ -0,0 +1,25 @@
'use strict';
const Fixture = require('./fixture.js');
class Grapple extends Fixture {
constructor(mount, data) {
super(mount, data);
this.grapple = false;
}
fireType(value) {
if (this.state == 1) {
this.grapple.release();
this.state = 0;
} else {
let x = this.body.aim.x;
let y = this.body.aim.y;
this.state = 1;
this.grapple = this.body.world.spawner.spawnGrapple(this, x, y);
}
}
}
module.exports = Grapple;

View file

@ -0,0 +1,87 @@
'use strict';
const Blaster = require('./blaster.js');
const Grapple = require('./grapple.js');
const traits = require('../../traits/fixtures.json');
class Hardpoint {
constructor(body, data, fixture) {
this.body = body;
this.type = data.type || 'turret';
this.size = data.size || 0;
this.hidden = data.hidden || 'false';
this.pos = {
x: data.pos[0],
y: data.pos[1]
}
this.traversal = data.traversal ? {
cw: data.traversal[0],
ccw: data.traversal[1]
} : 0;
this.deltaInterface = ['traversal'];
this.fixture = false;
this.setFixture(fixture);
}
destruct() {
if (!this.fixture) return;
this.fixture.destruct();
}
fire() {
if (!this.fixture) return;
this.fixture.fire();
}
rest() {
if (!this.fixture) return;
this.fixture.rest();
}
setFixture(fixture) {
if (!(fixture in traits)) return;
let fixtureClass = {
'blaster': Blaster,
'grapple': Grapple
}[traits[fixture].type];
if (!fixtureClass) return;
let data = traits[fixture];
data.id = fixture;
this.fixture = new fixtureClass(this, data);
}
tick() {
this.fixture.tick();
}
packDelta() {
return [this.angle || 0, this.fixture.state || 0];
}
packTypeDelta() {
return [0];
}
packFull() {
return {
x: this.pos.x,
y: this.pos.y,
hidden: this.hidden,
fixture: this.fixture ? this.fixture.id : 0
};
}
get angle() {
return this.fixture ? this.fixture.angle : 0;
}
}
module.exports = Hardpoint;