From e6e29440563e33bb67e0ad51f9fb6c5c2c3fe809 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 7 Mar 2024 20:45:47 -0700 Subject: level one (applications prototype finished!) --- src/engine/systems/Collision.ts | 1 + src/engine/systems/Grid.ts | 25 +++++++++++++++++-- src/engine/systems/Input.ts | 51 ++++++++++++++++++++++++++------------- src/engine/systems/Music.ts | 40 ++++++++++++++++++++++++++++++ src/engine/systems/SystemNames.ts | 1 + src/engine/systems/index.ts | 1 + 6 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 src/engine/systems/Music.ts (limited to 'src/engine/systems') diff --git a/src/engine/systems/Collision.ts b/src/engine/systems/Collision.ts index 8ef8215..7d843cc 100644 --- a/src/engine/systems/Collision.ts +++ b/src/engine/systems/Collision.ts @@ -6,6 +6,7 @@ import { BoundingBox, Colliding, ComponentNames, Grid } from "../components"; const collisionMap: Record> = { [EntityNames.Key]: new Set([EntityNames.LockedDoor]), [EntityNames.Curry]: new Set([EntityNames.Player]), + [EntityNames.FunctionApplication]: new Set([EntityNames.FunctionBox]), }; export class Collision extends System { diff --git a/src/engine/systems/Grid.ts b/src/engine/systems/Grid.ts index 1d4a623..9ab28e3 100644 --- a/src/engine/systems/Grid.ts +++ b/src/engine/systems/Grid.ts @@ -33,8 +33,8 @@ export class Grid extends System { this.rebuildGrid(game); this.highlightEntitiesLookedAt(game); - this.propogateEntityMovements(game); + this.propogateEntityMovements(game); this.updateMovingEntities(dt, game); } @@ -209,9 +209,11 @@ export class Grid extends System { ) { game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => { const grid = entity.getComponent(ComponentNames.Grid)!; + if (grid.movingDirection === Direction.NONE) { return; } + grid.previousDirection = grid.movingDirection; const boundingBox = entity.getComponent( ComponentNames.BoundingBox, @@ -270,7 +272,7 @@ export class Grid extends System { }); } - private getNewGridPosition(prev: Coord2D, direction: Direction) { + public getNewGridPosition(prev: Coord2D, direction: Direction) { let { x: newX, y: newY } = prev; switch (direction) { case Direction.LEFT: @@ -290,6 +292,25 @@ export class Grid extends System { return { x: newX, y: newY }; } + public oppositeDirection(direction: Direction) { + let opposite = Direction.NONE; + switch (direction) { + case Direction.LEFT: + opposite = Direction.RIGHT; + break; + case Direction.RIGHT: + opposite = Direction.LEFT; + break; + case Direction.UP: + opposite = Direction.DOWN; + break; + case Direction.DOWN: + opposite = Direction.UP; + break; + } + return opposite; + } + private isEntityPastCenterWhenMoving( direction: Direction, gridPosition: Coord2D, diff --git a/src/engine/systems/Input.ts b/src/engine/systems/Input.ts index 8900f4e..c527f29 100644 --- a/src/engine/systems/Input.ts +++ b/src/engine/systems/Input.ts @@ -2,7 +2,7 @@ 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 { Action, KeyConstants, MovingSound, SOUNDS } from "../config"; import { Entity, Particles } from "../entities"; import { Coord2D, Direction } from "../interfaces"; import { colors } from "../utils"; @@ -105,27 +105,44 @@ 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); + this.spawnParticlesAround(entity, game); + this.playMoveSound(); } entity.addComponent(gridComponent); } + private playMoveSound() { + const movingSounds = Array.from(MovingSound.states!.values()); + const soundName = + movingSounds[Math.floor(Math.random() * movingSounds.length)].name; + SOUNDS.get(soundName)!.play(); + } + + private spawnParticlesAround(entity: Entity, game: Game) { + const gridSystem = game.getSystem(SystemNames.Grid); + const gridComponent = entity.getComponent(ComponentNames.Grid)!; + const particles = new Particles({ + center: gridSystem.gridToScreenPosition(gridComponent.gridPosition), + particleCount: 4, + spawnerShape: "circle", + spawnerDimensions: { + width: 10, + height: 10, + }, + particleShape: "rectangle", + particleMeanSpeed: 0.05, + particleSpeedVariance: 0.005, + particleMeanLife: 120, + particleMeanSize: 3, + particleSizeVariance: 1, + particleLifeVariance: 30, + particleColors: [colors.gray, colors.darkGray, colors.lightPurple], + }); + + game.addEntity(particles); + } + private hasSomeKey(keys?: string[]): boolean { if (keys) { return keys.some((key) => this.keys.has(key)); diff --git a/src/engine/systems/Music.ts b/src/engine/systems/Music.ts new file mode 100644 index 0000000..6e2004d --- /dev/null +++ b/src/engine/systems/Music.ts @@ -0,0 +1,40 @@ +import { System, SystemNames } from "."; +import { Music as GameMusic, SOUNDS } from "../config"; + +export class Music extends System { + private songs: string[] = []; + private currentSong?: string; + + constructor() { + super(SystemNames.Music); + + this.songs = Array.from(GameMusic.states!.values()).map( + (state) => state.name, + ); + } + + private chooseRandomSong() { + return this.songs[Math.floor(Math.random() * this.songs.length)]; + } + + public playNext() { + let nextSong = this.chooseRandomSong(); + while (nextSong === this.currentSong) { + nextSong = this.chooseRandomSong(); + } + + this.currentSong = nextSong; + SOUNDS.get(this.currentSong)?.play(); + + // when done, play next song + SOUNDS.get(this.currentSong)?.addEventListener("ended", () => { + this.playNext(); + }); + } + + public update(_dt: number) { + if (!this.currentSong) { + this.playNext(); + } + } +} diff --git a/src/engine/systems/SystemNames.ts b/src/engine/systems/SystemNames.ts index 738dfba..363c72c 100644 --- a/src/engine/systems/SystemNames.ts +++ b/src/engine/systems/SystemNames.ts @@ -6,4 +6,5 @@ export namespace SystemNames { export const GridSpawner = "GridSpawner"; export const Collision = "Collision"; export const Life = "Life"; + export const Music = "Music"; } diff --git a/src/engine/systems/index.ts b/src/engine/systems/index.ts index a420216..46c534d 100644 --- a/src/engine/systems/index.ts +++ b/src/engine/systems/index.ts @@ -7,3 +7,4 @@ export * from "./Grid"; export * from "./GridSpawner"; export * from "./Collision"; export * from "./Life"; +export * from "./Music"; -- cgit v1.2.3-70-g09d2