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

@ -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 result = Array.from(range(0, size.y())).map(() => Array.from(range(0, size.x())).map(() => " "));
const min = center.sub(radius); const min = center.sub(radius);
const max = center.add(radius).sub(v2(1, 1)); const max = center.add(radius).sub(v2(1, 1));
for (const [pos, display] of engine.query_all(query_in_rect(min, max).with(CompDisplay))) { for (const [pos, display] of engine.all(query_in_rect(min, max).with(CompDisplay))) {
const local_pos = pos.position.sub(min); const local_pos = pos.pos.sub(min);
result[local_pos.y()][local_pos.x()] = display.display; result[local_pos.y()][local_pos.x()] = display.display;
} }
return result.map((line) => line.join("")).toReversed().join("\n"); return result.map((line) => line.join("")).toReversed().join("\n");

View file

@ -5,24 +5,24 @@ import { CompDisplay } from "./display.ts";
const log = log_from(import.meta); const log = log_from(import.meta);
export class CompPos { export class CompPos {
position; pos;
entity; entity;
constructor(entity: Entity, position: Vec2) { constructor(entity: Entity, position: Vec2) {
this.position = position; this.pos = position;
this.entity = entity; this.entity = entity;
} }
move(displacement: Vec2) { move(displacement: Vec2) {
this.position = this.position.add(displacement); this.pos = this.pos.add(displacement);
} }
move_collide(engine: Engine, displacement: Vec2) { move_collide(engine: Engine, displacement: Vec2) {
let result = false; let result = false;
for (const step of displacement_steps(this.position, displacement)) { for (const step of displacement_steps(this.pos, displacement)) {
const collider_exists = engine.query_one(query_at(step)) !== null; const collider_exists = engine.one(query_at(step)) !== null;
if (collider_exists) return result; if (collider_exists) return result;
this.position = step; this.pos = step;
result = true; result = true;
} }
return result; return result;
@ -61,11 +61,11 @@ Deno.test("test_displacement", () => {
}); });
export function query_at(pos: Vec2) { 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) { 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 {} 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) { export function sys_find_free_pos(target: Vec2) {
return (engine: Engine) => { return (engine: Engine) => {
for (const pos of spiral(target)) { 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; if (found === null) return pos;
} }
throw new Error("Unreachable."); throw new Error("Unreachable.");

View file

@ -2,12 +2,10 @@ import { Awaitable, ClassMap, Constructible, first, log_from, Prototyped, wait }
const log = log_from(import.meta); const log = log_from(import.meta);
export class Entity { export class Entity {
identifier;
components; components;
engine; engine;
constructor(identifier: number, engine: Engine) { constructor(engine: Engine) {
this.identifier = identifier;
this.engine = engine; this.engine = engine;
this.components = new ClassMap(); this.components = new ClassMap();
} }
@ -28,6 +26,13 @@ export class Entity {
} }
} }
export class CompId {
public readonly id;
constructor(id: number) {
this.id = id;
}
}
export class Engine { export class Engine {
next_identifier; next_identifier;
entities; entities;
@ -43,7 +48,8 @@ export class Engine {
spawn(op: (entity: Entity) => unknown) { spawn(op: (entity: Entity) => unknown) {
const identifier = this.next_identifier++; const identifier = this.next_identifier++;
const entity = new Entity(identifier, this); const entity = new Entity(this);
entity.insert(new CompId(identifier));
op(entity); op(entity);
this.entities.set(identifier, entity); this.entities.set(identifier, entity);
return entity; return entity;
@ -54,18 +60,19 @@ export class Engine {
} }
async global_system_loop(sys: (engine: Engine) => Awaitable, interval: number) { async global_system_loop(sys: (engine: Engine) => Awaitable, interval: number) {
// TODO : Wait for start.
while (true) { while (true) {
await sys(this); await sys(this);
await wait(interval); await wait(interval);
} }
} }
*query_all<O extends QueryRes>(query: Query<O>) { *all<O extends QueryRes>(query: Query<O>) {
yield* query.run(this.entities.values()); yield* query.run(this.entities.values());
} }
query_one<O extends QueryRes>(query: Query<O>) { one<O extends QueryRes>(query: Query<O>) {
return first(this.query_all(query)); return first(this.all(query));
} }
get(identifier: number) { get(identifier: number) {

View file

@ -48,7 +48,7 @@ export class Session {
} }
send_display(width: number, height: number) { send_display(width: number, height: number) {
const raw = this.engine.run(sys_render_world(this.entity.get_force(CompPos).position, v2(width, height))); const raw = this.engine.run(sys_render_world(this.entity.get_force(CompPos).pos, v2(width, height)));
this.client.outputs.send({ kind: "display", content: { raw } }); this.client.outputs.send({ kind: "display", content: { raw } });
} }
@ -70,7 +70,7 @@ export function sys_spawn_player(position: Vec2) {
}; };
} }
class CompPlayer { export class CompPlayer {
life; life;
constructor() { constructor() {
this.life = 10; this.life = 10;

View file

@ -6,12 +6,15 @@ import { Session } from "./entities/player.ts";
import { sys_spawn_structure } from "./components/world.ts"; import { sys_spawn_structure } from "./components/world.ts";
import { Gateway } from "./network.ts"; import { Gateway } from "./network.ts";
import { sys_spawn_enemy } from "./entities/enemy.ts"; import { sys_spawn_enemy } from "./entities/enemy.ts";
import { enemy_plugin } from "./entities/enemy.ts";
const log = log_from(import.meta); const log = log_from(import.meta);
async function main() { async function main() {
log("Starting."); log("Starting.");
const engine = new Engine(); const engine = new Engine();
enemy_plugin(engine);
engine.start(); engine.start();
engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2))); engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2)));
engine.run(sys_spawn_enemy(v2(1, 1))); engine.run(sys_spawn_enemy(v2(1, 1)));