summaryrefslogtreecommitdiff
path: root/server/src/main.ts
blob: 0e474914a61fc3140dc7e6b4d3da639c87990083 (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
import { Grid } from '@engine/structures';
import {
  ServerMessageProcessor,
  ServerSocketMessagePublisher,
  ServerSocketMessageReceiver,
  MemorySessionManager,
  SessionInputSystem
} from './network';
import { Collision, NetworkUpdate, Physics, WallBounds } from '@engine/systems';
import { Game } from '@engine/Game';
import { Constants } from './constants';
import { GameServer } from './server';
import { Floor } from '@engine/entities';
import { BoundingBox } from '@engine/components';
import { Miscellaneous } from '@engine/config';

const game = new Game();

const sessionManager = new MemorySessionManager();

const messageReceiver = new ServerSocketMessageReceiver();
const messagePublisher = new ServerSocketMessagePublisher();
const messageProcessor = new ServerMessageProcessor(game, sessionManager);

const server = new GameServer(
  game,
  messageReceiver,
  messagePublisher,
  sessionManager
);

[
  new SessionInputSystem(sessionManager),
  new NetworkUpdate(messageReceiver, messagePublisher, messageProcessor),
  new Physics(),
  new Collision(new Grid()),
  new WallBounds()
].forEach((system) => game.addSystem(system));

const floor = new Floor(160);
const floorHeight = 200;

floor.addComponent(
  new BoundingBox(
    {
      x: Miscellaneous.WIDTH / 2,
      y: Miscellaneous.HEIGHT + floorHeight / 2
    },
    { width: Miscellaneous.WIDTH, height: floorHeight }
  )
);
game.addEntity(floor);

game.start();
setInterval(() => {
  game.doGameLoop(performance.now());
}, Constants.SERVER_TICK_RATE);

server.serve();