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.");
|
||||
|
|
|
@ -2,12 +2,10 @@ import { Awaitable, ClassMap, Constructible, first, log_from, Prototyped, wait }
|
|||
const log = log_from(import.meta);
|
||||
|
||||
export class Entity {
|
||||
identifier;
|
||||
components;
|
||||
engine;
|
||||
|
||||
constructor(identifier: number, engine: Engine) {
|
||||
this.identifier = identifier;
|
||||
constructor(engine: Engine) {
|
||||
this.engine = engine;
|
||||
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 {
|
||||
next_identifier;
|
||||
entities;
|
||||
|
@ -43,7 +48,8 @@ export class Engine {
|
|||
|
||||
spawn(op: (entity: Entity) => unknown) {
|
||||
const identifier = this.next_identifier++;
|
||||
const entity = new Entity(identifier, this);
|
||||
const entity = new Entity(this);
|
||||
entity.insert(new CompId(identifier));
|
||||
op(entity);
|
||||
this.entities.set(identifier, entity);
|
||||
return entity;
|
||||
|
@ -54,18 +60,19 @@ export class Engine {
|
|||
}
|
||||
|
||||
async global_system_loop(sys: (engine: Engine) => Awaitable, interval: number) {
|
||||
// TODO : Wait for start.
|
||||
while (true) {
|
||||
await sys(this);
|
||||
await wait(interval);
|
||||
}
|
||||
}
|
||||
|
||||
*query_all<O extends QueryRes>(query: Query<O>) {
|
||||
*all<O extends QueryRes>(query: Query<O>) {
|
||||
yield* query.run(this.entities.values());
|
||||
}
|
||||
|
||||
query_one<O extends QueryRes>(query: Query<O>) {
|
||||
return first(this.query_all(query));
|
||||
one<O extends QueryRes>(query: Query<O>) {
|
||||
return first(this.all(query));
|
||||
}
|
||||
|
||||
get(identifier: number) {
|
||||
|
|
|
@ -48,7 +48,7 @@ export class Session {
|
|||
}
|
||||
|
||||
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 } });
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ export function sys_spawn_player(position: Vec2) {
|
|||
};
|
||||
}
|
||||
|
||||
class CompPlayer {
|
||||
export class CompPlayer {
|
||||
life;
|
||||
constructor() {
|
||||
this.life = 10;
|
||||
|
|
|
@ -6,12 +6,15 @@ import { Session } from "./entities/player.ts";
|
|||
import { sys_spawn_structure } from "./components/world.ts";
|
||||
import { Gateway } from "./network.ts";
|
||||
import { sys_spawn_enemy } from "./entities/enemy.ts";
|
||||
import { enemy_plugin } from "./entities/enemy.ts";
|
||||
const log = log_from(import.meta);
|
||||
|
||||
async function main() {
|
||||
log("Starting.");
|
||||
const engine = new Engine();
|
||||
enemy_plugin(engine);
|
||||
engine.start();
|
||||
|
||||
engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2)));
|
||||
engine.run(sys_spawn_enemy(v2(1, 1)));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue