From 823620b2a6ebb7ece619991e47a37ad46542b69f Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 6 Mar 2024 14:35:04 -0700 Subject: add particles --- src/engine/systems/Collision.ts | 40 ++++++++++----------------------------- src/engine/systems/Grid.ts | 2 +- src/engine/systems/Input.ts | 28 +++++++++++++++++++++++---- src/engine/systems/Life.ts | 18 ++++++++++++++++++ src/engine/systems/Render.ts | 4 ++-- src/engine/systems/SystemNames.ts | 1 + src/engine/systems/index.ts | 1 + 7 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 src/engine/systems/Life.ts (limited to 'src/engine/systems') diff --git a/src/engine/systems/Collision.ts b/src/engine/systems/Collision.ts index 1912fb6..8ef8215 100644 --- a/src/engine/systems/Collision.ts +++ b/src/engine/systems/Collision.ts @@ -1,7 +1,7 @@ import { System, SystemNames } from "."; import { Game } from ".."; import { Entity, EntityNames } from "../entities"; -import { BoundingBox, ComponentNames, Grid } from "../components"; +import { BoundingBox, Colliding, ComponentNames, Grid } from "../components"; const collisionMap: Record> = { [EntityNames.Key]: new Set([EntityNames.LockedDoor]), @@ -70,34 +70,14 @@ export class Collision extends System { return; } - const keyDoorPair = [EntityNames.Key, EntityNames.LockedDoor].map((x) => - [entity, otherEntity].find((y) => y.name === x), - ); - const [key, door] = keyDoorPair; - if (key && door) { - this.handleKeyDoorCollision(key, door, game); - } - - const curryPlayerPair = [EntityNames.Curry, EntityNames.Player].map((x) => - [entity, otherEntity].find((y) => y.name === x), - ); - const [curry, player] = curryPlayerPair; - if (curry && player) { - this.handleCurryPlayerCollision(curry, player, game); - } - } - - private handleKeyDoorCollision(key: Entity, door: Entity, game: Game) { - game.removeEntity(key.id); - game.removeEntity(door.id); - } - - private handleCurryPlayerCollision( - curry: Entity, - _player: Entity, - game: Game, - ) { - game.removeEntity(curry.id); - game.stop(); + [entity, otherEntity].forEach((e) => { + if (!e.hasComponent(ComponentNames.Colliding)) { + return; + } + const colliding = e.getComponent(ComponentNames.Colliding); + if (colliding?.onCollision) { + colliding.onCollision(game, e === entity ? otherEntity : entity); + } + }); } } diff --git a/src/engine/systems/Grid.ts b/src/engine/systems/Grid.ts index 915335b..1d4a623 100644 --- a/src/engine/systems/Grid.ts +++ b/src/engine/systems/Grid.ts @@ -309,7 +309,7 @@ export class Grid extends System { return false; } - private gridToScreenPosition(gridPosition: Coord2D) { + public gridToScreenPosition(gridPosition: Coord2D) { const { width, height } = this.dimension; return { x: gridPosition.x * width + width / 2, diff --git a/src/engine/systems/Input.ts b/src/engine/systems/Input.ts index 3da018d..8900f4e 100644 --- a/src/engine/systems/Input.ts +++ b/src/engine/systems/Input.ts @@ -1,10 +1,11 @@ -import { SystemNames, System } from "."; +import { Grid as GridSystem, SystemNames, System } from "."; import { Game } from ".."; import { ComponentNames, Grid, Interactable } from "../components"; import { Control } from "../components/Control"; import { Action, KeyConstants } from "../config"; -import { Entity } from "../entities"; +import { Entity, Particles } from "../entities"; import { Coord2D, Direction } from "../interfaces"; +import { colors } from "../utils"; export class Input extends System { private keys: Set; @@ -31,7 +32,7 @@ export class Input extends System { public update(_dt: number, game: Game) { game.forEachEntityWithComponent(ComponentNames.Control, (entity) => - this.handleMovement(entity), + this.handleMovement(entity, game), ); game.forEachEntityWithComponent(ComponentNames.Interactable, (entity) => this.handleInteraction(entity), @@ -57,7 +58,7 @@ export class Input extends System { ); } - public handleMovement(entity: Entity) { + public handleMovement(entity: Entity, game: Game) { const controlComponent = entity.getComponent( ComponentNames.Control, ); @@ -103,6 +104,25 @@ export class Input extends System { ); } + if (moveUp || moveLeft || moveRight || moveDown) { + const gridPosition = gridComponent.gridPosition; + const gridSystem = game.getSystem(SystemNames.Grid); + const particles = new Particles({ + center: gridSystem.gridToScreenPosition(gridPosition), + particleCount: 5, + particleShape: "circle", + particleMeanSpeed: 0.05, + particleSpeedVariance: 0.005, + particleMeanLife: 120, + particleMeanSize: 5, + particleSizeVariance: 2, + particleLifeVariance: 30, + particleColors: [colors.gray, colors.darkGray], + }); + + game.addEntity(particles); + } + entity.addComponent(gridComponent); } diff --git a/src/engine/systems/Life.ts b/src/engine/systems/Life.ts new file mode 100644 index 0000000..f454437 --- /dev/null +++ b/src/engine/systems/Life.ts @@ -0,0 +1,18 @@ +import { System, SystemNames } from "."; +import { Game } from ".."; +import { Life as LifeComponent, ComponentNames } from "../components"; + +export class Life extends System { + constructor() { + super(SystemNames.Life); + } + + public update(_dt: number, game: Game) { + game.forEachEntityWithComponent(ComponentNames.Life, (entity) => { + const life = entity.getComponent(ComponentNames.Life); + if (!life.alive) { + game.removeEntity(entity.id); + } + }); + } +} diff --git a/src/engine/systems/Render.ts b/src/engine/systems/Render.ts index f273deb..2dd35e2 100644 --- a/src/engine/systems/Render.ts +++ b/src/engine/systems/Render.ts @@ -4,7 +4,7 @@ import { Text, ComponentNames, Highlight, - Sprite, + Renderable, } from "../components"; import { Game } from ".."; import { clamp } from "../utils"; @@ -22,7 +22,7 @@ export class Render extends System { this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height); game.forEachEntityWithComponent(ComponentNames.Sprite, (entity) => { - const sprite = entity.getComponent(ComponentNames.Sprite); + const sprite = entity.getComponent(ComponentNames.Sprite); sprite.update(dt); const boundingBox = entity.getComponent( diff --git a/src/engine/systems/SystemNames.ts b/src/engine/systems/SystemNames.ts index 555746c..738dfba 100644 --- a/src/engine/systems/SystemNames.ts +++ b/src/engine/systems/SystemNames.ts @@ -5,4 +5,5 @@ export namespace SystemNames { export const Grid = "Grid"; export const GridSpawner = "GridSpawner"; export const Collision = "Collision"; + export const Life = "Life"; } diff --git a/src/engine/systems/index.ts b/src/engine/systems/index.ts index 34b369c..a420216 100644 --- a/src/engine/systems/index.ts +++ b/src/engine/systems/index.ts @@ -6,3 +6,4 @@ export * from "./FacingDirection"; export * from "./Grid"; export * from "./GridSpawner"; export * from "./Collision"; +export * from "./Life"; -- cgit v1.2.3-70-g09d2