summaryrefslogtreecommitdiff
path: root/src/engine/entities/LockedDoor.ts
blob: 3d1fbbe7a223f4c1d00f5c4ca19d81d0dbcd5ee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Entity, EntityNames, Particles } from ".";
import { Game } from "..";
import {
  BoundingBox,
  Colliding,
  Grid,
  Sprite,
  ComponentNames,
} from "../components";
import {
  IMAGES,
  KeyOpen,
  SOUNDS,
  SPRITE_SPECS,
  SpriteSpec,
  Sprites,
} from "../config";
import { Coord2D } from "../interfaces";
import { Grid as GridSystem, SystemNames } from "../systems";
import { colors } from "../utils";

export class LockedDoor extends Entity {
  private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
    Sprites.LOCKED_DOOR
  ) as SpriteSpec;

  constructor(gridPosition: Coord2D) {
    super(EntityNames.LockedDoor);

    this.addComponent(new Grid(gridPosition));

    this.addComponent(new Colliding(this.handleCollision.bind(this)));

    this.addComponent(
      new BoundingBox(
        {
          x: 0,
          y: 0,
        },
        {
          width: LockedDoor.spriteSpec.width,
          height: LockedDoor.spriteSpec.height,
        },
        0
      )
    );

    this.addComponent(
      new Sprite(
        IMAGES.get(LockedDoor.spriteSpec.sheet)!,
        { x: 0, y: 0 },
        {
          width: LockedDoor.spriteSpec.width,
          height: LockedDoor.spriteSpec.height,
        },
        LockedDoor.spriteSpec.msPerFrame,
        LockedDoor.spriteSpec.frames
      )
    );
  }

  private handleCollision(game: Game, entity: Entity) {
    if (entity.name !== EntityNames.Key) {
      return;
    }

    game.removeEntity(this.id);
    game.removeEntity(entity.id);

    const grid = this.getComponent<Grid>(ComponentNames.Grid);
    const gridSystem = game.getSystem<GridSystem>(SystemNames.Grid);
    const { dimension } = gridSystem;
    const particles = new Particles({
      center: gridSystem.gridToScreenPosition(grid.gridPosition),
      spawnerDimensions: {
        width: dimension.width / 2,
        height: dimension.height / 2,
      },
      particleCount: 20,
      spawnerShape: "rectangle",
      particleShape: "rectangle",
      particleMeanSpeed: 0.35,
      particleSpeedVariance: 0.15,
      particleMeanLife: 80,
      particleMeanSize: 3,
      particleSizeVariance: 1,
      particleLifeVariance: 20,
      particleColors: [colors.yellow, colors.lightYellow],
    });

    game.addEntity(particles);

    this.playKeySound();
  }

  private playKeySound() {
    const audio = SOUNDS.get(KeyOpen.name)!;
    audio.play();
  }
}