minimojs 1.0.0-alpha.22 → 1.0.0-alpha.24

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/minimo.d.ts CHANGED
@@ -186,9 +186,45 @@ export type BackgroundFit = "none" | "stretch" | "contain" | "cover";
186
186
  * scenes keep their expected `this` value.
187
187
  */
188
188
  export interface IScene {
189
+ /**
190
+ * Registers images this scene needs that the rest of the game does not.
191
+ *
192
+ * **Only for games with many scenes that each carry many assets of their
193
+ * own**, such as a game with many levels, where a player does not reach
194
+ * every scene in a session and loading all of them at boot would make the
195
+ * first wait needlessly long. Every other game should load its images in
196
+ * {@link Game.onPreload}, once, at boot.
197
+ *
198
+ * Called whenever the scene is entered — by {@link Game.start},
199
+ * {@link Game.reset}, or {@link Game.transitionTo} — before
200
+ * {@link IScene.onCreate}. Call {@link Game.loadImage} inside it, as inside
201
+ * `game.onPreload`. Images that are already loaded are skipped, so replaying
202
+ * a scene loads nothing.
203
+ *
204
+ * When images are pending, the scene being left freezes under a progress
205
+ * ring until they load, and only then is this scene created. Because of
206
+ * that, `reset()` and `transitionTo()` may return before this scene exists:
207
+ * continue in `onCreate()`, not in the code after the call.
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * class Level {
212
+ * constructor(n) { this.n = n; }
213
+ * onPreload() {
214
+ * game.loadImage(`level-${this.n}-bg`, `assets/level-${this.n}-bg.png`);
215
+ * }
216
+ * onCreate() {
217
+ * game.addBackground(new BackgroundLayer(`level-${this.n}-bg`));
218
+ * }
219
+ * }
220
+ * ```
221
+ */
222
+ onPreload?(): void;
189
223
  /**
190
224
  * Called when the scene is created, before the first frame and again after
191
- * each {@link Game.reset} that targets this scene.
225
+ * each {@link Game.reset} that targets this scene. When the scene has an
226
+ * {@link IScene.onPreload} with images still loading, it is called once they
227
+ * have loaded.
192
228
  */
193
229
  onCreate?(): void;
194
230
  /**
@@ -570,6 +606,10 @@ export declare class TextSprite extends Sprite {
570
606
  anchorX: "left" | "center" | "right";
571
607
  anchorY: "top" | "middle" | "bottom";
572
608
  text: string;
609
+ /**
610
+ * CSS font family. Default: `monospace`, which needs no download. Name a web
611
+ * font here, and register it with {@link Game.requireFont}, to use one.
612
+ */
573
613
  fontFamily: string;
574
614
  fontSize: number;
575
615
  fontWeight: string;
@@ -793,12 +833,12 @@ export interface CollisionInfo {
793
833
  *
794
834
  * ---
795
835
  *
796
- * ## Required Font Setup
836
+ * ## Font Setup
797
837
  *
798
- * `drawText()` and `TextSprite` default to `"Press Start 2P", monospace`.
799
- * MinimoJS automatically waits for that default family before the first frame.
800
- * If you use additional families, register them with {@link Game.requireFont}
801
- * before calling {@link Game.start}.
838
+ * `drawText()` and `TextSprite` default to `monospace`, which every device
839
+ * has, so a game that names no font downloads none and waits for none. To use
840
+ * a web font, declare it in your page, register it with {@link Game.requireFont}
841
+ * before calling {@link Game.start}, and name it in {@link TextSprite.fontFamily}.
802
842
  *
803
843
  * Prefer {@link TextSprite} for persistent UI text, interactive labels,
804
844
  * buttons, fixed-size text boxes, and styled text elements. Keep
@@ -1018,6 +1058,15 @@ export interface CollisionInfo {
1018
1058
  * Legacy `game.onCreate` / `game.onUpdate` callbacks still work when no active
1019
1059
  * {@link IScene} is set.
1020
1060
  *
1061
+ * ### Loading a Scene's Own Images
1062
+ *
1063
+ * Load images in {@link Game.onPreload}: once, at boot. Only a game with many
1064
+ * scenes that each carry many assets of their own — typically a game with many
1065
+ * levels, whose player does not reach every level in a session — should give
1066
+ * scenes an {@link IScene.onPreload}. Entering such a scene loads its images
1067
+ * first, with the scene being left frozen under a progress ring, so `reset()`
1068
+ * and `transitionTo()` may return before the new scene's `onCreate()` runs.
1069
+ *
1021
1070
  * ---
1022
1071
  *
1023
1072
  * ## EmojiSprite Lifecycle Ownership
@@ -1164,11 +1213,15 @@ export declare class Game {
1164
1213
  /**
1165
1214
  * Asset registration callback invoked once before the first scene is created.
1166
1215
  *
1167
- * Use this to register images with {@link Game.loadImage}. The callback is
1168
- * synchronous: it should only queue assets, not await network work.
1216
+ * This is the normal place to load a game's images. Use it to register them
1217
+ * with {@link Game.loadImage}. The callback is synchronous: it should only
1218
+ * queue assets, not await network work.
1169
1219
  *
1170
- * After `onPreload` returns, MinimoJS loads the queued images automatically
1171
- * and shows a default loading screen until they are ready.
1220
+ * After `onPreload` returns, MinimoJS loads the queued images, together with
1221
+ * those of the first scene's {@link IScene.onPreload} if it has one, and
1222
+ * shows a loading line until they are ready. If any image fails, it asks the
1223
+ * player with `window.confirm` whether to retry them; declining leaves the
1224
+ * game on the loading screen and reports the failed images on the console.
1172
1225
  *
1173
1226
  * @example
1174
1227
  * ```ts
@@ -1223,6 +1276,13 @@ export declare class Game {
1223
1276
  * Returns `true` while a full-screen scene transition is playing.
1224
1277
  */
1225
1278
  get isTransitioning(): boolean;
1279
+ /**
1280
+ * Returns `true` while images are loading: at boot, or while a scene with
1281
+ * pending images in its {@link IScene.onPreload} is being entered. While a
1282
+ * scene loads, the scene being left is frozen and `reset()` and
1283
+ * `transitionTo()` are ignored.
1284
+ */
1285
+ get isLoading(): boolean;
1226
1286
  /**
1227
1287
  * Creates a new MinimoJS game instance.
1228
1288
  *
@@ -1369,7 +1429,8 @@ export declare class Game {
1369
1429
  * This method registers the asset only. The actual network/image fetch begins
1370
1430
  * after `onPreload` returns and is managed automatically by the engine.
1371
1431
  *
1372
- * `loadImage` may only be called from within {@link Game.onPreload}.
1432
+ * `loadImage` may only be called from within {@link Game.onPreload} or a
1433
+ * scene's {@link IScene.onPreload}.
1373
1434
  *
1374
1435
  * @param key - Stable key used later by {@link BackgroundLayer.imageKey} or {@link Game.getImage}.
1375
1436
  * @param src - Image URL or relative path.
@@ -2122,14 +2183,8 @@ export declare class Game {
2122
2183
  * The text overlay list is cleared after each render. Call this inside `onUpdate`.
2123
2184
  *
2124
2185
  * **Layer:** Text is always drawn on top of all sprites.
2125
- * **Font:** Text uses `"Press Start 2P", monospace` by default.
2126
- * You MUST declare that font yourself in `index.html` (for example via Google
2127
- * Fonts) before calling {@link Game.start}. MinimoJS does NOT inject or
2128
- * download external fonts for you.
2129
- *
2130
- * `Game.start()` waits for the default font and for any extra families you
2131
- * registered with {@link Game.requireFont}. If a font is unavailable, the
2132
- * browser falls back to `monospace`.
2186
+ * **Font:** Text always uses `monospace`. Use {@link TextSprite} for any
2187
+ * other family.
2133
2188
  *
2134
2189
  * Prefer {@link TextSprite} for UI buttons or labels that need persistent
2135
2190
  * bounds, hit testing, fixed sizes, backgrounds, borders, or text stroke.
@@ -2236,6 +2291,12 @@ export declare class Game {
2236
2291
  * `onCreate()` (or legacy {@link Game.onCreate} if no scene is active) so it
2237
2292
  * can rebuild synchronously.
2238
2293
  *
2294
+ * The exception is a scene whose {@link IScene.onPreload} registers images
2295
+ * that are not loaded yet. Then the current scene freezes under a progress
2296
+ * ring, nothing is cleared until the images load, and `reset()` returns
2297
+ * before the scene is rebuilt; its `onCreate()` runs once they have loaded.
2298
+ * A `reset()` called while a scene is loading is ignored.
2299
+ *
2239
2300
  * @param scene - Optional new scene to make active before rebuilding.
2240
2301
  *
2241
2302
  * @example
@@ -2263,7 +2324,17 @@ export declare class Game {
2263
2324
  *
2264
2325
  * While the transition is active, gameplay updates are paused.
2265
2326
  *
2266
- * If the game loop is not running yet, this falls back to an immediate
2327
+ * If the target scene's {@link IScene.onPreload} registers images that are
2328
+ * not loaded yet, the current scene first freezes under a progress ring
2329
+ * until they load, and the transition plays after that. `transitionTo()`
2330
+ * then returns before the target scene exists; continue in its
2331
+ * `onCreate()` or in `onComplete`. If the player declines to retry failed
2332
+ * images, the game stays on the current scene and `onComplete` is not
2333
+ * called.
2334
+ *
2335
+ * A call made while a transition plays or a scene loads is ignored.
2336
+ *
2337
+ * If the game loop is not running yet, this falls back to
2267
2338
  * {@link Game.reset}.
2268
2339
  *
2269
2340
  * @param scene - Target scene to make active.
@@ -2274,12 +2345,11 @@ export declare class Game {
2274
2345
  /**
2275
2346
  * Starts the `requestAnimationFrame` game loop.
2276
2347
  * Safe to call multiple times — does nothing if already running.
2277
- * On the first start, if {@link Game.onPreload} is set, MinimoJS first runs
2278
- * asset registration, loads queued images, waits for all fonts registered via
2279
- * {@link Game.requireFont}, and only then, if images were queued, shows a
2280
- * default loading screen while those image assets are still loading.
2281
- * After preload completes, the active scene's `onCreate()` is called before
2282
- * the first frame.
2348
+ * On the first start, MinimoJS runs {@link Game.onPreload} and the active
2349
+ * scene's {@link IScene.onPreload}, loads the images they queued while
2350
+ * showing a thin loading line, and waits for all fonts registered via
2351
+ * {@link Game.requireFont}. After that, the active scene's `onCreate()` is
2352
+ * called before the first frame.
2283
2353
  *
2284
2354
  * The loop calls the active scene's `onUpdate(dt)` (or legacy
2285
2355
  * {@link Game.onUpdate}) once per frame, then renders all sprites and text