2023-06-24 07:40:35 +00:00
|
|
|
import * as world from "./world.mjs";
|
2023-06-24 07:52:53 +00:00
|
|
|
const BOX_SIZE = world.BOX_SIZE;
|
2023-06-24 07:40:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const entities = [];
|
|
|
|
|
2023-06-24 07:52:53 +00:00
|
|
|
export function create (box, x = 0, y = 0, color = "#fff") {
|
2023-06-24 07:40:35 +00:00
|
|
|
const entity = {
|
|
|
|
id: entities.length,
|
2023-06-24 07:52:53 +00:00
|
|
|
box,
|
2023-06-24 07:40:35 +00:00
|
|
|
x,
|
|
|
|
y,
|
|
|
|
color,
|
|
|
|
};
|
|
|
|
entities.push(entity);
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function destroy (id) {
|
|
|
|
// returns the destroyed entity, idk if that makes sense.
|
|
|
|
return entities.splice(id, 1)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function for_each (callback) {
|
|
|
|
entities.forEach(entity => {
|
|
|
|
callback(entity, entity.id);
|
|
|
|
});
|
|
|
|
}
|
2023-06-24 07:52:53 +00:00
|
|
|
|
|
|
|
function clamp_pos (entity) {
|
|
|
|
if (entity.x < 0) entity.x = 0;
|
|
|
|
if (entity.x >= BOX_SIZE) entity.x = BOX_SIZE - 1;
|
|
|
|
if (entity.y < 0) entity.y = 0;
|
|
|
|
if (entity.y >= BOX_SIZE) entity.y = BOX_SIZE - 1;
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function set_pos (entity, x, y) {
|
|
|
|
entity.x = x;
|
|
|
|
entity.y = y;
|
|
|
|
|
|
|
|
if (out_of_bounds(entity)) {
|
2023-06-24 08:08:37 +00:00
|
|
|
if (entity.box.parent) {
|
|
|
|
entity.box = entity.box.parent;
|
|
|
|
// TODO: exit from side of box.
|
2023-06-24 07:52:53 +00:00
|
|
|
}
|
2023-06-24 08:08:37 +00:00
|
|
|
clamp_pos(entity);
|
2023-06-24 07:52:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 08:03:32 +00:00
|
|
|
if (world.get_tile(entity.box, entity.x, entity.y)?.type === "box") {
|
2023-06-24 08:08:37 +00:00
|
|
|
entity.box = world.get_tile(entity.box, entity.x, entity.y).box;
|
|
|
|
// TODO: enter at edge of box.
|
2023-06-24 07:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function move (entity, d_x, d_y) {
|
|
|
|
set_pos(entity, entity.x + d_x, entity.y + d_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
function out_of_bounds (entity) {
|
|
|
|
const { x, y } = entity;
|
|
|
|
return x < 0 || x >= BOX_SIZE ||
|
|
|
|
y < 0 || y >= BOX_SIZE
|
|
|
|
}
|