add lasers

This commit is contained in:
Asraelite 2016-03-30 19:15:06 +01:00
parent cf4e8024af
commit f87aa1446d
31 changed files with 298 additions and 125 deletions

View file

@ -121,6 +121,7 @@ class Body {
packFull() {
let packet = {
form: 'body',
type: this.type,
class: this.class,
id: this.id,
@ -140,6 +141,10 @@ class Body {
return {};
}
getWorldPos(pos) {
return this.b2body.GetWorldPoint(new b2Vec2(pos.x, pos.y), {});
}
get awake() {
if (this.b2body.IsAwake()) {
this.sleepTime = 0;

View file

@ -58,26 +58,12 @@ class Ship extends Body {
this.thrust.right = packet.thrust[2];
packet.fire.forEach((m, i) => m ? this.mounts[i].fire(m) : 0);
if (packet.fire[0] && this.grapple) {
this.grapple.release();
} else if (packet.fire[0] && !this.grapple) {
if (this.grapple) {
this.grapple.retract();
} else {
this.launchGrapple(this.aim.x, this.aim.y);
}
}
}
launchMissile() {
this.world.spawner.spawnMissile(this);
}
launchGrapple(x, y) {
this.grapple = this.world.spawner.spawnGrapple(this, x, y);
}
tickType() {
if (this.thrust.forward) {
let power = this.power.forward;

View file

@ -1,7 +1,7 @@
'use strict';
const Fixture = require('./fixture.js');
const Laser = require('./shot/laser.js');
const Laser = require('./discharge/laser.js');
class Blaster extends Fixture {
constructor(mount, data) {
@ -9,10 +9,7 @@ class Blaster extends Fixture {
}
fire() {
let data = {
speed: 1
};
this.body.world.spawner.spawnLaser(this);
}
}

View file

@ -0,0 +1,11 @@
'use strict';
const Discharge = require('./discharge.js');
class Beam extends Discharge {
constructor() {
super();
}
}
module.exports = Beam;

View file

@ -0,0 +1,11 @@
'use strict';
const Discharge = require('./discharge.js');
class Bullet extends Discharge {
constructor() {
super();
}
}
module.exports = Bullet;

View file

@ -0,0 +1,49 @@
'use strict';
class Discharge {
constructor(fixture, data) {
this.x = data.x;
this.y = data.y;
this.xvel = data.xvel;
this.yvel = data.yvel;
this.r = data.r;
this.lifetime = data.lifetime || 100;
this.fixture = fixture;
// Might just make it this.world later if it turns out I need it more.
this.id = this.fixture.body.world.room.generateId();
}
destroy() {
this.fixture.body.world.removeDischarge(this);
}
packDelta() {
// TODO: Implement some sort of delta interface for discharges that is
// derived from the fixture so it's efficient.
return [this.id, this.x, this.y];
}
packFull() {
// TODO: Create creation interface using fixture then send this as
// an array.
return {
form: 'discharge',
id: this.id,
x: this.x,
y: this.y,
r: this.r,
xvel: this.xvel,
yvel: this.yvel,
delta: this.packDelta()
}
}
tick() {
if (this.lifetime-- <= 0)
this.destroy();
}
}
module.exports = Discharge;

View file

@ -0,0 +1,11 @@
'use strict';
const Discharge = require('./discharge.js');
class Laser extends Discharge {
constructor(fixture, data) {
super(fixture, data);
}
}
module.exports = Laser;

View file

@ -11,6 +11,7 @@ class Fixture {
this._angle = mount.traversal ? mount.traversal.cw : 0;
this.mount = mount;
this.body = this.mount.body;
}
destruct() {

View file

@ -5,10 +5,20 @@ const Fixture = require('./fixture.js');
class Grapple extends Fixture {
constructor(mount, data) {
super(mount, data);
this.grapple = false;
}
fire() {
fire(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);
}
}
}

View file

@ -6,13 +6,13 @@ const Grapple = require('./grapple.js');
const traits = require('../../traits/fixtures.json');
class Mount {
constructor(ship, data, fixture) {
//this.ship = ship;
constructor(body, data, fixture) {
this.body = body;
this.type = data.type || 'turret';
this.size = data.size || 0;
this.hidden = data.hidden || 'false';
this.position = {
this.pos = {
x: data.pos[0],
y: data.pos[1]
}
@ -63,8 +63,8 @@ class Mount {
packFull() {
return {
x: this.position.x,
y: this.position.y,
x: this.pos.x,
y: this.pos.y,
hidden: this.hidden,
fixture: this.fixture ? this.fixture.id : 0
};

View file

@ -1,11 +0,0 @@
'use strict';
const Shot = require('./shot.js');
class Beam extends Shot {
constructor() {
super();
}
}
module.exports = Beam;

View file

@ -1,11 +0,0 @@
'use strict';
const Shot = require('./shot.js');
class Bullet extends Shot {
constructor() {
super();
}
}
module.exports = Bullet;

View file

@ -1,11 +0,0 @@
'use strict';
const Shot = require('./shot.js');
class Laser extends Shot {
constructor() {
super();
}
}
module.exports = Laser;

View file

@ -1,13 +0,0 @@
'use strict';
class Shot {
constructor(pos) {
}
tick() {
}
}
module.exports = Shot;

View file

@ -11,6 +11,7 @@ class World {
this.physics = new Physics();
this.spawner = new Spawner(this);
this.bodies = new Set();
this.discharges = new Set();
this.asteroids = new Set();
this.copulae = new Set();
this.players = new Set();
@ -73,6 +74,11 @@ class World {
projectile.connect();
}
addDischarge(discharge) {
this.discharges.add(discharge);
this.room.broadcast('create', discharge.packFull());
}
addBody(body) {
this.bodies.add(body);
if (body.type == 'asteroid') this.asteroids.add(body);
@ -147,6 +153,11 @@ class World {
this.room.broadcast('destroy', copula.id);
}
removeDischarge(discharge) {
this.discharges.delete(discharge);
this.room.broadcast('destroy', discharge.id);
}
weld(bodyA, bodyB, point) {
this.physics.weld(bodyA, bodyB, point);
}
@ -162,18 +173,21 @@ class World {
tick() {
this.physics.step();
let tickBodies = (set, interval, canSleep) => {
let tickBodies = (set, interval, forceInterval) => {
set.forEach(body => {
if (this.tickCount % interval == 0 && body.awake)
let force = this.tickCount % forceInterval == 0;
if ((this.tickCount % interval == 0 && body.awake) || force)
body.applyDelta();
body.tick();
});
};
tickBodies(this.ships, 1);
tickBodies(this.asteroids, 1);
tickBodies(this.asteroids, 1, 100);
tickBodies(this.projectiles, 1);
this.discharges.forEach(d => d.tick());
if (Date.now() - this.tpsStart > 5000) {
this.tps = this.tpsCount / 5 | 0;
this.tpsCount = 0;

View file

@ -3,7 +3,7 @@
const Asteroid = require('./body/asteroid.js');
const Grapple = require('./body/projectile/grapple.js');
const Missile = require('./body/projectile/missile.js');
const Laser = require('./body/turret/shot/laser.js');
const Laser = require('./body/turret/discharge/laser.js');
class Spawner {
constructor(world) {
@ -35,38 +35,46 @@ class Spawner {
return missile;
}
spawnGrapple(ship, x, y) {
let sx = ship.center.x;
let sy = ship.center.y;
let dx = x - sx;
let dy = y - sy;
spawnGrapple(fixture, x, y) {
let fixturePos = fixture.body.getWorldPos(fixture.mount.pos);
let fx = fixturePos.x;
let fy = fixturePos.y;
let dx = x - fx;
let dy = y - fy;
let a = Math.atan2(dy, dx);
let pos = {
x: sx + Math.cos(a) * 1,
y: sy + Math.sin(a) * 1,
x: fx + Math.cos(a) * 1,
y: fy + Math.sin(a) * 1,
r: a,
xvel: ship.vel.x,
yvel: ship.vel.y
xvel: fixture.body.vel.x,
yvel: fixture.body.vel.y
};
let grapple = new Grapple(this.world, pos, ship);
let grapple = new Grapple(this.world, pos, fixture.body);
this.world.addProjectile(grapple);
return grapple;
}
spawnLaser(ship) {
let r = ship.pos.r;
let ox = Math.cos(r) * 0.7;
let oy = Math.sin(r) * 0.7;
let pos = {
x: ship.center.x + ox,
y: ship.center.y + oy,
r: r,
xvel: ship.vel.x,
yvel: ship.vel.y
spawnLaser(fixture) {
let r = fixture.angle;
let a = fixture.body.pos.r + fixture.angle;
let fixturePos = fixture.mount.pos;
let f = {
x: fixturePos.x + Math.cos(r) * 0.3,
y: fixturePos.y + Math.sin(r) * 0.3
};
let missile = new Missile(this.world, pos, ship);
this.world.addProjectile(missile);
return missile;
let vx = Math.cos(a) * 0.5;
let vy = Math.sin(a) * 0.5;
let spawnPos = fixture.body.getWorldPos(f);
let pos = {
x: spawnPos.x,
y: spawnPos.y,
r: r,
xvel: fixture.body.vel.x + vx,
yvel: fixture.body.vel.y + vy
};
let laser = new Laser(fixture, pos);
this.world.addDischarge(laser);
return laser;
}
}

View file

@ -19,21 +19,21 @@
],
"mounts": [
{
"pos": [18, 4],
"pos": [0.5625, 0.125],
"type": "fixed",
"size": 0,
"traversal": [0, 0],
"hidden": true
},
{
"pos": [18, 28],
"pos": [0.5625, 0.875],
"type": "fixed",
"size": 0,
"traversal": [0, 0],
"hidden": true
},
{
"pos": [6, 16],
"pos": [0.1875, 0.5],
"type": "fixed",
"size": 1,
"traversal": [3.14, 0],