jumpstorm/server/src/server.ts

54 lines
1.1 KiB
TypeScript
Raw Normal View History

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";
2023-07-21 01:22:26 -04:00
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}`,
},
2023-07-21 01:22:26 -04:00
});
return "200 OK";
},
websocket: {
open(ws) {},
message(ws, message) {
console.log(message);
},
close(ws) {},
},
});
console.log(`Listening on ${server.hostname}:${server.port}`);