From ce06fa7c29ba4e3d6137f7aa74fbfe45af0e8b73 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Tue, 5 Mar 2024 22:00:04 -0700 Subject: refactor spawners --- src/engine/systems/GridSpawner.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/engine/systems/GridSpawner.ts (limited to 'src/engine/systems/GridSpawner.ts') 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(ComponentNames.GridSpawn)!; + const hasGrid = entity.hasComponent(SystemNames.Grid); + + if (spawn.direction === Direction.NONE || !hasGrid) { + return; + } + + const grid = entity.getComponent(SystemNames.Grid)!; + + const direction = spawn.direction; + const spawned = spawn.spawner(); + if (!spawned) { + return; + } + spawn.direction = Direction.NONE; + entity.addComponent(spawn); + + const spawnedGrid = spawned.getComponent(SystemNames.Grid)!; + spawnedGrid.gridPosition = grid.gridPosition; + spawnedGrid.movingDirection = direction; + spawned.addComponent(spawnedGrid); + + game.addEntity(spawned); + entity.addComponent(spawned); + }); + } +} -- cgit v1.2.3-70-g09d2