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,237 +0,0 @@
1
- import { getGPName, GP } from './gamepad-buttons';
2
- import { InputStateTracker } from './input-state';
3
- import { gamepadMapping } from './gamepad-mapping';
4
- export { GP, getGPName };
5
- const AXIS_DEADZONE = 0.15;
6
- function applyDeadzone(value, deadzone = AXIS_DEADZONE) {
7
- if (Math.abs(value) < deadzone)
8
- return 0;
9
- const sign = value < 0 ? -1 : 1;
10
- const magnitude = Math.abs(value);
11
- return sign * (magnitude - deadzone) / (1 - deadzone);
12
- }
13
- function applyRadialDeadzone(x, y, deadzone = AXIS_DEADZONE) {
14
- const magnitude = Math.sqrt(x * x + y * y);
15
- if (magnitude < deadzone)
16
- return { x: 0, y: 0 };
17
- const scale = (magnitude - deadzone) / (magnitude * (1 - deadzone));
18
- return { x: x * scale, y: y * scale };
19
- }
20
- export class Gamepad {
21
- constructor() {
22
- Object.defineProperty(this, "buttonTrackers", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: new Map()
27
- });
28
- Object.defineProperty(this, "connectedGamepads", {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: new Map()
33
- });
34
- Object.defineProperty(this, "buttonMappings", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: new Map()
39
- });
40
- Object.defineProperty(this, "onButtonEvent", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: void 0
45
- });
46
- Object.defineProperty(this, "onConnected", {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: void 0
51
- });
52
- Object.defineProperty(this, "onDisconnected", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: void 0
57
- });
58
- // Event handler references for cleanup
59
- Object.defineProperty(this, "gamepadConnectedHandler", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: void 0
64
- });
65
- Object.defineProperty(this, "gamepadDisconnectedHandler", {
66
- enumerable: true,
67
- configurable: true,
68
- writable: true,
69
- value: void 0
70
- });
71
- Object.defineProperty(this, "blurHandler", {
72
- enumerable: true,
73
- configurable: true,
74
- writable: true,
75
- value: void 0
76
- });
77
- // Bind event handlers
78
- this.gamepadConnectedHandler = this.handleGamepadConnected.bind(this);
79
- this.gamepadDisconnectedHandler = this.handleGamepadDisconnected.bind(this);
80
- this.blurHandler = this.handleBlur.bind(this);
81
- // Register event listeners
82
- window.addEventListener('gamepadconnected', this.gamepadConnectedHandler);
83
- window.addEventListener('gamepaddisconnected', this.gamepadDisconnectedHandler);
84
- window.addEventListener('blur', this.blurHandler);
85
- }
86
- handleGamepadConnected(e) {
87
- this.onGamepadConnectedInternal(e.gamepad);
88
- this.onConnected?.(e.gamepad);
89
- }
90
- handleGamepadDisconnected(e) {
91
- this.onGamepadDisconnectedInternal(e.gamepad.index);
92
- this.onDisconnected?.(e.gamepad.index);
93
- }
94
- handleBlur() {
95
- for (const tracker of this.buttonTrackers.values()) {
96
- tracker.clear();
97
- }
98
- }
99
- setCallbacks(callbacks) {
100
- this.onConnected = callbacks.onConnected;
101
- this.onDisconnected = callbacks.onDisconnected;
102
- }
103
- dispose() {
104
- window.removeEventListener('gamepadconnected', this.gamepadConnectedHandler);
105
- window.removeEventListener('gamepaddisconnected', this.gamepadDisconnectedHandler);
106
- window.removeEventListener('blur', this.blurHandler);
107
- this.connectedGamepads.clear();
108
- this.buttonTrackers.clear();
109
- this.buttonMappings.clear();
110
- }
111
- async init() {
112
- await gamepadMapping.loadDatabase();
113
- }
114
- onGamepadConnectedInternal(gamepad) {
115
- this.connectedGamepads.set(gamepad.index, gamepad);
116
- this.buttonTrackers.set(gamepad.index, new InputStateTracker());
117
- const mapping = gamepadMapping.getMapping(gamepad);
118
- this.buttonMappings.set(gamepad.index, mapping);
119
- console.log(`[Gamepad] Connected: "${gamepad.id}"`);
120
- if (mapping.vendor !== null && mapping.product !== null) {
121
- console.log(`[Gamepad] Vendor: 0x${mapping.vendor.toString(16).padStart(4, '0')}, Product: 0x${mapping.product.toString(16).padStart(4, '0')}`);
122
- }
123
- const mappingType = gamepad.mapping === 'standard' ? 'browser standard' : (mapping.hasMapping ? 'SDL DB' : 'unmapped');
124
- console.log(`[Gamepad] Mapped as: "${mapping.controllerName}" (${mappingType})`);
125
- }
126
- onGamepadDisconnectedInternal(gamepadIndex) {
127
- this.connectedGamepads.delete(gamepadIndex);
128
- this.buttonTrackers.delete(gamepadIndex);
129
- this.buttonMappings.delete(gamepadIndex);
130
- }
131
- update() {
132
- const gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
133
- for (let i = 0; i < gamepads.length; i++) {
134
- const gamepad = gamepads[i];
135
- if (gamepad) {
136
- this.connectedGamepads.set(i, gamepad);
137
- let tracker = this.buttonTrackers.get(i);
138
- if (!tracker) {
139
- tracker = new InputStateTracker();
140
- this.buttonTrackers.set(i, tracker);
141
- }
142
- // Get or update the button mapping for this gamepad
143
- let mapping = this.buttonMappings.get(i);
144
- if (!mapping) {
145
- mapping = gamepadMapping.getMapping(gamepad);
146
- this.buttonMappings.set(i, mapping);
147
- }
148
- const pressedButtons = new Set();
149
- for (let j = 0; j < gamepad.buttons.length; j++) {
150
- if (gamepad.buttons[j].pressed) {
151
- // Map the raw button index to standard button index
152
- const standardIndex = mapping.toStandard.get(j);
153
- if (standardIndex !== undefined) {
154
- pressedButtons.add(standardIndex);
155
- }
156
- }
157
- }
158
- const changes = tracker.update(pressedButtons);
159
- for (const buttonIndex of changes.justPressed) {
160
- this.onButtonEvent?.(i, buttonIndex, getGPName(buttonIndex), true);
161
- }
162
- for (const buttonIndex of changes.justReleased) {
163
- this.onButtonEvent?.(i, buttonIndex, getGPName(buttonIndex), false);
164
- }
165
- }
166
- }
167
- }
168
- isConnected(gamepadIndex) {
169
- return this.connectedGamepads.has(gamepadIndex);
170
- }
171
- /**
172
- * Check if a button is currently pressed on a specific gamepad
173
- * Uses mapped button indices (standard layout)
174
- */
175
- isButtonDown(gamepadIndex, buttonIndex) {
176
- const tracker = this.buttonTrackers.get(gamepadIndex);
177
- return tracker ? tracker.isDown(buttonIndex) : false;
178
- }
179
- isButtonDownOnAny(buttonIndex) {
180
- for (const tracker of this.buttonTrackers.values()) {
181
- if (tracker.isDown(buttonIndex))
182
- return true;
183
- }
184
- return false;
185
- }
186
- getPressedButtons(gamepadIndex) {
187
- const tracker = this.buttonTrackers.get(gamepadIndex);
188
- return tracker ? tracker.getCurrentState() : new Set();
189
- }
190
- getConnectedGamepads() {
191
- return Array.from(this.connectedGamepads.keys());
192
- }
193
- /**
194
- * Get the raw Gamepad object for a specific index
195
- */
196
- getGamepad(gamepadIndex) {
197
- return this.connectedGamepads.get(gamepadIndex);
198
- }
199
- /**
200
- * Get the button mapping for a specific gamepad
201
- */
202
- getButtonMapping(gamepadIndex) {
203
- return this.buttonMappings.get(gamepadIndex);
204
- }
205
- /**
206
- * Check if a gamepad has a known mapping from the database
207
- */
208
- hasMapping(gamepadIndex) {
209
- const mapping = this.buttonMappings.get(gamepadIndex);
210
- return mapping?.hasMapping ?? false;
211
- }
212
- /**
213
- * Get the controller name for a specific gamepad
214
- */
215
- getControllerName(gamepadIndex) {
216
- const mapping = this.buttonMappings.get(gamepadIndex);
217
- return mapping?.controllerName;
218
- }
219
- getAxis(gamepadIndex, axisIndex) {
220
- const gamepad = this.connectedGamepads.get(gamepadIndex);
221
- if (!gamepad || axisIndex < 0 || axisIndex >= gamepad.axes.length)
222
- return 0;
223
- return applyDeadzone(gamepad.axes[axisIndex]);
224
- }
225
- getLeftStick(gamepadIndex) {
226
- const gamepad = this.connectedGamepads.get(gamepadIndex);
227
- if (!gamepad || gamepad.axes.length < 2)
228
- return { x: 0, y: 0 };
229
- return applyRadialDeadzone(gamepad.axes[0], gamepad.axes[1]);
230
- }
231
- getRightStick(gamepadIndex) {
232
- const gamepad = this.connectedGamepads.get(gamepadIndex);
233
- if (!gamepad || gamepad.axes.length < 4)
234
- return { x: 0, y: 0 };
235
- return applyRadialDeadzone(gamepad.axes[2], gamepad.axes[3]);
236
- }
237
- }
@@ -1,93 +0,0 @@
1
- import type { Vector2 } from './vector2';
2
- import type { Rect } from './rect';
3
- type DrawMode = 'fill' | 'line';
4
- export type Color = [number, number, number, number?] | string;
5
- export type Quad = Rect;
6
- export type { Vector2, Rect };
7
- export type Canvas = {
8
- size: Vector2;
9
- element: HTMLCanvasElement;
10
- ctx: CanvasRenderingContext2D;
11
- };
12
- export type ShapeProps = {
13
- lineWidth?: number;
14
- lineCap?: 'butt' | 'round' | 'square';
15
- lineJoin?: 'bevel' | 'miter' | 'round';
16
- miterLimit?: number;
17
- };
18
- export type DrawProps = ShapeProps & {
19
- quad?: Quad;
20
- r?: number;
21
- scale?: number | Vector2;
22
- origin?: number | Vector2;
23
- };
24
- export type PrintProps = {
25
- font?: string;
26
- limit?: number;
27
- align?: 'left' | 'center' | 'right';
28
- };
29
- export type GraphicsState = {
30
- screenCtx: CanvasRenderingContext2D;
31
- currentCtx: CanvasRenderingContext2D;
32
- canvases: Map<Canvas, true>;
33
- };
34
- export declare class ImageHandle {
35
- readonly path: string;
36
- private element;
37
- private loadPromise;
38
- private isLoaded;
39
- constructor(path: string);
40
- isReady(): boolean;
41
- ready(): Promise<void>;
42
- get size(): Vector2;
43
- getElement(): HTMLImageElement | null;
44
- }
45
- export declare function newState(ctx: CanvasRenderingContext2D): GraphicsState;
46
- export declare function clear(s: GraphicsState, color?: Color): void;
47
- export declare function rectangle(s: GraphicsState, mode: DrawMode, color: Color, rect: Rect, props?: ShapeProps): void;
48
- export declare function circle(s: GraphicsState, mode: DrawMode, color: Color, position: Vector2, radii: number | Vector2, props?: ShapeProps & {
49
- angle?: number;
50
- arc?: [number, number];
51
- }): void;
52
- export declare function line(s: GraphicsState, color: Color, points: Vector2[], props?: ShapeProps): void;
53
- export declare function print(s: GraphicsState, color: Color, text: string, position: Vector2, props?: PrintProps): void;
54
- export declare function drawImage(s: GraphicsState, handle: ImageHandle, position: Vector2, props?: DrawProps): void;
55
- export declare function getCanvasSize(s: GraphicsState): Vector2;
56
- export declare function newImage(_s: GraphicsState, path: string): ImageHandle;
57
- export declare function newCanvas(s: GraphicsState, size: Vector2): Canvas;
58
- export declare function setCanvas(s: GraphicsState, canvas?: Canvas | null): void;
59
- export declare function setScreenContext(s: GraphicsState, ctx: CanvasRenderingContext2D): void;
60
- export declare function clip(s: GraphicsState, rect?: Rect): void;
61
- export declare function polygon(s: GraphicsState, mode: DrawMode, color: Color, points: Vector2[], props?: ShapeProps): void;
62
- export declare function points(s: GraphicsState, color: Color, pts: Vector2[]): void;
63
- export declare function push(s: GraphicsState): void;
64
- export declare function pop(s: GraphicsState): void;
65
- export declare function translate(s: GraphicsState, offset: Vector2): void;
66
- export declare function rotate(s: GraphicsState, angle: number): void;
67
- export declare function scale(s: GraphicsState, factor: number | Vector2): void;
68
- type Bind<F> = F extends (s: GraphicsState, ...args: infer A) => infer R ? (...args: A) => R : never;
69
- export type BoundGraphics = {
70
- [K in keyof typeof graphicsFns]: Bind<(typeof graphicsFns)[K]>;
71
- };
72
- declare const graphicsFns: {
73
- readonly clear: typeof clear;
74
- readonly rectangle: typeof rectangle;
75
- readonly circle: typeof circle;
76
- readonly line: typeof line;
77
- readonly print: typeof print;
78
- readonly draw: typeof drawImage;
79
- readonly getCanvasSize: typeof getCanvasSize;
80
- readonly newCanvas: typeof newCanvas;
81
- readonly setCanvas: typeof setCanvas;
82
- readonly clip: typeof clip;
83
- readonly polygon: typeof polygon;
84
- readonly points: typeof points;
85
- readonly newImage: typeof newImage;
86
- readonly push: typeof push;
87
- readonly pop: typeof pop;
88
- readonly translate: typeof translate;
89
- readonly rotate: typeof rotate;
90
- readonly scale: typeof scale;
91
- };
92
- export declare function bindGraphics(s: GraphicsState): BoundGraphics;
93
- //# sourceMappingURL=graphics.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"graphics.d.ts","sourceRoot":"","sources":["../../src/core/graphics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAA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CAGjE;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAE5D;AAED,wBAAgB,KAAK,CAAC,CAAC,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAGtE;AAID,KAAK,IAAI,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,aAAa,EAAE,GAAG,IAAI,EAAE,MAAM,CAAC,KAAK,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;AAErG,MAAM,MAAM,aAAa,GAAG;KACzB,CAAC,IAAI,MAAM,OAAO,WAAW,GAAG,IAAI,CAAC,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;CAC/D,CAAC;AAEF,QAAA,MAAM,WAAW;;;;;;;;;;;;;;;;;;;CAKP,CAAC;AAEX,wBAAgB,YAAY,CAAC,CAAC,EAAE,aAAa,GAAG,aAAa,CAM5D"}
@@ -1,289 +0,0 @@
1
- export class ImageHandle {
2
- constructor(path) {
3
- Object.defineProperty(this, "path", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- Object.defineProperty(this, "element", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: null
14
- });
15
- Object.defineProperty(this, "loadPromise", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: void 0
20
- });
21
- Object.defineProperty(this, "isLoaded", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: false
26
- });
27
- this.path = path;
28
- this.loadPromise = new Promise((resolve, reject) => {
29
- const img = new Image();
30
- img.onload = () => {
31
- this.element = img;
32
- this.isLoaded = true;
33
- resolve();
34
- };
35
- img.onerror = () => {
36
- reject(new Error(`Failed to load image: ${path}`));
37
- };
38
- img.src = path;
39
- });
40
- }
41
- isReady() {
42
- return this.isLoaded;
43
- }
44
- ready() {
45
- return this.loadPromise;
46
- }
47
- get size() {
48
- return [this.element?.width ?? 0, this.element?.height ?? 0];
49
- }
50
- getElement() {
51
- return this.element;
52
- }
53
- }
54
- function parseColor(color) {
55
- if (typeof color === 'string')
56
- return color;
57
- const [r, g, b, a = 1] = color;
58
- return `rgba(${r * 255}, ${g * 255}, ${b * 255}, ${a})`;
59
- }
60
- function applyColor(color) {
61
- return parseColor(color ?? [1, 1, 1, 1]);
62
- }
63
- function setStrokeProps(ctx, props) {
64
- ctx.lineWidth = props?.lineWidth ?? 1;
65
- ctx.lineCap = props?.lineCap ?? 'butt';
66
- ctx.lineJoin = props?.lineJoin ?? 'miter';
67
- ctx.miterLimit = props?.miterLimit ?? 10;
68
- }
69
- export function newState(ctx) {
70
- return {
71
- screenCtx: ctx,
72
- currentCtx: ctx,
73
- canvases: new Map(),
74
- };
75
- }
76
- export function clear(s, color = [0, 0, 0, 1]) {
77
- const ctx = s.currentCtx;
78
- ctx.fillStyle = parseColor(color);
79
- ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
80
- }
81
- export function rectangle(s, mode, color, rect, props) {
82
- const ctx = s.currentCtx;
83
- const [x, y, w, h] = rect;
84
- const c = applyColor(color);
85
- if (mode === 'fill') {
86
- ctx.fillStyle = c;
87
- ctx.fillRect(x, y, w, h);
88
- }
89
- else {
90
- setStrokeProps(ctx, props);
91
- ctx.strokeStyle = c;
92
- ctx.strokeRect(x, y, w, h);
93
- }
94
- }
95
- export function circle(s, mode, color, position, radii, props) {
96
- const ctx = s.currentCtx;
97
- const [x, y] = position;
98
- const c = applyColor(color);
99
- const [rx, ry] = typeof radii === 'number' ? [radii, radii] : radii;
100
- const [startAngle, endAngle] = props?.arc ?? [0, Math.PI * 2];
101
- const rotation = props?.angle ?? 0;
102
- ctx.save();
103
- ctx.translate(x, y);
104
- ctx.rotate(rotation);
105
- ctx.scale(rx, ry);
106
- ctx.beginPath();
107
- ctx.arc(0, 0, 1, startAngle, endAngle);
108
- ctx.closePath();
109
- ctx.restore();
110
- if (mode === 'fill') {
111
- ctx.fillStyle = c;
112
- ctx.fill();
113
- }
114
- else {
115
- setStrokeProps(ctx, props);
116
- ctx.strokeStyle = c;
117
- ctx.stroke();
118
- }
119
- }
120
- export function line(s, color, points, props) {
121
- const ctx = s.currentCtx;
122
- if (points.length < 2)
123
- return;
124
- setStrokeProps(ctx, props);
125
- ctx.beginPath();
126
- const [[x0, y0], ...rest] = points;
127
- ctx.moveTo(x0, y0);
128
- rest.forEach(([x, y]) => ctx.lineTo(x, y));
129
- ctx.strokeStyle = applyColor(color);
130
- ctx.stroke();
131
- }
132
- export function print(s, color, text, position, props) {
133
- const ctx = s.currentCtx;
134
- const [x, y] = position;
135
- const { font = '16px sans-serif', limit, align = 'left' } = props ?? {};
136
- ctx.fillStyle = parseColor(color);
137
- ctx.font = font;
138
- if (limit !== undefined) {
139
- const lines = wrapText(ctx, text, limit);
140
- const lineHeight = getFontHeight(ctx);
141
- lines.forEach((line, i) => {
142
- const lineWidth = ctx.measureText(line).width;
143
- const drawX = align === 'center' ? x + (limit - lineWidth) / 2
144
- : align === 'right' ? x + limit - lineWidth
145
- : x;
146
- ctx.fillText(line, drawX, y + i * lineHeight);
147
- });
148
- }
149
- else {
150
- ctx.fillText(text, x, y);
151
- }
152
- }
153
- function wrapText(ctx, text, maxWidth) {
154
- const words = text.split(' ');
155
- const [first, ...rest] = words;
156
- const lines = [];
157
- let current = first ?? '';
158
- rest.forEach(word => {
159
- if (ctx.measureText(current + ' ' + word).width < maxWidth) {
160
- current += ' ' + word;
161
- }
162
- else {
163
- lines.push(current);
164
- current = word;
165
- }
166
- });
167
- lines.push(current);
168
- return lines;
169
- }
170
- function getFontHeight(ctx) {
171
- const match = ctx.font.match(/(\d+)px/);
172
- return match ? parseInt(match[1]) : 16;
173
- }
174
- export function drawImage(s, handle, position, props) {
175
- const ctx = s.currentCtx;
176
- if (!handle.isReady())
177
- return;
178
- const element = handle.getElement();
179
- if (!element)
180
- return;
181
- const [x, y] = position;
182
- const { r = 0, scale = 1, origin = 0, quad } = props ?? {};
183
- const [sx, sy] = typeof scale === 'number' ? [scale, scale] : scale;
184
- const [ox, oy] = typeof origin === 'number' ? [origin, origin] : origin;
185
- ctx.save();
186
- ctx.translate(x, y);
187
- ctx.rotate(r);
188
- ctx.scale(sx, sy);
189
- if (quad) {
190
- const [qx, qy, qw, qh] = quad;
191
- ctx.drawImage(element, qx, qy, qw, qh, -ox, -oy, qw, qh);
192
- }
193
- else {
194
- ctx.drawImage(element, -ox, -oy);
195
- }
196
- ctx.restore();
197
- }
198
- export function getCanvasSize(s) {
199
- return [s.currentCtx.canvas.width, s.currentCtx.canvas.height];
200
- }
201
- export function newImage(_s, path) {
202
- return new ImageHandle(path);
203
- }
204
- export function newCanvas(s, size) {
205
- const [w, h] = size;
206
- const element = document.createElement('canvas');
207
- element.width = w;
208
- element.height = h;
209
- const ctx = element.getContext('2d');
210
- if (!ctx)
211
- throw new Error('Failed to create canvas context');
212
- const canvas = { size, element, ctx };
213
- s.canvases.set(canvas, true);
214
- return canvas;
215
- }
216
- export function setCanvas(s, canvas) {
217
- s.currentCtx = canvas?.ctx ?? s.screenCtx;
218
- }
219
- export function setScreenContext(s, ctx) {
220
- s.screenCtx = ctx;
221
- s.currentCtx = ctx;
222
- }
223
- export function clip(s, rect) {
224
- const ctx = s.currentCtx;
225
- ctx.beginPath();
226
- if (rect) {
227
- const [x, y, w, h] = rect;
228
- ctx.rect(x, y, w, h);
229
- }
230
- else {
231
- ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height);
232
- }
233
- ctx.clip();
234
- }
235
- export function polygon(s, mode, color, points, props) {
236
- const ctx = s.currentCtx;
237
- if (points.length < 3)
238
- return;
239
- const c = applyColor(color);
240
- ctx.beginPath();
241
- const [[x0, y0], ...rest] = points;
242
- ctx.moveTo(x0, y0);
243
- rest.forEach(([x, y]) => ctx.lineTo(x, y));
244
- ctx.closePath();
245
- if (mode === 'fill') {
246
- ctx.fillStyle = c;
247
- ctx.fill();
248
- }
249
- else {
250
- setStrokeProps(ctx, props);
251
- ctx.strokeStyle = c;
252
- ctx.stroke();
253
- }
254
- }
255
- export function points(s, color, pts) {
256
- const ctx = s.currentCtx;
257
- ctx.fillStyle = applyColor(color);
258
- pts.forEach(([x, y]) => ctx.fillRect(x, y, 1, 1));
259
- }
260
- export function push(s) {
261
- s.currentCtx.save();
262
- }
263
- export function pop(s) {
264
- s.currentCtx.restore();
265
- }
266
- export function translate(s, offset) {
267
- const [x, y] = offset;
268
- s.currentCtx.translate(x, y);
269
- }
270
- export function rotate(s, angle) {
271
- s.currentCtx.rotate(angle);
272
- }
273
- export function scale(s, factor) {
274
- const [sx, sy] = typeof factor === 'number' ? [factor, factor] : factor;
275
- s.currentCtx.scale(sx, sy);
276
- }
277
- const graphicsFns = {
278
- clear, rectangle, circle, line, print,
279
- draw: drawImage, getCanvasSize, newCanvas, setCanvas,
280
- clip, polygon, points, newImage,
281
- push, pop, translate, rotate, scale,
282
- };
283
- export function bindGraphics(s) {
284
- const bound = {};
285
- for (const [name, fn] of Object.entries(graphicsFns)) {
286
- bound[name] = (...args) => fn(s, ...args);
287
- }
288
- return bound;
289
- }
@@ -1,14 +0,0 @@
1
- export declare class InputStateTracker<T> {
2
- private prevState;
3
- private currState;
4
- update(pressedKeys: Set<T>): {
5
- justPressed: T[];
6
- justReleased: T[];
7
- };
8
- isDown(key: T): boolean;
9
- justPressed(key: T): boolean;
10
- justReleased(key: T): boolean;
11
- getCurrentState(): Set<T>;
12
- clear(): void;
13
- }
14
- //# sourceMappingURL=input-state.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"input-state.d.ts","sourceRoot":"","sources":["../../src/core/input-state.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAiB,CAAC,CAAC;IAC9B,OAAO,CAAC,SAAS,CAAgB;IACjC,OAAO,CAAC,SAAS,CAAgB;IAEjC,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG;QAAE,WAAW,EAAE,CAAC,EAAE,CAAC;QAAC,YAAY,EAAE,CAAC,EAAE,CAAA;KAAE;IAuBpE,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAIvB,WAAW,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAI5B,YAAY,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAI7B,eAAe,IAAI,GAAG,CAAC,CAAC,CAAC;IAIzB,KAAK,IAAI,IAAI;CAId"}
@@ -1,50 +0,0 @@
1
- export class InputStateTracker {
2
- constructor() {
3
- Object.defineProperty(this, "prevState", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: new Set()
8
- });
9
- Object.defineProperty(this, "currState", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: new Set()
14
- });
15
- }
16
- update(pressedKeys) {
17
- const justPressed = [];
18
- const justReleased = [];
19
- const nextState = new Set(pressedKeys);
20
- for (const key of nextState) {
21
- if (!this.currState.has(key)) {
22
- justPressed.push(key);
23
- }
24
- }
25
- for (const key of this.currState) {
26
- if (!nextState.has(key)) {
27
- justReleased.push(key);
28
- }
29
- }
30
- this.prevState = new Set(this.currState);
31
- this.currState = nextState;
32
- return { justPressed, justReleased };
33
- }
34
- isDown(key) {
35
- return this.currState.has(key);
36
- }
37
- justPressed(key) {
38
- return !this.prevState.has(key) && this.currState.has(key);
39
- }
40
- justReleased(key) {
41
- return this.prevState.has(key) && !this.currState.has(key);
42
- }
43
- getCurrentState() {
44
- return new Set(this.currState);
45
- }
46
- clear() {
47
- this.prevState.clear();
48
- this.currState.clear();
49
- }
50
- }