summaryrefslogtreecommitdiff
path: root/src/engine/TheAbstractionEngine.ts
blob: e84093fd3b343cb49a7174fa6064c52a72a49665 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Game } from ".";
import { Miscellaneous, loadAssets } from "./config";
import {
  Player,
  FunctionBox,
  Wall,
  LambdaFactory,
  Key,
  LockedDoor,
  Curry,
} from "./entities";
import {
  Grid,
  FacingDirection,
  Input,
  Render,
  LambdaFactory as LambdaFactorySpawnSystem,
  Collision,
} from "./systems";

export class TheAbstractionEngine {
  private game: Game;
  private ctx: CanvasRenderingContext2D;
  private animationFrameId: number | null;

  constructor(game: Game, ctx: CanvasRenderingContext2D) {
    this.game = game;
    this.ctx = ctx;
    this.animationFrameId = null;
  }

  public async init() {
    await loadAssets();

    const inputSystem = new Input();
    this.addWindowEventListenersToInputSystem(inputSystem);

    const facingDirectionSystem = new FacingDirection(inputSystem);

    [
      inputSystem,
      facingDirectionSystem,
      new Grid(
        { width: Miscellaneous.GRID_COLUMNS, height: Miscellaneous.GRID_ROWS },
        {
          width: Miscellaneous.GRID_CELL_WIDTH,
          height: Miscellaneous.GRID_CELL_HEIGHT,
        },
      ),
      new LambdaFactorySpawnSystem(),
      new Collision(),
      new Render(this.ctx),
    ].forEach((system) => this.game.addSystem(system));

    const player = new Player();
    this.game.addEntity(player);

    const box = new FunctionBox({ x: 3, y: 1 }, "λ x . (x)");
    this.game.addEntity(box);
    const box2 = new FunctionBox({ x: 4, y: 1 }, "λ x . (x)");
    this.game.addEntity(box2);

    const wall = new Wall({ x: 5, y: 3 });
    this.game.addEntity(wall);

    const factory = new LambdaFactory({ x: 6, y: 6 }, "λ x . (x)", 10);
    this.game.addEntity(factory);

    const lockedDoor = new LockedDoor({ x: 8, y: 8 });
    this.game.addEntity(lockedDoor);

    const key = new Key({ x: 7, y: 7 });
    this.game.addEntity(key);

    const curry = new Curry({ x: 9, y: 8 });
    this.game.addEntity(curry);
  }

  public play() {
    this.game.start();

    const loop = (timestamp: number) => {
      this.game.doGameLoop(timestamp);
      this.animationFrameId = requestAnimationFrame(loop); // tail call recursion! /s
    };
    this.animationFrameId = requestAnimationFrame(loop);
  }

  public stop() {
    if (this.animationFrameId) {
      cancelAnimationFrame(this.animationFrameId);
      this.animationFrameId = null;
    }
  }

  private addWindowEventListenersToInputSystem(input: Input) {
    window.addEventListener("keydown", (e) => {
      if (!e.repeat) {
        input.keyPressed(e.key.toLowerCase());
      }
    });

    window.addEventListener("keyup", (e) =>
      input.keyReleased(e.key.toLowerCase()),
    );

    window.addEventListener("blur", () => input.clearKeys());

    window.addEventListener("mousemove", (e) => {
      const canvas = this.ctx.canvas;
      const rect = canvas.getBoundingClientRect();

      const scaleX = canvas.width / rect.width;
      const scaleY = canvas.height / rect.height;

      const x = (e.clientX - rect.left) * scaleX;
      const y = (e.clientY - rect.top) * scaleY;

      input.setMousePosition({ x, y });
    });
  }
}