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

@ -9,18 +9,15 @@ class BodyRenderer {
let pos = body.pos;
let x = pos.x * SCALE;
let y = pos.y * SCALE;
let vx = -game.world.center.x;
let vy = -game.world.center.y;
let pallet = this.pallet;
let context = pallet.context;
pallet.view(vx, vy, false, 0);
pallet.view(x, y, false, pos.r);
for (let f of body.fixtures) {
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);
}
@ -36,12 +33,11 @@ class BodyRenderer {
for (let f of body.fixtures) {
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);
}
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));
this.bodyRenderer = new BodyRenderer(this);
this.dischargeRenderer = new DischargeRenderer(this);
}
render(state) {
@ -53,10 +54,20 @@ class Renderer {
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) {
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 => {
effect.render();
});
@ -76,8 +87,10 @@ class Renderer {
let cw = this.canvas.width;
let ch = this.canvas.height;
var gridx = cx % 50;
var gridy = cy % 50;
let gridx = cx % 50;
let gridy = cy % 50;
let lastBlue = false;
this.pallet.opacity(0.05);
for (var x = gridx - cw / 2 - 50; x < cw + 50; x += 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 b = game.world.bounds;
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.opacity(0.05);
} else this.pallet.outline('#fff', x, y, 51, 51, 1);
} else {
if (lastBlue) {
this.pallet.opacity(0.05);
lastBlue = false;
}
this.pallet.outline('#fff', x, y, 51, 51, 1);
}
}
}
this.pallet.opacity(1);