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: {
'01': {
small: 'img/turrets/01/normal.png'
},
'02': {
small: 'img/turrets/02/normal.png'
}
},
projectiles: {

View file

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

View file

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

View file

@ -3,7 +3,8 @@
class Body {
constructor(data) {
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.id = data.id
this.frame = data.frame;
@ -36,9 +37,7 @@ class Body {
update(data) {
let values = {};
Array.from(data).map((v, i) => {
values[this.interface.order[i]] = v
});
this.interface.order.forEach(v => values[v] = data.shift());
this.x = values.x;
this.y = values.y;
this.xvel = values.xvel;
@ -46,7 +45,14 @@ class Body {
this.r = values.r;
this.rvel = values.rvel;
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);
}

View file

@ -32,9 +32,7 @@ class Player {
updateInputs(data) {
let input = {};
let sanitize = v => {
return v.length ? v.map(a => sanitize(a)) : +v || 0;
}
let sanitize = v => 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();

View file

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

View file

@ -1,36 +1,35 @@
'use strict';
const traits = require('../../traits/fixtures.json');
class Fixture {
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.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() {
this.projectiles.forEach(p => p.world.removeBody(p));
}
fire() {
}
packFull() {
return {
traversal: this.traversal
traversal: this.traversal,
angle: this.angle
}
}
packDelta() {
return [this.traversal];
}
get angle() {
@ -39,7 +38,7 @@ class Fixture {
set angle(angle) {
// TODO: Check if within traversal limit if on mount.
if (this.type == 'fixed') return;
if (this.mount.type == 'fixed') return;
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';
const Blaster = require('./blaster.js');
const Grapple = require('./grapple.js');
const traits = require('../../traits/fixtures.json');
class Mount {
constructor(ship, data, fixture) {
this.ship = ship;
//this.ship = ship;
this.type = data.type || 'turret';
this.fixture = fixture || false//new Fixture(fixture);
this.size = data.size || 0;
this.hidden = data.hidden || 'false';
this.position = {
x: data.pos[0],
y: data.pos[1]
}
this.traversal = data.traversal ? {
cw: data.bounds[0],
ccw: data.bounds[1]
cw: data.traversal[0],
ccw: data.traversal[1]
} : 0;
this.updateDeltaInterface();
this.deltaInterface = ['traversal'];
this.fixture = false;
this.setFixture(fixture);
}
destruct() {
if (!this.fixture) return;
//this.fixture.destruct();
this.fixture.destruct();
}
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() {
return [this.traversal || 0];
return [this.angle || 0, this.fixture.state || 0];
}
updateDeltaInterface() {
this.deltaInterface = ['traversal'];
packTypeDelta() {
return [0];
}
packFull() {
return {
x: this.position.x,
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;

View file

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

View file

@ -1,12 +1,14 @@
{
"01": {
"name": "Basic Blaster",
"class": "gun",
"type": "blaster",
"damage": {
"thermal": 5,
"kinetic": 1
},
"speed": 1,
"acceleration": 0,
"range": 10,
"autofire": true,
"rateOfFire": 4,
@ -23,5 +25,20 @@
"trail": 3,
"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],
"type": "fixed",
"size": 0,
"traversal": false
"traversal": [0, 0],
"hidden": true
},
{
"pos": [18, 28],
"type": "fixed",
"size": 0,
"traversal": false
"traversal": [0, 0],
"hidden": true
},
{
"pos": [1, 16],
"pos": [6, 16],
"type": "fixed",
"size": 1,
"traversal": false
"traversal": [3.14, 0],
"hidden": true
}
]
}