@combos-fun/engine 0.0.3 → 0.0.5

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.
@@ -954,21 +954,46 @@ class Scene extends GameObject {
954
954
  }
955
955
  }
956
956
 
957
+ /**
958
+ * Sent to `window.parent` after each `System.init` completes during `Game.addSystem`
959
+ * (when running inside an iframe and parent differs from self).
960
+ */
961
+ const COMBOS_GAME_PLUGIN_INIT_SUCCESS = "combos-game:plugin-init-success";
962
+ /**
963
+ * Notifies the embedding page that a system (plugin) finished async/sync `init`.
964
+ */
965
+ function postParentPluginInitSuccess(systemName, targetOrigin = "*") {
966
+ if (typeof window === "undefined")
967
+ return;
968
+ if (!window.parent || window.parent === window)
969
+ return;
970
+ const payload = {
971
+ type: COMBOS_GAME_PLUGIN_INIT_SUCCESS,
972
+ systemName,
973
+ };
974
+ try {
975
+ window.parent.postMessage(payload, targetOrigin);
976
+ }
977
+ catch {
978
+ /* ignore cross-origin or detached frame */
979
+ }
980
+ }
981
+
957
982
  function systemClassName(ctor) {
958
- if ('systemName' in ctor) {
983
+ if ("systemName" in ctor) {
959
984
  const sn = ctor.systemName;
960
- if (typeof sn === 'string')
985
+ if (typeof sn === "string")
961
986
  return sn;
962
987
  }
963
- return 'UnknownSystem';
988
+ return "UnknownSystem";
964
989
  }
965
990
  function componentClassName(ctor) {
966
- if ('componentName' in ctor) {
991
+ if ("componentName" in ctor) {
967
992
  const cn = ctor.componentName;
968
- if (typeof cn === 'string')
993
+ if (typeof cn === "string")
969
994
  return cn;
970
995
  }
971
- return 'UnknownComponent';
996
+ return "UnknownComponent";
972
997
  }
973
998
  exports.LOAD_SCENE_MODE = void 0;
974
999
  (function (LOAD_SCENE_MODE) {
@@ -993,7 +1018,7 @@ const triggerStart = (obj) => {
993
1018
  }
994
1019
  }
995
1020
  };
996
- const getAllGameObjects = game => {
1021
+ const getAllGameObjects = (game) => {
997
1022
  const mainSceneGameObjects = game?.scene?.gameObjects || [];
998
1023
  const gameObjectsArray = game?.multiScenes.map(({ gameObjects }) => gameObjects);
999
1024
  let otherSceneGameObjects = [];
@@ -1025,7 +1050,7 @@ const gameObjectLoop = (e, gameObjects = []) => {
1025
1050
  }
1026
1051
  }
1027
1052
  };
1028
- const gameObjectResume = gameObjects => {
1053
+ const gameObjectResume = (gameObjects) => {
1029
1054
  for (const gameObject of gameObjects) {
1030
1055
  for (const component of gameObject.components) {
1031
1056
  try {
@@ -1037,7 +1062,7 @@ const gameObjectResume = gameObjects => {
1037
1062
  }
1038
1063
  }
1039
1064
  };
1040
- const gameObjectPause = gameObjects => {
1065
+ const gameObjectPause = (gameObjects) => {
1041
1066
  for (const gameObject of gameObjects) {
1042
1067
  for (const component of gameObject.components) {
1043
1068
  try {
@@ -1050,7 +1075,7 @@ const gameObjectPause = gameObjects => {
1050
1075
  }
1051
1076
  };
1052
1077
  class Game extends EventEmitter__default.default {
1053
- constructor({ systems, frameRate = 60, autoStart = true, needScene = true, onSystemsBootstrapComplete, } = {}) {
1078
+ constructor({ systems, frameRate = 60, autoStart = true, needScene = true, onSystemsBootstrapComplete, pluginInitNotifyTargetOrigin = "*", } = {}) {
1054
1079
  super();
1055
1080
  /**
1056
1081
  * State of game
@@ -1061,6 +1086,11 @@ class Game extends EventEmitter__default.default {
1061
1086
  this.multiScenes = [];
1062
1087
  /** Systems alled to this game */
1063
1088
  this.systems = [];
1089
+ /**
1090
+ * Passed to `postMessage` when reporting `COMBOS_GAME_PLUGIN_INIT_SUCCESS` to `window.parent`.
1091
+ */
1092
+ this.pluginInitNotifyTargetOrigin = "*";
1093
+ this.pluginInitNotifyTargetOrigin = pluginInitNotifyTargetOrigin;
1064
1094
  this.ticker = new Ticker({ autoStart: false, frameRate });
1065
1095
  this.initTicker();
1066
1096
  if (systems && systems.length) {
@@ -1079,7 +1109,7 @@ class Game extends EventEmitter__default.default {
1079
1109
  await this.addSystem(system);
1080
1110
  }
1081
1111
  if (needScene) {
1082
- this.loadScene(new Scene('scene'));
1112
+ this.loadScene(new Scene("scene"));
1083
1113
  }
1084
1114
  if (autoStart) {
1085
1115
  this.start();
@@ -1087,7 +1117,7 @@ class Game extends EventEmitter__default.default {
1087
1117
  }
1088
1118
  catch (e) {
1089
1119
  error = e;
1090
- console.error('Game bootstrap failed', e);
1120
+ console.error("Game bootstrap failed", e);
1091
1121
  }
1092
1122
  finally {
1093
1123
  onComplete?.(this, error);
@@ -1120,10 +1150,10 @@ class Game extends EventEmitter__default.default {
1120
1150
  system = S;
1121
1151
  }
1122
1152
  else {
1123
- console.warn('can only add System');
1153
+ console.warn("can only add System");
1124
1154
  return;
1125
1155
  }
1126
- const hasTheSystem = this.systems.find(item => {
1156
+ const hasTheSystem = this.systems.find((item) => {
1127
1157
  return item.constructor === system.constructor;
1128
1158
  });
1129
1159
  if (hasTheSystem) {
@@ -1134,6 +1164,7 @@ class Game extends EventEmitter__default.default {
1134
1164
  if (system.init) {
1135
1165
  await Promise.resolve(system.init(system.__systemDefaultParams));
1136
1166
  }
1167
+ postParentPluginInitSuccess(systemClassName(system.constructor), this.pluginInitNotifyTargetOrigin);
1137
1168
  setSystemObserver(system, system.constructor);
1138
1169
  initObserver(system.constructor);
1139
1170
  try {
@@ -1153,14 +1184,14 @@ class Game extends EventEmitter__default.default {
1153
1184
  if (!system)
1154
1185
  return;
1155
1186
  let index = -1;
1156
- if (typeof system === 'string') {
1157
- index = this.systems.findIndex(s => s.name === system);
1187
+ if (typeof system === "string") {
1188
+ index = this.systems.findIndex((s) => s.name === system);
1158
1189
  }
1159
1190
  else if (system instanceof Function) {
1160
- index = this.systems.findIndex(s => s.constructor === system);
1191
+ index = this.systems.findIndex((s) => s.constructor === system);
1161
1192
  }
1162
1193
  else if (system instanceof System) {
1163
- index = this.systems.findIndex(s => s === system);
1194
+ index = this.systems.findIndex((s) => s === system);
1164
1195
  }
1165
1196
  if (index > -1) {
1166
1197
  this.systems[index].destroy && this.systems[index].destroy();
@@ -1173,8 +1204,8 @@ class Game extends EventEmitter__default.default {
1173
1204
  * @returns system instance
1174
1205
  */
1175
1206
  getSystem(S) {
1176
- return this.systems.find(system => {
1177
- if (typeof S === 'string') {
1207
+ return this.systems.find((system) => {
1208
+ if (typeof S === "string") {
1178
1209
  return system.name === S;
1179
1210
  }
1180
1211
  else {
@@ -1216,7 +1247,7 @@ class Game extends EventEmitter__default.default {
1216
1247
  * 4. call lastUpdate method on all system
1217
1248
  */
1218
1249
  initTicker() {
1219
- this.ticker.add(e => {
1250
+ this.ticker.add((e) => {
1220
1251
  this.scene && gameObjectLoop(e, this.gameObjects);
1221
1252
  for (const system of this.systems) {
1222
1253
  try {
@@ -1280,7 +1311,7 @@ class Game extends EventEmitter__default.default {
1280
1311
  this.canvas = null;
1281
1312
  this.multiScenes = null;
1282
1313
  }
1283
- loadScene({ scene, mode = exports.LOAD_SCENE_MODE.SINGLE, params = {} }) {
1314
+ loadScene({ scene, mode = exports.LOAD_SCENE_MODE.SINGLE, params = {}, }) {
1284
1315
  if (!scene) {
1285
1316
  return;
1286
1317
  }
@@ -1292,7 +1323,7 @@ class Game extends EventEmitter__default.default {
1292
1323
  this.multiScenes.push(scene);
1293
1324
  break;
1294
1325
  }
1295
- this.emit('sceneChanged', { scene, mode, params });
1326
+ this.emit("sceneChanged", { scene, mode, params });
1296
1327
  }
1297
1328
  }
1298
1329