40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { Action, GameAction } from "../actions";
|
|
import { type GameRules } from "../rules";
|
|
import { ResourceGenerator } from "./ResourceGenerator";
|
|
import { ResourceSet } from "./ResourceSet";
|
|
|
|
export class Game {
|
|
|
|
public readonly rules: GameRules;
|
|
|
|
public readonly resources = new ResourceSet();
|
|
public readonly generators: ResourceGenerator[] = [];
|
|
|
|
constructor(
|
|
rules: GameRules,
|
|
) {
|
|
this.rules = rules;
|
|
|
|
this.initResources();
|
|
this.initGenerators();
|
|
}
|
|
|
|
public initResources(): void {
|
|
this.resources.setMany(ResourceSet.from(this.rules.startResources));
|
|
}
|
|
|
|
public initGenerators(): void {
|
|
this.generators.splice(0);
|
|
this.generators.push(...this.rules.generators.map((rule) => new ResourceGenerator(this, rule)));
|
|
}
|
|
|
|
public *tick(time: number): Generator<Action, void, unknown> {
|
|
yield* this.runResourceGenerators(time);
|
|
}
|
|
|
|
private *runResourceGenerators(time: number): Generator<GameAction, void, unknown> {
|
|
for (const gen of this.generators)
|
|
gen.tick(time);
|
|
}
|
|
}
|