summaryrefslogtreecommitdiff
path: root/src/engine/systems
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-06 14:35:04 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-06 14:35:04 -0700
commit823620b2a6ebb7ece619991e47a37ad46542b69f (patch)
tree82a1501c5f707a1bcbc6c28bd6d0f5731cc9f618 /src/engine/systems
parentce06fa7c29ba4e3d6137f7aa74fbfe45af0e8b73 (diff)
downloadthe-abstraction-engine-823620b2a6ebb7ece619991e47a37ad46542b69f.tar.gz
the-abstraction-engine-823620b2a6ebb7ece619991e47a37ad46542b69f.zip
add particles
Diffstat (limited to 'src/engine/systems')
-rw-r--r--src/engine/systems/Collision.ts40
-rw-r--r--src/engine/systems/Grid.ts2
-rw-r--r--src/engine/systems/Input.ts28
-rw-r--r--src/engine/systems/Life.ts18
-rw-r--r--src/engine/systems/Render.ts4
-rw-r--r--src/engine/systems/SystemNames.ts1
-rw-r--r--src/engine/systems/index.ts1
7 files changed, 57 insertions, 37 deletions
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<string, Set<string>> = {
[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<Colliding>(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<string>;
@@ -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<Control>(
ComponentNames.Control,
);
@@ -103,6 +104,25 @@ export class Input extends System {
);
}
+ if (moveUp || moveLeft || moveRight || moveDown) {
+ const gridPosition = gridComponent.gridPosition;
+ const gridSystem = game.getSystem<GridSystem>(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<LifeComponent>(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<Sprite>(ComponentNames.Sprite);
+ const sprite = entity.getComponent<Renderable>(ComponentNames.Sprite);
sprite.update(dt);
const boundingBox = entity.getComponent<BoundingBox>(
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";