179 lines
3.4 KiB
JavaScript
179 lines
3.4 KiB
JavaScript
import { assert } from "./test.mjs";
|
|
|
|
|
|
|
|
let next_id = 0;
|
|
|
|
export function create (x = 0, y = 0, width = 0, height = 0, data = {}) {
|
|
return {
|
|
id: next_id++,
|
|
pos: {
|
|
x: x,
|
|
y: y,
|
|
},
|
|
size: {
|
|
x: width,
|
|
y: height,
|
|
},
|
|
visible: data.hasOwnProperty("visible") ? data.visible : true,
|
|
};
|
|
}
|
|
|
|
export function get_pos (entity) {
|
|
return entity.pos;
|
|
}
|
|
|
|
export function set_pos (entity, x, y) {
|
|
entity.pos.x = x;
|
|
entity.pos.y = y;
|
|
}
|
|
|
|
export function get_size (entity) {
|
|
return entity.size;
|
|
}
|
|
|
|
export function set_size (entity, x, y) {
|
|
entity.size.x = x;
|
|
entity.size.y = y;
|
|
}
|
|
|
|
export function move (entity, x, y) {
|
|
entity.pos.x += x;
|
|
entity.pos.y += y;
|
|
}
|
|
|
|
export function set_visible (entity, state) {
|
|
entity.visible = state;
|
|
}
|
|
|
|
export function is_visible (entity) {
|
|
return entity.visible;
|
|
}
|
|
|
|
|
|
|
|
const metadata = new Map();
|
|
|
|
export function get_meta (entity) {
|
|
if (!metadata.has(entity.id)) {
|
|
metadata.set(entity.id, new Map());
|
|
}
|
|
return metadata.get(entity.id);
|
|
}
|
|
|
|
|
|
|
|
function intersects_1D (pos1, size1, pos2, size2) {
|
|
return pos1 + size1 > pos2
|
|
&& pos2 + size2 > pos1
|
|
}
|
|
|
|
export function touches (a, b) {
|
|
const pos_a = get_pos(a);
|
|
const size_a = get_size(a);
|
|
const pos_b = get_pos(b);
|
|
const size_b = get_size(b);
|
|
|
|
if (!intersects_1D(pos_a.x, size_a.x, pos_b.x, size_b.x)) return false;
|
|
if (!intersects_1D(pos_a.y, size_a.y, pos_b.y, size_b.y)) return false;
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
// TESTS
|
|
|
|
assert("entity `get_pos` works.", _ => {
|
|
const e = create(1, 2);
|
|
const {x, y} = get_pos(e);
|
|
return(x === 1 && y === 2);
|
|
});
|
|
|
|
assert("entity `create` works.", _ => {
|
|
const e = create(1, 2, 3, 4);
|
|
|
|
const pos = get_pos(e);
|
|
if (pos.x !== 1 || pos.y !== 2) return false;
|
|
|
|
const size = get_size(e);
|
|
if (size.x !== 3 || size.y !== 4) return false;
|
|
|
|
return true;
|
|
});
|
|
|
|
assert("entity `set_pos` works.", _ => {
|
|
const e = create();
|
|
set_pos(e, 5, 7);
|
|
const {x, y} = get_pos(e);
|
|
return(x === 5 && y === 7);
|
|
});
|
|
|
|
assert("entity `get_size` works.", _ => {
|
|
const e = create(2, 4, 6, 8);
|
|
const {x, y} = get_size(e);
|
|
return(x === 6 && y === 8);
|
|
});
|
|
|
|
assert("entity `set_size` works.", _ => {
|
|
const e = create(2, 4, 6, 8);
|
|
set_size(e, 3, 2);
|
|
const {x, y} = get_size(e);
|
|
return(x === 3 && y === 2);
|
|
});
|
|
|
|
assert("entity `move` works.", _ => {
|
|
const e = create(0, 0);
|
|
move(e, 50, 25);
|
|
const {x, y} = get_pos(e);
|
|
return(x === 50 && y === 25);
|
|
});
|
|
|
|
assert("entity `set_visible` works.", _ => {
|
|
const e = create();
|
|
set_visible(e, false);
|
|
if (e.visible) return false;
|
|
set_visible(e, true);
|
|
return e.visible;
|
|
});
|
|
|
|
assert("entity `is_visible` works.", _ => {
|
|
const e = create();
|
|
set_visible(e, false);
|
|
if (is_visible(e) !== false) return false;
|
|
set_visible(e, true);
|
|
return e.visible === true;
|
|
});
|
|
|
|
assert("creating entities with preset visibility works.", _ => {
|
|
const e1 = create(0, 0, 0, 0, {
|
|
visible: false,
|
|
});
|
|
if (is_visible(e1)) return false;
|
|
const e2 = create(0, 0, 0, 0, {
|
|
visible: true,
|
|
});
|
|
return is_visible(e2);
|
|
});
|
|
|
|
assert("entity `get_meta` works.", _ => {
|
|
const e = create();
|
|
const meta = get_meta(e);
|
|
meta.set("test", "gay");
|
|
return get_meta(e).get("test") === "gay";
|
|
});
|
|
|
|
assert("entity `touches` works.", _ => {
|
|
const a = create(0, 0, 10, 10);
|
|
const b = create(5, 5, 1, 1);
|
|
|
|
if (!touches(a, b)) return false;
|
|
if (!touches(b, a)) return false;
|
|
|
|
const c = create(-1, -1, 2, 2);
|
|
|
|
if (!touches(a, c)) return false;
|
|
if (!touches(c, a)) return false;
|
|
if (touches(b, c)) return false;
|
|
|
|
return true;
|
|
});
|