mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-06 05:33:38 -10:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c2a524369 | ||
|
|
4044e442c2 |
@@ -1,5 +1,7 @@
|
||||
// A person's input as raea's FrameInput, in composition px: keys held, the primary pointer and every finger down.
|
||||
// The stage is the composition scaled uniformly into the screen, so each point is mapped back through its box.
|
||||
// A press is kept until a step has read it: the game steps at its fps (33 ms at 30), and a quick tap, or one on a phone
|
||||
// that drops frames, goes down and up between two steps, which a fold reading only what is held would never see.
|
||||
import type { FrameInput } from "raea";
|
||||
|
||||
/** window.addEventListener typed by the event's name (bun's globals type the bare addEventListener loosely) */
|
||||
@@ -11,6 +13,10 @@ export class Input {
|
||||
private keys = new Set<string>();
|
||||
private pointer?: { x: number; y: number; down: boolean };
|
||||
private touches = new Map<number, { x: number; y: number }>();
|
||||
/** pressed since the last step read the input: each finger where it last was, whether the pointer went down, keys */
|
||||
private unread = new Map<number, { x: number; y: number }>();
|
||||
private pressUnread = false;
|
||||
private keysUnread = new Set<string>();
|
||||
/** set on the first touch, click or key: networks keep sound off until then */
|
||||
touched = false;
|
||||
private firstTouch: (() => void)[] = [];
|
||||
@@ -31,11 +37,14 @@ export class Input {
|
||||
const p = at(e);
|
||||
this.pointer = { ...p, down: true };
|
||||
this.touches.set(e.pointerId, p);
|
||||
this.unread.set(e.pointerId, p);
|
||||
this.pressUnread = true;
|
||||
});
|
||||
on("pointermove", (e) => {
|
||||
const p = at(e);
|
||||
if (e.isPrimary) this.pointer = { ...p, down: this.pointer?.down ?? false };
|
||||
if (this.touches.has(e.pointerId)) this.touches.set(e.pointerId, p);
|
||||
if (this.unread.has(e.pointerId)) this.unread.set(e.pointerId, p);
|
||||
});
|
||||
const up = (e: PointerEvent) => {
|
||||
if (e.isPrimary && this.pointer) this.pointer = { ...this.pointer, down: false };
|
||||
@@ -46,6 +55,7 @@ export class Input {
|
||||
on("keydown", (e) => {
|
||||
first();
|
||||
this.keys.add(e.key);
|
||||
this.keysUnread.add(e.key);
|
||||
});
|
||||
on("keyup", (e) => this.keys.delete(e.key));
|
||||
on("blur", () => this.keys.clear());
|
||||
@@ -57,12 +67,19 @@ export class Input {
|
||||
else this.firstTouch.push(f);
|
||||
}
|
||||
|
||||
/** This frame's input, marked live (a person is playing: raea/games' fold reads it so) */
|
||||
/** This step's input, marked live (a person is playing: raea/games' fold reads it so). What went down since the last
|
||||
* step counts as held for this one, even if it is already up: the next step sees it let go. */
|
||||
now(): FrameInput & { live: true } {
|
||||
const keys = new Set([...this.keys, ...this.keysUnread]);
|
||||
const touches = new Map([...this.unread, ...this.touches]);
|
||||
const pointer = this.pointer && { ...this.pointer, down: this.pointer.down || this.pressUnread };
|
||||
this.unread.clear();
|
||||
this.pressUnread = false;
|
||||
this.keysUnread.clear();
|
||||
return {
|
||||
keys: [...this.keys],
|
||||
...(this.pointer ? { pointer: { ...this.pointer } } : {}),
|
||||
...(this.touches.size ? { touches: [...this.touches].map(([id, p]) => ({ id, ...p })) } : {}),
|
||||
keys: [...keys],
|
||||
...(pointer ? { pointer } : {}),
|
||||
...(touches.size ? { touches: [...touches].map(([id, p]) => ({ id, ...p })) } : {}),
|
||||
live: true,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user