recursively draw box contents inside of them.
This commit is contained in:
parent
b2b4d9dced
commit
204075465c
|
@ -11,45 +11,55 @@ const canvas = document.getElementById("canvas");
|
|||
const canvas_context = canvas.getContext("2d");
|
||||
graphics.set_size(canvas.width, canvas.height);
|
||||
|
||||
const SCALE = 81;
|
||||
const BASE_SCALE = 81;
|
||||
|
||||
export function draw_tile (x, y) {
|
||||
graphics.fill_rect(x * SCALE, y * SCALE, SCALE, SCALE);
|
||||
export function draw_tile (x, y, size = BASE_SCALE) {
|
||||
graphics.fill_rect(x, y, size, size);
|
||||
}
|
||||
|
||||
function checkerboard (x, y) {
|
||||
return ((x ^ y) & 1) === 0;
|
||||
}
|
||||
|
||||
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);
|
||||
export function draw_floor (start_x, start_y, scale) {
|
||||
iter_2d(range(0, world.BOX_SIZE - 1), (x, y) => {
|
||||
if (checkerboard(x, y)) {
|
||||
graphics.set_color("#aaa");
|
||||
} else {
|
||||
graphics.set_color("#888");
|
||||
}
|
||||
const [visual_x, visual_y] = project_pos(x, y, scale);
|
||||
draw_tile(start_x + visual_x, start_y + visual_y, scale);
|
||||
});
|
||||
}
|
||||
|
||||
export function draw_world (box) {
|
||||
draw_floor(world.BOX_SIZE);
|
||||
export function draw_world (box, start_x = 0, start_y = 0, scale = BASE_SCALE) {
|
||||
draw_floor(start_x, start_y, scale);
|
||||
|
||||
iter_2d(range(0, world.BOX_SIZE - 1), (x, y) => {
|
||||
const tile = world.get_tile(box, x, y);
|
||||
const [visual_x, visual_y] = project_pos(x, y, scale);
|
||||
|
||||
switch (tile.type) {
|
||||
case "box": {
|
||||
graphics.set_color("#ff0");
|
||||
draw_tile(x, y);
|
||||
draw_world(tile.box, start_x + visual_x, start_y + visual_y, scale / world.BOX_SIZE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
entity.for_each((entity, id) => {
|
||||
if (entity.box !== box) return;
|
||||
graphics.set_color(entity.color);
|
||||
draw_tile(entity.x, entity.y);
|
||||
const [visual_x, visual_y] = project_pos(entity.x, entity.y, scale);
|
||||
draw_tile(start_x + visual_x, start_y + visual_y, scale);
|
||||
});
|
||||
}
|
||||
|
||||
function project_pos (x, y, scale) {
|
||||
return [x, y].map(a => a * scale);
|
||||
}
|
||||
|
||||
export function render () {
|
||||
graphics.render(canvas_context);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue