refactor, simplify common access names
This commit is contained in:
parent
f277a71087
commit
55a5adcd92
5 changed files with 30 additions and 20 deletions
|
@ -16,8 +16,8 @@ export function sys_render_world(center: Vec2, size: Vec2) {
|
|||
const result = Array.from(range(0, size.y())).map(() => Array.from(range(0, size.x())).map(() => " "));
|
||||
const min = center.sub(radius);
|
||||
const max = center.add(radius).sub(v2(1, 1));
|
||||
for (const [pos, display] of engine.query_all(query_in_rect(min, max).with(CompDisplay))) {
|
||||
const local_pos = pos.position.sub(min);
|
||||
for (const [pos, display] of engine.all(query_in_rect(min, max).with(CompDisplay))) {
|
||||
const local_pos = pos.pos.sub(min);
|
||||
result[local_pos.y()][local_pos.x()] = display.display;
|
||||
}
|
||||
return result.map((line) => line.join("")).toReversed().join("\n");
|
||||
|
|
|
@ -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.");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue