diff options
| author | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-09-07 20:20:07 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth@simponic.xyz> | 2024-09-12 17:23:30 -0700 |
| commit | 8ec7f5368232d59f344e1067e1bad5e48dbcb7ae (patch) | |
| tree | 1ad2df4dc00773f2307d1525cc80ac7410ea8fba /static/src/engine/input.ts | |
| parent | e4e31978bae7e45be57b376415a4b925ac8cbc03 (diff) | |
| download | kennel.hatecomputers.club-8ec7f5368232d59f344e1067e1bad5e48dbcb7ae.tar.gz kennel.hatecomputers.club-8ec7f5368232d59f344e1067e1bad5e48dbcb7ae.zip | |
get "cats" up there
Diffstat (limited to 'static/src/engine/input.ts')
| -rw-r--r-- | static/src/engine/input.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/static/src/engine/input.ts b/static/src/engine/input.ts new file mode 100644 index 0000000..c1728e9 --- /dev/null +++ b/static/src/engine/input.ts @@ -0,0 +1,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) {} +} |
