summaryrefslogtreecommitdiff
path: root/client/src/network/MessageProcessor.ts
blob: 539937a651ecde1d0f6088082e41a41384508686 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type { Game } from '@engine/Game';
import { ComponentNames, Control, NetworkUpdateable } from '@engine/components';
import { Entity } from '@engine/entities';
import {
  MessageType,
  type Message,
  type EntityAddBody,
  type EntityUpdateBody,
  type MessageProcessor
} from '@engine/network';
import { Input, SystemNames } from '@engine/systems';

export class ClientMessageProcessor implements MessageProcessor {
  private game: Game;

  constructor(game: Game) {
    this.game = game;
  }

  public process(message: Message) {
    switch (message.type) {
      case MessageType.NEW_ENTITIES:
        const entityAdditions = message.body as unknown as EntityAddBody[];
        entityAdditions.forEach((addBody) => {
          const entity = Entity.from(
            addBody.entityName,
            addBody.id,
            addBody.args
          );
          if (entity.hasComponent(ComponentNames.Control)) {
            const clientId = this.game.getSystem<Input>(
              SystemNames.Input
            ).clientId;
            const control = entity.getComponent<Control>(
              ComponentNames.Control
            );

            if (control.controllableBy === clientId) {
              entity.addComponent(new NetworkUpdateable());
            }
          }

          this.game.addEntity(entity);
        });
        break;
      case MessageType.REMOVE_ENTITIES:
        const ids = message.body as unknown as string[];
        ids.forEach((id) => this.game.removeEntity(id));
        break;
      case MessageType.UPDATE_ENTITIES:
        const entityUpdates = message.body as unknown as EntityUpdateBody[];
        entityUpdates.forEach(({ id, args }) => {
          const entity = this.game.getEntity(id);
          if (!entity) return;

          if (entity && entity.hasComponent(ComponentNames.Control)) {
            const clientId = this.game.getSystem<Input>(
              SystemNames.Input
            ).clientId;
            const control = entity.getComponent<Control>(
              ComponentNames.Control
            );

            // don't listen to entities which we control
            if (control.controllableBy === clientId) return;
          }
          entity.setFrom(args);
        });
        break;
      default:
        break;
    }
  }
}