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

@ -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) {