like2d 2.12.0 → 2.13.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.
package/dist/like.d.ts CHANGED
@@ -1,28 +1,49 @@
1
- import type { Audio } from './audio/index';
2
- import type { Timer } from './timer/index';
3
- import type { Input } from './input/index';
1
+ import type { Audio } from './audio/';
2
+ import type { Timer } from './timer/';
3
+ import type { Input } from './input/';
4
4
  import type { Keyboard } from './input/keyboard';
5
5
  import type { Mouse } from './input/mouse';
6
6
  import type { Gamepad } from './input/gamepad';
7
7
  import type { Canvas } from './graphics/canvas';
8
- import type { Graphics } from './graphics/index';
9
- import { EventMap, EventType, LikeEvent } from './events';
10
- import { Scene } from './scene';
8
+ import type { Graphics } from './graphics/';
9
+ import { LikeEvent, LikeEventHandlers } from './events';
11
10
  /** @private */
12
11
  export type TopLevelEventHandler = (event: LikeEvent) => void;
13
- /** @private */
14
- export type Callback<K extends EventType> = (...args: EventMap[K]) => void;
15
- /** @private */
16
- export type Callbacks = {
17
- [K in EventType]?: Callback<K>;
12
+ /**
13
+ * Every possible event handler callback is in this interface.
14
+ *
15
+ * The engine will call these functions when the corresponding
16
+ * events fire unless {@link handleEvent} is customized, for example
17
+ * when the scene system is in use.
18
+ *
19
+ * @interface
20
+ */
21
+ export type LikeHandlers = Partial<LikeEventHandlers> & {
22
+ /**
23
+ * LIKE's runtime is concentrated into handleEvent.
24
+ *
25
+ * This function recieves all events.
26
+ * {@link callOwnHandlers} is the default behavior.
27
+ *
28
+ * Otherwise, a custom handler will totally override
29
+ * event handler callbacks like `like.draw`,
30
+ * replacing it with a system of your choice.
31
+ *
32
+ * For example, the scene architecture is built around
33
+ * setting this function. Setting to a custom
34
+ * function will disable the scene system.
35
+ *
36
+ * Setting `handleEvent` to `undefined` will revert
37
+ * to default behavior.
38
+ */
39
+ handleEvent?: TopLevelEventHandler;
18
40
  };
19
41
  /**
20
- * The main Like instance.
21
- * Use this object much how you would the `love` object in Love2D.
22
- * This is the interface returned by {@link createLike}.
42
+ * The main modules and builtins of `like`.
43
+ * @interface
23
44
  */
24
- export type Like = Callbacks & {
25
- /** Handle a pool of pseudo-synchronous audio sources with global volume control and more. */
45
+ export type LikeBase = {
46
+ /** Handle a pool of audio sources with global volume control and more. */
26
47
  readonly audio: Audio;
27
48
  /** Misc. time functions, including sleeping the game. ZZZ */
28
49
  readonly timer: Timer;
@@ -38,9 +59,9 @@ export type Like = Callbacks & {
38
59
  readonly canvas: Canvas;
39
60
  /** Graphics module: LOVE-style rendering, plus a pseudo-synchronous way to load images. */
40
61
  readonly gfx: Graphics;
41
- /** I think you meant to type `canvas`. */
62
+ /** @private Use {@link canvas} instead. */
42
63
  window?: never;
43
- /** Shortened to `like.gfx`. */
64
+ /** @private Use {@link gfx} instead. */
44
65
  graphics?: never;
45
66
  /**
46
67
  * Start the game loop. Call this only once.
@@ -52,55 +73,6 @@ export type Like = Callbacks & {
52
73
  * running, and probably break if you try to use it.
53
74
  */
54
75
  dispose(): void;
55
- /**
56
- * Push a scene to the scene stack.
57
- *
58
- * If the engine is running, this is the new running scene replacing the old one
59
- * which can, in some cases, call out to the lower scene.
60
- *
61
- * @param overlay Set to true, and the current scene (before pushing) will stay loaded. Otherwise not.
62
- */
63
- pushScene(scene: Scene, overlay: boolean): void;
64
- /**
65
- * Pop the current scene off the stack.
66
- *
67
- * To clear the stack, just run:
68
- * ```ts
69
- * while (like.popScene());
70
- * ```
71
- */
72
- popScene(): Scene | undefined;
73
- /**
74
- * Set the current scene at the top of the scene stack.
75
- * If the stack is empty, push it onto the stack.
76
- *
77
- * Equivalent to `popScene` + `pushScene`.
78
- *
79
- * Use {@link popScene} to clear away the current scene,
80
- * and to possibly revert to callback mode.
81
- */
82
- setScene(scene: Scene): void;
83
- /**
84
- * Get the current scene, or a specific index.
85
- *
86
- * Uses `Array.at` under the hood, so -1 is the
87
- * top scene, -2 is the parent scene, etc.
88
- */
89
- getScene(index?: number): Scene | undefined;
90
- /**
91
- * LIKE's runtime is built around calling handleEvent.
92
- *
93
- * This function recieves all events. If set to undefined,
94
- * {@link callOwnHandlers} is the default behavior.
95
- *
96
- * Otherwise, you can really customize LIKE by setting this
97
- * to a custom handler.
98
- *
99
- * For example, the scene architecture is built around
100
- * setting this function. Setting it to a custom
101
- * function will disable the scene system.
102
- */
103
- handleEvent?: TopLevelEventHandler;
104
76
  /**
105
77
  * Used as the default `like.handleEvent`, simply dispatches
106
78
  * an event into LIKE callbacks.
@@ -108,4 +80,10 @@ export type Like = Callbacks & {
108
80
  */
109
81
  callOwnHandlers(event: LikeEvent): void;
110
82
  };
83
+ /**
84
+ * The main Like instance.
85
+ * Use this object much how you would the `love` object in Love2D.
86
+ * This is the interface returned by {@link createLike}.
87
+ */
88
+ export type Like = LikeHandlers & LikeBase;
111
89
  //# sourceMappingURL=like.d.ts.map
@@ -41,6 +41,7 @@ import { type Vector2 } from '../math/vector2';
41
41
  *
42
42
  * */
43
43
  export type Rectangle = [number, number, number, number];
44
+ /** The full library of {@link Rectangle} functions. */
44
45
  export declare const Rect: {
45
46
  fromPoints(a: Vector2, b: Vector2): Rectangle;
46
47
  fromCenter(center: Vector2, size: Vector2): Rectangle;
package/dist/math/rect.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Vec2 } from '../math/vector2';
2
+ /** The full library of {@link Rectangle} functions. */
2
3
  export const Rect = {
3
4
  fromPoints(a, b) {
4
5
  const minX = Math.min(a[0], b[0]);
@@ -76,6 +76,9 @@ export type Vector2 = Pair<number>;
76
76
  declare function map2x1<I, O>(op: (a: I) => O): (a: Pair<I>) => Pair<O>;
77
77
  /** @see {@link Vec2.map2} */
78
78
  declare function map2x2<I, O>(op: (a: I, b: I) => O): (a: Pair<I>, b: I | Pair<I>) => Pair<O>;
79
+ /**
80
+ * The full library of {@link Vector2} functions.
81
+ */
79
82
  export declare const Vec2: {
80
83
  /**
81
84
  * Turn a unary function into a pair-wise unary function.
@@ -15,6 +15,9 @@ function map2x2(op) {
15
15
  ? [op(a[0], b[0]), op(a[1], b[1])]
16
16
  : [op(a[0], b), op(a[1], b)];
17
17
  }
18
+ /**
19
+ * The full library of {@link Vector2} functions.
20
+ */
18
21
  export const Vec2 = {
19
22
  /**
20
23
  * Turn a unary function into a pair-wise unary function.
@@ -0,0 +1,368 @@
1
+ /**
2
+ * Scenes are a modular component of LÏKE based on setting `like.handleEvent`.
3
+ * The scene system is simple and powerful, once understood.
4
+ *
5
+ * For devs using the built-in callback pattern, they're an easy way
6
+ * to stack functionality on to the current project such as
7
+ * gamepad mapping or debug overlays.
8
+ *
9
+ * For multi-scene games, they codify a common state-management pattern based
10
+ * on switching between (or nesting) event handler callbacks. It's
11
+ * a lot better than switch-casing on each handler, or manually setting/clearing
12
+ * handler functions on each transition.
13
+ *
14
+ * Using scenes for your game also replaces the need to pass around global `like`
15
+ * or `sceneManager` wherever it is used.
16
+ *
17
+ * ## Jump in
18
+ *
19
+ * Get started: {@link SceneManager}
20
+ *
21
+ * Make your own scene: {@link Scene}
22
+ *
23
+ * Check out built-in utility scenes:
24
+ * - {@link scene/prefab/mapGamepad}
25
+ * - {@link scene/prefab/startScreen}
26
+ *
27
+ * @module scene
28
+ */
29
+ import type { Like, LikeHandlers } from '../like';
30
+ /**
31
+ * ## Creating your own scenes
32
+ *
33
+ * Scenes are a factory function that receives `Like` and `SceneManager`
34
+ * and returns a {@link LikeHandlers | scene instance with event handlers}.
35
+ *
36
+ * ## Examples
37
+ *
38
+ * Minimal usage:
39
+ * ```typescript
40
+ * const gameOver: Scene = (like, scenes) => ({
41
+ * titleCard: like.gfx.newImage(path);
42
+ * spawnTime: like.timer.getTime();
43
+ * draw() {
44
+ * // draw 'game over' over the parent scene
45
+ * like.gfx.draw(this.titleCard);
46
+ * scenes.get(-2)?.draw();
47
+ * }
48
+ * update() {
49
+ * // back to title screen after 3 seconds
50
+ // (assuming title screen is using callback pattern)
51
+ * if (like.timer.getTime() > spawnTime + 3) {
52
+ * while(scenes.pop());
53
+ * }
54
+ * }
55
+ * })
56
+ * ```
57
+ *
58
+ * For configurable scenes, it is reccommended to use a function
59
+ * that returns a Scene.
60
+ * ```typescript
61
+ * const myScene = (options: { speed: number }): Scene =>
62
+ * (like: Like, scenes: SceneManager) => {
63
+ *
64
+ * const playerImage = like.gfx.newImage('player.png');
65
+ * let x = 0, y = 0;
66
+ *
67
+ * return {
68
+ * update(dt) {
69
+ * x += options.speed * dt;
70
+ * },
71
+ * draw() {
72
+ * like.gfx.draw(playerImage, [x, y]);
73
+ * }
74
+ * mousepressed() {
75
+ * // exit this scene when user clicks
76
+ * scene.pop();
77
+ * }
78
+ * };
79
+ * };
80
+ * ```
81
+ *
82
+ * Of course, a class pattern is also possible.
83
+ * ```typescript
84
+ * class ThingDoer extends SceneInstance {
85
+ * constructor(like, scenes) {...}
86
+ * ...
87
+ * }
88
+ *
89
+ * const thingDoerScene: Scene =
90
+ * (like, scenes) => new ThingDoer(like, scenes);
91
+ * ```
92
+ * Or a configurable class:
93
+ * ```typescript
94
+ * class ThingDoer extends SceneInstance {
95
+ * constructor(like, scenes, options) {...}
96
+ * ...
97
+ * }
98
+ *
99
+ * const thingDoerScene = (options): Scene =>
100
+ * (like, scenes) => new ThingDoer(like, scenes, options);
101
+ * ```
102
+ *
103
+ * ## Converting from Callbacks
104
+ *
105
+ * When converting from global callbacks to a scene:
106
+ *
107
+ * ```typescript
108
+ * // Before (callbacks)
109
+ * like.update = function(dt) { player.update(dt); }
110
+ * like.draw = () => { player.draw(like.gfx); }
111
+ *
112
+ * // After (scene)
113
+ * scenes.set((like, scenes) => {
114
+ * const scene: SceneInstance = {}
115
+ * scene.update = function (dt) { player.update(dt); },
116
+ * scene.draw = () => { player.draw(like.gfx); }
117
+ * return scene;
118
+ * });
119
+ * ```
120
+ * ## Composing scenes
121
+ *
122
+ * Just like the `like` object, scenes have handleEvent on them.
123
+ * So, you could layer them like this, for example:
124
+ *
125
+ * ```typescript
126
+ * // Composing scenes lets us know about the children.
127
+ * // This allows communication, for example:
128
+ * type UISceneInstance = SceneInstance & {
129
+ * // Sending events to child scene
130
+ * buttonClicked(name: string): void;
131
+ * // Getting info from child scene
132
+ * getStatus(): string;
133
+ * };
134
+ * type UIScene = SceneEx<UISceneInstance>;
135
+ *
136
+ * const uiScene = (game: UIScene): Scene =>
137
+ * (like, scenes) => {
138
+ * const childScene = scenes.instantiate(game);
139
+ * return {
140
+ * handleEvent(event) {
141
+ * // Block mouse events in order to create a top bar.
142
+ * // Otherwise, propogate them.
143
+ * const mouseY = like.mouse.getPosition()[1];
144
+ * if (!event.type.startsWith('mouse') || mouseY > 100) {
145
+ * // Use likeDispatch so that nested handleEvent can fire,
146
+ * // if relevant.
147
+ * likeDispatch(childScene, event);
148
+ * }
149
+ * // Then, call my own callbacks.
150
+ * // Using likeDispatch here will result in an infinite loop.
151
+ * callOwnHandlers(this, event);
152
+ * },
153
+ * mousepressed(pos) {
154
+ * if (buttonClicked(pos)) {
155
+ * childScene.buttonClicked('statusbar')
156
+ * }
157
+ * },
158
+ * draw() {
159
+ * drawStatus(like, childScene.getStatus());
160
+ * }
161
+ * };
162
+ * }
163
+ *
164
+ * const gameScene = (level: number): UIScene =>
165
+ * (like, scene) => ({
166
+ * update() { ... },
167
+ * draw() { ... },
168
+ * // mandatory UI methods from interface
169
+ * buttonClicked(name) {
170
+ * doSomething(),
171
+ * },
172
+ * getStatus() {
173
+ * return 'all good!';
174
+ * }
175
+ * });
176
+ *
177
+ * like.pushScene(uiScene(gameScene);
178
+ * ```
179
+ *
180
+ * The main advance of composing scenes versus the stack-overlay
181
+ * technique is that the parent scene knows about its child.
182
+ * Because there's a **known interface**, the two scenes
183
+ * can communicate.
184
+ *
185
+ * This makes it perfect for reusable UI,
186
+ * level editors, debug viewers, and more.
187
+ *
188
+ * ## Overlay scenes
189
+ *
190
+ * You might assume that the purpose of a scene stack is
191
+ * visual: first push the BG, then the FG, etc.
192
+ *
193
+ * Actually, composing scenes (above) is a
194
+ * better pattern for that, since it's both explicit
195
+ * _and_ the parent can have a known interface on its child.
196
+ * Here, the **upper** scene only knows that the
197
+ * **lower** scene _is_ a scene.
198
+ *
199
+ * That's the tradeoff. Overlay scenes are good for things
200
+ * like pause screens or gamepad overlays. Anything where
201
+ * the upper doesn't care _what_ the lower is, and where
202
+ * the upper scene should be easily addable/removable.
203
+ *
204
+ * Using `like.getScene(-2)`, the overlay scene can see
205
+ * the lower scene and choose how to propagate events.
206
+ *
207
+ * The only technical difference between overlay and
208
+ * opaque is whether or not the scene we've pushed
209
+ * on top of stays loaded.
210
+ */
211
+ export type Scene = (like: Like, scenes: SceneManager) => SceneInstance;
212
+ /**
213
+ * A helper for extending Scenes as to have known interfaces beyond
214
+ * the generic ones. For example:
215
+ *
216
+ * ```ts
217
+ * type UISceneInstance = SceneInstance & {
218
+ * buttonClicked(name: string) => void;
219
+ * };
220
+ * type UIScene = SceneEx<UISceneInstance>;
221
+ * ```
222
+ *
223
+ * Now, a parent composing scene can take in UIScene rather than Scene,
224
+ * and it has no need to cast anything.
225
+ */
226
+ export type SceneEx<S> = (like: Like, scenes: SceneManager) => S & SceneInstance;
227
+ /**
228
+ * A scene instance is just an object with event handlers. It's
229
+ * the `like` object but without the modules -- just the event handling
230
+ * callbacks.
231
+ *
232
+ * See {@link Scene} for usage.
233
+ */
234
+ export type SceneInstance = LikeHandlers & {
235
+ /**
236
+ * Called when a scene is started or resumed (pop after a push w/o unload).
237
+ * Prefer to initialize in the scene constructor.
238
+ *
239
+ * Use case: This secene does a push without unload because we want to preserve
240
+ * its state, but we unload a few large assets before doing the push. When
241
+ * upper scene is popped, `load` fires so we can get those assets back.
242
+ *
243
+ * (Same signature as the one in {@link LikeHandlers}, just redeclared for docs)
244
+ */
245
+ load?: () => void;
246
+ /**
247
+ * Called when a scene is pushed with unload, or popped off.
248
+ *
249
+ * Use case: We want to clear out any native event handlers or global resource
250
+ * allocations (LÏKE has neither, but maybe you do?) in case another scene
251
+ * kicks this one off the stack.
252
+ *
253
+ * (Same signature as the one in {@link LikeHandlers}, just redeclared for docs)
254
+ */
255
+ quit?: () => void;
256
+ };
257
+ /** Goofy ahh Typescript thingy to avoid excess generics @private */
258
+ export type InstantiateReturn<F> = F extends SceneEx<infer S> ? S & SceneInstance : never;
259
+ /**
260
+ * Scenemanager is the entry point for the LÏKE scene system.
261
+ * Without it, there are no scene functions; it's entirely modular.
262
+ *
263
+ * Usage:
264
+ * ```typescript
265
+ * const like = createLike(document.body);
266
+ * const sceneMan = new SceneManager(like);
267
+ * sceneMan.push(myScene)
268
+ * ```
269
+ * For arbitrary scene management (non stack based),
270
+ * just use {@link SceneManager.set} which switches out the stack top.
271
+ *
272
+ * For stack-based, use {@link SceneManager.push} and {@link SceneManager.pop}.
273
+ * Note that for stack-based games, it is wise to put the first initialization in as
274
+ * a callback-based system rather than going straight to scene. This allows
275
+ * for easy resets / game overs.
276
+ *
277
+ * Note that the SceneManager sets {@link LikeHandlers.handleEvent | like.handleEvent}.
278
+ * To get rid of scene functionality entirely, simply set it back to default.
279
+ * ```typescript
280
+ * like.handleEvent = undefined;
281
+ * ```
282
+ * Otherwise, the `SceneManager` stays allocated even if the scene stack was cleared.
283
+ */
284
+ export declare class SceneManager {
285
+ private like;
286
+ private scenes;
287
+ constructor(like: Like);
288
+ /**
289
+ * Get the current scene, or a specific index.
290
+ *
291
+ * Uses `Array.at` under the hood, so -1 is the
292
+ * top scene, -2 is the parent scene, etc.
293
+ *
294
+ * During instantiation, the stack is not shifted
295
+ * relative to during event/lifecycle functions.
296
+ * The only difference is that during load,
297
+ * scene.get(-1) of course returns no value.
298
+ */
299
+ get(pos?: number): SceneInstance | undefined;
300
+ /**
301
+ * Set the current scene at the top of the scene stack.
302
+ * If the stack is empty, push it onto the stack.
303
+ *
304
+ * The new scene is instantiated after the old one is
305
+ * quit and removed from the stack.
306
+ *
307
+ * Set cannot clear the current scene; for that use {@link pop}.
308
+ *
309
+ * @param scene is a Scene (factory pattern).
310
+ * @param instance is an optional preloaded instance.
311
+ */
312
+ set(scene: Scene, instance?: SceneInstance): void;
313
+ /**
314
+ * Push a scene to the scene stack and run it.
315
+ *
316
+ * @param scene A function that creates and returns a scene instance, which is just event handlers.
317
+ *
318
+ * @param unload
319
+ *
320
+ * If a scene calls `scenes.push(nextScene, true)`, it will be unloaded
321
+ * and re-constructed upon the parent scene calling `scenes.pop()`.
322
+ * Good for resource-intensive
323
+ * scenes or ones that rely heavily on their lifecycle. If you do want
324
+ * the lower scene to know what happened in the upper while unloaded, (i.e. overworld
325
+ * updating with a battle), consider using scene composition instead, or
326
+ * using localStorage to track persistent game state.
327
+ *
328
+ * If a scene calls `scenes.push(nextScene, false)`, it will stay loaded:
329
+ * this means when we pop its parent, it will simply continue running, though
330
+ * `load` will be called. Assets will of course stay loaded in during that time.
331
+ *
332
+ * Further, with unload disabled the upper scene now has the ability to reference
333
+ * the instance that called `scene.push` and call down to it in a generic way
334
+ * via `scene.get(-2)`
335
+ *
336
+ * See {@link Scene} for more detail -- while stacking is good for certain
337
+ * things, you're likely looking for Scene Composition.
338
+ *
339
+ */
340
+ push(scene: Scene, unload: boolean): void;
341
+ /**
342
+ * Pop the current scene off the stack, calling `quit` on it and
343
+ * dropping the instance reference.
344
+ *
345
+ * If the lower scene had called `pushScene` with the second arg (unload)
346
+ * set to true, it will be re-loaded. Otherwise it will continue where it
347
+ * left off. Either way its `load` fill fire.
348
+ *
349
+ * To clear the stack, just run:
350
+ * ```ts
351
+ * while (like.popScene());
352
+ * ```
353
+ */
354
+ pop(): Scene | undefined;
355
+ /**
356
+ * Make a scene into an instance and dispatch `load` into it.
357
+ */
358
+ instantiate<T extends SceneEx<SceneInstance>>(scene: T): InstantiateReturn<T>;
359
+ /**
360
+ * Unload a parent scene. Only use this if the lower scene requested to be
361
+ * unloaded, or if you're certain that you want to reload the lower
362
+ * completely. Otherwise, this can easily lose state or break functions.
363
+ */
364
+ deinstance(pos: number): void;
365
+ debugDraw(): void;
366
+ private handleEvent;
367
+ }
368
+ //# sourceMappingURL=index.d.ts.map