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 +++++++++++++++++++++++++++++++++++++ src/engine/systems/LambdaFactory.ts | 34 -------------------------------- src/engine/systems/Spawner.ts | 34 ++++++++++++++++++++++++++++++++ src/engine/systems/SystemNames.ts | 2 +- src/engine/systems/index.ts | 2 +- 5 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 src/engine/systems/GridSpawner.ts delete mode 100644 src/engine/systems/LambdaFactory.ts create mode 100644 src/engine/systems/Spawner.ts (limited to 'src/engine/systems') 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); + }); + } +} diff --git a/src/engine/systems/LambdaFactory.ts b/src/engine/systems/LambdaFactory.ts deleted file mode 100644 index 1263eae..0000000 --- a/src/engine/systems/LambdaFactory.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { System, SystemNames } from "."; -import { Game } from ".."; -import { ComponentNames, Grid, LambdaSpawn } from "../components"; -import { FunctionBox } from "../entities"; - -export class LambdaFactory extends System { - constructor() { - super(SystemNames.LambdaFactory); - } - - public update(_dt: number, game: Game) { - game.forEachEntityWithComponent(ComponentNames.LambdaSpawn, (entity) => { - const lambdaSpawn = entity.getComponent( - ComponentNames.LambdaSpawn, - )!; - const hasGrid = entity.hasComponent(SystemNames.Grid); - - if (!lambdaSpawn.direction || !hasGrid) { - return; - } - - const grid = entity.getComponent(SystemNames.Grid)!; - - const lambda = new FunctionBox(grid.gridPosition, lambdaSpawn.code); - const lambdaGrid = lambda.getComponent(SystemNames.Grid)!; - lambdaGrid.movingDirection = lambdaSpawn.direction; - lambda.addComponent(lambdaGrid); - game.addEntity(lambda); - - lambdaSpawn.direction = null; - entity.addComponent(lambdaSpawn); - }); - } -} diff --git a/src/engine/systems/Spawner.ts b/src/engine/systems/Spawner.ts new file mode 100644 index 0000000..6a4d382 --- /dev/null +++ b/src/engine/systems/Spawner.ts @@ -0,0 +1,34 @@ +import { System, SystemNames } from "."; +import { Game } from ".."; +import { ComponentNames, Grid, GridSpawn } from "../components"; +import { FunctionBox } from "../entities"; + +export class GridSpawner extends System { + constructor() { + super(SystemNames.GridSpawner); + } + + public update(_dt: number, game: Game) { + game.forEachEntityWithComponent(ComponentNames.GridSpawn, (entity) => { + const lambdaSpawn = entity.getComponent( + ComponentNames.GridSpawn, + )!; + const hasGrid = entity.hasComponent(SystemNames.Grid); + + if (!lambdaSpawn.direction || !hasGrid) { + return; + } + + const grid = entity.getComponent(SystemNames.Grid)!; + + const lambda = new FunctionBox(grid.gridPosition, lambdaSpawn.code); + const lambdaGrid = lambda.getComponent(SystemNames.Grid)!; + lambdaGrid.movingDirection = lambdaSpawn.direction; + lambda.addComponent(lambdaGrid); + game.addEntity(lambda); + + lambdaSpawn.direction = null; + entity.addComponent(lambdaSpawn); + }); + } +} diff --git a/src/engine/systems/SystemNames.ts b/src/engine/systems/SystemNames.ts index c96dc48..555746c 100644 --- a/src/engine/systems/SystemNames.ts +++ b/src/engine/systems/SystemNames.ts @@ -3,6 +3,6 @@ export namespace SystemNames { export const Input = "Input"; export const FacingDirection = "FacingDirection"; export const Grid = "Grid"; - export const LambdaFactory = "LambdaFactory"; + export const GridSpawner = "GridSpawner"; export const Collision = "Collision"; } diff --git a/src/engine/systems/index.ts b/src/engine/systems/index.ts index 6ee5392..34b369c 100644 --- a/src/engine/systems/index.ts +++ b/src/engine/systems/index.ts @@ -4,5 +4,5 @@ export * from "./Render"; export * from "./Input"; export * from "./FacingDirection"; export * from "./Grid"; -export * from "./LambdaFactory"; +export * from "./GridSpawner"; export * from "./Collision"; -- cgit v1.2.3-70-g09d2