summaryrefslogtreecommitdiff
path: root/src/run.ts
blob: c798f876815e9148e1653d6d2cb0ad986e816878 (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
import { advanceState } from "./game";
import { Action } from "./game/types";
import { createSessionState, SessionState } from "./state";
import { renderState, createAndBindHandler, prepareTerminal, Key } from "./ui";

export const run = async () => {
  let state: SessionState = createSessionState("xyz");
  let actionQueue: Action[] = [];

  const updateAction = (key: Key) => {
    if (actionQueue.length > 1) {
      actionQueue = actionQueue.slice(1);
    }
    switch (key) {
      case Key.LEFT_ARROW:
        actionQueue.push(Action.MOVE_LEFT);
        break;
      case Key.RIGHT_ARROW:
        actionQueue.push(Action.MOVE_RIGHT);
        break;
      default:
        break;
    }
  };

  prepareTerminal();

  createAndBindHandler(updateAction, process.exit);

  while (true) {
    let nextAction = actionQueue.pop();
    state = await advanceState(state, nextAction);
    renderState(state);
  }
};

run().then(console.log).catch(console.error);