aboutsummaryrefslogtreecommitdiff
path: root/static/src/main.ts
blob: cc6f42ced50c04848cdeafe7347b2066e47897de (plain) (blame)
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
import $ from "jquery";
import {
  EventPublisher,
  EventQueue,
  WebsocketEventPublisher,
  WebSocketEventQueue,
} from "./engine/events";
import { Game } from "./engine/game";
import { NetworkSystem } from "./engine/network";
import { RenderSystem } from "./engine/render";
import { InputSystem } from "./engine/input";
import { TrailingPositionSystem } from "./engine/trailing_position";
import { drainPoints, setDelay, setRoundCap } from "laser-pen";

$(async () => {
  const client_id = await fetch("/assign", {
    credentials: "include",
  })
    .then((res) => res.json())
    .then(({ session }) => session);

  const ws = new WebSocket("/ws");
  await new Promise<void>((resolve) => {
    ws.onopen = () => resolve();
  });

  const queue: EventQueue = new WebSocketEventQueue(ws);
  const publisher: EventPublisher = new WebsocketEventPublisher(ws);
  const network_system = new NetworkSystem(queue, publisher);

  const gamecanvas = $("#gamecanvas").get(0)! as HTMLCanvasElement;
  const input_system = new InputSystem(publisher, gamecanvas);

  setDelay(500);
  setRoundCap(true);
  const render_system = new RenderSystem(gamecanvas);

  const trailing_position = new TrailingPositionSystem(drainPoints);

  const systems = [
    network_system,
    trailing_position,
    input_system,
    render_system,
  ];

  const game = new Game(client_id, systems);
  ws.onclose = () => game.stop();

  game.start();
});