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

0
TODO Normal file
View file

View file

Before

Width:  |  Height:  |  Size: 215 B

After

Width:  |  Height:  |  Size: 215 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 293 B

After

Width:  |  Height:  |  Size: 293 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 210 B

After

Width:  |  Height:  |  Size: 210 B

Before After
Before After

View file

@ -13,10 +13,11 @@ Game.prototype.loadAssets = _ => {
}, },
turrets: { turrets: {
'01': { '01': {
small: 'img/turrets/01/normal.png' '0': 'img/turrets/01/0.png'
}, },
'02': { '02': {
small: 'img/turrets/02/normal.png' '0': 'img/turrets/02/0.png',
'1': 'img/turrets/02/1.png'
} }
}, },
projectiles: { projectiles: {

View file

@ -13,12 +13,12 @@ GUI.prototype.Weapons = class {
this.element.innerHTML = ''; this.element.innerHTML = '';
ship.fixtures.forEach((fixture, i) => { ship.fixtures.forEach((fixture, i) => {
if (!fixture.type) return; if (!fixture.fixture) return;
let div = document.createElement('div'); let div = document.createElement('div');
div.classList.add('weapon'); div.classList.add('weapon');
if (ship.activeFixture == i) if (ship.activeFixture == i)
div.classList.add('active'); div.classList.add('active');
let img = `url(/img/turrets/${fixture.type}/normal.png)`; let img = `url(/img/turrets/${fixture.fixture}/normal.png)`;
div.style.backgroundImage = img; div.style.backgroundImage = img;
this.element.appendChild(div); this.element.appendChild(div);
}); });

View file

@ -17,7 +17,6 @@ class Net {
}); });
this.socket.on('update', data => { this.socket.on('update', data => {
window.q = data;
game.world.update(data); game.world.update(data);
}); });

View file

@ -15,7 +15,7 @@ class Player {
let packet = {}; let packet = {};
packet.thrust = ['w', 'a', 'd', 's'].map(k => +input.keys.held[k] || 0); packet.thrust = ['w', 'a', 'd', 's'].map(k => +input.keys.held[k] || 0);
packet.fire = [1, 3].map(k => +input.mouse.pressed[k] || 0); packet.fire = [1, 1, 3].map(k => +input.mouse.pressed[k] || 0);
packet.aim = [ packet.aim = [
+input.mouse.wx.toFixed(2), +input.mouse.wx.toFixed(2),
+input.mouse.wy.toFixed(2) +input.mouse.wy.toFixed(2)

View file

@ -9,18 +9,15 @@ class BodyRenderer {
let pos = body.pos; let pos = body.pos;
let x = pos.x * SCALE; let x = pos.x * SCALE;
let y = pos.y * SCALE; let y = pos.y * SCALE;
let vx = -game.world.center.x;
let vy = -game.world.center.y;
let pallet = this.pallet; let pallet = this.pallet;
let context = pallet.context; let context = pallet.context;
pallet.view(vx, vy, false, 0);
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.fixture || !f.hidden) continue; if (!f.fixture || !f.hidden) continue;
let img = game.assets.images.turrets[f.fixture].small; let img = game.assets.images.turrets[f.fixture][f.state];
this.pallet.image(img, f.x - 32, f.y - 32, f.angle); this.pallet.image(img, f.x - 32, f.y - 32, f.angle);
} }
@ -36,12 +33,11 @@ class BodyRenderer {
for (let f of body.fixtures) { for (let f of body.fixtures) {
if (!f.fixture || f.hidden) continue; if (!f.fixture || f.hidden) continue;
let img = game.assets.images.turrets[f.fixture].small; let img = game.assets.images.turrets[f.fixture][f.state];
this.pallet.image(img, f.x - 32, f.y - 32, f.angle); this.pallet.image(img, f.x - 32, f.y - 32, f.angle);
} }
pallet.restore(); pallet.restore();
pallet.restore();
} }

View file

@ -0,0 +1,21 @@
class DischargeRenderer {
constructor(renderer) {
this.pallet = renderer.pallet;
this.canvas = this.pallet.canvas;
this.context = this.pallet.context;
}
render(discharge) {
let x = discharge.x * SCALE;
let y = discharge.y * SCALE;
let pallet = this.pallet;
let context = this.context;
pallet.view(x, y, false, discharge.r);
pallet.square('#f00', 0, 0, 2);
pallet.restore();
}
}

View file

@ -16,6 +16,7 @@ class Renderer {
window.addEventListener('resize', _ => pallet.fillScreen(1000, 600)); window.addEventListener('resize', _ => pallet.fillScreen(1000, 600));
this.bodyRenderer = new BodyRenderer(this); this.bodyRenderer = new BodyRenderer(this);
this.dischargeRenderer = new DischargeRenderer(this);
} }
render(state) { render(state) {
@ -53,10 +54,20 @@ class Renderer {
this.renderGrid(); this.renderGrid();
let vx = -game.world.center.x;
let vy = -game.world.center.y;
this.pallet.view(vx, vy, false, 0);
for (var id in game.world.bodies) { for (var id in game.world.bodies) {
this.bodyRenderer.render(game.world.bodies[id]); this.bodyRenderer.render(game.world.bodies[id]);
} }
for (var id in game.world.discharges) {
this.dischargeRenderer.render(game.world.discharges[id]);
}
this.pallet.restore();
this.effects.forEach(effect => { this.effects.forEach(effect => {
effect.render(); effect.render();
}); });
@ -76,8 +87,10 @@ class Renderer {
let cw = this.canvas.width; let cw = this.canvas.width;
let ch = this.canvas.height; let ch = this.canvas.height;
var gridx = cx % 50; let gridx = cx % 50;
var gridy = cy % 50; let gridy = cy % 50;
let lastBlue = false;
this.pallet.opacity(0.05); this.pallet.opacity(0.05);
for (var x = gridx - cw / 2 - 50; x < cw + 50; x += 50) { for (var x = gridx - cw / 2 - 50; x < cw + 50; x += 50) {
for (var y = gridy - ch / 2 - 50; y < ch + 50; y += 50) { for (var y = gridy - ch / 2 - 50; y < ch + 50; y += 50) {
@ -85,10 +98,18 @@ class Renderer {
var wy = ((-cy + y) / SCALE) | 0; var wy = ((-cy + y) / SCALE) | 0;
var b = game.world.bounds; var b = game.world.bounds;
if (wx > b.right || wx < b.left || wy > b.bottom || wy < b.top) { if (wx > b.right || wx < b.left || wy > b.bottom || wy < b.top) {
this.pallet.opacity(0.2); if (!lastBlue) {
this.pallet.opacity(0.2);
lastBlue = true;
}
this.pallet.outline('#8af', x, y, 51, 51, 1); this.pallet.outline('#8af', x, y, 51, 51, 1);
this.pallet.opacity(0.05); } else {
} else this.pallet.outline('#fff', x, y, 51, 51, 1); if (lastBlue) {
this.pallet.opacity(0.05);
lastBlue = false;
}
this.pallet.outline('#fff', x, y, 51, 51, 1);
}
} }
} }
this.pallet.opacity(1); this.pallet.opacity(1);

View file

@ -9,6 +9,11 @@ class Body {
this.id = data.id this.id = data.id
this.frame = data.frame; this.frame = data.frame;
this.fixtures = data.fixtures; this.fixtures = data.fixtures;
this.fixtures = this.fixtures.map(f => {
f.x *= 32;
f.y *= 32;
return f;
});
this.b2body = false; this.b2body = false;
this.updated = 0; this.updated = 0;
this.bodyClass = data.class; this.bodyClass = data.class;
@ -49,7 +54,7 @@ class Body {
this.fixtures.forEach(fixture => { this.fixtures.forEach(fixture => {
let obj = {}; let obj = {};
this.interface.fixtures.order.forEach(v => obj[v] = data.shift()); this.interface.fixtures.order.forEach(v => obj[v] = data.shift());
fixture.angle = obj.angle; fixture.angle = obj.angle;
fixture.state = obj.state; fixture.state = obj.state;
}); });

View file

@ -0,0 +1,51 @@
//@10
class Discharge {
constructor(data) {
this.interface = {
order: ['x', 'y'],
size: 2
};
this.xvel = data.xvel;
this.yvel = data.yvel;
this.r = data.r;
this.id = data.id;
this.updated = 0;
this.update(data.delta.slice(1));
}
getPos() {
return this.pos;
}
applyForce(x, y) {
var b = this.b2body;
b.ApplyForce(new b2Vec2(x, y), b.GetWorldCenter());
}
applyTorque(f) {
this.b2body.ApplyTorque(f);
}
update(data) {
let values = {};
console.log(data);
this.interface.order.forEach(v => values[v] = data.shift());
this.x = values.x;
this.y = values.y;
this.updated = 10;
}
tick() {
this.x += this.xvel;
this.y += this.yvel;
}
get pos() {
return {
x: this.x,
y: this.y
};
}
}

View file

@ -3,6 +3,7 @@ const SCALE = 32;
class World { class World {
constructor() { constructor() {
this.bodies = {}; this.bodies = {};
this.discharges = {};
this.playerShip = false; this.playerShip = false;
this.physics = new Physics(); this.physics = new Physics();
@ -15,6 +16,15 @@ class World {
} }
add(data) { add(data) {
if(data.form != 'body') console.log(data);
if (data.form == 'body') {
this.addBody(data);
} else if (data.form == 'discharge') {
this.addDischarge(data);
}
};
addBody(data) {
var body; var body;
if (data.type == 'asteroid') body = new Asteroid(data); if (data.type == 'asteroid') body = new Asteroid(data);
if (data.type == 'ship') body = new Ship(data); if (data.type == 'ship') body = new Ship(data);
@ -24,11 +34,18 @@ class World {
this.bodies[body.id] = body; this.bodies[body.id] = body;
this.physics.createBody(body); this.physics.createBody(body);
}; }
addDischarge(data) {
var discharge;
discharge = new Discharge(data);
this.discharges[discharge.id] = discharge;
}
remove(id) { remove(id) {
this.physics.removeBody(this.bodies[id]); this.physics.removeBody(this.bodies[id]);
delete this.bodies[id]; delete this.bodies[id];
delete this.discharges[id];
}; };
clear() { clear() {
@ -43,13 +60,14 @@ class World {
while (i < data.length) { while (i < data.length) {
let id = data[i++]; let id = data[i++];
let body = this.bodies[id]; let body = this.bodies[id];
let discharge = this.discharges[id];
if (!body) { if (body) {
game.net.send('requestBodyData', id); body.update(data.slice(i, i + body.interface.size));
return; } else if (discharge) {
discharge.update(data.slice(i, i + 2));
} }
body.update(data.slice(i, i + body.interface.size));
i += body.interface.size; i += body.interface.size;
} }
}; };
@ -66,6 +84,10 @@ class World {
for (var i in this.bodies) { for (var i in this.bodies) {
this.bodies[i].tick(); this.bodies[i].tick();
} }
for (var i in this.discharges) {
this.discharges[i].tick();
}
}; };
get center() { get center() {

View file

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

View file

@ -58,26 +58,12 @@ class Ship extends Body {
this.thrust.right = packet.thrust[2]; 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) : 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() { launchMissile() {
this.world.spawner.spawnMissile(this); this.world.spawner.spawnMissile(this);
} }
launchGrapple(x, y) {
this.grapple = this.world.spawner.spawnGrapple(this, x, y);
}
tickType() { tickType() {
if (this.thrust.forward) { if (this.thrust.forward) {
let power = this.power.forward; let power = this.power.forward;

View file

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

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._angle = mount.traversal ? mount.traversal.cw : 0;
this.mount = mount; this.mount = mount;
this.body = this.mount.body;
} }
destruct() { destruct() {

View file

@ -5,10 +5,20 @@ const Fixture = require('./fixture.js');
class Grapple extends Fixture { class Grapple extends Fixture {
constructor(mount, data) { constructor(mount, data) {
super(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'); const traits = require('../../traits/fixtures.json');
class Mount { class Mount {
constructor(ship, data, fixture) { constructor(body, data, fixture) {
//this.ship = ship; this.body = body;
this.type = data.type || 'turret'; this.type = data.type || 'turret';
this.size = data.size || 0; this.size = data.size || 0;
this.hidden = data.hidden || 'false'; this.hidden = data.hidden || 'false';
this.position = { this.pos = {
x: data.pos[0], x: data.pos[0],
y: data.pos[1] y: data.pos[1]
} }
@ -63,8 +63,8 @@ class Mount {
packFull() { packFull() {
return { return {
x: this.position.x, x: this.pos.x,
y: this.position.y, y: this.pos.y,
hidden: this.hidden, hidden: this.hidden,
fixture: this.fixture ? this.fixture.id : 0 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.physics = new Physics();
this.spawner = new Spawner(this); this.spawner = new Spawner(this);
this.bodies = new Set(); this.bodies = new Set();
this.discharges = new Set();
this.asteroids = new Set(); this.asteroids = new Set();
this.copulae = new Set(); this.copulae = new Set();
this.players = new Set(); this.players = new Set();
@ -73,6 +74,11 @@ class World {
projectile.connect(); projectile.connect();
} }
addDischarge(discharge) {
this.discharges.add(discharge);
this.room.broadcast('create', discharge.packFull());
}
addBody(body) { addBody(body) {
this.bodies.add(body); this.bodies.add(body);
if (body.type == 'asteroid') this.asteroids.add(body); if (body.type == 'asteroid') this.asteroids.add(body);
@ -147,6 +153,11 @@ class World {
this.room.broadcast('destroy', copula.id); this.room.broadcast('destroy', copula.id);
} }
removeDischarge(discharge) {
this.discharges.delete(discharge);
this.room.broadcast('destroy', discharge.id);
}
weld(bodyA, bodyB, point) { weld(bodyA, bodyB, point) {
this.physics.weld(bodyA, bodyB, point); this.physics.weld(bodyA, bodyB, point);
} }
@ -162,18 +173,21 @@ class World {
tick() { tick() {
this.physics.step(); this.physics.step();
let tickBodies = (set, interval, canSleep) => { let tickBodies = (set, interval, forceInterval) => {
set.forEach(body => { 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.applyDelta();
body.tick(); body.tick();
}); });
}; };
tickBodies(this.ships, 1); tickBodies(this.ships, 1);
tickBodies(this.asteroids, 1); tickBodies(this.asteroids, 1, 100);
tickBodies(this.projectiles, 1); tickBodies(this.projectiles, 1);
this.discharges.forEach(d => d.tick());
if (Date.now() - this.tpsStart > 5000) { if (Date.now() - this.tpsStart > 5000) {
this.tps = this.tpsCount / 5 | 0; this.tps = this.tpsCount / 5 | 0;
this.tpsCount = 0; this.tpsCount = 0;

View file

@ -3,7 +3,7 @@
const Asteroid = require('./body/asteroid.js'); const Asteroid = require('./body/asteroid.js');
const Grapple = require('./body/projectile/grapple.js'); const Grapple = require('./body/projectile/grapple.js');
const Missile = require('./body/projectile/missile.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 { class Spawner {
constructor(world) { constructor(world) {
@ -35,38 +35,46 @@ class Spawner {
return missile; return missile;
} }
spawnGrapple(ship, x, y) { spawnGrapple(fixture, x, y) {
let sx = ship.center.x; let fixturePos = fixture.body.getWorldPos(fixture.mount.pos);
let sy = ship.center.y; let fx = fixturePos.x;
let dx = x - sx; let fy = fixturePos.y;
let dy = y - sy; let dx = x - fx;
let dy = y - fy;
let a = Math.atan2(dy, dx); let a = Math.atan2(dy, dx);
let pos = { let pos = {
x: sx + Math.cos(a) * 1, x: fx + Math.cos(a) * 1,
y: sy + Math.sin(a) * 1, y: fy + Math.sin(a) * 1,
r: a, r: a,
xvel: ship.vel.x, xvel: fixture.body.vel.x,
yvel: ship.vel.y 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); this.world.addProjectile(grapple);
return grapple; return grapple;
} }
spawnLaser(ship) { spawnLaser(fixture) {
let r = ship.pos.r; let r = fixture.angle;
let ox = Math.cos(r) * 0.7; let a = fixture.body.pos.r + fixture.angle;
let oy = Math.sin(r) * 0.7; let fixturePos = fixture.mount.pos;
let pos = { let f = {
x: ship.center.x + ox, x: fixturePos.x + Math.cos(r) * 0.3,
y: ship.center.y + oy, y: fixturePos.y + Math.sin(r) * 0.3
r: r,
xvel: ship.vel.x,
yvel: ship.vel.y
}; };
let missile = new Missile(this.world, pos, ship); let vx = Math.cos(a) * 0.5;
this.world.addProjectile(missile); let vy = Math.sin(a) * 0.5;
return missile; 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": [ "mounts": [
{ {
"pos": [18, 4], "pos": [0.5625, 0.125],
"type": "fixed", "type": "fixed",
"size": 0, "size": 0,
"traversal": [0, 0], "traversal": [0, 0],
"hidden": true "hidden": true
}, },
{ {
"pos": [18, 28], "pos": [0.5625, 0.875],
"type": "fixed", "type": "fixed",
"size": 0, "size": 0,
"traversal": [0, 0], "traversal": [0, 0],
"hidden": true "hidden": true
}, },
{ {
"pos": [6, 16], "pos": [0.1875, 0.5],
"type": "fixed", "type": "fixed",
"size": 1, "size": 1,
"traversal": [3.14, 0], "traversal": [3.14, 0],