silphius/app/ecs/system.js

90 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-06-25 10:43:12 -05:00
import Digraph from '@/util/digraph.js';
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
ecs;
2024-06-30 15:54:23 -05:00
elapsed = 0;
frequency;
2024-06-10 22:42:30 -05:00
queries = {};
constructor(ecs) {
this.ecs = ecs;
const queries = this.constructor.queries();
for (const i in queries) {
2024-06-26 07:41:07 -05:00
this.queries[i] = new Query(queries[i], ecs);
2024-06-10 22:42:30 -05:00
}
2024-06-14 15:18:55 -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-25 10:43:12 -05:00
static get priority() {
return {
phase: 'normal',
}
}
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-08-01 14:31:08 -05:00
schedule() {
this.elapsed = this.frequency;
}
2024-06-10 22:42:30 -05:00
select(query) {
return this.queries[query].select();
}
2024-06-25 10:43:12 -05:00
static sort(Systems) {
const phases = {
'pre': new Digraph(),
'normal': new Digraph(),
'post': new Digraph(),
};
for (const systemName in Systems) {
const {priority} = Systems[systemName];
const phase = phases[priority.phase || 'normal'];
phase.ensureTail(systemName);
if (priority.before) {
for (const before of Array.isArray(priority.before) ? priority.before : [priority.before]) {
phase.addDependency(before, systemName);
}
}
if (priority.after) {
for (const after of Array.isArray(priority.after) ? priority.after : [priority.after]) {
phase.addDependency(systemName, after);
}
}
}
const sorted = [
...phases['pre'].sort(),
...phases['normal'].sort(),
...phases['post'].sort(),
];
return Object.fromEntries(
Object.entries(Systems)
.toSorted(([l], [r]) => sorted.indexOf(l) - sorted.indexOf(r)),
);
}
2024-06-10 22:42:30 -05:00
tick() {}
}