silphius/app/ecs/system.js

87 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-06-10 22:42:30 -05:00
import Query from './query.js';
export default class System {
2024-06-14 15:18:55 -05:00
active = false;
2024-06-10 22:42:30 -05:00
destroying = [];
ecs;
queries = {};
constructor(ecs) {
this.ecs = ecs;
const queries = this.constructor.queries();
for (const i in queries) {
2024-06-15 19:38:49 -05:00
this.queries[i] = new Query(queries[i], ecs.Components);
2024-06-10 22:42:30 -05:00
}
2024-06-14 15:18:55 -05:00
this.reindex(ecs.entities);
}
createEntity(components) {
return this.ecs.create(components);
}
createManyEntities(componentsList) {
return this.ecs.createMany(componentsList);
2024-06-10 22:42:30 -05:00
}
2024-06-11 19:10:57 -05:00
deindex(entityIds) {
2024-06-10 22:42:30 -05:00
for (const i in this.queries) {
2024-06-11 19:10:57 -05:00
this.queries[i].deindex(entityIds);
2024-06-10 22:42:30 -05:00
}
}
2024-06-11 19:10:57 -05:00
destroyEntity(entityId) {
this.destroyManyEntities([entityId]);
2024-06-10 22:42:30 -05:00
}
2024-06-11 19:10:57 -05:00
destroyManyEntities(entityIds) {
for (let i = 0; i < entityIds.length; i++) {
this.destroying.push(entityIds[i]);
2024-06-10 22:42:30 -05:00
}
}
finalize() {}
2024-06-14 15:18:55 -05:00
insertComponents(entityId, components) {
this.ecs.insert(entityId, components);
}
insertManyComponents(components) {
this.ecs.insertMany(components);
2024-06-10 22:42:30 -05:00
}
static queries() {
return {};
}
2024-06-11 19:10:57 -05:00
reindex(entityIds) {
2024-06-10 22:42:30 -05:00
for (const i in this.queries) {
2024-06-11 19:10:57 -05:00
this.queries[i].reindex(entityIds);
2024-06-10 22:42:30 -05:00
}
}
2024-06-14 15:18:55 -05:00
removeComponents(entityId, components) {
this.ecs.remove(entityId, components);
}
removeManyComponents(entityIds) {
this.ecs.removeMany(entityIds);
}
2024-06-10 22:42:30 -05:00
select(query) {
return this.queries[query].select();
}
tickDestruction() {
this.deindex(this.destroying);
this.destroying = [];
}
tick() {}
}