precision-game/js/graphics.mjs
2023-10-25 20:10:32 +02:00

43 lines
837 B
JavaScript

import { assert } from "./test.mjs";
import { player } from "./player.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();
draw_entity(player, "#0f0");
for (const e of enemy.get_all()) {
draw_entity(e, "#f00");
}
}
render();