minimojs 1.0.0-alpha.28 → 1.0.0-alpha.29
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/internal/AssetSystem.js +53 -0
- package/dist/internal/DesignNamingCheck.d.ts +1 -0
- package/dist/internal/DesignNamingCheck.js +7 -0
- package/dist/minimo-format.d.ts +407 -0
- package/dist/minimo-format.js +446 -0
- package/dist/minimo.d.ts +230 -4
- package/dist/minimo.js +348 -1
- package/package.json +5 -1
package/dist/minimo.d.ts
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
* ALL ROTATIONS ARE IN DEGREES.
|
|
8
8
|
* THE ENGINE LOOP USES requestAnimationFrame ONLY.
|
|
9
9
|
*/
|
|
10
|
+
import type { DesignObject } from "./minimo-format.js";
|
|
11
|
+
export { Design, parseAssetFile } from "./minimo-format.js";
|
|
12
|
+
export type { AssetFile, DesignBackground, DesignEmoji, DesignFile, DesignImage, DesignObject, DesignObjectBase, DesignObjectType, DesignSpriteBase, DesignText, ImageAsset, UnknownAsset, } from "./minimo-format.js";
|
|
10
13
|
/**
|
|
11
14
|
* Optional tuning values for {@link Game.animateExplode}.
|
|
12
15
|
*
|
|
@@ -197,9 +200,9 @@ export interface IScene {
|
|
|
197
200
|
*
|
|
198
201
|
* Called whenever the scene is entered — by {@link Game.start},
|
|
199
202
|
* {@link Game.reset}, or {@link Game.transitionTo} — before
|
|
200
|
-
* {@link IScene.onCreate}. Call {@link Game.
|
|
201
|
-
* `game.onPreload`. Images that
|
|
202
|
-
* a scene loads nothing.
|
|
203
|
+
* {@link IScene.onCreate}. Call {@link Game.loadAssets} or
|
|
204
|
+
* {@link Game.loadImage} inside it, as inside `game.onPreload`. Images that
|
|
205
|
+
* are already loaded are skipped, so replaying a scene loads nothing.
|
|
203
206
|
*
|
|
204
207
|
* When images are pending, the scene being left freezes, dimmed, under a
|
|
205
208
|
* progress ring until they load, and only then is this scene created.
|
|
@@ -419,6 +422,35 @@ export declare abstract class Sprite {
|
|
|
419
422
|
* Values > 1 amplify gravity; negative values invert it.
|
|
420
423
|
*/
|
|
421
424
|
gravityScale: number;
|
|
425
|
+
/**
|
|
426
|
+
* Optional name, so code can ask an object what it is. Names may repeat,
|
|
427
|
+
* like a `name` attribute in the DOM. The engine never reads it.
|
|
428
|
+
* A sprite created from a design file takes the object's `name`.
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* ```ts
|
|
432
|
+
* if (other.name === "door") openDoor();
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
name: string | undefined;
|
|
436
|
+
/**
|
|
437
|
+
* Tags for grouping, like CSS classes in the DOM. Empty by default. The
|
|
438
|
+
* engine never reads them. A sprite created from a design file takes the
|
|
439
|
+
* object's `tags`, which {@link Game.registerBuilderByTag} also matches.
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```ts
|
|
443
|
+
* if (other.tags.includes("coin")) collect(other);
|
|
444
|
+
* ```
|
|
445
|
+
*/
|
|
446
|
+
tags: string[];
|
|
447
|
+
/**
|
|
448
|
+
* The game's own values for this object, such as a button's action or an
|
|
449
|
+
* enemy's speed. An empty object by default, so `sprite.data.hp = 3` needs
|
|
450
|
+
* no check. The engine never reads it. A sprite created from a design file
|
|
451
|
+
* takes a copy of the object's `data`.
|
|
452
|
+
*/
|
|
453
|
+
data: Record<string, unknown>;
|
|
422
454
|
/**
|
|
423
455
|
* Stable cache key describing the sprite's rendered appearance.
|
|
424
456
|
*
|
|
@@ -728,6 +760,21 @@ export declare class BackgroundLayer {
|
|
|
728
760
|
* Default: `"cover"`.
|
|
729
761
|
*/
|
|
730
762
|
fit: BackgroundFit;
|
|
763
|
+
/**
|
|
764
|
+
* Optional name, so code can ask a layer what it is. Names may repeat. The
|
|
765
|
+
* engine never reads it. See {@link Sprite.name}.
|
|
766
|
+
*/
|
|
767
|
+
name: string | undefined;
|
|
768
|
+
/**
|
|
769
|
+
* Tags for grouping. Empty by default. The engine never reads them. See
|
|
770
|
+
* {@link Sprite.tags}.
|
|
771
|
+
*/
|
|
772
|
+
tags: string[];
|
|
773
|
+
/**
|
|
774
|
+
* The game's own values for this layer. An empty object by default. The
|
|
775
|
+
* engine never reads it. See {@link Sprite.data}.
|
|
776
|
+
*/
|
|
777
|
+
data: Record<string, unknown>;
|
|
731
778
|
/**
|
|
732
779
|
* Creates a new background layer.
|
|
733
780
|
*
|
|
@@ -739,6 +786,52 @@ export declare class BackgroundLayer {
|
|
|
739
786
|
*/
|
|
740
787
|
constructor(imageKey: string, x?: number, y?: number, width?: number, height?: number);
|
|
741
788
|
}
|
|
789
|
+
/**
|
|
790
|
+
* The objects {@link Game.createObjectList} created, in the order of their
|
|
791
|
+
* data, and searches on them.
|
|
792
|
+
*
|
|
793
|
+
* An `ObjectList` only holds and finds its objects; it never changes them.
|
|
794
|
+
* Remove them through the game, as any other object:
|
|
795
|
+
*
|
|
796
|
+
* ```ts
|
|
797
|
+
* for (const obj of ui.objects) {
|
|
798
|
+
* if (obj instanceof BackgroundLayer) game.destroyBackgroundLayer(obj);
|
|
799
|
+
* else game.destroySprite(obj);
|
|
800
|
+
* }
|
|
801
|
+
* ```
|
|
802
|
+
*
|
|
803
|
+
* Searches read each object's current {@link Sprite.name} and
|
|
804
|
+
* {@link Sprite.tags}. In TypeScript, name the type you expect:
|
|
805
|
+
* `ui.findByName<ImageSprite>("play")`.
|
|
806
|
+
*/
|
|
807
|
+
export declare class ObjectList {
|
|
808
|
+
/** Every object created, in the order of the data. */
|
|
809
|
+
readonly objects: readonly (Sprite | BackgroundLayer)[];
|
|
810
|
+
/**
|
|
811
|
+
* Returns the first object whose `name` is `name`, in order.
|
|
812
|
+
* Throws an error naming `name` when there is none.
|
|
813
|
+
*
|
|
814
|
+
* @example
|
|
815
|
+
* ```ts
|
|
816
|
+
* const play = ui.findByName("play");
|
|
817
|
+
* ```
|
|
818
|
+
*/
|
|
819
|
+
findByName<T extends Sprite | BackgroundLayer = Sprite | BackgroundLayer>(name: string): T;
|
|
820
|
+
/** Returns every object whose `name` is `name`, in order; an empty list when there is none. */
|
|
821
|
+
findAllByName<T extends Sprite | BackgroundLayer = Sprite | BackgroundLayer>(name: string): T[];
|
|
822
|
+
/**
|
|
823
|
+
* Returns the first object whose `tags` include `tag`, in order.
|
|
824
|
+
* Throws an error naming `tag` when there is none.
|
|
825
|
+
*/
|
|
826
|
+
findByTag<T extends Sprite | BackgroundLayer = Sprite | BackgroundLayer>(tag: string): T;
|
|
827
|
+
/** Returns every object whose `tags` include `tag`, in order; an empty list when there is none. */
|
|
828
|
+
findAllByTag<T extends Sprite | BackgroundLayer = Sprite | BackgroundLayer>(tag: string): T[];
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Builds the object for a design object whose tags include the tag it was
|
|
832
|
+
* registered for. See {@link Game.registerBuilderByTag}.
|
|
833
|
+
*/
|
|
834
|
+
export type ObjectBuilder = (obj: DesignObject, game: Game) => Sprite | BackgroundLayer;
|
|
742
835
|
/**
|
|
743
836
|
* Snapshot of an active pointer tracked by the engine.
|
|
744
837
|
*
|
|
@@ -1214,7 +1307,7 @@ export declare class Game {
|
|
|
1214
1307
|
* Asset registration callback invoked once before the first scene is created.
|
|
1215
1308
|
*
|
|
1216
1309
|
* 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
|
|
1310
|
+
* with {@link Game.loadAssets} or {@link Game.loadImage}. The callback is synchronous: it should only
|
|
1218
1311
|
* queue assets, not await network work.
|
|
1219
1312
|
*
|
|
1220
1313
|
* After `onPreload` returns, MinimoJS loads the queued images, together with
|
|
@@ -1448,6 +1541,35 @@ export declare class Game {
|
|
|
1448
1541
|
* ```
|
|
1449
1542
|
*/
|
|
1450
1543
|
loadImage(key: string, src: string): void;
|
|
1544
|
+
/**
|
|
1545
|
+
* Queues every asset an asset file declares, as one {@link Game.loadImage}
|
|
1546
|
+
* call per `image` entry would.
|
|
1547
|
+
*
|
|
1548
|
+
* Pass the content of a `*.assets.json` file, obtained by JSON import. The
|
|
1549
|
+
* file is checked with {@link parseAssetFile}, and an invalid one throws an
|
|
1550
|
+
* error naming what is wrong. Like `loadImage`, `loadAssets` may only be
|
|
1551
|
+
* called from within {@link Game.onPreload} or a scene's
|
|
1552
|
+
* {@link IScene.onPreload}.
|
|
1553
|
+
*
|
|
1554
|
+
* - Keys are unique in the whole game. A key that another asset file
|
|
1555
|
+
* declares, or that {@link Game.loadImage} or {@link Game.createTexture}
|
|
1556
|
+
* registered, throws an error naming it, and so does a later
|
|
1557
|
+
* `loadImage` or `createTexture` of a key an asset file declares.
|
|
1558
|
+
* - Loading the same file again, as a scene's `onPreload()` does each time
|
|
1559
|
+
* the scene is entered, queues only its images that are not loaded.
|
|
1560
|
+
* - An entry of a type this engine does not support is skipped, with a
|
|
1561
|
+
* console warning naming it, so the game still loads every asset it can.
|
|
1562
|
+
*
|
|
1563
|
+
* @param assets - The content of a `*.assets.json` file. See {@link AssetFile}.
|
|
1564
|
+
*
|
|
1565
|
+
* @example
|
|
1566
|
+
* ```ts
|
|
1567
|
+
* import common from "./common.assets.json" with { type: "json" };
|
|
1568
|
+
*
|
|
1569
|
+
* game.onPreload = () => game.loadAssets(common);
|
|
1570
|
+
* ```
|
|
1571
|
+
*/
|
|
1572
|
+
loadAssets(assets: unknown): void;
|
|
1451
1573
|
/**
|
|
1452
1574
|
* Creates a dynamic texture immediately and registers it under a stable key.
|
|
1453
1575
|
*
|
|
@@ -1484,6 +1606,110 @@ export declare class Game {
|
|
|
1484
1606
|
* Returns `true` if a texture key is available.
|
|
1485
1607
|
*/
|
|
1486
1608
|
hasImage(key: string): boolean;
|
|
1609
|
+
/**
|
|
1610
|
+
* Creates one runtime object from object data, such as a template found in
|
|
1611
|
+
* a {@link Design}, and adds it to the game.
|
|
1612
|
+
*
|
|
1613
|
+
* The object is built through the same public constructors and
|
|
1614
|
+
* {@link Game.add} or {@link Game.addBackground} that code uses, so it
|
|
1615
|
+
* behaves as the equivalent code. Its type picks its class: `image` builds
|
|
1616
|
+
* an {@link ImageSprite}, `emoji` an {@link EmojiSprite}, `text` a
|
|
1617
|
+
* {@link TextSprite}, and `background` a {@link BackgroundLayer}, unless a
|
|
1618
|
+
* builder registered with {@link Game.registerBuilderByTag} matches one of
|
|
1619
|
+
* its tags. Then every property the data holds is applied, including
|
|
1620
|
+
* `name`, `tags`, and a copy of `data`.
|
|
1621
|
+
*
|
|
1622
|
+
* Each call creates a new object, so one template can be created any number
|
|
1623
|
+
* of times. Set its position afterwards to place it; the object is never
|
|
1624
|
+
* drawn at the template's position.
|
|
1625
|
+
*
|
|
1626
|
+
* Throws an error naming what is wrong when the data is not a valid design
|
|
1627
|
+
* object, or naming the key when it uses an image key that is not loaded.
|
|
1628
|
+
*
|
|
1629
|
+
* @param data - One design object, such as `design.findByName("player")`. Not a `Design`.
|
|
1630
|
+
* @param options - `add: false` creates the object without adding it to the game. Default: added.
|
|
1631
|
+
* @returns The created object. In TypeScript, name its type: `game.createObject<ImageSprite>(data)`.
|
|
1632
|
+
*
|
|
1633
|
+
* @example
|
|
1634
|
+
* ```ts
|
|
1635
|
+
* import charactersJson from "./characters.design.json" with { type: "json" };
|
|
1636
|
+
* const characters = new Design(charactersJson);
|
|
1637
|
+
*
|
|
1638
|
+
* const bat = game.createObject(characters.findByName("bat"));
|
|
1639
|
+
* bat.x = spawn.x;
|
|
1640
|
+
* bat.y = spawn.y;
|
|
1641
|
+
* ```
|
|
1642
|
+
*/
|
|
1643
|
+
createObject<T extends Sprite | BackgroundLayer = Sprite | BackgroundLayer>(data: DesignObject, options?: {
|
|
1644
|
+
add?: boolean;
|
|
1645
|
+
}): T;
|
|
1646
|
+
/**
|
|
1647
|
+
* Creates a runtime object from each object data, in order, and adds them
|
|
1648
|
+
* to the game, as {@link Game.createObject} does for one. Objects in the
|
|
1649
|
+
* same layer draw in the order of the data.
|
|
1650
|
+
*
|
|
1651
|
+
* Every object is checked before any is created, so invalid data or an
|
|
1652
|
+
* image key that is not loaded creates nothing.
|
|
1653
|
+
*
|
|
1654
|
+
* @param objects - Object data, such as `design.objects` or `design.findAllByTag("enemy")`. Not a `Design`.
|
|
1655
|
+
* @param options - `add: false` creates the objects without adding them to the game. Default: added.
|
|
1656
|
+
* @returns An {@link ObjectList} of the created objects, to find them by name or tag.
|
|
1657
|
+
*
|
|
1658
|
+
* @example
|
|
1659
|
+
* ```ts
|
|
1660
|
+
* import menuJson from "./menu.design.json" with { type: "json" };
|
|
1661
|
+
* const menu = new Design(menuJson);
|
|
1662
|
+
*
|
|
1663
|
+
* class MenuScene {
|
|
1664
|
+
* onCreate() {
|
|
1665
|
+
* const ui = game.createObjectList(menu.objects);
|
|
1666
|
+
* this.play = ui.findByName("play");
|
|
1667
|
+
* }
|
|
1668
|
+
* onUpdate() {
|
|
1669
|
+
* if (game.isPointerPressedOverSprite(this.play)) start(this.play.data.action);
|
|
1670
|
+
* }
|
|
1671
|
+
* }
|
|
1672
|
+
* ```
|
|
1673
|
+
*/
|
|
1674
|
+
createObjectList(objects: readonly DesignObject[], options?: {
|
|
1675
|
+
add?: boolean;
|
|
1676
|
+
}): ObjectList;
|
|
1677
|
+
/**
|
|
1678
|
+
* Makes {@link Game.createObject} and {@link Game.createObjectList} build
|
|
1679
|
+
* the design objects tagged `tag` with `builder`, such as an `Enemy` class
|
|
1680
|
+
* of the game's own.
|
|
1681
|
+
*
|
|
1682
|
+
* - The builder returns an instance of the object's type class or a
|
|
1683
|
+
* subclass of it: {@link ImageSprite} for `image`, {@link EmojiSprite} for
|
|
1684
|
+
* `emoji`, {@link TextSprite} for `text`, and {@link BackgroundLayer} for
|
|
1685
|
+
* `background`. Anything else throws an error naming the object.
|
|
1686
|
+
* - The engine then applies every property the data holds, so the file wins
|
|
1687
|
+
* over the constructor. Properties the data omits keep what the builder set.
|
|
1688
|
+
* An `emoji` object's `size` cannot be changed after construction, so a
|
|
1689
|
+
* builder must pass it on, or the creation throws.
|
|
1690
|
+
* - An object matching the builders of two of its tags throws an error
|
|
1691
|
+
* naming the object and both tags.
|
|
1692
|
+
* - Builders are scene state: {@link Game.reset} clears them, as it clears
|
|
1693
|
+
* timers, so each scene registers its own in `onCreate()`.
|
|
1694
|
+
*
|
|
1695
|
+
* @param tag - The tag whose objects the builder builds.
|
|
1696
|
+
* @param builder - Receives the object's data and the game, and returns the object, not yet added.
|
|
1697
|
+
*
|
|
1698
|
+
* @example
|
|
1699
|
+
* ```ts
|
|
1700
|
+
* class Enemy extends ImageSprite {
|
|
1701
|
+
* update(dt) { this.x += this.data.speed * dt; }
|
|
1702
|
+
* }
|
|
1703
|
+
*
|
|
1704
|
+
* class Level1Scene {
|
|
1705
|
+
* onCreate() {
|
|
1706
|
+
* game.registerBuilderByTag("enemy", (obj, game) => new Enemy(game, obj.imageKey));
|
|
1707
|
+
* this.enemies = game.createObjectList(level1.findAllByTag("enemy")).objects;
|
|
1708
|
+
* }
|
|
1709
|
+
* }
|
|
1710
|
+
* ```
|
|
1711
|
+
*/
|
|
1712
|
+
registerBuilderByTag(tag: string, builder: ObjectBuilder): void;
|
|
1487
1713
|
/**
|
|
1488
1714
|
* Tests whether two sprites overlap using **Axis-Aligned Bounding Box (AABB)**
|
|
1489
1715
|
* collision detection.
|