summaryrefslogtreecommitdiff
path: root/src/engine/systems/RadialObserve.ts
blob: 8d418df24df200dd4b88b95eacce2385b3786d6e (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { System, SystemNames } from ".";
import { ComponentNames, Grid, RadialObserve } from "../components";
import { Entity, EntityNames } from "../entities";
import { Game } from "../Game";
import { cartesianDistance } from "../interfaces";

const radialObservations: Record<string, Set<string>> = {
  TODO: new Set([]),
};

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

  public update(_dt: number, game: Game) {
    game.forEachEntityWithComponent(ComponentNames.RadialObserve, (entity) => {
      if (!(entity.name in radialObservations)) {
        return;
      }
      const observable = radialObservations[entity.name];

      const entityObserve = entity.getComponent<RadialObserve>(
        ComponentNames.RadialObserve,
      );
      if (!entityObserve.onObservation) {
	return;
      }

      const entityPosition = entity.getComponent<Grid>(
        ComponentNames.Grid,
      ).gridPosition;

      const observations: Entity[] = [];
      game.forEachEntityWithComponent(ComponentNames.RadialObserve, (other) => {
        if (entity === other) {
          return;
        }
        if (!observable.has(other.name)) {
          return;
        }

        const otherPosition = other.getComponent<Grid>(
          ComponentNames.Grid,
        ).gridPosition;
        if (
          cartesianDistance(entityPosition, otherPosition) >
          entityObserve.radius
        ) {
          return;
        }

        observations.push(other);
      });

      for (const observation of observations) {
	entityObserve.onObservation!(game, observation);
      }
    });
  }
}