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
@@ -0,0 +1,181 @@
1
+ import { defaultMapping } from "../input";
2
+ const mapOrder = [
3
+ "BRight",
4
+ "BBottom",
5
+ "Up",
6
+ "Down",
7
+ "Left",
8
+ "Right",
9
+ "MenuLeft",
10
+ "MenuRight",
11
+ // 8: NES buttons
12
+ "L1",
13
+ "R1",
14
+ // 10: GBA buttons
15
+ "BLeft",
16
+ "BTop",
17
+ // 12: SNES buttons
18
+ "L2",
19
+ "R2",
20
+ // 14: PS1 buttons
21
+ "LeftStick",
22
+ "RightStick",
23
+ ];
24
+ export const buttonSetNES = new Set(mapOrder.slice(0, 8));
25
+ export const buttonSetGBA = new Set(mapOrder.slice(0, 10));
26
+ export const buttonSetSNES = new Set(mapOrder.slice(0, 12));
27
+ export const buttonSetPS1 = new Set(mapOrder.slice(0, 14));
28
+ export const buttonSetAll = new Set(mapOrder);
29
+ const drawCircButt = (pos, size) => (like, color) => like.gfx.circle("fill", color, pos, size);
30
+ const drawDpadPart = (rot) => (like, color) => {
31
+ like.gfx.push();
32
+ like.gfx.translate([2.5, 6]);
33
+ like.gfx.rotate(rot);
34
+ like.gfx.rectangle("fill", color, [0.5, -0.5, 1.3, 1]);
35
+ like.gfx.pop();
36
+ };
37
+ const drawShoulder = (y, width, flip) => (like, color) => {
38
+ const r = 0.8;
39
+ const rectPos = [5 - width, y];
40
+ const circPos = [5 - width - r, y];
41
+ like.gfx.push();
42
+ if (flip) {
43
+ like.gfx.translate([16, 0]);
44
+ like.gfx.scale([-1, 1]);
45
+ }
46
+ like.gfx.circle("fill", color, circPos, r, { arc: [Math.PI, Math.PI * 3 / 2], center: false });
47
+ like.gfx.rectangle("fill", color, [...rectPos, width, r]);
48
+ like.gfx.pop();
49
+ };
50
+ // Buttons assume a centered resolution of 16x9px. Transforms exist for a reason lol.
51
+ // LLLLL . RRRRR
52
+ // LLLLLLLLL . RRRRRRRRR
53
+ // .
54
+ // DDD S . S B
55
+ // -.....DDD.....................................
56
+ // DDD DDD . B B
57
+ // DDD LS . RS
58
+ // DDD . B
59
+ // .
60
+ const buttonProps = {
61
+ BLeft: { draw: drawCircButt([12, 6], 0.8) },
62
+ BRight: { draw: drawCircButt([15, 6], 0.8) },
63
+ BTop: { draw: drawCircButt([13.5, 4.5], 0.8) },
64
+ BBottom: { draw: drawCircButt([13.5, 7.5], 0.8) },
65
+ MenuLeft: { draw: drawCircButt([6, 4], 0.5) },
66
+ MenuRight: { draw: drawCircButt([10, 4], 0.5) },
67
+ LeftStick: { draw: drawCircButt([6.5, 7], 1.4) },
68
+ RightStick: { draw: drawCircButt([9.5, 7], 1.4) },
69
+ L1: { draw: drawShoulder(2, 3, false) },
70
+ L2: { draw: drawShoulder(1, 2, false) },
71
+ R1: { draw: drawShoulder(2, 3, true) },
72
+ R2: { draw: drawShoulder(1, 2, true) },
73
+ Right: { draw: drawDpadPart(0) },
74
+ Up: { draw: drawDpadPart(-Math.PI / 2) },
75
+ Left: { draw: drawDpadPart(Math.PI) },
76
+ Down: { draw: drawDpadPart(Math.PI / 2) },
77
+ };
78
+ export class MapGamepad {
79
+ constructor(mapMode, targetPad, next) {
80
+ Object.defineProperty(this, "mapMode", {
81
+ enumerable: true,
82
+ configurable: true,
83
+ writable: true,
84
+ value: mapMode
85
+ });
86
+ Object.defineProperty(this, "targetPad", {
87
+ enumerable: true,
88
+ configurable: true,
89
+ writable: true,
90
+ value: targetPad
91
+ });
92
+ Object.defineProperty(this, "next", {
93
+ enumerable: true,
94
+ configurable: true,
95
+ writable: true,
96
+ value: next
97
+ });
98
+ Object.defineProperty(this, "currentlyUnmapped", {
99
+ enumerable: true,
100
+ configurable: true,
101
+ writable: true,
102
+ value: []
103
+ });
104
+ Object.defineProperty(this, "mapping", {
105
+ enumerable: true,
106
+ configurable: true,
107
+ writable: true,
108
+ value: void 0
109
+ });
110
+ Object.defineProperty(this, "held", {
111
+ enumerable: true,
112
+ configurable: true,
113
+ writable: true,
114
+ value: void 0
115
+ });
116
+ Object.defineProperty(this, "alreadyMapped", {
117
+ enumerable: true,
118
+ configurable: true,
119
+ writable: true,
120
+ value: new Set()
121
+ });
122
+ }
123
+ load(like) {
124
+ this.mapping = like.gamepad.getMapping(this.targetPad) ?? defaultMapping(2);
125
+ for (const btn of mapOrder.reverse()) {
126
+ if (this.mapMode.buttons.has(btn)) {
127
+ this.currentlyUnmapped.push(btn);
128
+ }
129
+ }
130
+ like.canvas.setMode([320, 240]);
131
+ }
132
+ draw(like) {
133
+ const centerText = {
134
+ font: "1px sans-serif",
135
+ align: "center",
136
+ width: 16,
137
+ };
138
+ const active = this.currentlyUnmapped.at(-1);
139
+ like.gfx.clear();
140
+ like.gfx.scale(20);
141
+ like.gfx.translate([0, 1]);
142
+ like.gfx.print("white", "Map gamepad (click to close)", [8, 0.0], centerText);
143
+ for (const prop of this.mapMode.buttons.keys()) {
144
+ const color = this.held == prop
145
+ ? "green"
146
+ : active == prop
147
+ ? "red"
148
+ : this.mapMode.buttons.has(prop)
149
+ ? "gray"
150
+ : "black";
151
+ buttonProps[prop].draw(like, color);
152
+ }
153
+ like.gfx.print("white", active
154
+ ? `Press ${like.gamepad.fullButtonName(active)}!`
155
+ : "Press any button to resume.", [2, 10], { font: "1px sans-serif" });
156
+ }
157
+ gamepadpressed(like, source, _name, num) {
158
+ if (source !== this.targetPad || this.held)
159
+ return;
160
+ const active = this.currentlyUnmapped.pop();
161
+ if (active && !this.alreadyMapped.has(num)) {
162
+ this.alreadyMapped.add(num);
163
+ this.mapping.buttons[num] = active;
164
+ this.held = active;
165
+ }
166
+ else if (!active) {
167
+ like.gamepad.setMapping(this.targetPad, this.mapping);
168
+ setTimeout(() => like.setScene(this.next), 100);
169
+ }
170
+ }
171
+ gamepadreleased(_like, source, _name, num) {
172
+ if (source !== this.targetPad)
173
+ return;
174
+ if (this.held == this.mapping.buttons[num]) {
175
+ this.held = undefined;
176
+ }
177
+ }
178
+ mousepressed(like) {
179
+ like.setScene(this.next);
180
+ }
181
+ }
@@ -0,0 +1,59 @@
1
+ import { type Scene } from '../scene';
2
+ import { Like } from '..';
3
+ /**
4
+ * ## Why
5
+ *
6
+ * 1. Because the LIKE logo looks awesome.
7
+ * 2. Autoplay restriction, doesn't let you play audio until the page is clicked.
8
+ * 3. You have to click on the game in order to send inputs, anyway.
9
+ * 4. It's polite.
10
+ *
11
+ * ## Usage
12
+ *
13
+ * ```typescript
14
+ * import { createLike, StartScreen } from 'like';
15
+ * import { GameScene } from './game';
16
+ *
17
+ * const container = document.getElementById("myGame");
18
+ * const like = createLike(container);
19
+ *
20
+ * // these callbacks will be ignored until the scene is clicked
21
+ * like.update = function () { ... }
22
+ * like.draw = function () { ... }
23
+ *
24
+ * // Set up the start screen
25
+ * like.setScene(new StartScreen())
26
+ * like.start();
27
+ * ```
28
+ *
29
+ * Alternatively, copy-paste this code into your own project and modify it freely.
30
+ * Update imports:
31
+ *
32
+ * ```ts
33
+ * import { type Scene } from 'like/scene';
34
+ * import type { ImageHandle } from 'like/graphics';
35
+ * import { Vec2 } from 'like/math';
36
+ * import { Like } from 'like';
37
+ * ```
38
+ *
39
+ * ## Custom Rendering
40
+ *
41
+ * Pass a custom draw function to replace the default logo:
42
+ *
43
+ * ```typescript
44
+ * const startup = new StartupScene(gameScene, (like) => {
45
+ * like.gfx.clear([0, 0, 0, 1]);
46
+ * like.gfx.print([1, 1, 1], 'Click to Start', [100, 100]);
47
+ * });
48
+ * ```
49
+ */
50
+ export declare class StartScreen implements Scene {
51
+ private next;
52
+ private onDraw?;
53
+ private logo;
54
+ constructor(next: Scene, onDraw?: ((like: Like) => void) | undefined);
55
+ load(like: Like): void;
56
+ draw(like: Like): void;
57
+ mousepressed(like: Like): void;
58
+ }
59
+ //# sourceMappingURL=startScreen.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startScreen.d.ts","sourceRoot":"","sources":["../../src/prefab-scenes/startScreen.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,UAA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i8+CiAgPGNpcmNsZSBjeD0iMzcuMDkzIiBjeT0iNzEuMTYxIiByPSIyMy42MTIiLz4KIDwvZz4KPC9zdmc+Cg==';
3
3
  /**
4
- * A simple startup scene that waits for a mouse click before advancing.
4
+ * ## Why
5
5
  *
6
- * This exists to work around browser autoplay restrictions - browsers require
7
- * user interaction (like a click) before allowing audio playback. Show this
8
- * scene first, then transition to your game scene on click.
6
+ * 1. Because the LIKE logo looks awesome.
7
+ * 2. Autoplay restriction, doesn't let you play audio until the page is clicked.
8
+ * 3. You have to click on the game in order to send inputs, anyway.
9
+ * 4. It's polite.
10
+ *
11
+ * ## Usage
12
+ *
13
+ * ```typescript
14
+ * import { createLike, StartScreen } from 'like';
15
+ * import { GameScene } from './game';
16
+ *
17
+ * const container = document.getElementById("myGame");
18
+ * const like = createLike(container);
19
+ *
20
+ * // these callbacks will be ignored until the scene is clicked
21
+ * like.update = function () { ... }
22
+ * like.draw = function () { ... }
23
+ *
24
+ * // Set up the start screen
25
+ * like.setScene(new StartScreen())
26
+ * like.start();
27
+ * ```
28
+ *
29
+ * Alternatively, copy-paste this code into your own project and modify it freely.
30
+ * Update imports:
31
+ *
32
+ * ```ts
33
+ * import { type Scene } from 'like/scene';
34
+ * import type { ImageHandle } from 'like/graphics';
35
+ * import { Vec2 } from 'like/math';
36
+ * import { Like } from 'like';
37
+ * ```
38
+ *
39
+ * ## Custom Rendering
40
+ *
41
+ * Pass a custom draw function to replace the default logo:
42
+ *
43
+ * ```typescript
44
+ * const startup = new StartupScene(gameScene, (like) => {
45
+ * like.gfx.clear([0, 0, 0, 1]);
46
+ * like.gfx.print([1, 1, 1], 'Click to Start', [100, 100]);
47
+ * });
48
+ * ```
9
49
  */
10
- export class StartupScene {
50
+ export class StartScreen {
11
51
  constructor(next, onDraw) {
12
52
  Object.defineProperty(this, "next", {
13
53
  enumerable: true,
@@ -37,12 +77,12 @@ export class StartupScene {
37
77
  }
38
78
  else if (this.logo.isReady()) {
39
79
  like.gfx.clear([0.05, 0.05, 0.08, 1]);
40
- const winSize = like.gfx.getCanvasSize();
80
+ const winSize = like.canvas.getSize();
41
81
  const scale = (winSize[0] * 0.5) / this.logo.size[0];
42
82
  like.gfx.draw(this.logo, Vec2.div(winSize, 2), { scale, origin: Vec2.div(this.logo.size, 2) });
43
83
  }
44
84
  }
45
- mousepressed(like, _pos, _button) {
85
+ mousepressed(like) {
46
86
  like.setScene(this.next);
47
87
  }
48
88
  }
package/dist/scene.d.ts CHANGED
@@ -1,10 +1,108 @@
1
- import type { Like2DEvent, EventMap, EventType } from './core/events';
2
- import type { Like } from './core/like';
3
- type EventHandler<K extends EventType> = (like: Like, ...args: EventMap[K]) => void;
1
+ /**
2
+ * @module scene
3
+ * @description A helpful callback / state management layer, plus utility scenes.
4
+ *
5
+ */
6
+ import type { LikeEvent, EventMap } from './events';
7
+ import type { Like } from './like';
8
+ /**
9
+ * An interface for creating scenes.
10
+ *
11
+ * ## Why Scenes?
12
+ *
13
+ * For any game with more than one scene, we have to either:
14
+ * - switch-case on game state in every single callback
15
+ * - rebind all of the callbacks ourselves
16
+ * - wrap handleEvent (hint: that's what this does)
17
+ *
18
+ * Also, some no need to pass around a `like` object.
19
+ * Here, `like` instead piggybacks on a closure that follows around
20
+ * your running scene and shows up as an additional first argument
21
+ * to every callback.
22
+ *
23
+ * ## Quick Start
24
+ *
25
+ * Have a scene handle all the callbacks, disabling global
26
+ * callbacks.
27
+ * ```typescript
28
+ * // set up a scene
29
+ * class MagicalGrowingRectangle extends Scene {
30
+ * rectangleSize = 10;
31
+ * constructor() {}
32
+ *
33
+ * keypressed(_like: Like) {
34
+ * this.rectangleSize += 10;
35
+ * }
36
+ *
37
+ * draw(like: Like) {
38
+ * like.gfx.rectangle('fill', 'green',
39
+ * [10, 10, this.rectangleSize, this.rectangleSize])
40
+ * }
41
+ * }
42
+ *
43
+ * like.setScene(new MagicalGrowingRectangle());
44
+ * ```
45
+ *
46
+ * To get back to global callbacks, just use `like.handleEvent = undefined`
47
+ *
48
+ * ## Scene Lifecycle
49
+ *
50
+ * Works a lot like global callbacks.
51
+ *
52
+ * 1. `like.setScene(scene)` is called
53
+ * 2. Scene's `load` callback fires immediately
54
+ * 3. `update` and `draw` begin on next frame
55
+ * 4. Scene receives input events as they occur
56
+ *
57
+ * ## Composing scenes
58
+ *
59
+ * Thought you'd never ask.
60
+ * Just like the `like` object, scenes have handleEvent on them.
61
+ * So, you could layer them like this, for example:
62
+ *
63
+ * ```typescript
64
+ * class UI implements Scene {
65
+ * constructor(public game: Game) {}
66
+ *
67
+ * handleEvent(like: Like, event: Like2DEvent) {
68
+ * // Block mouse events in order to create a top bar.
69
+ * const mouseY = like.mouse.getPosition()[1];
70
+ * if (!event.type.startsWith('mouse') || mouseY > 100) {
71
+ * sceneDispatch(this.game, like, event);
72
+ * }
73
+ *
74
+ * // Then, call my own callbacks.
75
+ * // By calling it here, the UI draws on top.
76
+ * callSceneHandlers(this, like, event);
77
+ * }
78
+ * ...
79
+ * }
80
+ *
81
+ * class Game implements Scene {
82
+ * ...
83
+ * }
84
+ *
85
+ * like.setScene(new UI(new Game()))
86
+ * ```
87
+ *
88
+ * Composing scenes lets you filter events, layer game elements,
89
+ * and more. Don't sleep on it.
90
+ */
4
91
  export type Scene = {
5
- [K in EventType]?: EventHandler<K>;
92
+ [K in keyof EventMap]?: (like: Like, ...args: EventMap[K]) => void;
6
93
  } & {
7
- handleEvent?(like: Like, event: Like2DEvent): void;
94
+ handleEvent?(like: Like, event: LikeEvent): void;
8
95
  };
9
- export {};
96
+ /**
97
+ * Used to call a scene's own handlers like `update` or `draw`,
98
+ * typically at the end of handleEvent
99
+ * after modifying the event stream or composing sub-scenes.
100
+ */
101
+ export declare const callSceneHandlers: (scene: Scene, like: Like, event: LikeEvent) => void;
102
+ /**
103
+ * Used to call sub scenes while respecting potential `handleEvent` within them.
104
+ * The main scene is similar to a sub-scene of the root (like) object in this
105
+ * regard.
106
+ */
107
+ export declare const sceneDispatch: (scene: Scene, like: Like, event: LikeEvent) => void;
10
108
  //# sourceMappingURL=scene.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"scene.d.ts","sourceRoot":"","sources":["../src/scene.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC,KAAK,YAAY,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AAEpF,MAAM,MAAM,KAAK,GAAG;KACjB,CAAC,IAAI,SAAS,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC;CACnC,GAAG;IACF,WAAW,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;CACpD,CAAC"}
1
+ {"version":3,"file":"scene.d.ts","sourceRoot":"","sources":["../src/scene.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkFG;AAEH,MAAM,MAAM,KAAK,GAAG;KACjB,CAAC,IAAI,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI;CACnE,GAAG;IACF,WAAW,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;CAClD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,OAAO,KAAK,EAAE,MAAM,IAAI,EAAE,OAAO,SAAS,SAI3E,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,KAAK,EAAE,MAAM,IAAI,EAAE,OAAO,SAAS,SAMvE,CAAA"}
package/dist/scene.js CHANGED
@@ -1 +1,28 @@
1
- export {};
1
+ /**
2
+ * @module scene
3
+ * @description A helpful callback / state management layer, plus utility scenes.
4
+ *
5
+ */
6
+ /**
7
+ * Used to call a scene's own handlers like `update` or `draw`,
8
+ * typically at the end of handleEvent
9
+ * after modifying the event stream or composing sub-scenes.
10
+ */
11
+ export const callSceneHandlers = (scene, like, event) => {
12
+ if (event.type in scene) {
13
+ scene[event.type](like, ...event.args);
14
+ }
15
+ };
16
+ /**
17
+ * Used to call sub scenes while respecting potential `handleEvent` within them.
18
+ * The main scene is similar to a sub-scene of the root (like) object in this
19
+ * regard.
20
+ */
21
+ export const sceneDispatch = (scene, like, event) => {
22
+ if (scene.handleEvent) {
23
+ scene.handleEvent(like, event);
24
+ }
25
+ else {
26
+ callSceneHandlers(scene, like, event);
27
+ }
28
+ };
@@ -0,0 +1,2 @@
1
+ export { type Timer } from "./timer";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/timer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,SAAS,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { EngineProps } from "../engine";
2
+ export declare class Timer {
3
+ private currentDelta;
4
+ private totalTime;
5
+ private frameCount;
6
+ private fps;
7
+ private fpsAccumulator;
8
+ private sleepUntil;
9
+ constructor(props: EngineProps<never>);
10
+ private update;
11
+ /** Get `dt` (from the update loop) anywhere.
12
+ * AKA the time since the last frame.
13
+ */
14
+ getDelta(): number;
15
+ /** Get an estimated FPS based on one-second average. */
16
+ getFPS(): number;
17
+ /** Get the ingame time. */
18
+ getTime(): number;
19
+ /**
20
+ * Whether or not the game is (supposed to be) frozen.
21
+ * The only callback while sleeping is `draw`, and
22
+ * calling this outside of `draw` will always return
23
+ * false -- except if you have a custom runtime.
24
+ */
25
+ isSleeping(): boolean;
26
+ /**
27
+ * Freeze the whole game for a time. Audio will keep playing,
28
+ * but update functions won't be called and events won't fire.
29
+ */
30
+ sleep(duration: number): void;
31
+ }
32
+ //# sourceMappingURL=timer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../../src/timer/timer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,qBAAa,KAAK;IAChB,OAAO,CAAC,YAAY,CAAK;IACzB,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,GAAG,CAAK;IAChB,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,UAAU,CAAuB;gBAE7B,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC;IAIrC,OAAO,CAAC,MAAM;IAcd;;OAEG;IACH,QAAQ,IAAI,MAAM;IAIlB,wDAAwD;IACxD,MAAM,IAAI,MAAM;IAIhB,2BAA2B;IAC3B,OAAO,IAAI,MAAM;IAIjB;;;;;OAKG;IACH,UAAU,IAAI,OAAO;IAUrB;;;OAGG;IACH,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;CAG9B"}
@@ -1,5 +1,5 @@
1
1
  export class Timer {
2
- constructor() {
2
+ constructor(props) {
3
3
  Object.defineProperty(this, "currentDelta", {
4
4
  enumerable: true,
5
5
  configurable: true,
@@ -36,14 +36,10 @@ export class Timer {
36
36
  writable: true,
37
37
  value: null
38
38
  });
39
- Object.defineProperty(this, "sceneStartTime", {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: 0
44
- });
39
+ props.canvas.addEventListener("like:update", this.update.bind(this), { signal: props.abort });
45
40
  }
46
- update(dt) {
41
+ update(ev) {
42
+ const { dt } = ev.detail;
47
43
  this.currentDelta = dt;
48
44
  this.totalTime += dt;
49
45
  this.frameCount++;
@@ -54,21 +50,26 @@ export class Timer {
54
50
  this.fpsAccumulator = 0;
55
51
  }
56
52
  }
57
- resetSceneTime() {
58
- this.sceneStartTime = this.totalTime;
59
- }
53
+ /** Get `dt` (from the update loop) anywhere.
54
+ * AKA the time since the last frame.
55
+ */
60
56
  getDelta() {
61
57
  return this.currentDelta;
62
58
  }
59
+ /** Get an estimated FPS based on one-second average. */
63
60
  getFPS() {
64
61
  return this.fps;
65
62
  }
63
+ /** Get the ingame time. */
66
64
  getTime() {
67
65
  return this.totalTime;
68
66
  }
69
- getSceneTime() {
70
- return this.totalTime - this.sceneStartTime;
71
- }
67
+ /**
68
+ * Whether or not the game is (supposed to be) frozen.
69
+ * The only callback while sleeping is `draw`, and
70
+ * calling this outside of `draw` will always return
71
+ * false -- except if you have a custom runtime.
72
+ */
72
73
  isSleeping() {
73
74
  if (this.sleepUntil === null)
74
75
  return false;
@@ -79,6 +80,10 @@ export class Timer {
79
80
  this.sleepUntil = null;
80
81
  return false;
81
82
  }
83
+ /**
84
+ * Freeze the whole game for a time. Audio will keep playing,
85
+ * but update functions won't be called and events won't fire.
86
+ */
82
87
  sleep(duration) {
83
88
  this.sleepUntil = performance.now() + (duration * 1000);
84
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "like2d",
3
- "version": "2.8.0",
3
+ "version": "2.10.0",
4
4
  "description": "A web-native game framework inspired by Love2D",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -10,6 +10,30 @@
10
10
  ".": {
11
11
  "import": "./dist/index.js",
12
12
  "types": "./dist/index.d.ts"
13
+ },
14
+ "./math": {
15
+ "import": "./dist/math/index.js",
16
+ "types": "./dist/math/index.d.ts"
17
+ },
18
+ "./prefab-scenes": {
19
+ "import": "./dist/prefab-scenes/index.js",
20
+ "types": "./dist/prefab-scenes/index.d.ts"
21
+ },
22
+ "./graphics": {
23
+ "import": "./dist/graphics/index.js",
24
+ "types": "./dist/graphics/index.d.ts"
25
+ },
26
+ "./audio": {
27
+ "import": "./dist/audio/index.js",
28
+ "types": "./dist/audio/index.d.ts"
29
+ },
30
+ "./input": {
31
+ "import": "./dist/input/index.js",
32
+ "types": "./dist/input/index.d.ts"
33
+ },
34
+ "./timer": {
35
+ "import": "./dist/timer/index.js",
36
+ "types": "./dist/timer/index.d.ts"
13
37
  }
14
38
  },
15
39
  "files": [
@@ -23,7 +47,8 @@
23
47
  "typecheck": "tsc --noEmit",
24
48
  "lint": "echo 'No linting configured'",
25
49
  "clean": "rm -rf dist",
26
- "test": "vitest run"
50
+ "test": "vitest run",
51
+ "bench": "vitest bench"
27
52
  },
28
53
  "keywords": [
29
54
  "game",
@@ -1 +0,0 @@
1
- {"version":3,"file":"audio.d.ts","sourceRoot":"","sources":["../../src/core/audio.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAA;AAMD;;;;;;GAMG;AACH,qBAAa,MAAM;IACjB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6IAA6I;IAC7I,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,CAAC;IAC1C,2DAA2D;IAC3D,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9B,OAAO,CAAC,SAAS,CAAoE;IACrF,OAAO,CAAC,QAAQ,CAAQ;gBAEZ,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,GAAE,aAAkB;IAmCtE,OAAO,IAAI,OAAO;IAIlB,IAAI,IAAI,IAAI;IAUZ,IAAI,IAAI,IAAI;IAUZ,KAAK,IAAI,IAAI;IAQb,MAAM,IAAI,IAAI;IAYd,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAQ5B,IAAI,IAAI,MAAM;IAKd,SAAS,IAAI,OAAO;IAKpB,QAAQ,IAAI,OAAO;IAKnB,SAAS,IAAI,OAAO;IAKpB,+FAA+F;IAC/F,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAK/B,SAAS,IAAI,MAAM;IAInB,WAAW,IAAI,MAAM;CAItB;AAED,qBAAa,KAAK;IAChB,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,YAAY,CAAK;IAEzB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM;IAMxD;;;QAGI;IACJ,OAAO,CAAC,aAAa;IASrB,OAAO,IAAI,IAAI;IAIf,QAAQ,IAAI,IAAI;IAIhB,SAAS,IAAI,IAAI;IAIjB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAO/B,SAAS,IAAI,MAAM;IAInB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;CAG9B"}
@@ -1,22 +0,0 @@
1
- import type { Vector2 } from './vector2';
2
- export type CanvasMode = {
3
- pixelResolution: Vector2 | null;
4
- fullscreen: boolean;
5
- };
6
- export type PartialCanvasMode = {
7
- pixelResolution?: Vector2 | null;
8
- fullscreen?: boolean;
9
- };
10
- /**
11
- * Calculate the scale and offset for rendering fixed-size content to a target canvas.
12
- * This is useful when you want to render in "native" mode but maintain a fixed game resolution.
13
- *
14
- * @param canvasSize - The actual canvas size in pixels
15
- * @param gameSize - The desired game resolution (fixed size)
16
- * @returns Object containing the scale factor and offset for centering
17
- */
18
- export declare function calcFixedScale(canvasSize: Vector2, gameSize: Vector2): {
19
- scale: number;
20
- offset: Vector2;
21
- };
22
- //# sourceMappingURL=canvas-config.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"canvas-config.d.ts","sourceRoot":"","sources":["../../src/core/canvas-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC,MAAM,MAAM,UAAU,GAAG;IACvB,eAAe,EAAE,OAAO,GAAG,IAAI,CAAC;IAChC,UAAU,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IACjC,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAKzG"}