@yangyongtao/gaea 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/GaeaMethods.js +41 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yangyongtao/gaea",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Gaea 3D 可视化组件库,包含天气控制、沿线飞行、台风标注、模型加载等功能",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -53,12 +53,36 @@ function rgbToGaeaColor(rgb) {
53
53
  function checkGaea() {
54
54
  const G = window.Gaea;
55
55
  if (!G || !G.World || !G.World.Instance) {
56
- console.warn('Gaea 尚未加载完毕,请确保在 Gaea 初始化之后再调用此方法');
56
+ console.warn('Gaea 尚未加载完毕,请先调用 await GaeaApp.waitForReady()');
57
57
  return null;
58
58
  }
59
59
  return G;
60
60
  }
61
61
 
62
+ /**
63
+ * 等待 Gaea 引擎初始化完毕(轮询 World.Instance)
64
+ * @param {number} timeout - 超时毫秒数,默认 30000
65
+ * @returns {Promise<boolean>}
66
+ */
67
+ async function waitForGaea(timeout = 30000) {
68
+ if (window.Gaea && window.Gaea.World && window.Gaea.World.Instance) {
69
+ return true;
70
+ }
71
+ const start = Date.now();
72
+ return new Promise((resolve) => {
73
+ const timer = setInterval(() => {
74
+ if (window.Gaea && window.Gaea.World && window.Gaea.World.Instance) {
75
+ clearInterval(timer);
76
+ resolve(true);
77
+ } else if (Date.now() - start > timeout) {
78
+ clearInterval(timer);
79
+ console.error('Gaea 初始化超时');
80
+ resolve(false);
81
+ }
82
+ }, 200);
83
+ });
84
+ }
85
+
62
86
 
63
87
  // ==================== GaeaApp 类 ====================
64
88
 
@@ -93,6 +117,15 @@ export class GaeaApp {
93
117
  this._tweenGroup = new Group();
94
118
  }
95
119
 
120
+ /**
121
+ * 静态方法:等待 Gaea 引擎就绪
122
+ * @example await GaeaApp.waitForReady(); // 30s 超时
123
+ * @example await GaeaApp.waitForReady(60000); // 自定义超时
124
+ */
125
+ static waitForReady(timeout) {
126
+ return waitForGaea(timeout);
127
+ }
128
+
96
129
  // ==================== 点标注 ====================
97
130
 
98
131
  /**
@@ -351,13 +384,16 @@ export class GaeaApp {
351
384
  }
352
385
 
353
386
  /** 初始化基础环境(关闭默认雾、设置默认光照等) */
354
- initDefaultEnvironment() {
355
- const Gaea = window.Gaea;
356
- if (!Gaea || !Gaea.World || !Gaea.World.Instance) {
357
- console.warn('Gaea 尚未加载完毕,请确保在 Gaea 初始化之后调用此方法');
387
+ async initDefaultEnvironment() {
388
+ // 等待 Gaea 就绪
389
+ const ready = await waitForGaea(15000);
390
+ if (!ready) {
391
+ console.error('Gaea 引擎初始化超时');
358
392
  return;
359
393
  }
360
394
 
395
+ const Gaea = window.Gaea;
396
+
361
397
  // 关闭默认雾
362
398
  const fogEnv = Gaea.Environment.ConvertBy(
363
399
  Gaea.GaeaResourceLoader.Instance.LoadLoaclResource('res://default_env.tres')