summaryrefslogtreecommitdiff
path: root/server/src/server.ts
blob: 18829e46c651a65b6d063c69a6d70e6528c5774f (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
import { Game } from "@engine/Game";
import { Floor, Player } from "@engine/entities";
import {
  WallBounds,
  Physics,
  Collision,
  MessageQueueProvider,
  MessagePublisher,
} from "@engine/systems";
import { Grid } from "@engine/structures";
import { Miscellaneous } from "@engine/config";

const TICK_RATE = 60 / 1000;

const game = new Game();

[
  new Physics(),
  new Collision(new Grid()),
  new WallBounds(Miscellaneous.WIDTH),
].forEach((system) => game.addSystem(system));

[new Floor(160), new Player()].forEach((entity) => game.addEntity(entity));

game.start();

setInterval(() => {
  game.doGameLoop(performance.now());
}, TICK_RATE);

const server = Bun.serve({
  port: 8080,
  fetch: async (req, server): Promise<string> => {
    const sessionId = Math.floor(Math.random() * 1e10).toString();

    server.upgrade(req, {
      headers: {
        "Set-Cookie": `SessionId=${sessionId}`,
      },
    });

    return "200 OK";
  },
  websocket: {
    open(ws) {},
    message(ws, message) {
      console.log(message);
    },
    close(ws) {},
  },
});

console.log(`Listening on ${server.hostname}:${server.port}`);