add robust fixture rendering

This commit is contained in:
Asraelite 2016-03-30 15:58:06 +01:00
parent ef2d067b38
commit cf4e8024af
13 changed files with 140 additions and 64 deletions

View file

@ -14,6 +14,9 @@ Game.prototype.loadAssets = _ => {
turrets: { turrets: {
'01': { '01': {
small: 'img/turrets/01/normal.png' small: 'img/turrets/01/normal.png'
},
'02': {
small: 'img/turrets/02/normal.png'
} }
}, },
projectiles: { projectiles: {

View file

@ -19,9 +19,9 @@ class BodyRenderer {
pallet.view(x, y, false, pos.r); pallet.view(x, y, false, pos.r);
for (let f of body.fixtures) { for (let f of body.fixtures) {
if (!f.type) continue; if (!f.fixture || !f.hidden) continue;
let img = game.assets.images.turrets[f.type].small; let img = game.assets.images.turrets[f.fixture].small;
this.pallet.image(img, f.x - 32, f.y - 32, 0); this.pallet.image(img, f.x - 32, f.y - 32, f.angle);
} }
if (body.bodyType == 'ship') { if (body.bodyType == 'ship') {
@ -34,6 +34,13 @@ class BodyRenderer {
this.renderBody(body); this.renderBody(body);
} }
for (let f of body.fixtures) {
if (!f.fixture || f.hidden) continue;
let img = game.assets.images.turrets[f.fixture].small;
this.pallet.image(img, f.x - 32, f.y - 32, f.angle);
}
pallet.restore();
pallet.restore(); pallet.restore();
} }
@ -62,7 +69,7 @@ class BodyRenderer {
context.stroke(); context.stroke();
} }
this.pallet.restore(); //this.pallet.restore();
} }
renderBody(body) { renderBody(body) {
@ -83,7 +90,7 @@ class BodyRenderer {
context.stroke(); context.stroke();
} }
this.pallet.restore(); //this.pallet.restore();
} }
renderShip(ship) { renderShip(ship) {
@ -103,7 +110,7 @@ class BodyRenderer {
this.pallet.square('#f00', ship.debug.x * SCALE, ship.debug.y * SCALE, 2); this.pallet.square('#f00', ship.debug.x * SCALE, ship.debug.y * SCALE, 2);
} }
this.pallet.restore(); //this.pallet.restore();
} }
renderShipNameplate(ship) { renderShipNameplate(ship) {
@ -122,6 +129,6 @@ class BodyRenderer {
let img = game.assets.images.projectiles['02']; let img = game.assets.images.projectiles['02'];
let pos = body.pos; let pos = body.pos;
this.pallet.image(img, -32, -32, 0); this.pallet.image(img, -32, -32, 0);
this.pallet.restore(); //this.pallet.restore();
} }
}; };

View file

@ -6,10 +6,10 @@ class Asteroid extends Body {
} }
updateType(data) { updateType(data) {
//this.debug = data.debug; this.debug = data.debug;
} }
tickType() { tickType() {
} }
} }

View file

@ -3,7 +3,8 @@
class Body { class Body {
constructor(data) { constructor(data) {
this.interface = data.interface; this.interface = data.interface;
let s = this.interface.order.length + this.interface.fixtures; let s = this.interface.order.length;
s += this.interface.fixtures.order.length * this.interface.fixtures.num;
this.interface.size = s; this.interface.size = s;
this.id = data.id this.id = data.id
this.frame = data.frame; this.frame = data.frame;
@ -36,9 +37,7 @@ class Body {
update(data) { update(data) {
let values = {}; let values = {};
Array.from(data).map((v, i) => { this.interface.order.forEach(v => values[v] = data.shift());
values[this.interface.order[i]] = v
});
this.x = values.x; this.x = values.x;
this.y = values.y; this.y = values.y;
this.xvel = values.xvel; this.xvel = values.xvel;
@ -46,7 +45,14 @@ class Body {
this.r = values.r; this.r = values.r;
this.rvel = values.rvel; this.rvel = values.rvel;
this.updated = 10; this.updated = 10;
this.debug = values.debug;
this.fixtures.forEach(fixture => {
let obj = {};
this.interface.fixtures.order.forEach(v => obj[v] = data.shift());
fixture.angle = obj.angle;
fixture.state = obj.state;
});
this.updateType(values); this.updateType(values);
} }

View file

@ -32,9 +32,7 @@ class Player {
updateInputs(data) { updateInputs(data) {
let input = {}; let input = {};
let sanitize = v => { let sanitize = v => v.length ? v.map(a => sanitize(a)) : +v || 0;
return v.length ? v.map(a => sanitize(a)) : +v || 0;
}
data.forEach((v, i) => input[this.inputInterface[i]] = sanitize(v)); data.forEach((v, i) => input[this.inputInterface[i]] = sanitize(v));
this.ship.updateInputs(input); this.ship.updateInputs(input);
this.lastAction = Date.now(); this.lastAction = Date.now();

View file

@ -39,7 +39,13 @@ class Body {
'rvel' 'rvel'
], ],
type: 'body', type: 'body',
fixtures: this.mounts.length fixtures: {
order: [
'angle',
'state'
],
num: this.mounts.length
}
}; };
this.sleepTime = 0; this.sleepTime = 0;
@ -99,16 +105,12 @@ class Body {
} }
packDelta() { packDelta() {
let pos = this.b2body.GetPosition(); let pos = this.pos;
let vel = this.b2body.GetLinearVelocity(); let vel = this.vel;
let rot = this.b2body.GetAngleRadians();
let rvel = this.b2body.GetAngularVelocity();
let values = [this.id, pos.x, pos.y, vel.x, vel.y, rot, rvel]; let values = [this.id, pos.x, pos.y, vel.x, vel.y, pos.r, vel.r];
values = values.concat(this.packTypeDelta()); values = values.concat(this.packTypeDelta());
this.mounts.forEach(m => { this.mounts.forEach(m => [].push.apply(values, m.packDelta()));
values = values.concat(m.packDelta());
});
return values; return values;
} }
@ -128,9 +130,8 @@ class Body {
interface: this.interface interface: this.interface
}; };
let typePacket = this.packTypeFull(); // Merge default and type-specific packets.
for (let i in typePacket) packet = Object.assign(packet, this.packTypeFull());
packet[i] = typePacket[i];
return packet; return packet;
} }
@ -173,7 +174,8 @@ class Body {
get vel() { get vel() {
return { return {
x: this.b2body.GetLinearVelocity().x, x: this.b2body.GetLinearVelocity().x,
y: this.b2body.GetLinearVelocity().y y: this.b2body.GetLinearVelocity().y,
r: this.b2body.GetAngularVelocity()
} }
} }
} }

View file

@ -9,7 +9,6 @@ class Blaster extends Fixture {
} }
fire() { fire() {
wingbase.debug('pew pew');
let data = { let data = {
speed: 1 speed: 1

View file

@ -1,36 +1,35 @@
'use strict'; 'use strict';
const traits = require('../../traits/fixtures.json');
class Fixture { class Fixture {
constructor(mount, data) { constructor(mount, data) {
this.type = data.type;
this.id = data.id;
this.rof = data.rateOfFire;
this.state = 0;
this.projectiles = new Set();
this._angle = mount.traversal ? mount.traversal.cw : 0;
this.mount = mount; this.mount = mount;
this.projectiles = new WeakSet();
let turretTraits = traits[data.type];
console.log(turretTraits);
this.rof = turretTraits.rateOfFire;
this.traversal = this.mount.traversal || false;
this.fired = false;
this._angle = this.traversal ? this.traversal.cw : 0;
} }
destruct() { destruct() {
this.projectiles.forEach(p => p.world.removeBody(p)); this.projectiles.forEach(p => p.world.removeBody(p));
} }
fire() {
}
packFull() { packFull() {
return { return {
traversal: this.traversal traversal: this.traversal,
angle: this.angle
} }
} }
packDelta() { packDelta() {
return [this.traversal];
} }
get angle() { get angle() {
@ -39,7 +38,7 @@ class Fixture {
set angle(angle) { set angle(angle) {
// TODO: Check if within traversal limit if on mount. // TODO: Check if within traversal limit if on mount.
if (this.type == 'fixed') return; if (this.mount.type == 'fixed') return;
this._angle = angle; this._angle = angle;
} }
} }

View file

@ -0,0 +1,15 @@
'use strict';
const Fixture = require('./fixture.js');
class Grapple extends Fixture {
constructor(mount, data) {
super(mount, data);
}
fire() {
}
}
module.exports = Grapple;

View file

@ -1,51 +1,78 @@
'use strict'; 'use strict';
const Blaster = require('./blaster.js'); const Blaster = require('./blaster.js');
const Grapple = require('./grapple.js');
const traits = require('../../traits/fixtures.json');
class Mount { class Mount {
constructor(ship, data, fixture) { constructor(ship, data, fixture) {
this.ship = ship; //this.ship = ship;
this.type = data.type || 'turret'; this.type = data.type || 'turret';
this.fixture = fixture || false//new Fixture(fixture);
this.size = data.size || 0; this.size = data.size || 0;
this.hidden = data.hidden || 'false';
this.position = { this.position = {
x: data.pos[0], x: data.pos[0],
y: data.pos[1] y: data.pos[1]
} }
this.traversal = data.traversal ? { this.traversal = data.traversal ? {
cw: data.bounds[0], cw: data.traversal[0],
ccw: data.bounds[1] ccw: data.traversal[1]
} : 0; } : 0;
this.updateDeltaInterface(); this.deltaInterface = ['traversal'];
this.fixture = false;
this.setFixture(fixture);
} }
destruct() { destruct() {
if (!this.fixture) return; if (!this.fixture) return;
//this.fixture.destruct(); this.fixture.destruct();
} }
fire() { fire() {
console.log(this.fixture); if (!this.fixture) return;
this.fixture.fire();
}
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);
} }
packDelta() { packDelta() {
return [this.traversal || 0]; return [this.angle || 0, this.fixture.state || 0];
} }
updateDeltaInterface() { packTypeDelta() {
this.deltaInterface = ['traversal']; return [0];
} }
packFull() { packFull() {
return { return {
x: this.position.x, x: this.position.x,
y: this.position.y, y: this.position.y,
type: this.fixture ? this.fixture : 0 hidden: this.hidden,
fixture: this.fixture ? this.fixture.id : 0
}; };
} }
get angle() {
return this.fixture ? this.fixture.angle : 0;
}
} }
module.exports = Mount; module.exports = Mount;

View file

@ -1,6 +1,6 @@
{ {
"spawnShip": { "spawnShip": {
"ship": "01", "ship": "01",
"fixtures": ["01", "01"] "fixtures": ["01", "01", "02"]
} }
} }

View file

@ -1,12 +1,14 @@
{ {
"01": { "01": {
"name": "Basic Blaster", "name": "Basic Blaster",
"class": "gun",
"type": "blaster", "type": "blaster",
"damage": { "damage": {
"thermal": 5, "thermal": 5,
"kinetic": 1 "kinetic": 1
}, },
"speed": 1, "speed": 1,
"acceleration": 0,
"range": 10, "range": 10,
"autofire": true, "autofire": true,
"rateOfFire": 4, "rateOfFire": 4,
@ -23,5 +25,20 @@
"trail": 3, "trail": 3,
"size": 2 "size": 2
} }
},
"02": {
"name": "Grappling Hook",
"class": "launcher",
"type": "grapple",
"speed": 0.1,
"range": 8,
"cost": {
},
"appearance": {
"type": "projectile",
"sprite": "grapple",
"size": 1
}
} }
} }

View file

@ -22,19 +22,22 @@
"pos": [18, 4], "pos": [18, 4],
"type": "fixed", "type": "fixed",
"size": 0, "size": 0,
"traversal": false "traversal": [0, 0],
"hidden": true
}, },
{ {
"pos": [18, 28], "pos": [18, 28],
"type": "fixed", "type": "fixed",
"size": 0, "size": 0,
"traversal": false "traversal": [0, 0],
"hidden": true
}, },
{ {
"pos": [1, 16], "pos": [6, 16],
"type": "fixed", "type": "fixed",
"size": 1, "size": 1,
"traversal": false "traversal": [3.14, 0],
"hidden": true
} }
] ]
} }