@eva/spine-base 2.0.1-beta.9 → 2.0.1

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.
@@ -1,7 +1,7 @@
1
1
  import { Component, resource, OBSERVER_TYPE, decorators } from '@eva/eva.js';
2
2
  import { Renderer, RendererSystem } from '@eva/plugin-renderer';
3
+ import { Container } from 'pixi.js';
3
4
  import { type } from '@eva/inspector-decorator';
4
- import { Assets } from 'pixi.js';
5
5
 
6
6
  /*! *****************************************************************************
7
7
  Copyright (c) Microsoft Corporation. All rights reserved.
@@ -34,15 +34,84 @@ function __awaiter(thisArg, _arguments, P, generator) {
34
34
  });
35
35
  }
36
36
 
37
+ /**
38
+ * Spine 骨骼动画组件
39
+ *
40
+ * Spine 组件用于播放 Esoteric Software 的 Spine 骨骼动画。
41
+ * 支持骨骼动画播放控制、动画混合、附件替换等高级功能,
42
+ * 适用于角色动画、复杂特效等需要骨骼动画的场景。
43
+ *
44
+ * 主要功能:
45
+ * - 骨骼动画播放和控制
46
+ * - 动画轨道管理(多动画并行)
47
+ * - 动画混合过渡
48
+ * - 骨骼和附件访问
49
+ * - 支持 Spine 3.6 和 3.8 版本
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * // 创建 Spine 动画
54
+ * const character = new GameObject('character');
55
+ * const spine = new Spine({
56
+ * resource: 'heroSpine', // Spine 资源
57
+ * animationName: 'idle', // 默认动画
58
+ * autoPlay: true, // 自动播放
59
+ * scale: 0.5 // 缩放比例
60
+ * });
61
+ * character.addComponent(spine);
62
+ *
63
+ * // 播放动画
64
+ * spine.play('walk', true); // 循环播放 walk 动画
65
+ *
66
+ * // 停止动画
67
+ * spine.stop();
68
+ *
69
+ * // 动画混合
70
+ * spine.setMix('idle', 'walk', 0.3); // 设置过渡时间
71
+ * spine.play('walk');
72
+ *
73
+ * // 添加动画队列
74
+ * spine.play('attack', false); // 播放攻击动画
75
+ * spine.addAnimation('idle', 0, true); // 攻击完成后回到 idle
76
+ *
77
+ * // 替换附件(换装)
78
+ * spine.setAttachment('weapon', 'sword'); // 将武器槽替换为剑
79
+ *
80
+ * // 访问骨骼
81
+ * const headBone = spine.getBone('head');
82
+ * if (headBone) {
83
+ * headBone.rotation = 15; // 旋转头部
84
+ * }
85
+ *
86
+ * // 多轨道动画
87
+ * spine.play('walk', true, 0); // 轨道0:身体动画
88
+ * spine.play('shoot', false, 1); // 轨道1:上半身动画
89
+ * ```
90
+ */
37
91
  class Spine extends Component {
38
92
  constructor() {
39
93
  super(...arguments);
94
+ /** Spine 资源名称 */
40
95
  this.resource = '';
96
+ /** 动画缩放比例 */
41
97
  this.scale = 1;
98
+ /** 当前播放的动画名称 */
42
99
  this.animationName = '';
100
+ /** 是否自动播放动画 */
43
101
  this.autoPlay = true;
102
+ /** 是否保留资源(销毁时不释放) */
103
+ this.keepResource = false;
104
+ /** 挂载到插槽的 GameObject 映射(GameObject -> { slot, wrapper }) */
105
+ this._slotGameObjects = new Map();
106
+ /** 等待容器就绪的 slot 挂载请求 */
107
+ this._pendingSlotObjects = [];
108
+ /** 等待执行的动画操作队列 */
44
109
  this.waitExecuteInfos = [];
45
110
  }
111
+ /**
112
+ * 设置骨架实例
113
+ * 当骨架加载完成后自动执行等待队列中的动画操作
114
+ */
46
115
  set armature(val) {
47
116
  this._armature = val;
48
117
  if (!val)
@@ -61,17 +130,36 @@ class Spine extends Component {
61
130
  }
62
131
  this.waitExecuteInfos = [];
63
132
  }
133
+ /** 获取骨架实例 */
64
134
  get armature() {
65
135
  return this._armature;
66
136
  }
137
+ /**
138
+ * 初始化组件
139
+ * @param obj - 初始化参数
140
+ * @param obj.resource - Spine 资源名称
141
+ * @param obj.animationName - 默认动画名称
142
+ * @param obj.scale - 缩放比例
143
+ * @param obj.autoPlay - 是否自动播放
144
+ */
67
145
  init(obj) {
68
146
  if (!obj)
69
147
  return;
70
148
  Object.assign(this, obj);
71
149
  }
150
+ /** 组件销毁时调用 */
72
151
  onDestroy() {
73
152
  this.destroied = true;
74
153
  }
154
+ /**
155
+ * 播放指定动画
156
+ *
157
+ * 如果骨架尚未加载完成,动画操作将被加入等待队列。
158
+ *
159
+ * @param name - 动画名称,不指定则使用 animationName 属性
160
+ * @param loopAnimation - 是否循环播放,默认跟随 autoPlay 属性
161
+ * @param track - 动画轨道编号,默认为 0
162
+ */
75
163
  play(name, loopAnimation, track) {
76
164
  try {
77
165
  const loop = loopAnimation !== null && loopAnimation !== void 0 ? loopAnimation : this.autoPlay;
@@ -81,6 +169,12 @@ class Spine extends Component {
81
169
  this.waitExecuteInfos.push({
82
170
  playType: true,
83
171
  name,
172
+ /**
173
+ * 在 v1.2.2 之前,Spine 动画的 autoPlay 为 true,动画会循环播放 https://github.com/eva-engine/eva.js/pull/164/files#diff-46e9ae36c04e7a0abedc1e14fd9d1c4e81d8386e9bb851f85971ccdba8957804L131
174
+ * 在 v1.2.2 之前,Spine 动画在每加载完( armature 设置之前)调用 play 是不生效的, 在 v1.2.2 [#164](https://github.com/eva-engine/eva.js/pull/164) 解决了这个问题
175
+ * 解决了不生效的问题以后,加载完成之前调用 play 默认循环是false,导致 autoPlay 下本来循环动画不循环了,和之前表现不一致
176
+ * 为了解决这个问题,在 autoPlay 的情况下,未加载完之前调用 play ,默认循环播放,除非设置不循环参数
177
+ */
84
178
  loop,
85
179
  track,
86
180
  });
@@ -96,6 +190,13 @@ class Spine extends Component {
96
190
  console.log(e);
97
191
  }
98
192
  }
193
+ /**
194
+ * 停止指定轨道的动画
195
+ *
196
+ * 如果骨架尚未加载完成,停止操作将被加入等待队列。
197
+ *
198
+ * @param track - 动画轨道编号,默认为 0
199
+ */
99
200
  stop(track) {
100
201
  if (!this.armature) {
101
202
  this.waitExecuteInfos.push({
@@ -109,6 +210,16 @@ class Spine extends Component {
109
210
  }
110
211
  this.armature.state.setEmptyAnimation(track, 0);
111
212
  }
213
+ /**
214
+ * 在当前动画之后添加新动画到队列
215
+ *
216
+ * 用于创建动画序列,当前动画播放完毕后自动播放下一个动画。
217
+ *
218
+ * @param name - 动画名称
219
+ * @param delay - 延迟时间(秒)
220
+ * @param loop - 是否循环播放
221
+ * @param track - 动画轨道编号,默认为 0
222
+ */
112
223
  addAnimation(name, delay, loop, track) {
113
224
  try {
114
225
  if (!this.armature) {
@@ -124,12 +235,27 @@ class Spine extends Component {
124
235
  console.log(e);
125
236
  }
126
237
  }
238
+ /**
239
+ * 设置两个动画之间的混合过渡时间
240
+ *
241
+ * 当从一个动画切换到另一个动画时,会在指定时间内进行平滑过渡。
242
+ *
243
+ * @param from - 起始动画名称
244
+ * @param to - 目标动画名称
245
+ * @param duration - 过渡时长(秒)
246
+ */
127
247
  setMix(from, to, duration) {
128
248
  if (!this.armature) ;
129
249
  else {
130
250
  this.armature.state.data.setMix(from, to, duration);
131
251
  }
132
252
  }
253
+ /**
254
+ * 获取指定轨道当前播放的动画名称
255
+ *
256
+ * @param track - 动画轨道编号,默认为 0
257
+ * @returns 动画名称,如果未找到则返回 undefined
258
+ */
133
259
  getAnim(track = 0) {
134
260
  try {
135
261
  if (!this.armature) {
@@ -142,25 +268,159 @@ class Spine extends Component {
142
268
  console.log(e);
143
269
  }
144
270
  }
271
+ /**
272
+ * 设置默认的动画混合时间
273
+ *
274
+ * 当没有为特定动画对指定混合时间时,将使用此默认值。
275
+ *
276
+ * @param duration - 默认混合时长(秒)
277
+ */
145
278
  setDefaultMix(duration) {
146
279
  if (!this.armature) ;
147
280
  else {
148
281
  this.armature.state.data.defaultMix = duration;
149
282
  }
150
283
  }
284
+ /**
285
+ * 替换指定插槽的附件
286
+ *
287
+ * 用于换装、武器切换等场景。
288
+ *
289
+ * @param slotName - 插槽名称
290
+ * @param attachmentName - 附件名称
291
+ */
151
292
  setAttachment(slotName, attachmentName) {
152
293
  if (!this.armature) {
153
294
  return;
154
295
  }
155
296
  this.armature.skeleton.setAttachment(slotName, attachmentName);
156
297
  }
298
+ /**
299
+ * 获取指定名称的骨骼
300
+ *
301
+ * 可用于直接操作骨骼的位置、旋转、缩放等属性。
302
+ *
303
+ * @param boneName - 骨骼名称
304
+ * @returns 骨骼对象,如果未找到则返回 undefined
305
+ */
157
306
  getBone(boneName) {
158
307
  if (!this.armature) {
159
308
  return;
160
309
  }
161
310
  return this.armature.skeleton.findBone(boneName);
162
311
  }
312
+ /**
313
+ * 将一个 GameObject 挂载到 Spine 的指定插槽上
314
+ *
315
+ * 挂载后 GameObject 会跟随骨骼运动。当 Spine 组件销毁时,
316
+ * 挂载的 GameObject 也会被自动销毁。
317
+ *
318
+ * @param slot - 插槽名称或索引
319
+ * @param gameObject - 要挂载的 GameObject
320
+ * @param options - 可选配置
321
+ * @param options.followAttachmentTimeline - 是否跟随插槽的附件时间线
322
+ */
323
+ addSlotObject(slot, gameObject, options) {
324
+ if (!this.armature) {
325
+ console.warn('Spine armature is not ready, cannot addSlotObject');
326
+ return;
327
+ }
328
+ if (!this._containerManager) {
329
+ console.warn('ContainerManager is not available');
330
+ return;
331
+ }
332
+ const container = this._containerManager.getContainer(gameObject.id);
333
+ if (!container) {
334
+ // 容器尚未就绪,加入 pending 队列,等待下一帧自动处理
335
+ this._pendingSlotObjects.push({ slot, gameObject, options });
336
+ return;
337
+ }
338
+ this._doAddSlotObject(slot, gameObject, container, options);
339
+ }
340
+ _doAddSlotObject(slot, gameObject, container, options) {
341
+ // 创建 wrapper 容器:Spine 骨骼矩阵作用在 wrapper 上,
342
+ // gameObject 的 container 作为子节点,其 transform 作为相对 slot 的局部偏移
343
+ const wrapper = new Container();
344
+ wrapper.addChild(container);
345
+ this.armature.addSlotObject(slot, wrapper, options);
346
+ this._slotGameObjects.set(gameObject, { slot, wrapper });
347
+ // slot object 可能不在 game.gameObjects 中,RendererSystem 不会自动同步 transform
348
+ // 手动同步 gameObject 及其子树的 transform 到 container
349
+ this._syncTransformTree(gameObject);
350
+ }
351
+ /**
352
+ * 递归同步 gameObject 及其子树的 transform 到对应的渲染容器
353
+ */
354
+ _syncTransformTree(gameObject) {
355
+ var _a;
356
+ if (!this._containerManager)
357
+ return;
358
+ this._containerManager.updateTransform({
359
+ name: gameObject.id,
360
+ transform: gameObject.transform,
361
+ });
362
+ if ((_a = gameObject.transform) === null || _a === void 0 ? void 0 : _a.children) {
363
+ for (const childTransform of gameObject.transform.children) {
364
+ if (childTransform.gameObject) {
365
+ this._syncTransformTree(childTransform.gameObject);
366
+ }
367
+ }
368
+ }
369
+ }
370
+ /**
371
+ * 处理等待容器就绪的 slot 挂载请求(由 SpineSystem 每帧调用)
372
+ */
373
+ _flushPendingSlotObjects() {
374
+ if (this._pendingSlotObjects.length === 0)
375
+ return;
376
+ if (!this.armature || !this._containerManager)
377
+ return;
378
+ const still = [];
379
+ for (const pending of this._pendingSlotObjects) {
380
+ const container = this._containerManager.getContainer(pending.gameObject.id);
381
+ if (container) {
382
+ this._doAddSlotObject(pending.slot, pending.gameObject, container, pending.options);
383
+ }
384
+ else {
385
+ still.push(pending);
386
+ }
387
+ }
388
+ this._pendingSlotObjects = still;
389
+ }
390
+ /**
391
+ * 从插槽上移除挂载的 GameObject
392
+ *
393
+ * @param gameObject - 要移除的 GameObject
394
+ */
395
+ removeSlotObject(gameObject) {
396
+ // 从 pending 队列中移除
397
+ this._pendingSlotObjects = this._pendingSlotObjects.filter(p => p.gameObject !== gameObject);
398
+ const entry = this._slotGameObjects.get(gameObject);
399
+ if (entry && this.armature) {
400
+ this.armature.removeSlotObject(entry.wrapper);
401
+ entry.wrapper.destroy({ children: false });
402
+ }
403
+ this._slotGameObjects.delete(gameObject);
404
+ }
405
+ /**
406
+ * 销毁所有挂载到插槽的 GameObject(内部使用)
407
+ */
408
+ _destroySlotGameObjects() {
409
+ for (const [gameObject, entry] of this._slotGameObjects) {
410
+ if (!gameObject.destroyed) {
411
+ // 先从 spine 插槽移除 wrapper,避免 destroy 时重复操作
412
+ if (this.armature) {
413
+ this.armature.removeSlotObject(entry.wrapper);
414
+ }
415
+ entry.wrapper.destroy({ children: false });
416
+ gameObject.destroy();
417
+ }
418
+ }
419
+ this._slotGameObjects.clear();
420
+ this._pendingSlotObjects = [];
421
+ }
163
422
  }
423
+ /** 组件名称 */
164
424
  Spine.componentName = 'Spine';
165
425
  __decorate([
166
426
  type('string')
@@ -173,7 +433,10 @@ __decorate([
173
433
  ], Spine.prototype, "animationName", void 0);
174
434
  __decorate([
175
435
  type('boolean')
176
- ], Spine.prototype, "autoPlay", void 0);
436
+ ], Spine.prototype, "autoPlay", void 0);
437
+ __decorate([
438
+ type('boolean')
439
+ ], Spine.prototype, "keepResource", void 0);
177
440
 
178
441
  let dataMap = {};
179
442
  function createSpineData(name, data, scale, pixiSpine) {
@@ -213,14 +476,6 @@ function releaseSpineData(res, _imageSrc) {
213
476
  data.ref--;
214
477
  setTimeout(() => __awaiter(this, void 0, void 0, function* () {
215
478
  if (data.ref <= 0) {
216
- yield Assets.unload([res.src.image.url, res.src.atlas.url, res.src.ske.url]);
217
- const resolver = Assets.resolver;
218
- delete resolver._assetMap[res.src.image.url];
219
- delete resolver._assetMap[res.src.atlas.url];
220
- delete resolver._assetMap[res.src.ske.url];
221
- delete resolver._resolverHash[res.src.image.url];
222
- delete resolver._resolverHash[res.src.atlas.url];
223
- delete resolver._resolverHash[res.src.ske.url];
224
479
  resource.destroy(resourceName);
225
480
  delete dataMap[resourceName];
226
481
  }
@@ -228,17 +483,41 @@ function releaseSpineData(res, _imageSrc) {
228
483
  }
229
484
 
230
485
  const MaxRetryCount = 20;
486
+ /**
487
+ * Spine 骨骼动画系统
488
+ *
489
+ * SpineSystem 负责管理所有 Spine 组件的骨架创建、动画更新和资源管理。
490
+ * 系统会监听 Spine 组件的变化,自动加载骨骼数据并创建动画实例,
491
+ * 并在每帧更新所有活跃的 Spine 动画。
492
+ *
493
+ * 主要功能:
494
+ * - 骨骼数据加载和缓存
495
+ * - 动画实例创建和销毁
496
+ * - 每帧动画状态更新
497
+ * - WebGL 上下文恢复处理
498
+ * - 资源重试机制
499
+ */
231
500
  let SpineSystem = class SpineSystem extends Renderer {
232
501
  constructor() {
233
502
  super(...arguments);
503
+ /** 骨架实例映射表(游戏对象 ID -> 骨架容器) */
234
504
  this.armatures = {};
505
+ /** Spine 组件实例映射(游戏对象 ID -> Spine 组件) */
506
+ this._spineComponents = {};
235
507
  }
508
+ /**
509
+ * 初始化系统
510
+ * @param obj - 初始化参数
511
+ * @param obj.pixiSpine - PixiJS Spine 插件实例
512
+ */
236
513
  init({ pixiSpine }) {
237
514
  this.renderSystem = this.game.getSystem(RendererSystem);
238
515
  this.renderSystem.rendererManager.register(this);
239
516
  this.pixiSpine = pixiSpine;
240
517
  this.game.canvas.addEventListener('webglcontextrestored', () => {
518
+ // 重建所有spine
241
519
  const objs = this.game.gameObjects;
520
+ // clearCache();
242
521
  let toAdd = [];
243
522
  for (let k in this.armatures) {
244
523
  const id = +k;
@@ -271,10 +550,20 @@ let SpineSystem = class SpineSystem extends Renderer {
271
550
  }, 1000);
272
551
  }, false);
273
552
  }
553
+ /**
554
+ * 每帧更新所有 Spine 动画
555
+ * @param e - 更新参数,包含帧间隔时间
556
+ */
274
557
  update(e) {
275
558
  for (let key in this.armatures) {
559
+ // TODO: 类型
560
+ // @ts-ignore
276
561
  this.armatures[key].update(e.deltaTime * 0.001);
277
562
  }
563
+ // 处理等待容器就绪的 slot 挂载请求
564
+ for (let key in this._spineComponents) {
565
+ this._spineComponents[key]._flushPendingSlotObjects();
566
+ }
278
567
  super.update();
279
568
  }
280
569
  componentChanged(changed) {
@@ -297,7 +586,7 @@ let SpineSystem = class SpineSystem extends Renderer {
297
586
  });
298
587
  }
299
588
  add(changed, count) {
300
- var _a, _b;
589
+ var _a, _b, _c;
301
590
  return __awaiter(this, void 0, void 0, function* () {
302
591
  const component = changed.component;
303
592
  clearTimeout(component.addHandler);
@@ -313,6 +602,7 @@ let SpineSystem = class SpineSystem extends Renderer {
313
602
  component.addHandler = setTimeout(() => {
314
603
  if (!component.destroied) {
315
604
  if (count === undefined) {
605
+ // 最大重试次数
316
606
  count = MaxRetryCount;
317
607
  }
318
608
  count--;
@@ -329,37 +619,48 @@ let SpineSystem = class SpineSystem extends Renderer {
329
619
  this.remove(changed);
330
620
  const container = (_b = (_a = this.renderSystem) === null || _a === void 0 ? void 0 : _a.containerManager) === null || _b === void 0 ? void 0 : _b.getContainer(changed.gameObject.id);
331
621
  if (!container) {
622
+ // console.warn('添加spine的container不存在');
332
623
  return;
333
624
  }
334
625
  component.lastResource = component.resource;
626
+ // @ts-ignore
335
627
  const armature = new this.pixiSpine.Spine({
336
628
  skeletonData: spineData,
337
629
  autoUpdate: false,
338
630
  });
339
631
  this.armatures[changed.gameObject.id] = armature;
632
+ this._spineComponents[changed.gameObject.id] = component;
340
633
  if (changed.gameObject && changed.gameObject.transform) {
341
634
  const tran = changed.gameObject.transform;
342
635
  armature.x = tran.size.width * tran.origin.x;
343
636
  armature.y = tran.size.height * tran.origin.y;
344
637
  }
345
638
  container.addChildAt(armature, 0);
639
+ /** 保证第一帧显示正常 */
346
640
  armature.update();
641
+ component._containerManager = (_c = this.renderSystem) === null || _c === void 0 ? void 0 : _c.containerManager;
347
642
  component.armature = armature;
643
+ // @ts-ignore
348
644
  component.emit('loaded', { resource: component.resource });
349
645
  armature.state.addListener({
646
+ // @ts-ignore
350
647
  start: (track, event) => {
351
648
  component.emit('start', { track, name: track.animation.name });
352
649
  },
650
+ // @ts-ignore
353
651
  complete: (track, event) => {
354
652
  component.emit('complete', { track, name: track.animation.name });
355
653
  },
654
+ // @ts-ignore
356
655
  interrupt: (track, event) => {
357
656
  component.emit('interrupt', { track, name: track.animation.name });
358
657
  },
359
- end: (track, event) => {
658
+ end: (track, // @ts-ignore
659
+ event) => {
360
660
  component.emit('end', { track, name: track.animation.name });
361
661
  },
362
662
  event: (track, event) => {
663
+ // @ts-ignore
363
664
  component.emit('event', track, event);
364
665
  },
365
666
  });
@@ -381,17 +682,23 @@ let SpineSystem = class SpineSystem extends Renderer {
381
682
  container.removeChild(armature);
382
683
  }
383
684
  if (component.armature) {
685
+ // 销毁所有挂载到插槽的 GameObject
686
+ component._destroySlotGameObjects();
384
687
  component.armature.destroy({ children: true });
385
- const res = yield resource.getResource(component.lastResource);
386
- ((_d = (_c = res.data) === null || _c === void 0 ? void 0 : _c.image) === null || _d === void 0 ? void 0 : _d.src) || ((_f = (_e = res.data) === null || _e === void 0 ? void 0 : _e.image) === null || _f === void 0 ? void 0 : _f.label);
387
- releaseSpineData(res);
688
+ if (!component.keepResource) {
689
+ const res = yield resource.getResource(component.lastResource);
690
+ ((_d = (_c = res.data) === null || _c === void 0 ? void 0 : _c.image) === null || _d === void 0 ? void 0 : _d.src) || ((_f = (_e = res.data) === null || _e === void 0 ? void 0 : _e.image) === null || _f === void 0 ? void 0 : _f.label);
691
+ releaseSpineData(res);
692
+ }
388
693
  }
389
694
  component.armature = null;
390
695
  delete this.armatures[changed.gameObject.id];
696
+ delete this._spineComponents[changed.gameObject.id];
391
697
  if (changed.type === OBSERVER_TYPE.CHANGE) ;
392
698
  });
393
699
  }
394
700
  };
701
+ /** 系统名称 */
395
702
  SpineSystem.systemName = 'SpineSystem';
396
703
  SpineSystem = __decorate([
397
704
  decorators.componentObserver({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eva/spine-base",
3
- "version": "2.0.1-beta.9",
3
+ "version": "2.0.1",
4
4
  "description": "@eva/spine-base",
5
5
  "main": "index.js",
6
6
  "module": "dist/spine-base.esm.js",
@@ -18,9 +18,9 @@
18
18
  "license": "MIT",
19
19
  "homepage": "https://eva.js.org",
20
20
  "dependencies": {
21
- "@eva/eva.js": "2.0.1-beta.9",
22
- "@eva/plugin-renderer": "2.0.1-beta.9",
21
+ "@eva/eva.js": "2.0.1",
22
+ "@eva/plugin-renderer": "2.0.1",
23
23
  "@eva/inspector-decorator": "^0.0.5",
24
- "pixi.js": "^8.8.1"
24
+ "pixi.js": "^8.17.0"
25
25
  }
26
26
  }