like2d 2.8.0 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +44 -41
  2. package/dist/__benchmarks__/vector2.bench.d.ts +2 -0
  3. package/dist/__benchmarks__/vector2.bench.d.ts.map +1 -0
  4. package/dist/__benchmarks__/vector2.bench.js +74 -0
  5. package/dist/{core → audio}/audio.d.ts +21 -9
  6. package/dist/audio/audio.d.ts.map +1 -0
  7. package/dist/{core → audio}/audio.js +15 -4
  8. package/dist/audio/index.d.ts +2 -0
  9. package/dist/audio/index.d.ts.map +1 -0
  10. package/dist/engine.d.ts +30 -15
  11. package/dist/engine.d.ts.map +1 -1
  12. package/dist/engine.js +79 -160
  13. package/dist/events.d.ts +86 -0
  14. package/dist/events.d.ts.map +1 -0
  15. package/dist/events.js +5 -0
  16. package/dist/gamecontrollerdb.txt +9 -8
  17. package/dist/graphics/canvas.d.ts +65 -0
  18. package/dist/graphics/canvas.d.ts.map +1 -0
  19. package/dist/graphics/canvas.js +224 -0
  20. package/dist/graphics/drawing.d.ts +203 -0
  21. package/dist/graphics/drawing.d.ts.map +1 -0
  22. package/dist/graphics/drawing.js +388 -0
  23. package/dist/graphics/index.d.ts +19 -0
  24. package/dist/graphics/index.d.ts.map +1 -0
  25. package/dist/graphics/index.js +13 -0
  26. package/dist/index.d.ts +18 -24
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +15 -15
  29. package/dist/input/gamepad-mapping.d.ts +134 -0
  30. package/dist/input/gamepad-mapping.d.ts.map +1 -0
  31. package/dist/input/gamepad-mapping.js +146 -0
  32. package/dist/input/gamepad.d.ts +74 -0
  33. package/dist/input/gamepad.d.ts.map +1 -0
  34. package/dist/input/gamepad.js +288 -0
  35. package/dist/input/index.d.ts +6 -0
  36. package/dist/input/index.d.ts.map +1 -0
  37. package/dist/input/index.js +1 -0
  38. package/dist/input/input.d.ts +76 -0
  39. package/dist/input/input.d.ts.map +1 -0
  40. package/dist/input/input.js +164 -0
  41. package/dist/input/keyboard.d.ts +15 -0
  42. package/dist/input/keyboard.d.ts.map +1 -0
  43. package/dist/input/keyboard.js +53 -0
  44. package/dist/input/mouse.d.ts +108 -0
  45. package/dist/input/mouse.d.ts.map +1 -0
  46. package/dist/input/mouse.js +241 -0
  47. package/dist/like.d.ts +80 -0
  48. package/dist/like.d.ts.map +1 -0
  49. package/dist/like.js +5 -0
  50. package/dist/math/index.d.ts +18 -0
  51. package/dist/math/index.d.ts.map +1 -0
  52. package/dist/math/index.js +17 -0
  53. package/dist/math/rect.d.ts +68 -0
  54. package/dist/math/rect.d.ts.map +1 -0
  55. package/dist/{core → math}/rect.js +48 -66
  56. package/dist/math/vector2.d.ts +133 -0
  57. package/dist/math/vector2.d.ts.map +1 -0
  58. package/dist/math/vector2.js +111 -0
  59. package/dist/prefab-scenes/index.d.ts +8 -0
  60. package/dist/prefab-scenes/index.d.ts.map +1 -0
  61. package/dist/prefab-scenes/index.js +7 -0
  62. package/dist/prefab-scenes/mapGamepad.d.ts +28 -0
  63. package/dist/prefab-scenes/mapGamepad.d.ts.map +1 -0
  64. package/dist/prefab-scenes/mapGamepad.js +181 -0
  65. package/dist/prefab-scenes/startScreen.d.ts +59 -0
  66. package/dist/prefab-scenes/startScreen.d.ts.map +1 -0
  67. package/dist/{scenes/startup.js → prefab-scenes/startScreen.js} +48 -8
  68. package/dist/scene.d.ts +104 -6
  69. package/dist/scene.d.ts.map +1 -1
  70. package/dist/scene.js +28 -1
  71. package/dist/timer/index.d.ts +2 -0
  72. package/dist/timer/index.d.ts.map +1 -0
  73. package/dist/timer/timer.d.ts +32 -0
  74. package/dist/timer/timer.d.ts.map +1 -0
  75. package/dist/{core → timer}/timer.js +19 -14
  76. package/package.json +27 -2
  77. package/dist/core/audio.d.ts.map +0 -1
  78. package/dist/core/canvas-config.d.ts +0 -22
  79. package/dist/core/canvas-config.d.ts.map +0 -1
  80. package/dist/core/canvas-config.js +0 -14
  81. package/dist/core/canvas-manager.d.ts +0 -32
  82. package/dist/core/canvas-manager.d.ts.map +0 -1
  83. package/dist/core/canvas-manager.js +0 -179
  84. package/dist/core/events.d.ts +0 -31
  85. package/dist/core/events.d.ts.map +0 -1
  86. package/dist/core/gamepad-buttons.d.ts +0 -23
  87. package/dist/core/gamepad-buttons.d.ts.map +0 -1
  88. package/dist/core/gamepad-buttons.js +0 -36
  89. package/dist/core/gamepad-mapping.d.ts +0 -19
  90. package/dist/core/gamepad-mapping.d.ts.map +0 -1
  91. package/dist/core/gamepad-mapping.js +0 -223
  92. package/dist/core/gamepad.d.ts +0 -61
  93. package/dist/core/gamepad.d.ts.map +0 -1
  94. package/dist/core/gamepad.js +0 -237
  95. package/dist/core/graphics.d.ts +0 -93
  96. package/dist/core/graphics.d.ts.map +0 -1
  97. package/dist/core/graphics.js +0 -289
  98. package/dist/core/input-state.d.ts +0 -14
  99. package/dist/core/input-state.d.ts.map +0 -1
  100. package/dist/core/input-state.js +0 -50
  101. package/dist/core/input.d.ts +0 -33
  102. package/dist/core/input.d.ts.map +0 -1
  103. package/dist/core/input.js +0 -117
  104. package/dist/core/keyboard.d.ts +0 -16
  105. package/dist/core/keyboard.d.ts.map +0 -1
  106. package/dist/core/keyboard.js +0 -83
  107. package/dist/core/like.d.ts +0 -59
  108. package/dist/core/like.d.ts.map +0 -1
  109. package/dist/core/mouse.d.ts +0 -45
  110. package/dist/core/mouse.d.ts.map +0 -1
  111. package/dist/core/mouse.js +0 -182
  112. package/dist/core/rect.d.ts +0 -26
  113. package/dist/core/rect.d.ts.map +0 -1
  114. package/dist/core/timer.d.ts +0 -18
  115. package/dist/core/timer.d.ts.map +0 -1
  116. package/dist/core/vector2.d.ts +0 -26
  117. package/dist/core/vector2.d.ts.map +0 -1
  118. package/dist/core/vector2.js +0 -105
  119. package/dist/scenes/startup.d.ts +0 -18
  120. package/dist/scenes/startup.d.ts.map +0 -1
  121. /package/dist/{core/events.js → audio/index.js} +0 -0
  122. /package/dist/{core/like.js → timer/index.js} +0 -0
@@ -1,14 +0,0 @@
1
- /**
2
- * Calculate the scale and offset for rendering fixed-size content to a target canvas.
3
- * This is useful when you want to render in "native" mode but maintain a fixed game resolution.
4
- *
5
- * @param canvasSize - The actual canvas size in pixels
6
- * @param gameSize - The desired game resolution (fixed size)
7
- * @returns Object containing the scale factor and offset for centering
8
- */
9
- export function calcFixedScale(canvasSize, gameSize) {
10
- const scale = Math.min(canvasSize[0] / gameSize[0], canvasSize[1] / gameSize[1]);
11
- const scaledGame = [gameSize[0] * scale, gameSize[1] * scale];
12
- const offset = [(canvasSize[0] - scaledGame[0]) * 0.5, (canvasSize[1] - scaledGame[1]) * 0.5];
13
- return { scale, offset };
14
- }
@@ -1,32 +0,0 @@
1
- import type { CanvasMode, PartialCanvasMode } from './canvas-config';
2
- import { type Vector2 } from './vector2';
3
- export declare class CanvasManager {
4
- private canvas;
5
- private container;
6
- private ctx;
7
- private config;
8
- private resizeObserver;
9
- private pixelCanvas;
10
- private pixelCtx;
11
- private onWindowResize;
12
- onResize: ((size: Vector2, pixelSize: Vector2, fullscreen: boolean) => void) | null;
13
- constructor(canvas: HTMLCanvasElement, container: HTMLElement, ctx: CanvasRenderingContext2D, config?: CanvasMode);
14
- private listenForPixelRatioChanges;
15
- setMode(mode: PartialCanvasMode): void;
16
- getMode(): CanvasMode;
17
- private applyConfig;
18
- private applyPixelMode;
19
- private applyNativeMode;
20
- dispose(): void;
21
- present(): void;
22
- getDisplayCanvas(): HTMLCanvasElement;
23
- /** Canvas to render to. Pixel canvas in pixel mode, display canvas otherwise. */
24
- getRenderTarget(): {
25
- canvas: HTMLCanvasElement;
26
- ctx: CanvasRenderingContext2D;
27
- };
28
- /** Render target size in pixels. Pixel resolution in pixel mode, scaled size otherwise. */
29
- getCanvasSize(): Vector2;
30
- transformMousePosition(offsetX: number, offsetY: number): Vector2;
31
- }
32
- //# sourceMappingURL=canvas-manager.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"canvas-manager.d.ts","sourceRoot":"","sources":["../../src/core/canvas-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAQ,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAmB/C,qBAAa,aAAa;IAUtB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,GAAG;IACX,OAAO,CAAC,MAAM;IAZhB,OAAO,CAAC,cAAc,CAA+B;IACrD,OAAO,CAAC,WAAW,CAAkC;IACrD,OAAO,CAAC,QAAQ,CAAyC;IAEzD,OAAO,CAAC,cAAc,CAA4B;IAE3C,QAAQ,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,KAAK,IAAI,CAAC,GAAG,IAAI,CAAQ;gBAGxF,MAAM,EAAE,iBAAiB,EACzB,SAAS,EAAE,WAAW,EACtB,GAAG,EAAE,wBAAwB,EAC7B,MAAM,GAAE,UAAyD;IAW3E,OAAO,CAAC,0BAA0B;IAQlC,OAAO,CAAC,IAAI,EAAE,iBAAiB,GAAG,IAAI;IAKtC,OAAO,IAAI,UAAU;IAIrB,OAAO,CAAC,WAAW;IAwBnB,OAAO,CAAC,cAAc;IAqBtB,OAAO,CAAC,eAAe;IAkBvB,OAAO,IAAI,IAAI;IAQf,OAAO,IAAI,IAAI;IAaf,gBAAgB,IAAI,iBAAiB;IAIrC,iFAAiF;IACjF,eAAe,IAAI;QAAE,MAAM,EAAE,iBAAiB,CAAC;QAAC,GAAG,EAAE,wBAAwB,CAAA;KAAE;IAO/E,2FAA2F;IAC3F,aAAa,IAAI,OAAO;IAKxB,sBAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO;CAYlE"}
@@ -1,179 +0,0 @@
1
- import { Vec2 } from './vector2';
2
- function setCanvasSize(canvas, size) {
3
- canvas.width = size[0];
4
- canvas.height = size[1];
5
- }
6
- function setCanvasDisplaySize(canvas, size) {
7
- canvas.style.width = `${size[0]}px`;
8
- canvas.style.height = `${size[1]}px`;
9
- }
10
- function centerElement(el) {
11
- el.style.position = 'absolute';
12
- el.style.left = '50%';
13
- el.style.top = '50%';
14
- el.style.transform = 'translate(-50%, -50%)';
15
- }
16
- export class CanvasManager {
17
- constructor(canvas, container, ctx, config = { pixelResolution: null, fullscreen: false }) {
18
- Object.defineProperty(this, "canvas", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: canvas
23
- });
24
- Object.defineProperty(this, "container", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: container
29
- });
30
- Object.defineProperty(this, "ctx", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: ctx
35
- });
36
- Object.defineProperty(this, "config", {
37
- enumerable: true,
38
- configurable: true,
39
- writable: true,
40
- value: config
41
- });
42
- Object.defineProperty(this, "resizeObserver", {
43
- enumerable: true,
44
- configurable: true,
45
- writable: true,
46
- value: null
47
- });
48
- Object.defineProperty(this, "pixelCanvas", {
49
- enumerable: true,
50
- configurable: true,
51
- writable: true,
52
- value: null
53
- });
54
- Object.defineProperty(this, "pixelCtx", {
55
- enumerable: true,
56
- configurable: true,
57
- writable: true,
58
- value: null
59
- });
60
- Object.defineProperty(this, "onWindowResize", {
61
- enumerable: true,
62
- configurable: true,
63
- writable: true,
64
- value: () => this.applyConfig()
65
- });
66
- Object.defineProperty(this, "onResize", {
67
- enumerable: true,
68
- configurable: true,
69
- writable: true,
70
- value: null
71
- });
72
- this.resizeObserver = new ResizeObserver(() => this.applyConfig());
73
- this.resizeObserver.observe(this.container);
74
- window.addEventListener('resize', this.onWindowResize);
75
- this.listenForPixelRatioChanges();
76
- this.applyConfig();
77
- }
78
- listenForPixelRatioChanges() {
79
- const media = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
80
- media.addEventListener('change', () => {
81
- this.applyConfig();
82
- this.listenForPixelRatioChanges();
83
- }, { once: true });
84
- }
85
- setMode(mode) {
86
- this.config = { ...this.config, ...mode };
87
- this.applyConfig();
88
- }
89
- getMode() {
90
- return { ...this.config };
91
- }
92
- applyConfig() {
93
- const containerSize = document.fullscreenElement
94
- ? [document.fullscreenElement.clientWidth, document.fullscreenElement.clientHeight]
95
- : [this.container.clientWidth, this.container.clientHeight];
96
- if (this.pixelCanvas) {
97
- this.pixelCanvas.remove();
98
- this.pixelCanvas = null;
99
- this.pixelCtx = null;
100
- }
101
- if (this.config.pixelResolution) {
102
- this.applyPixelMode(containerSize);
103
- }
104
- else {
105
- this.applyNativeMode(containerSize);
106
- }
107
- this.onResize?.(containerSize, [this.canvas.width, this.canvas.height], this.config.fullscreen);
108
- }
109
- applyPixelMode(csize) {
110
- const gameSize = this.config.pixelResolution;
111
- const pixelRatio = window.devicePixelRatio || 1;
112
- const scale = Math.min(csize[0] / gameSize[0], csize[1] / gameSize[1]);
113
- const physicalScale = scale * pixelRatio;
114
- const intScale = Math.max(1, Math.round(physicalScale));
115
- this.pixelCanvas = document.createElement('canvas');
116
- this.pixelCtx = this.pixelCanvas.getContext('2d');
117
- setCanvasSize(this.pixelCanvas, gameSize);
118
- setCanvasSize(this.canvas, Vec2.mul(gameSize, intScale));
119
- setCanvasDisplaySize(this.canvas, Vec2.mul(gameSize, scale));
120
- this.canvas.style.maxWidth = '100%';
121
- this.canvas.style.maxHeight = '100%';
122
- this.canvas.style.imageRendering = 'auto';
123
- centerElement(this.canvas);
124
- }
125
- applyNativeMode(csize) {
126
- const pixelRatio = window.devicePixelRatio || 1;
127
- const canvasSize = Vec2.mul(csize, pixelRatio);
128
- setCanvasSize(this.canvas, Vec2.floor(canvasSize));
129
- setCanvasDisplaySize(this.canvas, csize);
130
- this.canvas.style.position = 'absolute';
131
- this.canvas.style.left = '0';
132
- this.canvas.style.top = '0';
133
- this.canvas.style.transform = 'none';
134
- this.canvas.style.margin = '0';
135
- this.canvas.style.display = 'block';
136
- this.canvas.style.imageRendering = 'auto';
137
- this.ctx.setTransform(1, 0, 0, 1, 0, 0);
138
- }
139
- dispose() {
140
- this.resizeObserver?.disconnect();
141
- window.removeEventListener('resize', this.onWindowResize);
142
- this.pixelCanvas?.remove();
143
- this.pixelCanvas = null;
144
- this.pixelCtx = null;
145
- }
146
- present() {
147
- if (!this.pixelCtx || !this.pixelCanvas) {
148
- return;
149
- }
150
- this.ctx.imageSmoothingEnabled = false;
151
- this.ctx.drawImage(this.pixelCanvas, 0, 0, this.pixelCanvas.width, this.pixelCanvas.height, 0, 0, this.canvas.width, this.canvas.height);
152
- }
153
- getDisplayCanvas() {
154
- return this.canvas;
155
- }
156
- /** Canvas to render to. Pixel canvas in pixel mode, display canvas otherwise. */
157
- getRenderTarget() {
158
- if (this.pixelCanvas && this.pixelCtx) {
159
- return { canvas: this.pixelCanvas, ctx: this.pixelCtx };
160
- }
161
- return { canvas: this.canvas, ctx: this.ctx };
162
- }
163
- /** Render target size in pixels. Pixel resolution in pixel mode, scaled size otherwise. */
164
- getCanvasSize() {
165
- const target = this.getRenderTarget();
166
- return [target.canvas.width, target.canvas.height];
167
- }
168
- transformMousePosition(offsetX, offsetY) {
169
- if (this.config.pixelResolution) {
170
- const rect = this.canvas.getBoundingClientRect();
171
- return [
172
- offsetX * (this.config.pixelResolution[0] / rect.width),
173
- offsetY * (this.config.pixelResolution[1] / rect.height),
174
- ];
175
- }
176
- const pixelRatio = window.devicePixelRatio || 1;
177
- return [offsetX * pixelRatio, offsetY * pixelRatio];
178
- }
179
- }
@@ -1,31 +0,0 @@
1
- import type { Vector2 } from './vector2';
2
- /** Game events. All events flow through handleEvent; these are also available as individual callbacks. */
3
- export type EventMap = {
4
- load: [];
5
- update: [dt: number];
6
- draw: [];
7
- resize: [size: Vector2, pixelSize: Vector2, fullscreen: boolean];
8
- keypressed: [scancode: string, keycode: string];
9
- keyreleased: [scancode: string, keycode: string];
10
- focus: [];
11
- blur: [];
12
- /** Mouse moved. relative=true means pos is delta [dx, dy] (pointer lock). */
13
- mousemoved: [pos: Vector2, relative: boolean];
14
- /** Mouse button pressed. pos in canvas pixels. Button 1 = left, 2 = middle, 3 = right. */
15
- mousepressed: [pos: Vector2, button: number];
16
- /** Mouse button released. */
17
- mousereleased: [pos: Vector2, button: number];
18
- gamepadpressed: [gamepadIndex: number, buttonIndex: number, buttonName: string];
19
- gamepadreleased: [gamepadIndex: number, buttonIndex: number, buttonName: string];
20
- actionpressed: [action: string];
21
- actionreleased: [action: string];
22
- };
23
- export type EventType = keyof EventMap;
24
- export type Like2DEvent = {
25
- [K in EventType]: {
26
- type: K;
27
- args: EventMap[K];
28
- timestamp: number;
29
- };
30
- }[EventType];
31
- //# sourceMappingURL=events.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/core/events.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC,0GAA0G;AAC1G,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,EAAE,CAAC;IACT,MAAM,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IACrB,IAAI,EAAE,EAAE,CAAC;IACT,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IACjE,UAAU,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAChD,WAAW,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACjD,KAAK,EAAE,EAAE,CAAC;IACV,IAAI,EAAE,EAAE,CAAC;IACT,6EAA6E;IAC7E,UAAU,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC9C,0FAA0F;IAC1F,YAAY,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7C,6BAA6B;IAC7B,aAAa,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,cAAc,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;IAChF,eAAe,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;IACjF,aAAa,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,cAAc,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC;AAGvC,MAAM,MAAM,WAAW,GAAG;KACvB,CAAC,IAAI,SAAS,GAAG;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE;CACpE,CAAC,SAAS,CAAC,CAAC"}
@@ -1,23 +0,0 @@
1
- export declare namespace GP {
2
- const Bottom = 0;
3
- const Right = 1;
4
- const Left = 2;
5
- const Top = 3;
6
- const LB = 4;
7
- const RB = 5;
8
- const LT = 6;
9
- const RT = 7;
10
- const Back = 8;
11
- const Start = 9;
12
- const Guide = 10;
13
- const LS = 11;
14
- const RS = 12;
15
- const DUp = 13;
16
- const DDown = 14;
17
- const DLeft = 15;
18
- const DRight = 16;
19
- }
20
- export declare const GP_NAMES: Record<number, string>;
21
- export declare const GP_NAME_MAP: Record<string, number>;
22
- export declare function getGPName(buttonIndex: number): string;
23
- //# sourceMappingURL=gamepad-buttons.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"gamepad-buttons.d.ts","sourceRoot":"","sources":["../../src/core/gamepad-buttons.ts"],"names":[],"mappings":"AAGA,yBAAiB,EAAE,CAAC;IAEX,MAAM,MAAM,IAAI,CAAC;IACjB,MAAM,KAAK,IAAI,CAAC;IAChB,MAAM,IAAI,IAAI,CAAC;IACf,MAAM,GAAG,IAAI,CAAC;IAGd,MAAM,EAAE,IAAI,CAAC;IACb,MAAM,EAAE,IAAI,CAAC;IACb,MAAM,EAAE,IAAI,CAAC;IACb,MAAM,EAAE,IAAI,CAAC;IAGb,MAAM,IAAI,IAAI,CAAC;IACf,MAAM,KAAK,IAAI,CAAC;IAChB,MAAM,KAAK,KAAK,CAAC;IAGjB,MAAM,EAAE,KAAK,CAAC;IACd,MAAM,EAAE,KAAK,CAAC;IAGd,MAAM,GAAG,KAAK,CAAC;IACf,MAAM,KAAK,KAAK,CAAC;IACjB,MAAM,KAAK,KAAK,CAAC;IACjB,MAAM,MAAM,KAAK,CAAC;CAC1B;AAMD,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAE3C,CAAC;AAGF,eAAO,MAAM,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAkC,CAAC;AAElF,wBAAgB,SAAS,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAErD"}
@@ -1,36 +0,0 @@
1
- // Gamepad button constants
2
- // Use these constants instead of magic numbers
3
- export var GP;
4
- (function (GP) {
5
- // Face buttons (physical positions)
6
- GP.Bottom = 0; // A on Xbox, X on PlayStation, B on Nintendo
7
- GP.Right = 1; // B on Xbox, O on PlayStation, A on Nintendo
8
- GP.Left = 2; // X on Xbox, □ on PlayStation, Y on Nintendo
9
- GP.Top = 3; // Y on Xbox, △ on PlayStation, X on Nintendo
10
- // Bumpers and triggers
11
- GP.LB = 4; // Left bumper (L1)
12
- GP.RB = 5; // Right bumper (R1)
13
- GP.LT = 6; // Left trigger (L2)
14
- GP.RT = 7; // Right trigger (R2)
15
- // Menu buttons
16
- GP.Back = 8; // Select/Back/Minus
17
- GP.Start = 9; // Start/Plus
18
- GP.Guide = 10; // Home/Guide button
19
- // Stick presses
20
- GP.LS = 11; // Left stick press (L3)
21
- GP.RS = 12; // Right stick press (R3)
22
- // D-Pad
23
- GP.DUp = 13;
24
- GP.DDown = 14;
25
- GP.DLeft = 15;
26
- GP.DRight = 16;
27
- })(GP || (GP = {}));
28
- // Build reverse mappings programmatically
29
- const GP_ENTRIES = Object.entries(GP);
30
- // Map button index to name (for debugging/display)
31
- export const GP_NAMES = Object.fromEntries(GP_ENTRIES.map(([name, index]) => [index, name]));
32
- // Map name to button index (for action system parsing)
33
- export const GP_NAME_MAP = Object.fromEntries(GP_ENTRIES);
34
- export function getGPName(buttonIndex) {
35
- return GP_NAMES[buttonIndex] ?? `Button${buttonIndex}`;
36
- }
@@ -1,19 +0,0 @@
1
- export interface ButtonMapping {
2
- toStandard: Map<number, number>;
3
- controllerName: string;
4
- hasMapping: boolean;
5
- vendor: number | null;
6
- product: number | null;
7
- }
8
- export declare class GamepadMapping {
9
- private dbLoaded;
10
- loadDatabase(): Promise<void>;
11
- loadDatabaseFromText(content: string): void;
12
- /**
13
- * Get button mapping for a specific gamepad
14
- */
15
- getMapping(gamepad: Gamepad): ButtonMapping;
16
- private extractVendorProduct;
17
- }
18
- export declare const gamepadMapping: GamepadMapping;
19
- //# sourceMappingURL=gamepad-mapping.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"gamepad-mapping.d.ts","sourceRoot":"","sources":["../../src/core/gamepad-mapping.ts"],"names":[],"mappings":"AA+HA,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,OAAO,CAAC;IACpB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAS;IAEnB,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IA+BnC,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAK3C;;OAEG;IACH,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,aAAa;IAsC3C,OAAO,CAAC,oBAAoB;CAuB7B;AAUD,eAAO,MAAM,cAAc,gBAAuB,CAAC"}
@@ -1,223 +0,0 @@
1
- // Gamepad button mapping layer
2
- // Bridges SDL database mappings with our internal button naming system
3
- import { GP } from './gamepad-buttons';
4
- // Map SDL button names to our standard button indices
5
- const SDL_TO_GP = {
6
- 'a': GP.Bottom,
7
- 'b': GP.Right,
8
- 'x': GP.Left,
9
- 'y': GP.Top,
10
- 'leftshoulder': GP.LB,
11
- 'rightshoulder': GP.RB,
12
- 'lefttrigger': GP.LT,
13
- 'righttrigger': GP.RT,
14
- 'back': GP.Back,
15
- 'start': GP.Start,
16
- 'guide': GP.Guide,
17
- 'leftstick': GP.LS,
18
- 'rightstick': GP.RS,
19
- 'dpup': GP.DUp,
20
- 'dpdown': GP.DDown,
21
- 'dpleft': GP.DLeft,
22
- 'dpright': GP.DRight,
23
- };
24
- // Internal database for storing pre-built controller mappings
25
- class GamepadDatabase {
26
- constructor() {
27
- // vendorProductKey -> mapping
28
- Object.defineProperty(this, "mappings", {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: new Map()
33
- });
34
- Object.defineProperty(this, "loaded", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: false
39
- });
40
- }
41
- load(dbContent) {
42
- this.mappings.clear();
43
- const lines = dbContent.split('\n');
44
- for (const line of lines) {
45
- const trimmed = line.trim();
46
- if (!trimmed || trimmed.startsWith('#')) {
47
- continue;
48
- }
49
- const mapping = this.parseLine(trimmed);
50
- if (mapping) {
51
- // Extract vendor/product from GUID and store mapping
52
- const guid = line.split(',')[0].toLowerCase().trim();
53
- const vpKey = this.extractVendorProductKey(guid);
54
- if (vpKey !== null && !this.mappings.has(vpKey)) {
55
- this.mappings.set(vpKey, mapping);
56
- }
57
- }
58
- }
59
- this.loaded = true;
60
- }
61
- extractVendorProductKey(guid) {
62
- if (guid.length < 20)
63
- return null;
64
- const vendorHex = guid.substring(8, 12);
65
- const productHex = guid.substring(16, 20);
66
- const vendor = parseInt(vendorHex.substring(2, 4) + vendorHex.substring(0, 2), 16);
67
- const product = parseInt(productHex.substring(2, 4) + productHex.substring(0, 2), 16);
68
- if (isNaN(vendor) || isNaN(product))
69
- return null;
70
- return 0x10000 * vendor + product;
71
- }
72
- isLoaded() {
73
- return this.loaded;
74
- }
75
- getMappingCount() {
76
- return this.mappings.size;
77
- }
78
- getMapping(vendor, product) {
79
- const key = 0x10000 * vendor + product;
80
- return this.mappings.get(key);
81
- }
82
- parseLine(line) {
83
- const parts = line.split(',');
84
- if (parts.length < 3) {
85
- return null;
86
- }
87
- const name = parts[1].trim();
88
- const toStandard = new Map();
89
- // Parse mapping pairs (key:value)
90
- for (let i = 2; i < parts.length; i++) {
91
- const part = parts[i].trim();
92
- if (!part || part.startsWith('platform:'))
93
- continue;
94
- const colonIndex = part.indexOf(':');
95
- if (colonIndex === -1)
96
- continue;
97
- const sdlName = part.substring(0, colonIndex).trim();
98
- const value = part.substring(colonIndex + 1).trim();
99
- if (!sdlName || !value)
100
- continue;
101
- // Only handle button mappings (b0, b1, etc.)
102
- if (value.startsWith('b')) {
103
- const controllerIndex = parseInt(value.substring(1), 10);
104
- const gpIndex = SDL_TO_GP[sdlName];
105
- if (!isNaN(controllerIndex) && gpIndex !== undefined) {
106
- toStandard.set(controllerIndex, gpIndex);
107
- }
108
- }
109
- }
110
- return { name, toStandard };
111
- }
112
- }
113
- // Singleton instance
114
- const gamepadDatabase = new GamepadDatabase();
115
- export class GamepadMapping {
116
- constructor() {
117
- Object.defineProperty(this, "dbLoaded", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: false
122
- });
123
- }
124
- async loadDatabase() {
125
- if (this.dbLoaded)
126
- return;
127
- try {
128
- const res = await fetch('./gamecontrollerdb.txt');
129
- if (res.ok) {
130
- const text = await res.text();
131
- // Validate it's actually the DB file, not HTML error page
132
- if (text.startsWith('# Game Controller DB') || text.includes('03000000')) {
133
- gamepadDatabase.load(text);
134
- this.dbLoaded = true;
135
- }
136
- }
137
- }
138
- catch { }
139
- if (!this.dbLoaded) {
140
- try {
141
- // @ts-ignore - Vite handles ?raw imports
142
- const module = await import('../gamecontrollerdb.txt?raw');
143
- if (typeof module.default === 'string') {
144
- gamepadDatabase.load(module.default);
145
- this.dbLoaded = true;
146
- }
147
- }
148
- catch { }
149
- }
150
- if (this.dbLoaded) {
151
- console.log(`[Gamepad] Loaded ${gamepadDatabase.getMappingCount()} controller mappings`);
152
- }
153
- }
154
- loadDatabaseFromText(content) {
155
- gamepadDatabase.load(content);
156
- this.dbLoaded = true;
157
- }
158
- /**
159
- * Get button mapping for a specific gamepad
160
- */
161
- getMapping(gamepad) {
162
- const vp = this.extractVendorProduct(gamepad);
163
- // If browser provides "standard" mapping, use identity mapping
164
- if (gamepad.mapping === 'standard') {
165
- return {
166
- toStandard: IDENTITY_MAP,
167
- controllerName: gamepad.id,
168
- hasMapping: true,
169
- vendor: vp?.vendor ?? null,
170
- product: vp?.product ?? null,
171
- };
172
- }
173
- // Look up in database
174
- if (vp && this.dbLoaded) {
175
- const dbMapping = gamepadDatabase.getMapping(vp.vendor, vp.product);
176
- if (dbMapping) {
177
- return {
178
- toStandard: dbMapping.toStandard,
179
- controllerName: dbMapping.name,
180
- hasMapping: true,
181
- vendor: vp.vendor,
182
- product: vp.product,
183
- };
184
- }
185
- }
186
- // No mapping found - use identity
187
- return {
188
- toStandard: IDENTITY_MAP,
189
- controllerName: gamepad.id,
190
- hasMapping: false,
191
- vendor: vp?.vendor ?? null,
192
- product: vp?.product ?? null,
193
- };
194
- }
195
- extractVendorProduct(gamepad) {
196
- const id = gamepad.id;
197
- const vendorProductMatch = id.match(/Vendor:\s*([0-9a-fA-F]+)\s+Product:\s*([0-9a-fA-F]+)/i);
198
- if (vendorProductMatch) {
199
- const vendor = parseInt(vendorProductMatch[1], 16);
200
- const product = parseInt(vendorProductMatch[2], 16);
201
- if (!isNaN(vendor) && !isNaN(product)) {
202
- return { vendor, product };
203
- }
204
- }
205
- const hexMatch = id.match(/^([0-9a-fA-F]{4})[\s-]+([0-9a-fA-F]{4})/);
206
- if (hexMatch) {
207
- const vendor = parseInt(hexMatch[1], 16);
208
- const product = parseInt(hexMatch[2], 16);
209
- if (!isNaN(vendor) && !isNaN(product)) {
210
- return { vendor, product };
211
- }
212
- }
213
- return null;
214
- }
215
- }
216
- // Reusable identity map for standard/unmapped controllers
217
- const IDENTITY_MAP = new Map([
218
- [0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7],
219
- [8, 8], [9, 9], [10, 10], [11, 11], [12, 12], [13, 13], [14, 14], [15, 15],
220
- [16, 16], [17, 17], [18, 18], [19, 19],
221
- ]);
222
- // Singleton instance
223
- export const gamepadMapping = new GamepadMapping();
@@ -1,61 +0,0 @@
1
- import { getGPName, GP } from './gamepad-buttons';
2
- import { ButtonMapping } from './gamepad-mapping';
3
- export { GP, getGPName };
4
- export interface StickPosition {
5
- x: number;
6
- y: number;
7
- }
8
- export type ButtonCallback = (gamepadIndex: number, buttonIndex: number, buttonName: string, pressed: boolean) => void;
9
- export declare class Gamepad {
10
- private buttonTrackers;
11
- private connectedGamepads;
12
- private buttonMappings;
13
- onButtonEvent?: (gamepadIndex: number, buttonIndex: number, buttonName: string, pressed: boolean) => void;
14
- private onConnected?;
15
- private onDisconnected?;
16
- private gamepadConnectedHandler;
17
- private gamepadDisconnectedHandler;
18
- private blurHandler;
19
- constructor();
20
- private handleGamepadConnected;
21
- private handleGamepadDisconnected;
22
- private handleBlur;
23
- setCallbacks(callbacks: {
24
- onConnected?: (gamepad: globalThis.Gamepad) => void;
25
- onDisconnected?: (gamepadIndex: number) => void;
26
- }): void;
27
- dispose(): void;
28
- init(): Promise<void>;
29
- private onGamepadConnectedInternal;
30
- private onGamepadDisconnectedInternal;
31
- update(): void;
32
- isConnected(gamepadIndex: number): boolean;
33
- /**
34
- * Check if a button is currently pressed on a specific gamepad
35
- * Uses mapped button indices (standard layout)
36
- */
37
- isButtonDown(gamepadIndex: number, buttonIndex: number): boolean;
38
- isButtonDownOnAny(buttonIndex: number): boolean;
39
- getPressedButtons(gamepadIndex: number): Set<number>;
40
- getConnectedGamepads(): number[];
41
- /**
42
- * Get the raw Gamepad object for a specific index
43
- */
44
- getGamepad(gamepadIndex: number): globalThis.Gamepad | undefined;
45
- /**
46
- * Get the button mapping for a specific gamepad
47
- */
48
- getButtonMapping(gamepadIndex: number): ButtonMapping | undefined;
49
- /**
50
- * Check if a gamepad has a known mapping from the database
51
- */
52
- hasMapping(gamepadIndex: number): boolean;
53
- /**
54
- * Get the controller name for a specific gamepad
55
- */
56
- getControllerName(gamepadIndex: number): string | undefined;
57
- getAxis(gamepadIndex: number, axisIndex: number): number;
58
- getLeftStick(gamepadIndex: number): StickPosition;
59
- getRightStick(gamepadIndex: number): StickPosition;
60
- }
61
- //# sourceMappingURL=gamepad.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"gamepad.d.ts","sourceRoot":"","sources":["../../src/core/gamepad.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAkB,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC;AAEzB,MAAM,WAAW,aAAa;IAC5B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAkBD,MAAM,MAAM,cAAc,GAAG,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;AAEvH,qBAAa,OAAO;IAClB,OAAO,CAAC,cAAc,CAAgD;IACtE,OAAO,CAAC,iBAAiB,CAAyC;IAClE,OAAO,CAAC,cAAc,CAAoC;IACnD,aAAa,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IACjH,OAAO,CAAC,WAAW,CAAC,CAAwC;IAC5D,OAAO,CAAC,cAAc,CAAC,CAAiC;IAGxD,OAAO,CAAC,uBAAuB,CAAuC;IACtE,OAAO,CAAC,0BAA0B,CAAuC;IACzE,OAAO,CAAC,WAAW,CAAa;;IAchC,OAAO,CAAC,sBAAsB;IAK9B,OAAO,CAAC,yBAAyB;IAKjC,OAAO,CAAC,UAAU;IAMlB,YAAY,CAAC,SAAS,EAAE;QACtB,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,KAAK,IAAI,CAAC;QACpD,cAAc,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,IAAI,CAAC;KACjD,GAAG,IAAI;IAKR,OAAO,IAAI,IAAI;IAST,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,6BAA6B;IAMrC,MAAM,IAAI,IAAI;IA4Cd,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO;IAI1C;;;OAGG;IACH,YAAY,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO;IAKhE,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO;IAO/C,iBAAiB,CAAC,YAAY,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;IAKpD,oBAAoB,IAAI,MAAM,EAAE;IAIhC;;OAEG;IACH,UAAU,CAAC,YAAY,EAAE,MAAM,GAAG,UAAU,CAAC,OAAO,GAAG,SAAS;IAIhE;;OAEG;IACH,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAIjE;;OAEG;IACH,UAAU,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO;IAKzC;;OAEG;IACH,iBAAiB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAK3D,OAAO,CAAC,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM;IAMxD,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,aAAa;IAMjD,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,aAAa;CAKnD"}