@combos-fun/engine 0.1.18 → 0.1.20

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/agent-skill.md CHANGED
@@ -14,5 +14,5 @@ ECS microkernel. New plugins: `plugin-authoring.md`.
14
14
  - Scene: `game.scene`.
15
15
  - Component `game` is a getter (`gameObject?.scene?.game`), after the GameObject is in the scene. System `game` is set in `addSystem`.
16
16
  - `componentObserver.clear()` events may omit `gameObject`.
17
- - `resource.addResource`. Each `src` slot is a url string or `{ type, url }`. `preload` only removes first-use delay.
17
+ - `resource.addResource`. Each `src` slot is a url string or `{ type, url }`. `preload` posts `combos-game:resource-progress` per item and `combos-game:resources-ready` once per batch (`failed` empty means success; `total: 0` means nothing was marked `preload`). Lazy `getResource` does not post these.
18
18
  - Inspector fields: `@Field` from `@combos-fun/inspector-decorator`. Engine `IDEProp` is legacy.
@@ -891,7 +891,7 @@ class Scene extends GameObject {
891
891
  }
892
892
 
893
893
  /** Generated at build from package.json */
894
- const version = "0.1.18";
894
+ const version = "0.1.20";
895
895
 
896
896
  /**
897
897
  * Sent to `window.parent` after each `System.init` completes during `Game.addSystem`
@@ -901,7 +901,8 @@ const COMBOS_GAME_PLUGIN_INIT_SUCCESS = "combos-game:plugin-init-success";
901
901
  /**
902
902
  * iframe → parent: game bootstrap finished (all systems `init`/`awake`, optional
903
903
  * scene load and `start`). Sent once, even when `autoStart` is `false`, so the host
904
- * knows assets + scene graph are ready and it may pause / hold before play.
904
+ * knows the scene graph is ready and it may pause / hold before play.
905
+ * Asset preload is a separate batch: {@link COMBOS_GAME_RESOURCES_READY}.
905
906
  */
906
907
  const COMBOS_GAME_READY = "combos-game:ready";
907
908
  /**
@@ -913,6 +914,23 @@ const COMBOS_GAME_SET_PLAYING = "combos-game:set-playing";
913
914
  * iframe → parent: current run state after a `start` / `pause` / `resume`.
914
915
  */
915
916
  const COMBOS_GAME_STATE_CHANGED = "combos-game:state-changed";
917
+ /**
918
+ * iframe → parent: one preload-batch resource settled (success or failure).
919
+ * Lazy `getResource` loads do not emit this.
920
+ */
921
+ const COMBOS_GAME_RESOURCE_PROGRESS = "combos-game:resource-progress";
922
+ /**
923
+ * iframe → parent: the current `preload()` batch reached `loaded === total`.
924
+ * Sent once per batch. `failed.length === 0` means every item succeeded.
925
+ * `total: 0` means that call had no `preload: true` resources.
926
+ */
927
+ const COMBOS_GAME_RESOURCES_READY = "combos-game:resources-ready";
928
+ /** Shared with `Game.pluginInitNotifyTargetOrigin` so preload can post before or after `new Game`. */
929
+ let parentNotifyTargetOrigin = "*";
930
+ /** Called from `Game` so later preload batches use the same `targetOrigin` as lifecycle posts. */
931
+ function setParentNotifyTargetOrigin(targetOrigin) {
932
+ parentNotifyTargetOrigin = targetOrigin || "*";
933
+ }
916
934
  function postToParent(payload, targetOrigin) {
917
935
  if (typeof window === "undefined")
918
936
  return;
@@ -949,6 +967,28 @@ function postParentGameReady(error, targetOrigin = "*") {
949
967
  };
950
968
  postToParent(payload, targetOrigin);
951
969
  }
970
+ /** Notifies the embedding page that one preload-batch resource settled. */
971
+ function postParentResourceProgress(input, targetOrigin = parentNotifyTargetOrigin) {
972
+ const payload = {
973
+ type: COMBOS_GAME_RESOURCE_PROGRESS,
974
+ name: input.name,
975
+ loaded: input.loaded,
976
+ total: input.total,
977
+ progress: input.progress,
978
+ success: input.success,
979
+ ...(input.success ? {} : { error: input.error ?? "load failed" }),
980
+ };
981
+ postToParent(payload, targetOrigin);
982
+ }
983
+ /** Notifies the embedding page that the current preload batch finished. */
984
+ function postParentResourcesReady(input, targetOrigin = parentNotifyTargetOrigin) {
985
+ const payload = {
986
+ type: COMBOS_GAME_RESOURCES_READY,
987
+ total: input.total,
988
+ failed: input.failed,
989
+ };
990
+ postToParent(payload, targetOrigin);
991
+ }
952
992
  /** Notifies the embedding page of the current play/pause state. */
953
993
  function postParentGameState(state, targetOrigin = "*") {
954
994
  const payload = {
@@ -1157,7 +1197,7 @@ class Game extends EventEmitter__default.default {
1157
1197
  this.systems = [];
1158
1198
  /**
1159
1199
  * Passed to `postMessage` when reporting lifecycle events to `window.parent`
1160
- * (`plugin-init-success`, `ready`, `state-changed`).
1200
+ * (`plugin-init-success`, `ready`, `state-changed`, `resource-progress`, `resources-ready`).
1161
1201
  */
1162
1202
  this.pluginInitNotifyTargetOrigin = "*";
1163
1203
  /** Inbound `postMessage` origins allowed to send host commands. */
@@ -1172,6 +1212,7 @@ class Game extends EventEmitter__default.default {
1172
1212
  this.setPlaying(playing);
1173
1213
  };
1174
1214
  this.pluginInitNotifyTargetOrigin = pluginInitNotifyTargetOrigin;
1215
+ setParentNotifyTargetOrigin(pluginInitNotifyTargetOrigin);
1175
1216
  this.allowedMessageOrigins = mergeAllowedMessageOrigins(allowedMessageOrigins);
1176
1217
  this.ticker = new Ticker({ autoStart: false, frameRate });
1177
1218
  this.initTicker();
@@ -1534,10 +1575,12 @@ class Progress extends EventEmitter__default.default {
1534
1575
  this.progress = 0;
1535
1576
  this.resourceTotal = 0;
1536
1577
  this.resourceLoadedCount = 0;
1578
+ this.failed = [];
1537
1579
  this.resource = resource;
1538
1580
  this.resourceTotal = resourceTotal;
1539
1581
  if (resourceTotal === 0) {
1540
1582
  this.resource.emit(exports.LOAD_EVENT.COMPLETE, this);
1583
+ this.notifyResourcesReady();
1541
1584
  }
1542
1585
  }
1543
1586
  onStart() {
@@ -1550,13 +1593,30 @@ class Progress extends EventEmitter__default.default {
1550
1593
  this.resource.emit(exports.LOAD_EVENT.LOADED, this, param);
1551
1594
  }
1552
1595
  else {
1596
+ const error = param.errMsg || 'load failed';
1597
+ this.failed.push({ name: param.name, error });
1553
1598
  this.resource.emit(exports.LOAD_EVENT.ERROR, this, param);
1554
1599
  }
1555
1600
  this.resource.emit(exports.LOAD_EVENT.PROGRESS, this, param);
1601
+ postParentResourceProgress({
1602
+ name: param.name,
1603
+ loaded: this.resourceLoadedCount,
1604
+ total: this.resourceTotal,
1605
+ progress: this.progress,
1606
+ success: param.success,
1607
+ ...(param.success ? {} : { error: param.errMsg || 'load failed' }),
1608
+ });
1556
1609
  if (this.resourceLoadedCount === this.resourceTotal) {
1557
1610
  this.resource.emit(exports.LOAD_EVENT.COMPLETE, this);
1611
+ this.notifyResourcesReady();
1558
1612
  }
1559
1613
  }
1614
+ notifyResourcesReady() {
1615
+ postParentResourcesReady({
1616
+ total: this.resourceTotal,
1617
+ failed: this.failed.slice(),
1618
+ });
1619
+ }
1560
1620
  }
1561
1621
 
1562
1622
  const SLOT_DEFAULT_TYPE = {
@@ -1683,9 +1743,30 @@ function missingUrlError(resourceName, slot) {
1683
1743
  `Use { type, url } (e.g. { type: "webp", url: "https://..." }). ` +
1684
1744
  `A flat path string is also accepted and will be normalized.`);
1685
1745
  }
1746
+ function isSpriteSheetResource(resourceType) {
1747
+ return resourceType === 'SPRITE' || resourceType === 'SPRITE_ANIMATION';
1748
+ }
1749
+ /**
1750
+ * Agent atlases name the sheet `atlas_json` and may also attach `animation_json`.
1751
+ * Playback only reads `data.json`. Prefer an existing `json` slot, and drop the
1752
+ * animator so a failed extra request cannot fail the whole resource.
1753
+ */
1754
+ function foldSpriteSheetSlots(src) {
1755
+ const next = { ...src };
1756
+ delete next.animation_json;
1757
+ if (next.json != null) {
1758
+ delete next.atlas_json;
1759
+ }
1760
+ else if (next.atlas_json != null) {
1761
+ next.json = next.atlas_json;
1762
+ delete next.atlas_json;
1763
+ }
1764
+ return next;
1765
+ }
1686
1766
  /**
1687
1767
  * Accept the documented `{ type, url }` slots, plus the common guesses
1688
1768
  * `src: "file.png"` and `src: { image: "a.webp", json: "a.json" }`.
1769
+ * `SPRITE` / `SPRITE_ANIMATION` also accept `atlas_json` in place of `json`.
1689
1770
  */
1690
1771
  function normalizeResourceSrc(src, resourceType, resourceName = '(unnamed)') {
1691
1772
  if (src == null) {
@@ -1698,8 +1779,11 @@ function normalizeResourceSrc(src, resourceType, resourceName = '(unnamed)') {
1698
1779
  if (typeof src !== 'object' || Array.isArray(src)) {
1699
1780
  throw new Error(`Resource "${resourceName}" src must be a url string or a slot object.`);
1700
1781
  }
1782
+ const slots = isSpriteSheetResource(resourceType)
1783
+ ? foldSpriteSheetSlots(src)
1784
+ : src;
1701
1785
  const out = {};
1702
- for (const [slot, value] of Object.entries(src)) {
1786
+ for (const [slot, value] of Object.entries(slots)) {
1703
1787
  if (value == null)
1704
1788
  continue;
1705
1789
  out[slot] = wrapSlot(slot, value, resourceName);
@@ -2461,6 +2545,8 @@ console.log(`@combos-fun/engine version: ${version}`);
2461
2545
  exports.AdaptivePixelRatio = AdaptivePixelRatio;
2462
2546
  exports.COMBOS_GAME_PLUGIN_INIT_SUCCESS = COMBOS_GAME_PLUGIN_INIT_SUCCESS;
2463
2547
  exports.COMBOS_GAME_READY = COMBOS_GAME_READY;
2548
+ exports.COMBOS_GAME_RESOURCES_READY = COMBOS_GAME_RESOURCES_READY;
2549
+ exports.COMBOS_GAME_RESOURCE_PROGRESS = COMBOS_GAME_RESOURCE_PROGRESS;
2464
2550
  exports.COMBOS_GAME_SET_PLAYING = COMBOS_GAME_SET_PLAYING;
2465
2551
  exports.COMBOS_GAME_STATE_CHANGED = COMBOS_GAME_STATE_CHANGED;
2466
2552
  exports.Component = Component;
@@ -2485,6 +2571,8 @@ exports.parseSetPlayingMessage = parseSetPlayingMessage;
2485
2571
  exports.postParentGameReady = postParentGameReady;
2486
2572
  exports.postParentGameState = postParentGameState;
2487
2573
  exports.postParentPluginInitSuccess = postParentPluginInitSuccess;
2574
+ exports.postParentResourceProgress = postParentResourceProgress;
2575
+ exports.postParentResourcesReady = postParentResourcesReady;
2488
2576
  exports.readSpriteClipSheet = readSpriteClipSheet;
2489
2577
  exports.resolveRenderQuality = resolveRenderQuality;
2490
2578
  exports.resolveSpriteClip = resolveSpriteClip;