summaryrefslogtreecommitdiff
path: root/src/engine/systems/LambdaFactory.ts
blob: 1263eaeefb06152a1ae24281eb720fc491b006f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { System, SystemNames } from ".";
import { Game } from "..";
import { ComponentNames, Grid, LambdaSpawn } from "../components";
import { FunctionBox } from "../entities";

export class LambdaFactory extends System {
  constructor() {
    super(SystemNames.LambdaFactory);
  }

  public update(_dt: number, game: Game) {
    game.forEachEntityWithComponent(ComponentNames.LambdaSpawn, (entity) => {
      const lambdaSpawn = entity.getComponent<LambdaSpawn>(
        ComponentNames.LambdaSpawn,
      )!;
      const hasGrid = entity.hasComponent(SystemNames.Grid);

      if (!lambdaSpawn.direction || !hasGrid) {
        return;
      }

      const grid = entity.getComponent<Grid>(SystemNames.Grid)!;

      const lambda = new FunctionBox(grid.gridPosition, lambdaSpawn.code);
      const lambdaGrid = lambda.getComponent<Grid>(SystemNames.Grid)!;
      lambdaGrid.movingDirection = lambdaSpawn.direction;
      lambda.addComponent(lambdaGrid);
      game.addEntity(lambda);

      lambdaSpawn.direction = null;
      entity.addComponent(lambdaSpawn);
    });
  }
}