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
|
import { DebouncePublisher } from "./debounce_publisher";
import { EntityPositionUpdateEvent, EventPublisher, EventType } from "./events";
import { Game } from "./game";
import { System, SystemType } from "./system";
export class InputSystem extends System {
private readonly controllable_entities: Set<string> = new Set();
private readonly mouse_movement_debouncer: DebouncePublisher<{
x: number;
y: number;
}>;
constructor(
private readonly message_publisher: EventPublisher,
target: HTMLElement,
) {
super(SystemType.INPUT);
this.mouse_movement_debouncer = new DebouncePublisher((data) =>
this.publish_mouse_movement(data),
);
target.addEventListener("mousemove", (event) => {
this.mouse_movement_debouncer.update({
x: event.clientX,
y: event.clientY,
});
});
}
private publish_mouse_movement({ x, y }: { x: number; y: number }) {
console.log(`publishing mouse movement at (${x}, ${y})`);
for (const entity_id of this.controllable_entities) {
this.message_publisher.add({
event_type: EventType.ENTITY_POSITION_UPDATE,
data: {
id: entity_id,
position: { x, y },
},
} as EntityPositionUpdateEvent);
}
}
public add_controllable_entity(entity_id: string) {
this.controllable_entities.add(entity_id);
}
public update(_dt: number, _game: Game) {}
}
|