summaryrefslogtreecommitdiff
path: root/src/engine/entities/Portal.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/entities/Portal.ts')
-rw-r--r--src/engine/entities/Portal.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/engine/entities/Portal.ts b/src/engine/entities/Portal.ts
new file mode 100644
index 0000000..a747aa9
--- /dev/null
+++ b/src/engine/entities/Portal.ts
@@ -0,0 +1,60 @@
+import { Entity, EntityNames } from ".";
+import { Game } from "..";
+import { BoundingBox, Colliding, Grid, Sprite, Text } from "../components";
+import { IMAGES, SPRITE_SPECS, SpriteSpec, Sprites } from "../config";
+import { Coord2D } from "../interfaces";
+import { Level, SystemNames } from "../systems";
+
+export class Portal extends Entity {
+ private static spriteSpec = SPRITE_SPECS.get(Sprites.PORTAL) as SpriteSpec;
+
+ private toLevel: string;
+
+ constructor(toLevel: string, gridPosition: Coord2D) {
+ super(EntityNames.Portal);
+
+ this.toLevel = toLevel;
+
+ this.addComponent(
+ new BoundingBox(
+ {
+ x: 0,
+ y: 0,
+ },
+ {
+ width: Portal.spriteSpec.width,
+ height: Portal.spriteSpec.height,
+ },
+ 0,
+ ),
+ );
+
+ this.addComponent(
+ new Sprite(
+ IMAGES.get(Portal.spriteSpec.sheet)!,
+ { x: 0, y: 0 },
+ {
+ width: Portal.spriteSpec.width,
+ height: Portal.spriteSpec.height,
+ },
+ Portal.spriteSpec.msPerFrame,
+ Portal.spriteSpec.frames,
+ ),
+ );
+
+ this.addComponent(new Colliding(this.handleCollision.bind(this)));
+
+ this.addComponent(new Grid(gridPosition));
+
+ this.addComponent(new Text(toLevel));
+ }
+
+ public handleCollision(game: Game, entity: Entity) {
+ if (entity.name !== EntityNames.Player) {
+ return;
+ }
+
+ const levelSystem = game.getSystem<Level>(SystemNames.Level);
+ levelSystem.setLevel(this.toLevel);
+ }
+}