like2d 1.0.0 → 2.0.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 (96) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +67 -43
  3. package/dist/adapters/callback/index.d.ts +43 -0
  4. package/dist/adapters/callback/index.d.ts.map +1 -0
  5. package/dist/adapters/callback/index.js +80 -0
  6. package/dist/adapters/scene/index.d.ts +42 -0
  7. package/dist/adapters/scene/index.d.ts.map +1 -0
  8. package/dist/adapters/scene/index.js +112 -0
  9. package/dist/adapters/scene/scene.d.ts +18 -0
  10. package/dist/adapters/scene/scene.d.ts.map +1 -0
  11. package/dist/adapters/scene/startup-scene.d.ts +17 -0
  12. package/dist/adapters/scene/startup-scene.d.ts.map +1 -0
  13. package/dist/adapters/scene/startup-scene.js +41 -0
  14. package/dist/core/audio.d.ts +61 -0
  15. package/dist/core/audio.d.ts.map +1 -0
  16. package/dist/core/audio.js +226 -0
  17. package/dist/core/canvas-config.d.ts +22 -0
  18. package/dist/core/canvas-config.d.ts.map +1 -0
  19. package/dist/core/canvas-config.js +14 -0
  20. package/dist/core/canvas-manager.d.ts +26 -0
  21. package/dist/core/canvas-manager.d.ts.map +1 -0
  22. package/dist/core/canvas-manager.js +197 -0
  23. package/dist/core/events.d.ts +52 -0
  24. package/dist/core/events.d.ts.map +1 -0
  25. package/dist/core/gamepad-button-map.d.ts.map +1 -0
  26. package/dist/core/gamepad-buttons.d.ts +23 -0
  27. package/dist/core/gamepad-buttons.d.ts.map +1 -0
  28. package/dist/core/gamepad-buttons.js +36 -0
  29. package/dist/core/gamepad-db.d.ts.map +1 -0
  30. package/dist/{gamepad-mapping.d.ts → core/gamepad-mapping.d.ts} +3 -15
  31. package/dist/core/gamepad-mapping.d.ts.map +1 -0
  32. package/dist/core/gamepad-mapping.js +223 -0
  33. package/dist/{gamepad.d.ts → core/gamepad.d.ts} +22 -17
  34. package/dist/core/gamepad.d.ts.map +1 -0
  35. package/dist/{gamepad.js → core/gamepad.js} +91 -70
  36. package/dist/{graphics.d.ts → core/graphics.d.ts} +2 -8
  37. package/dist/core/graphics.d.ts.map +1 -0
  38. package/dist/{graphics.js → core/graphics.js} +4 -41
  39. package/dist/core/input-state.d.ts.map +1 -0
  40. package/dist/{input.d.ts → core/input.d.ts} +11 -14
  41. package/dist/core/input.d.ts.map +1 -0
  42. package/dist/{input.js → core/input.js} +31 -41
  43. package/dist/core/keyboard.d.ts +15 -0
  44. package/dist/core/keyboard.d.ts.map +1 -0
  45. package/dist/core/keyboard.js +70 -0
  46. package/dist/core/mouse.d.ts +29 -0
  47. package/dist/core/mouse.d.ts.map +1 -0
  48. package/dist/core/mouse.js +130 -0
  49. package/dist/{rect.d.ts → core/rect.d.ts} +1 -2
  50. package/dist/core/rect.d.ts.map +1 -0
  51. package/dist/{rect.js → core/rect.js} +24 -28
  52. package/dist/{timer.d.ts → core/timer.d.ts} +0 -1
  53. package/dist/core/timer.d.ts.map +1 -0
  54. package/dist/{timer.js → core/timer.js} +0 -1
  55. package/dist/{vector2.d.ts → core/vector2.d.ts} +4 -10
  56. package/dist/core/vector2.d.ts.map +1 -0
  57. package/dist/{vector2.js → core/vector2.js} +40 -40
  58. package/dist/engine.d.ts +42 -0
  59. package/dist/engine.d.ts.map +1 -0
  60. package/dist/engine.js +154 -0
  61. package/dist/index.d.ts +38 -44
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +24 -250
  64. package/package.json +8 -23
  65. package/dist/audio.d.ts +0 -52
  66. package/dist/audio.d.ts.map +0 -1
  67. package/dist/audio.js +0 -250
  68. package/dist/events.d.ts +0 -36
  69. package/dist/events.d.ts.map +0 -1
  70. package/dist/gamepad-button-map.d.ts.map +0 -1
  71. package/dist/gamepad-db.d.ts.map +0 -1
  72. package/dist/gamepad-mapping.d.ts.map +0 -1
  73. package/dist/gamepad-mapping.js +0 -191
  74. package/dist/gamepad.d.ts.map +0 -1
  75. package/dist/graphics.d.ts.map +0 -1
  76. package/dist/input-state.d.ts.map +0 -1
  77. package/dist/input.d.ts.map +0 -1
  78. package/dist/keyboard.d.ts +0 -9
  79. package/dist/keyboard.d.ts.map +0 -1
  80. package/dist/keyboard.js +0 -33
  81. package/dist/mouse.d.ts +0 -20
  82. package/dist/mouse.d.ts.map +0 -1
  83. package/dist/mouse.js +0 -84
  84. package/dist/rect.d.ts.map +0 -1
  85. package/dist/scene.d.ts +0 -10
  86. package/dist/scene.d.ts.map +0 -1
  87. package/dist/timer.d.ts.map +0 -1
  88. package/dist/vector2.d.ts.map +0 -1
  89. /package/dist/{scene.js → adapters/scene/scene.js} +0 -0
  90. /package/dist/{events.js → core/events.js} +0 -0
  91. /package/dist/{gamepad-button-map.d.ts → core/gamepad-button-map.d.ts} +0 -0
  92. /package/dist/{gamepad-button-map.js → core/gamepad-button-map.js} +0 -0
  93. /package/dist/{gamepad-db.d.ts → core/gamepad-db.d.ts} +0 -0
  94. /package/dist/{gamepad-db.js → core/gamepad-db.js} +0 -0
  95. /package/dist/{input-state.d.ts → core/input-state.d.ts} +0 -0
  96. /package/dist/{input-state.js → core/input-state.js} +0 -0
package/dist/index.js CHANGED
@@ -1,250 +1,24 @@
1
- import { graphics } from './graphics';
2
- import { audio } from './audio';
3
- import { keyboard } from './keyboard';
4
- import { mouse } from './mouse';
5
- import { input } from './input';
6
- import { gamepad } from './gamepad';
7
- import { timer } from './timer';
8
- class Like {
9
- constructor() {
10
- Object.defineProperty(this, "canvas", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: null
15
- });
16
- Object.defineProperty(this, "ctx", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: null
21
- });
22
- Object.defineProperty(this, "currentScene", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: null
27
- });
28
- Object.defineProperty(this, "isRunning", {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: false
33
- });
34
- Object.defineProperty(this, "lastTime", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: 0
39
- });
40
- Object.defineProperty(this, "currentWidth", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: 800
45
- });
46
- Object.defineProperty(this, "currentHeight", {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: 600
51
- });
52
- Object.defineProperty(this, "graphics", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: graphics
57
- });
58
- Object.defineProperty(this, "audio", {
59
- enumerable: true,
60
- configurable: true,
61
- writable: true,
62
- value: audio
63
- });
64
- Object.defineProperty(this, "keyboard", {
65
- enumerable: true,
66
- configurable: true,
67
- writable: true,
68
- value: keyboard
69
- });
70
- Object.defineProperty(this, "mouse", {
71
- enumerable: true,
72
- configurable: true,
73
- writable: true,
74
- value: mouse
75
- });
76
- Object.defineProperty(this, "input", {
77
- enumerable: true,
78
- configurable: true,
79
- writable: true,
80
- value: input
81
- });
82
- Object.defineProperty(this, "gamepad", {
83
- enumerable: true,
84
- configurable: true,
85
- writable: true,
86
- value: gamepad
87
- });
88
- Object.defineProperty(this, "timer", {
89
- enumerable: true,
90
- configurable: true,
91
- writable: true,
92
- value: timer
93
- });
94
- }
95
- async init(container, width = 800, height = 600) {
96
- this.currentWidth = width;
97
- this.currentHeight = height;
98
- this.canvas = document.createElement('canvas');
99
- this.canvas.width = width;
100
- this.canvas.height = height;
101
- this.canvas.style.border = '1px solid #ccc';
102
- this.canvas.style.display = 'block';
103
- this.ctx = this.canvas.getContext('2d');
104
- if (!this.ctx) {
105
- throw new Error('Failed to get 2D context');
106
- }
107
- graphics.setContext(this.ctx);
108
- mouse.setCanvas(this.canvas);
109
- container.appendChild(this.canvas);
110
- this.setupInputHandlers();
111
- // Initialize gamepad mapping database
112
- await gamepad.init();
113
- }
114
- toggleFullscreen() {
115
- if (!this.canvas)
116
- return;
117
- if (!document.fullscreenElement) {
118
- this.canvas.requestFullscreen().catch(err => {
119
- console.error(`Error attempting to enable fullscreen: ${err.message}`);
120
- });
121
- }
122
- else {
123
- document.exitFullscreen();
124
- }
125
- }
126
- setupInputHandlers() {
127
- window.addEventListener('keydown', (e) => {
128
- if (this.currentScene?.handleEvent) {
129
- this.currentScene.handleEvent({ type: 'keypressed', scancode: e.code, keycode: e.key });
130
- }
131
- });
132
- window.addEventListener('keyup', (e) => {
133
- if (this.currentScene?.handleEvent) {
134
- this.currentScene.handleEvent({ type: 'keyreleased', scancode: e.code, keycode: e.key });
135
- }
136
- });
137
- if (this.canvas) {
138
- this.canvas.addEventListener('mousedown', (e) => {
139
- if (this.currentScene?.handleEvent) {
140
- const rect = this.canvas.getBoundingClientRect();
141
- this.currentScene.handleEvent({
142
- type: 'mousepressed',
143
- x: e.clientX - rect.left,
144
- y: e.clientY - rect.top,
145
- button: e.button + 1
146
- });
147
- }
148
- });
149
- this.canvas.addEventListener('mouseup', (e) => {
150
- if (this.currentScene?.handleEvent) {
151
- const rect = this.canvas.getBoundingClientRect();
152
- this.currentScene.handleEvent({
153
- type: 'mousereleased',
154
- x: e.clientX - rect.left,
155
- y: e.clientY - rect.top,
156
- button: e.button + 1
157
- });
158
- }
159
- });
160
- }
161
- }
162
- setScene(scene) {
163
- this.currentScene = scene;
164
- timer.resetSceneTime();
165
- if (this.canvas) {
166
- if (scene.width !== this.currentWidth || scene.height !== this.currentHeight) {
167
- this.currentWidth = scene.width;
168
- this.currentHeight = scene.height;
169
- this.canvas.width = scene.width;
170
- this.canvas.height = scene.height;
171
- }
172
- }
173
- if (this.isRunning && scene.load) {
174
- scene.load();
175
- }
176
- }
177
- start(scene) {
178
- if (this.isRunning)
179
- return;
180
- this.setScene(scene);
181
- this.isRunning = true;
182
- if (this.currentScene?.load) {
183
- this.currentScene.load();
184
- }
185
- this.lastTime = performance.now();
186
- this.loop();
187
- }
188
- loop() {
189
- if (!this.isRunning || !this.currentScene)
190
- return;
191
- const currentTime = performance.now();
192
- const dt = (currentTime - this.lastTime) / 1000;
193
- this.lastTime = currentTime;
194
- if (timer.isSleeping()) {
195
- requestAnimationFrame(() => this.loop());
196
- return;
197
- }
198
- timer.update(dt);
199
- const { pressed, released, gamepadPressed, gamepadReleased } = input.update();
200
- // Trigger action callbacks
201
- if (this.currentScene?.handleEvent) {
202
- for (const action of pressed) {
203
- this.currentScene.handleEvent({ type: 'actionpressed', action });
204
- }
205
- }
206
- if (this.currentScene?.handleEvent) {
207
- for (const action of released) {
208
- this.currentScene.handleEvent({ type: 'actionreleased', action });
209
- }
210
- }
211
- // Trigger gamepad callbacks
212
- if (this.currentScene?.handleEvent) {
213
- for (const { gamepadIndex, buttonIndex, buttonName } of gamepadPressed) {
214
- this.currentScene.handleEvent({ type: 'gamepadpressed', gamepadIndex, buttonIndex, buttonName });
215
- }
216
- }
217
- if (this.currentScene?.handleEvent) {
218
- for (const { gamepadIndex, buttonIndex, buttonName } of gamepadReleased) {
219
- this.currentScene.handleEvent({ type: 'gamepadreleased', gamepadIndex, buttonIndex, buttonName });
220
- }
221
- }
222
- this.currentScene.update(dt);
223
- if (this.ctx) {
224
- graphics.clear();
225
- this.currentScene.draw();
226
- }
227
- requestAnimationFrame(() => this.loop());
228
- }
229
- getCanvas() {
230
- return this.canvas;
231
- }
232
- getContext() {
233
- return this.ctx;
234
- }
235
- getWidth() {
236
- return this.currentWidth;
237
- }
238
- getHeight() {
239
- return this.currentHeight;
240
- }
241
- }
242
- export const like = new Like();
243
- export const love = like;
244
- export { Source } from './audio';
245
- export { timer } from './timer';
246
- export { ImageHandle } from './graphics';
247
- export { input } from './input';
248
- export { gamepad, getButtonName } from './gamepad';
249
- export { V2 } from './vector2';
250
- export { R } from './rect';
1
+ // Like2D - Multi-adapter game framework
2
+ // Use specific adapters for different patterns:
3
+ // - import { like, graphics } from 'like2d/callback' (Love2D-style callbacks)
4
+ // - import { SceneRunner, Scene } from 'like2d/scene' (Class-based scenes)
5
+ /**
6
+ * 2D vector math and utility functions
7
+ */
8
+ export { Vec2 } from './core/vector2';
9
+ /**
10
+ * Rectangle math and utility functions.
11
+ * Rects are represented as [x, y, width, height] tuples.
12
+ */
13
+ export { Rect } from './core/rect';
14
+ /**
15
+ * Handle to an image asset that may be loading in the background.
16
+ */
17
+ export { ImageHandle } from './core/graphics';
18
+ /**
19
+ * Gamepad utility functions and types.
20
+ */
21
+ export { getGPName, GP } from './core/gamepad';
22
+ // Note: For actual usage, import from specific adapters:
23
+ // import { like, graphics } from 'like2d/callback';
24
+ // import { SceneRunner, Scene } from 'like2d/scene';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "like2d",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "A web-native game framework inspired by Love2D",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -11,29 +11,13 @@
11
11
  "import": "./dist/index.js",
12
12
  "types": "./dist/index.d.ts"
13
13
  },
14
- "./graphics": {
15
- "import": "./dist/graphics.js",
16
- "types": "./dist/graphics.d.ts"
17
- },
18
- "./audio": {
19
- "import": "./dist/audio.js",
20
- "types": "./dist/audio.d.ts"
21
- },
22
- "./input": {
23
- "import": "./dist/input.js",
24
- "types": "./dist/input.d.ts"
14
+ "./callback": {
15
+ "import": "./dist/adapters/callback/index.js",
16
+ "types": "./dist/adapters/callback/index.d.ts"
25
17
  },
26
18
  "./scene": {
27
- "import": "./dist/scene.js",
28
- "types": "./dist/scene.d.ts"
29
- },
30
- "./vector2": {
31
- "import": "./dist/vector2.js",
32
- "types": "./dist/vector2.d.ts"
33
- },
34
- "./rect": {
35
- "import": "./dist/rect.js",
36
- "types": "./dist/rect.d.ts"
19
+ "import": "./dist/adapters/scene/index.js",
20
+ "types": "./dist/adapters/scene/index.d.ts"
37
21
  }
38
22
  },
39
23
  "files": [
@@ -45,7 +29,8 @@
45
29
  "build": "tsc && cp src/gamecontrollerdb.txt dist/",
46
30
  "typecheck": "tsc --noEmit",
47
31
  "lint": "echo 'No linting configured'",
48
- "clean": "rm -rf dist"
32
+ "clean": "rm -rf dist",
33
+ "test": "vitest run"
49
34
  },
50
35
  "keywords": [
51
36
  "game",
package/dist/audio.d.ts DELETED
@@ -1,52 +0,0 @@
1
- export interface SourceOptions {
2
- volume?: number;
3
- pitch?: number;
4
- looping?: boolean;
5
- }
6
- export declare class Source {
7
- private audio;
8
- private _volume;
9
- private _pitch;
10
- private _looping;
11
- private path;
12
- private isLoaded;
13
- private loadPromise;
14
- private globalVolume;
15
- constructor(path: string, options?: SourceOptions);
16
- _setGlobalVolume(vol: number): void;
17
- ready(): Promise<void>;
18
- private updatePlaybackRate;
19
- play(): boolean;
20
- stop(): void;
21
- pause(): void;
22
- resume(): boolean;
23
- seek(position: number): void;
24
- tell(): number;
25
- getDuration(): number;
26
- isPlaying(): boolean;
27
- isPaused(): boolean;
28
- isStopped(): boolean;
29
- isReady(): boolean;
30
- setVolume(volume: number): void;
31
- getVolume(): number;
32
- setPitch(pitch: number): void;
33
- getPitch(): number;
34
- setLooping(looping: boolean): void;
35
- isLooping(): boolean;
36
- clone(): Source;
37
- }
38
- export declare class Audio {
39
- private sources;
40
- private globalVolume;
41
- newSource(path: string, options?: SourceOptions): Source;
42
- play(source: Source): boolean;
43
- stop(source?: Source): void;
44
- pause(source?: Source): void;
45
- resume(source?: Source): void;
46
- setVolume(volume: number): void;
47
- getVolume(): number;
48
- getActiveSourceCount(): number;
49
- getActiveSources(): Source[];
50
- }
51
- export declare const audio: Audio;
52
- //# sourceMappingURL=audio.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"audio.d.ts","sourceRoot":"","sources":["../src/audio.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,qBAAa,MAAM;IACjB,OAAO,CAAC,KAAK,CAAmB;IAChC,OAAO,CAAC,OAAO,CAAa;IAC5B,OAAO,CAAC,MAAM,CAAa;IAC3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EAAE,MAAM,GAAG,IAAI;IAY7B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAO/B,SAAS,IAAI,MAAM;IAInB,oBAAoB,IAAI,MAAM;IAU9B,gBAAgB,IAAI,MAAM,EAAE;CAS7B;AAED,eAAO,MAAM,KAAK,OAAc,CAAC"}
package/dist/audio.js DELETED
@@ -1,250 +0,0 @@
1
- export class Source {
2
- constructor(path, options = {}) {
3
- Object.defineProperty(this, "audio", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- Object.defineProperty(this, "_volume", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: 1
14
- });
15
- Object.defineProperty(this, "_pitch", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: 1
20
- });
21
- Object.defineProperty(this, "_looping", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: false
26
- });
27
- Object.defineProperty(this, "path", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: void 0
32
- });
33
- Object.defineProperty(this, "isLoaded", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: false
38
- });
39
- Object.defineProperty(this, "loadPromise", {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: void 0
44
- });
45
- Object.defineProperty(this, "globalVolume", {
46
- enumerable: true,
47
- configurable: true,
48
- writable: true,
49
- value: 1
50
- });
51
- this.path = path;
52
- this.audio = document.createElement('audio');
53
- this.audio.src = path;
54
- this._volume = options.volume ?? 1;
55
- this._pitch = options.pitch ?? 1;
56
- this._looping = options.looping ?? false;
57
- this.audio.volume = this._volume;
58
- this.audio.loop = this._looping;
59
- this.updatePlaybackRate();
60
- this.loadPromise = new Promise((resolve, reject) => {
61
- this.audio.oncanplaythrough = () => {
62
- this.isLoaded = true;
63
- resolve();
64
- };
65
- this.audio.onerror = () => {
66
- reject(new Error(`Failed to load audio: ${path}`));
67
- };
68
- if (this.audio.readyState >= 4) {
69
- this.isLoaded = true;
70
- resolve();
71
- }
72
- });
73
- }
74
- _setGlobalVolume(vol) {
75
- this.globalVolume = vol;
76
- this.audio.volume = this._volume * this.globalVolume;
77
- }
78
- ready() {
79
- return this.loadPromise;
80
- }
81
- updatePlaybackRate() {
82
- this.audio.playbackRate = this._pitch;
83
- }
84
- play() {
85
- if (!this.isLoaded) {
86
- // Silently return false - asset not loaded yet
87
- return false;
88
- }
89
- // Only reset to beginning if stopped (not paused)
90
- if (this.isStopped() || this.audio.ended) {
91
- this.audio.currentTime = 0;
92
- }
93
- const playPromise = this.audio.play();
94
- if (playPromise) {
95
- playPromise.catch(err => {
96
- console.warn(`Failed to play audio "${this.path}":`, err.message);
97
- });
98
- }
99
- return true;
100
- }
101
- stop() {
102
- this.audio.pause();
103
- this.audio.currentTime = 0;
104
- }
105
- pause() {
106
- this.audio.pause();
107
- }
108
- resume() {
109
- if (this.audio.paused) {
110
- return this.play();
111
- }
112
- return false;
113
- }
114
- seek(position) {
115
- this.audio.currentTime = position;
116
- }
117
- tell() {
118
- return this.audio.currentTime;
119
- }
120
- getDuration() {
121
- return this.audio.duration || 0;
122
- }
123
- isPlaying() {
124
- return !this.audio.paused && !this.audio.ended;
125
- }
126
- isPaused() {
127
- return this.audio.paused && this.audio.currentTime > 0;
128
- }
129
- isStopped() {
130
- return this.audio.paused && this.audio.currentTime === 0;
131
- }
132
- isReady() {
133
- return this.isLoaded;
134
- }
135
- setVolume(volume) {
136
- this._volume = Math.max(0, Math.min(1, volume));
137
- this.audio.volume = this._volume * this.globalVolume;
138
- }
139
- getVolume() {
140
- return this._volume;
141
- }
142
- setPitch(pitch) {
143
- this._pitch = Math.max(0.125, Math.min(8, pitch));
144
- this.updatePlaybackRate();
145
- }
146
- getPitch() {
147
- return this._pitch;
148
- }
149
- setLooping(looping) {
150
- this._looping = looping;
151
- this.audio.loop = looping;
152
- }
153
- isLooping() {
154
- return this._looping;
155
- }
156
- clone() {
157
- const clone = new Source(this.path, {
158
- volume: this._volume,
159
- pitch: this._pitch,
160
- looping: this._looping
161
- });
162
- clone._setGlobalVolume(this.globalVolume);
163
- return clone;
164
- }
165
- }
166
- export class Audio {
167
- constructor() {
168
- Object.defineProperty(this, "sources", {
169
- enumerable: true,
170
- configurable: true,
171
- writable: true,
172
- value: new Set()
173
- });
174
- Object.defineProperty(this, "globalVolume", {
175
- enumerable: true,
176
- configurable: true,
177
- writable: true,
178
- value: 1
179
- });
180
- }
181
- newSource(path, options) {
182
- const source = new Source(path, options);
183
- source._setGlobalVolume(this.globalVolume);
184
- this.sources.add(source);
185
- return source;
186
- }
187
- play(source) {
188
- return source.play();
189
- }
190
- stop(source) {
191
- if (source) {
192
- source.stop();
193
- }
194
- else {
195
- this.sources.forEach(s => s.stop());
196
- }
197
- }
198
- pause(source) {
199
- if (source) {
200
- source.pause();
201
- }
202
- else {
203
- this.sources.forEach(s => {
204
- if (s.isPlaying()) {
205
- s.pause();
206
- }
207
- });
208
- }
209
- }
210
- resume(source) {
211
- if (source) {
212
- source.resume();
213
- }
214
- else {
215
- this.sources.forEach(s => {
216
- if (s.isPaused()) {
217
- s.resume();
218
- }
219
- });
220
- }
221
- }
222
- setVolume(volume) {
223
- this.globalVolume = Math.max(0, Math.min(1, volume));
224
- this.sources.forEach(source => {
225
- source._setGlobalVolume(this.globalVolume);
226
- });
227
- }
228
- getVolume() {
229
- return this.globalVolume;
230
- }
231
- getActiveSourceCount() {
232
- let count = 0;
233
- this.sources.forEach(source => {
234
- if (source.isPlaying()) {
235
- count++;
236
- }
237
- });
238
- return count;
239
- }
240
- getActiveSources() {
241
- const active = [];
242
- this.sources.forEach(source => {
243
- if (source.isPlaying()) {
244
- active.push(source);
245
- }
246
- });
247
- return active;
248
- }
249
- }
250
- export const audio = new Audio();
package/dist/events.d.ts DELETED
@@ -1,36 +0,0 @@
1
- export type Event = {
2
- type: 'keypressed';
3
- scancode: string;
4
- keycode: string;
5
- } | {
6
- type: 'keyreleased';
7
- scancode: string;
8
- keycode: string;
9
- } | {
10
- type: 'mousepressed';
11
- x: number;
12
- y: number;
13
- button: number;
14
- } | {
15
- type: 'mousereleased';
16
- x: number;
17
- y: number;
18
- button: number;
19
- } | {
20
- type: 'actionpressed';
21
- action: string;
22
- } | {
23
- type: 'actionreleased';
24
- action: string;
25
- } | {
26
- type: 'gamepadpressed';
27
- gamepadIndex: number;
28
- buttonIndex: number;
29
- buttonName: string;
30
- } | {
31
- type: 'gamepadreleased';
32
- gamepadIndex: number;
33
- buttonIndex: number;
34
- buttonName: string;
35
- };
36
- //# sourceMappingURL=events.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GACb;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACzD;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC1D;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC9D;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC/D;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC1C;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GACzF;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"gamepad-button-map.d.ts","sourceRoot":"","sources":["../src/gamepad-button-map.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAiCrD,CAAC;AAGF,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAiBvD,CAAC;AAEF,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAErE"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"gamepad-db.d.ts","sourceRoot":"","sources":["../src/gamepad-db.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IAEjB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE7B,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE1B,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACpD;AAGD,eAAO,MAAM,WAAW,qQASd,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;AAGrD,eAAO,MAAM,QAAQ,iDAGX,CAAC;AAEX,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC;AAEhD,qB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