improcket/js/graphics/world.mjs
2018-03-06 10:35:54 +00:00

69 lines
1.7 KiB
JavaScript

import {canvas, context} from './index.mjs';
import * as graphics from './index.mjs';
import {images as assets} from '../assets.mjs';
import * as world from '../world/index.mjs';
import {state} from '../game/index.mjs';
export function render() {
world.particles.forEach(renderParticle);
world.celestials.forEach(renderCelestial);
if (graphics.trace) world.tracers.forEach(renderTracer);
world.ships.forEach(renderShip);
world.entities.forEach(renderEntity);
}
function renderParticle(particle) {
context.fillStyle = particle.color;
context.fillRect(...particle.com, particle.size, particle.size);
}
function renderEntity(entity) {
context.save();
context.translate(...entity.com);
context.rotate(entity.r);
context.drawImage(entity.image, -0.5, -0.5, 1, 1);
context.restore();
}
function renderShip(ship) {
context.save();
context.translate(...ship.com);
context.rotate(ship.r);
let [cx, cy] = ship.localCom;
context.translate(-cx, -cy);
ship.modules.forEach(m => {
let [mx, my] = [m.x, m.y];
if (state.editing) {
}
context.drawImage(m.currentImage, m.x, m.y, 1, 1);
});
context.restore();
}
const celestialImages = {
green: Object.values(assets.celestials.green)
}
function renderCelestial(cel) {
context.drawImage(cel.image, cel.x, cel.y,
cel.diameter, cel.diameter);
}
function renderTracer(tracer) {
context.lineWidth = 0.1;
context.strokeStyle = '#fff';
context.beginPath();
context.moveTo(...tracer.pos);
let path = tracer.path;
for (let i = 0; i < path.length; i++) {
context.lineTo(...path[i]);
if (i % 5 === 0 || i == path.length - 1) {
context.stroke();
context.globalAlpha = (1 - (i / path.length)) * 0.5;
}
}
context.globalAlpha = 1;
}