converter work on demand
This commit is contained in:
+29
-4
@@ -12,6 +12,30 @@ export type Node = {
|
||||
behaviour: Behaviour;
|
||||
}
|
||||
|
||||
/** Orders behaviours so that whatever a behaviour's inputs link to comes before it (a dependency cycle just falls back to encounter order). */
|
||||
function orderByDependency(behaviours: Behaviour[]): Behaviour[] {
|
||||
const byId = new Map(behaviours.map((behaviour) => [behaviour.id, behaviour]));
|
||||
const visited = new Set<string>();
|
||||
const order: Behaviour[] = [];
|
||||
|
||||
function visit(behaviour: Behaviour) {
|
||||
if (visited.has(behaviour.id))
|
||||
return;
|
||||
visited.add(behaviour.id);
|
||||
for (const input of behaviour.inputs.pins) {
|
||||
const dependency = byId.get(input.link.target);
|
||||
if (dependency)
|
||||
visit(dependency);
|
||||
}
|
||||
order.push(behaviour);
|
||||
}
|
||||
|
||||
for (const behaviour of behaviours)
|
||||
visit(behaviour);
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
export class Game {
|
||||
public time: Decimal;
|
||||
public readonly nodes: Node[];
|
||||
@@ -29,11 +53,12 @@ export class Game {
|
||||
this.time = this.time.add(deltaTime);
|
||||
|
||||
const behavioursById = new Map(this.nodes.map((node) => [node.behaviour.id, node.behaviour]));
|
||||
const resolve = (id: string) => behavioursById.get(id);
|
||||
|
||||
for (const node of this.nodes)
|
||||
node.behaviour.tick(deltaTime);
|
||||
for (const node of this.nodes)
|
||||
node.behaviour.pull((id) => behavioursById.get(id));
|
||||
// Producers must tick (and so fill their storage) before whatever pulls from them,
|
||||
// so resources can flow all the way down a chain within a single tick.
|
||||
for (const behaviour of orderByDependency(this.nodes.map((node) => node.behaviour)))
|
||||
behaviour.tick(deltaTime, resolve);
|
||||
}
|
||||
|
||||
public reset() {
|
||||
|
||||
Reference in New Issue
Block a user