summaryrefslogtreecommitdiff
path: root/src/engine/systems/GridSpawner.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/systems/GridSpawner.ts')
-rw-r--r--src/engine/systems/GridSpawner.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/engine/systems/GridSpawner.ts b/src/engine/systems/GridSpawner.ts
new file mode 100644
index 0000000..c566444
--- /dev/null
+++ b/src/engine/systems/GridSpawner.ts
@@ -0,0 +1,39 @@
+import { System, SystemNames } from ".";
+import { Game } from "..";
+import { ComponentNames, Grid, GridSpawn } from "../components";
+import { Direction } from "../interfaces";
+
+export class GridSpawner extends System {
+ constructor() {
+ super(SystemNames.GridSpawner);
+ }
+
+ public update(_dt: number, game: Game) {
+ game.forEachEntityWithComponent(ComponentNames.GridSpawn, (entity) => {
+ const spawn = entity.getComponent<GridSpawn>(ComponentNames.GridSpawn)!;
+ const hasGrid = entity.hasComponent(SystemNames.Grid);
+
+ if (spawn.direction === Direction.NONE || !hasGrid) {
+ return;
+ }
+
+ const grid = entity.getComponent<Grid>(SystemNames.Grid)!;
+
+ const direction = spawn.direction;
+ const spawned = spawn.spawner();
+ if (!spawned) {
+ return;
+ }
+ spawn.direction = Direction.NONE;
+ entity.addComponent(spawn);
+
+ const spawnedGrid = spawned.getComponent<Grid>(SystemNames.Grid)!;
+ spawnedGrid.gridPosition = grid.gridPosition;
+ spawnedGrid.movingDirection = direction;
+ spawned.addComponent(spawnedGrid);
+
+ game.addEntity(spawned);
+ entity.addComponent(spawned);
+ });
+ }
+}