summaryrefslogtreecommitdiff
path: root/src/engine/systems/RadialObserve.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth@simponic.xyz>2025-03-02 17:06:49 -0700
committerElizabeth Hunt <elizabeth@simponic.xyz>2025-03-02 17:11:01 -0700
commit491a1d05a5f001d8f5155b9169547d461ac7e8b2 (patch)
tree708128dff7c4ad61716680646f2c11def4e331e5 /src/engine/systems/RadialObserve.ts
parentd8511f4ad3bc3a326de7f7af2fb8703d5f471e36 (diff)
downloadthe-abstraction-engine-491a1d05a5f001d8f5155b9169547d461ac7e8b2.tar.gz
the-abstraction-engine-491a1d05a5f001d8f5155b9169547d461ac7e8b2.zip
implement a radial observation system
Diffstat (limited to 'src/engine/systems/RadialObserve.ts')
-rw-r--r--src/engine/systems/RadialObserve.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/engine/systems/RadialObserve.ts b/src/engine/systems/RadialObserve.ts
new file mode 100644
index 0000000..8d418df
--- /dev/null
+++ b/src/engine/systems/RadialObserve.ts
@@ -0,0 +1,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);
+ }
+ });
+ }
+}