refactor, simplify common access names

This commit is contained in:
Matthieu Jolimaitre 2024-04-10 03:15:01 +02:00
parent f277a71087
commit 55a5adcd92
5 changed files with 30 additions and 20 deletions

View file

@ -5,24 +5,24 @@ import { CompDisplay } from "./display.ts";
const log = log_from(import.meta);
export class CompPos {
position;
pos;
entity;
constructor(entity: Entity, position: Vec2) {
this.position = position;
this.pos = position;
this.entity = entity;
}
move(displacement: Vec2) {
this.position = this.position.add(displacement);
this.pos = this.pos.add(displacement);
}
move_collide(engine: Engine, displacement: Vec2) {
let result = false;
for (const step of displacement_steps(this.position, displacement)) {
const collider_exists = engine.query_one(query_at(step)) !== null;
for (const step of displacement_steps(this.pos, displacement)) {
const collider_exists = engine.one(query_at(step)) !== null;
if (collider_exists) return result;
this.position = step;
this.pos = step;
result = true;
}
return result;
@ -61,11 +61,11 @@ Deno.test("test_displacement", () => {
});
export function query_at(pos: Vec2) {
return Query.with(CompPos).filter(([c]) => c.position.overlaps(pos));
return Query.with(CompPos).filter(([c]) => c.pos.overlaps(pos));
}
export function query_in_rect(min: Vec2, max: Vec2) {
return Query.with(CompPos).filter(([c]) => c.position.inside(min, max));
return Query.with(CompPos).filter(([c]) => c.pos.inside(min, max));
}
class CompStructure {}
@ -100,7 +100,7 @@ export async function sys_spawn_structure(file_path: string, origin: Vec2) {
export function sys_find_free_pos(target: Vec2) {
return (engine: Engine) => {
for (const pos of spiral(target)) {
const found = engine.query_one(query_at(pos));
const found = engine.one(query_at(pos));
if (found === null) return pos;
}
throw new Error("Unreachable.");