2023-06-23 09:37:25 +00:00
|
|
|
import * as graphics from "./graphics_core.mjs";
|
|
|
|
export * from "./graphics_core.mjs";
|
2023-06-23 09:00:12 +00:00
|
|
|
|
2023-06-23 09:37:25 +00:00
|
|
|
import { range, iter_2d } from "./utils/range.mjs";
|
2023-06-24 07:40:35 +00:00
|
|
|
import * as entity from "./entity.mjs";
|
2023-06-24 08:03:32 +00:00
|
|
|
import * as world from "./world.mjs";
|
2023-06-23 09:37:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const canvas = document.getElementById("canvas");
|
|
|
|
const canvas_context = canvas.getContext("2d");
|
|
|
|
graphics.set_size(canvas.width, canvas.height);
|
|
|
|
|
2023-06-24 07:40:35 +00:00
|
|
|
const SCALE = 81;
|
2023-06-23 09:37:25 +00:00
|
|
|
|
|
|
|
export function draw_tile (x, y) {
|
|
|
|
graphics.fill_rect(x * SCALE, y * SCALE, SCALE, SCALE);
|
2023-06-23 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 09:37:25 +00:00
|
|
|
function checkerboard (x, y) {
|
|
|
|
return ((x ^ y) & 1) === 0;
|
2023-06-23 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 09:37:25 +00:00
|
|
|
export function draw_floor (box_size) {
|
|
|
|
graphics.clear("#888");
|
|
|
|
graphics.set_color("#aaa");
|
|
|
|
iter_2d(range(0, box_size - 1), (x, y) => {
|
|
|
|
if (checkerboard(x, y)) draw_tile(x, y);
|
|
|
|
});
|
2023-06-23 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 08:03:32 +00:00
|
|
|
export function draw_world (box) {
|
|
|
|
draw_floor(world.BOX_SIZE);
|
2023-06-23 09:37:25 +00:00
|
|
|
|
2023-06-24 08:03:32 +00:00
|
|
|
iter_2d(range(0, world.BOX_SIZE - 1), (x, y) => {
|
|
|
|
const tile = world.get_tile(box, x, y);
|
2023-06-23 10:05:40 +00:00
|
|
|
|
|
|
|
switch (tile.type) {
|
|
|
|
case "box": {
|
|
|
|
graphics.set_color("#ff0");
|
|
|
|
draw_tile(x, y);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-23 09:37:25 +00:00
|
|
|
}
|
|
|
|
});
|
2023-06-24 07:40:35 +00:00
|
|
|
|
|
|
|
entity.for_each((entity, id) => {
|
|
|
|
graphics.set_color(entity.color);
|
|
|
|
draw_tile(entity.x, entity.y);
|
|
|
|
});
|
2023-06-23 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 09:37:25 +00:00
|
|
|
export function render () {
|
|
|
|
graphics.render(canvas_context);
|
2023-06-23 09:00:12 +00:00
|
|
|
}
|