diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-05 22:00:04 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-05 22:00:04 -0700 |
commit | ce06fa7c29ba4e3d6137f7aa74fbfe45af0e8b73 (patch) | |
tree | ef17306b0fe5f2f957d9c37638a6af76236c7881 /src/engine/systems/GridSpawner.ts | |
parent | 110fe21a2340365b7b7cb72f6f44ad13ed39f4ea (diff) | |
download | the-abstraction-engine-ce06fa7c29ba4e3d6137f7aa74fbfe45af0e8b73.tar.gz the-abstraction-engine-ce06fa7c29ba4e3d6137f7aa74fbfe45af0e8b73.zip |
refactor spawners
Diffstat (limited to 'src/engine/systems/GridSpawner.ts')
-rw-r--r-- | src/engine/systems/GridSpawner.ts | 39 |
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); + }); + } +} |