precision-game/js/graphics.mjs
2023-10-27 12:54:38 +02:00

45 lines
926 B
JavaScript

import { assert } from "./test.mjs";
import { get_player } from "./player.mjs";
import { get_goal } from "./goal.mjs";
import * as enemy from "./enemy.mjs";
import { get_pos, get_size, is_visible } from "./entity.mjs";
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
function clear (color = "#000") {
ctx.fillStyle = color;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function fill_rect (x, y, w, h, color = "#fff") {
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
}
function draw_entity (entity, color) {
if (!is_visible(entity)) return false;
let pos = get_pos(entity);
let size = get_size(entity);
fill_rect(pos.x, pos.y, size.x, size.y, color);
return true;
}
export function render () {
clear("#111");
draw_entity(get_goal(), "#880");
draw_entity(get_player(), "#0f0");
for (const e of enemy.get_all()) {
draw_entity(e, "#f00");
}
}
render();