@eva/plugin-renderer-sprite-animation 2.0.1-beta.8 → 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.
- package/dist/EVA.plugin.renderer.spriteAnimation.js +6 -3
- package/dist/EVA.plugin.renderer.spriteAnimation.min.js +1 -1
- package/dist/plugin-renderer-sprite-animation.cjs.js +59 -3
- package/dist/plugin-renderer-sprite-animation.cjs.prod.js +2 -2
- package/dist/plugin-renderer-sprite-animation.d.ts +58 -0
- package/dist/plugin-renderer-sprite-animation.esm.js +59 -3
- package/package.json +5 -5
|
@@ -155,6 +155,7 @@ var _EVA_IIFE_spriteAnimation = function (exports, eva_js, pluginRenderer, rende
|
|
|
155
155
|
const texture = data.image instanceof pixi_js.Texture ? data.image : pixi_js.Texture.from(data.image);
|
|
156
156
|
const frames = textureObj.frames || {};
|
|
157
157
|
const animations = textureObj.animations || {};
|
|
158
|
+
const newAnimations = {};
|
|
158
159
|
const newFrames = {};
|
|
159
160
|
for (const key in frames) {
|
|
160
161
|
const newKey = name + resourceKeySplit + key;
|
|
@@ -168,10 +169,12 @@ var _EVA_IIFE_spriteAnimation = function (exports, eva_js, pluginRenderer, rende
|
|
|
168
169
|
spriteList.push(newSpriteName);
|
|
169
170
|
}
|
|
170
171
|
}
|
|
171
|
-
|
|
172
|
+
newAnimations[key] = spriteList;
|
|
172
173
|
}
|
|
173
|
-
|
|
174
|
-
|
|
174
|
+
const spriteSheet = new pixi_js.Spritesheet(texture, _extends(_extends({}, textureObj), {
|
|
175
|
+
frames: newFrames,
|
|
176
|
+
animations: newAnimations
|
|
177
|
+
}));
|
|
175
178
|
spriteSheet.parse().then(() => {
|
|
176
179
|
const {
|
|
177
180
|
textures
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function _extends(){return _extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var i=arguments[t];for(var n in i)({}).hasOwnProperty.call(i,n)&&(e[n]=i[n])}return e},_extends.apply(null,arguments)}globalThis.EVA=globalThis.EVA||{},globalThis.EVA.plugin=globalThis.EVA.plugin||{},globalThis.EVA.plugin.renderer=globalThis.EVA.plugin.renderer||{};var _EVA_IIFE_spriteAnimation=function(e,t,i,n,o){"use strict";function r(e,t,i,n){var o,r=arguments.length,s=r<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,i):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,i,n);else for(var a=e.length-1;a>=0;a--)(o=e[a])&&(s=(r<3?o(s):r>3?o(t,i,s):o(t,i))||s);return r>3&&s&&Object.defineProperty(t,i,s),s}function s(e,t,i,n){return new(i||(i=Promise))((function(o,r){function s(e){try{c(n.next(e))}catch(e){r(e)}}function a(e){try{c(n.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(s,a)}c((n=n.apply(e,t||[])).next())}))}function a(e,t){return e.constructor.IDEProps||(e.constructor.IDEProps={}),e.constructor.IDEProps[t]||(e.constructor.IDEProps[t]={}),e.constructor.IDEProps[t]}function c(e){return function(t,i){var n=a(t,i);n.key=i,n.type=e}}class m extends t.Component{constructor(){super(...arguments),this.resource="",this.autoPlay=!0,this.speed=100,this.forwards=!1,this.waitPlay=!1,this.waitStop=!1,this.times=1/0,this.count=0,this.complete=!1}init(e){e&&_extends(this,e),this.on("loop",(()=>{++this.count>=this.times&&(this.forwards?this.gotoAndStop(this.totalFrames-1):this.animate.stop(),this.complete=!0,this.emit("complete"))}))}play(e=1/0){0!==e&&(this.times=e,this.animate?(this.complete&&this.gotoAndStop(0),this.animate.play(),this.count=0,this.complete=!1):this.waitPlay=!0)}stop(){this.animate?this.animate.stop():this.waitStop=!0}set animate(e){this._animate=e,this.waitPlay&&(this.waitPlay=!1,this.play(this.times)),this.waitStop&&(this.waitStop=!1,this.stop())}get animate(){return this._animate}gotoAndPlay(e){this.animate.gotoAndPlay(e)}gotoAndStop(e){this.animate.gotoAndStop(e)}get currentFrame(){var e,t;return null===(t=null===(e=this.animate)||void 0===e?void 0:e.animatedSprite)||void 0===t?void 0:t.currentFrame}get totalFrames(){var e,t;return null===(t=null===(e=this.animate)||void 0===e?void 0:e.animatedSprite)||void 0===t?void 0:t.totalFrames}}m.componentName="SpriteAnimation",r([c("string")],m.prototype,"resource",void 0),r([c("boolean")],m.prototype,"autoPlay",void 0),r([c("number"),function(e){return function(t,i){a(t,i).step=e}}(10)],m.prototype,"speed",void 0),r([c("boolean")],m.prototype,"forwards",void 0);const p="_s|r|c_";t.resource.registerInstance(t.RESOURCE_TYPE.SPRITE_ANIMATION,(({name:e,data:t})=>new Promise((i=>{const n=t.json.data,r=t.image instanceof o.Texture?t.image:o.Texture.from(t.image),s=n.frames||{},a=n.animations||{},c={};for(const t in s){
|
|
1
|
+
function _extends(){return _extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var i=arguments[t];for(var n in i)({}).hasOwnProperty.call(i,n)&&(e[n]=i[n])}return e},_extends.apply(null,arguments)}globalThis.EVA=globalThis.EVA||{},globalThis.EVA.plugin=globalThis.EVA.plugin||{},globalThis.EVA.plugin.renderer=globalThis.EVA.plugin.renderer||{};var _EVA_IIFE_spriteAnimation=function(e,t,i,n,o){"use strict";function r(e,t,i,n){var o,r=arguments.length,s=r<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,i):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,i,n);else for(var a=e.length-1;a>=0;a--)(o=e[a])&&(s=(r<3?o(s):r>3?o(t,i,s):o(t,i))||s);return r>3&&s&&Object.defineProperty(t,i,s),s}function s(e,t,i,n){return new(i||(i=Promise))((function(o,r){function s(e){try{c(n.next(e))}catch(e){r(e)}}function a(e){try{c(n.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(s,a)}c((n=n.apply(e,t||[])).next())}))}function a(e,t){return e.constructor.IDEProps||(e.constructor.IDEProps={}),e.constructor.IDEProps[t]||(e.constructor.IDEProps[t]={}),e.constructor.IDEProps[t]}function c(e){return function(t,i){var n=a(t,i);n.key=i,n.type=e}}class m extends t.Component{constructor(){super(...arguments),this.resource="",this.autoPlay=!0,this.speed=100,this.forwards=!1,this.waitPlay=!1,this.waitStop=!1,this.times=1/0,this.count=0,this.complete=!1}init(e){e&&_extends(this,e),this.on("loop",(()=>{++this.count>=this.times&&(this.forwards?this.gotoAndStop(this.totalFrames-1):this.animate.stop(),this.complete=!0,this.emit("complete"))}))}play(e=1/0){0!==e&&(this.times=e,this.animate?(this.complete&&this.gotoAndStop(0),this.animate.play(),this.count=0,this.complete=!1):this.waitPlay=!0)}stop(){this.animate?this.animate.stop():this.waitStop=!0}set animate(e){this._animate=e,this.waitPlay&&(this.waitPlay=!1,this.play(this.times)),this.waitStop&&(this.waitStop=!1,this.stop())}get animate(){return this._animate}gotoAndPlay(e){this.animate.gotoAndPlay(e)}gotoAndStop(e){this.animate.gotoAndStop(e)}get currentFrame(){var e,t;return null===(t=null===(e=this.animate)||void 0===e?void 0:e.animatedSprite)||void 0===t?void 0:t.currentFrame}get totalFrames(){var e,t;return null===(t=null===(e=this.animate)||void 0===e?void 0:e.animatedSprite)||void 0===t?void 0:t.totalFrames}}m.componentName="SpriteAnimation",r([c("string")],m.prototype,"resource",void 0),r([c("boolean")],m.prototype,"autoPlay",void 0),r([c("number"),function(e){return function(t,i){a(t,i).step=e}}(10)],m.prototype,"speed",void 0),r([c("boolean")],m.prototype,"forwards",void 0);const p="_s|r|c_";t.resource.registerInstance(t.RESOURCE_TYPE.SPRITE_ANIMATION,(({name:e,data:t})=>new Promise((i=>{const n=t.json.data,r=t.image instanceof o.Texture?t.image:o.Texture.from(t.image),s=n.frames||{},a=n.animations||{},c={},m={};for(const t in s){m[e+p+t]=s[t]}for(const t in a){const i=[];if(a[t]&&a[t].length>=0)for(const n of a[t]){const t=e+p+n;i.push(t)}c[t]=i}const d=new o.Spritesheet(r,_extends(_extends({},n),{frames:m,animations:c}));d.parse().then((()=>{const{textures:e}=d,t=[];for(const i in e)t.push(e[i]);i(t)}))})))),t.resource.registerDestroy(t.RESOURCE_TYPE.SPRITE_ANIMATION,(({instance:e})=>{if(e)for(const t of e)t.destroy(!0)}));let d=class extends i.Renderer{constructor(){super(...arguments),this.name="SpriteAnimation",this.animates={},this.autoPlay={}}init(){this.renderSystem=this.game.getSystem(i.RendererSystem),this.renderSystem.rendererManager.register(this)}rendererUpdate(e){const{width:t,height:i}=e.transform.size;this.animates[e.id]&&(this.animates[e.id].animatedSprite.width=t,this.animates[e.id].animatedSprite.height=i)}componentChanged(e){return s(this,void 0,void 0,(function*(){const i=e.gameObject.id;if("SpriteAnimation"===e.componentName){const n=e.component;if(this.autoPlay[e.gameObject.id]=n.autoPlay,e.type===t.OBSERVER_TYPE.ADD){const o=this.increaseAsyncId(i),{instance:r}=yield t.resource.getResource(n.resource);if(!this.validateAsyncId(i,o))return;r||console.error(`GameObject:${e.gameObject.name}'s Img resource load error`),this.add({frames:r,id:e.gameObject.id,component:n})}else if(e.type===t.OBSERVER_TYPE.CHANGE)if(e.prop&&"speed"===e.prop.prop[0])this.animates[e.gameObject.id].speed=1e3/60/n.speed;else{const o=this.increaseAsyncId(i),{instance:r}=yield t.resource.getResource(n.resource);if(!this.validateAsyncId(i,o))return;r||console.error(`GameObject:${e.gameObject.name}'s Img resource load error`),this.change({frames:r,id:e.gameObject.id,component:n})}else e.type===t.OBSERVER_TYPE.REMOVE&&(this.increaseAsyncId(i),this.remove(e.gameObject.id))}}))}add({frames:e,id:t,component:i}){const o=new n.SpriteAnimation({frames:e});this.animates[t]=o,this.containerManager.getContainer(t).addChildAt(o.animatedSprite,0),o.animatedSprite.onComplete=()=>{i.emit("complete")},o.animatedSprite.onFrameChange=()=>{i.emit("frameChange")},o.animatedSprite.onLoop=()=>{i.emit("loop")},i.animate=this.animates[t],this.animates[t].speed=1e3/60/i.speed,this.autoPlay[t]&&o.animatedSprite.play()}change({frames:e,id:t,component:i}){this.remove(t,!0),this.add({frames:e,id:t,component:i})}remove(e,t){const i=this.animates[e];i&&(this.autoPlay[e]=i.animatedSprite.playing,this.containerManager.getContainer(e).removeChild(i.animatedSprite),i.animatedSprite.destroy(),delete this.animates[e],t||delete this.autoPlay[e])}};d.systemName="SpriteAnimation",d=r([t.decorators.componentObserver({SpriteAnimation:["speed","resource"]})],d);var h=d;return e.SpriteAnimation=m,e.SpriteAnimationSystem=h,Object.defineProperty(e,"__esModule",{value:!0}),e}({},EVA,EVA.plugin.renderer,EVA.rendererAdapter,PIXI);globalThis.EVA.plugin.renderer.spriteAnimation=globalThis.EVA.plugin.renderer.spriteAnimation||_EVA_IIFE_spriteAnimation;
|
|
@@ -39,19 +39,74 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* 精灵动画组件
|
|
44
|
+
*
|
|
45
|
+
* SpriteAnimation 组件用于播放帧动画(序列帧动画)。
|
|
46
|
+
* 通过快速切换精灵图集中的不同帧,实现动画效果,
|
|
47
|
+
* 适用于角色动作、特效、UI 动画等场景。
|
|
48
|
+
*
|
|
49
|
+
* 主要功能:
|
|
50
|
+
* - 支持自动播放和手动控制
|
|
51
|
+
* - 可配置播放速度和循环次数
|
|
52
|
+
* - 支持跳转到指定帧
|
|
53
|
+
* - 提供播放完成事件
|
|
54
|
+
* - 支持 forwards 模式(停在最后一帧)
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // 基础用法 - 自动播放
|
|
59
|
+
* const character = new GameObject('character');
|
|
60
|
+
* character.addComponent(new SpriteAnimation({
|
|
61
|
+
* resource: 'walkAnimation', // 帧动画资源
|
|
62
|
+
* autoPlay: true,
|
|
63
|
+
* speed: 100 // 播放速度(毫秒/帧)
|
|
64
|
+
* }));
|
|
65
|
+
*
|
|
66
|
+
* // 控制播放
|
|
67
|
+
* const anim = character.getComponent('SpriteAnimation');
|
|
68
|
+
* anim.play(3); // 播放 3 次
|
|
69
|
+
* anim.play(Infinity); // 无限循环
|
|
70
|
+
* anim.stop(); // 停止播放
|
|
71
|
+
* anim.gotoAndPlay(10); // 跳到第 10 帧并播放
|
|
72
|
+
* anim.gotoAndStop(0); // 跳到第 0 帧并停止
|
|
73
|
+
*
|
|
74
|
+
* // 监听播放完成
|
|
75
|
+
* anim.on('complete', () => {
|
|
76
|
+
* console.log('动画播放完成');
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
42
80
|
class SpriteAnimation$2 extends eva_js.Component {
|
|
43
81
|
constructor() {
|
|
44
82
|
super(...arguments);
|
|
83
|
+
/** 帧动画资源名称 */
|
|
45
84
|
this.resource = '';
|
|
85
|
+
/** 是否自动播放 */
|
|
46
86
|
this.autoPlay = true;
|
|
87
|
+
/** 播放速度(毫秒/帧) */
|
|
47
88
|
this.speed = 100;
|
|
89
|
+
/** 是否在最后一帧停止(forwards 模式) */
|
|
48
90
|
this.forwards = false;
|
|
91
|
+
/** 等待播放标志(资源加载前调用 play) */
|
|
49
92
|
this.waitPlay = false;
|
|
93
|
+
/** 等待停止标志 */
|
|
50
94
|
this.waitStop = false;
|
|
95
|
+
/** 播放次数 */
|
|
51
96
|
this.times = Infinity;
|
|
97
|
+
/** 当前已播放次数 */
|
|
52
98
|
this.count = 0;
|
|
99
|
+
/** 是否播放完成 */
|
|
53
100
|
this.complete = false;
|
|
54
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* 初始化组件
|
|
104
|
+
* @param obj - 初始化参数
|
|
105
|
+
* @param obj.resource - 帧动画资源名称
|
|
106
|
+
* @param obj.autoPlay - 是否自动播放
|
|
107
|
+
* @param obj.speed - 播放速度
|
|
108
|
+
* @param obj.forwards - 是否停在最后一帧
|
|
109
|
+
*/
|
|
55
110
|
init(obj) {
|
|
56
111
|
obj && Object.assign(this, obj);
|
|
57
112
|
this.on('loop', () => {
|
|
@@ -121,6 +176,7 @@ class SpriteAnimation$2 extends eva_js.Component {
|
|
|
121
176
|
return (_b = (_a = this.animate) === null || _a === void 0 ? void 0 : _a.animatedSprite) === null || _b === void 0 ? void 0 : _b.totalFrames;
|
|
122
177
|
}
|
|
123
178
|
}
|
|
179
|
+
/** 组件名称 */
|
|
124
180
|
SpriteAnimation$2.componentName = 'SpriteAnimation';
|
|
125
181
|
__decorate([
|
|
126
182
|
inspectorDecorator.type('string')
|
|
@@ -143,6 +199,7 @@ eva_js.resource.registerInstance(eva_js.RESOURCE_TYPE.SPRITE_ANIMATION, ({ name,
|
|
|
143
199
|
const texture = data.image instanceof pixi_js.Texture ? data.image : pixi_js.Texture.from(data.image);
|
|
144
200
|
const frames = textureObj.frames || {};
|
|
145
201
|
const animations = textureObj.animations || {};
|
|
202
|
+
const newAnimations = {};
|
|
146
203
|
const newFrames = {};
|
|
147
204
|
for (const key in frames) {
|
|
148
205
|
const newKey = name + resourceKeySplit + key;
|
|
@@ -156,10 +213,9 @@ eva_js.resource.registerInstance(eva_js.RESOURCE_TYPE.SPRITE_ANIMATION, ({ name,
|
|
|
156
213
|
spriteList.push(newSpriteName);
|
|
157
214
|
}
|
|
158
215
|
}
|
|
159
|
-
|
|
216
|
+
newAnimations[key] = spriteList;
|
|
160
217
|
}
|
|
161
|
-
textureObj
|
|
162
|
-
const spriteSheet = new pixi_js.Spritesheet(texture, textureObj);
|
|
218
|
+
const spriteSheet = new pixi_js.Spritesheet(texture, Object.assign(Object.assign({}, textureObj), { frames: newFrames, animations: newAnimations }));
|
|
163
219
|
spriteSheet.parse().then(() => {
|
|
164
220
|
const { textures } = spriteSheet;
|
|
165
221
|
const spriteFrames = [];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/inspector-decorator"),i=require("@eva/plugin-renderer"),a=require("@eva/renderer-adapter"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/inspector-decorator"),i=require("@eva/plugin-renderer"),a=require("@eva/renderer-adapter"),s=require("pixi.js");
|
|
2
2
|
/*! *****************************************************************************
|
|
3
3
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
4
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
@@ -13,4 +13,4 @@ MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
|
13
13
|
See the Apache Version 2.0 License for specific language governing permissions
|
|
14
14
|
and limitations under the License.
|
|
15
15
|
***************************************************************************** */
|
|
16
|
-
function
|
|
16
|
+
function n(e,t,i,a){var s,n=arguments.length,o=n<3?t:null===a?a=Object.getOwnPropertyDescriptor(t,i):a;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(e,t,i,a);else for(var r=e.length-1;r>=0;r--)(s=e[r])&&(o=(n<3?s(o):n>3?s(t,i,o):s(t,i))||o);return n>3&&o&&Object.defineProperty(t,i,o),o}class o extends e.Component{constructor(){super(...arguments),this.resource="",this.autoPlay=!0,this.speed=100,this.forwards=!1,this.waitPlay=!1,this.waitStop=!1,this.times=1/0,this.count=0,this.complete=!1}init(e){e&&Object.assign(this,e),this.on("loop",(()=>{++this.count>=this.times&&(this.forwards?this.gotoAndStop(this.totalFrames-1):this.animate.stop(),this.complete=!0,this.emit("complete"))}))}play(e=1/0){0!==e&&(this.times=e,this.animate?(this.complete&&this.gotoAndStop(0),this.animate.play(),this.count=0,this.complete=!1):this.waitPlay=!0)}stop(){this.animate?this.animate.stop():this.waitStop=!0}set animate(e){this._animate=e,this.waitPlay&&(this.waitPlay=!1,this.play(this.times)),this.waitStop&&(this.waitStop=!1,this.stop())}get animate(){return this._animate}gotoAndPlay(e){this.animate.gotoAndPlay(e)}gotoAndStop(e){this.animate.gotoAndStop(e)}get currentFrame(){var e,t;return null===(t=null===(e=this.animate)||void 0===e?void 0:e.animatedSprite)||void 0===t?void 0:t.currentFrame}get totalFrames(){var e,t;return null===(t=null===(e=this.animate)||void 0===e?void 0:e.animatedSprite)||void 0===t?void 0:t.totalFrames}}o.componentName="SpriteAnimation",n([t.type("string")],o.prototype,"resource",void 0),n([t.type("boolean")],o.prototype,"autoPlay",void 0),n([t.type("number"),t.step(10)],o.prototype,"speed",void 0),n([t.type("boolean")],o.prototype,"forwards",void 0);const r="_s|r|c_";e.resource.registerInstance(e.RESOURCE_TYPE.SPRITE_ANIMATION,(({name:e,data:t})=>new Promise((i=>{const a=t.json.data,n=t.image instanceof s.Texture?t.image:s.Texture.from(t.image),o=a.frames||{},m=a.animations||{},c={},p={};for(const t in o){p[e+r+t]=o[t]}for(const t in m){const i=[];if(m[t]&&m[t].length>=0)for(const a of m[t]){const t=e+r+a;i.push(t)}c[t]=i}const d=new s.Spritesheet(n,Object.assign(Object.assign({},a),{frames:p,animations:c}));d.parse().then((()=>{const{textures:e}=d,t=[];for(const i in e)t.push(e[i]);i(t)}))})))),e.resource.registerDestroy(e.RESOURCE_TYPE.SPRITE_ANIMATION,(({instance:e})=>{if(e)for(const t of e)t.destroy(!0)}));let m=class extends i.Renderer{constructor(){super(...arguments),this.name="SpriteAnimation",this.animates={},this.autoPlay={}}init(){this.renderSystem=this.game.getSystem(i.RendererSystem),this.renderSystem.rendererManager.register(this)}rendererUpdate(e){const{width:t,height:i}=e.transform.size;this.animates[e.id]&&(this.animates[e.id].animatedSprite.width=t,this.animates[e.id].animatedSprite.height=i)}componentChanged(t){return i=this,a=void 0,n=function*(){const i=t.gameObject.id;if("SpriteAnimation"===t.componentName){const a=t.component;if(this.autoPlay[t.gameObject.id]=a.autoPlay,t.type===e.OBSERVER_TYPE.ADD){const s=this.increaseAsyncId(i),{instance:n}=yield e.resource.getResource(a.resource);if(!this.validateAsyncId(i,s))return;n||console.error(`GameObject:${t.gameObject.name}'s Img resource load error`),this.add({frames:n,id:t.gameObject.id,component:a})}else if(t.type===e.OBSERVER_TYPE.CHANGE)if(t.prop&&"speed"===t.prop.prop[0])this.animates[t.gameObject.id].speed=1e3/60/a.speed;else{const s=this.increaseAsyncId(i),{instance:n}=yield e.resource.getResource(a.resource);if(!this.validateAsyncId(i,s))return;n||console.error(`GameObject:${t.gameObject.name}'s Img resource load error`),this.change({frames:n,id:t.gameObject.id,component:a})}else t.type===e.OBSERVER_TYPE.REMOVE&&(this.increaseAsyncId(i),this.remove(t.gameObject.id))}},new((s=void 0)||(s=Promise))((function(e,t){function o(e){try{m(n.next(e))}catch(e){t(e)}}function r(e){try{m(n.throw(e))}catch(e){t(e)}}function m(t){t.done?e(t.value):new s((function(e){e(t.value)})).then(o,r)}m((n=n.apply(i,a||[])).next())}));var i,a,s,n}add({frames:e,id:t,component:i}){const s=new a.SpriteAnimation({frames:e});this.animates[t]=s,this.containerManager.getContainer(t).addChildAt(s.animatedSprite,0),s.animatedSprite.onComplete=()=>{i.emit("complete")},s.animatedSprite.onFrameChange=()=>{i.emit("frameChange")},s.animatedSprite.onLoop=()=>{i.emit("loop")},i.animate=this.animates[t],this.animates[t].speed=1e3/60/i.speed,this.autoPlay[t]&&s.animatedSprite.play()}change({frames:e,id:t,component:i}){this.remove(t,!0),this.add({frames:e,id:t,component:i})}remove(e,t){const i=this.animates[e];i&&(this.autoPlay[e]=i.animatedSprite.playing,this.containerManager.getContainer(e).removeChild(i.animatedSprite),i.animatedSprite.destroy(),delete this.animates[e],t||delete this.autoPlay[e])}};m.systemName="SpriteAnimation",m=n([e.decorators.componentObserver({SpriteAnimation:["speed","resource"]})],m);var c=m;exports.SpriteAnimation=o,exports.SpriteAnimationSystem=c;
|
|
@@ -7,18 +7,75 @@ import { RendererManager } from '@eva/plugin-renderer';
|
|
|
7
7
|
import { RendererSystem } from '@eva/plugin-renderer';
|
|
8
8
|
import { SpriteAnimation as SpriteAnimation_2 } from '@eva/renderer-adapter';
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* 精灵动画组件
|
|
12
|
+
*
|
|
13
|
+
* SpriteAnimation 组件用于播放帧动画(序列帧动画)。
|
|
14
|
+
* 通过快速切换精灵图集中的不同帧,实现动画效果,
|
|
15
|
+
* 适用于角色动作、特效、UI 动画等场景。
|
|
16
|
+
*
|
|
17
|
+
* 主要功能:
|
|
18
|
+
* - 支持自动播放和手动控制
|
|
19
|
+
* - 可配置播放速度和循环次数
|
|
20
|
+
* - 支持跳转到指定帧
|
|
21
|
+
* - 提供播放完成事件
|
|
22
|
+
* - 支持 forwards 模式(停在最后一帧)
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // 基础用法 - 自动播放
|
|
27
|
+
* const character = new GameObject('character');
|
|
28
|
+
* character.addComponent(new SpriteAnimation({
|
|
29
|
+
* resource: 'walkAnimation', // 帧动画资源
|
|
30
|
+
* autoPlay: true,
|
|
31
|
+
* speed: 100 // 播放速度(毫秒/帧)
|
|
32
|
+
* }));
|
|
33
|
+
*
|
|
34
|
+
* // 控制播放
|
|
35
|
+
* const anim = character.getComponent('SpriteAnimation');
|
|
36
|
+
* anim.play(3); // 播放 3 次
|
|
37
|
+
* anim.play(Infinity); // 无限循环
|
|
38
|
+
* anim.stop(); // 停止播放
|
|
39
|
+
* anim.gotoAndPlay(10); // 跳到第 10 帧并播放
|
|
40
|
+
* anim.gotoAndStop(0); // 跳到第 0 帧并停止
|
|
41
|
+
*
|
|
42
|
+
* // 监听播放完成
|
|
43
|
+
* anim.on('complete', () => {
|
|
44
|
+
* console.log('动画播放完成');
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
10
48
|
export declare class SpriteAnimation extends Component<SpriteAnimationParams> {
|
|
49
|
+
/** 组件名称 */
|
|
11
50
|
static componentName: string;
|
|
51
|
+
/** 帧动画资源名称 */
|
|
12
52
|
resource: string;
|
|
53
|
+
/** 是否自动播放 */
|
|
13
54
|
autoPlay: boolean;
|
|
55
|
+
/** 播放速度(毫秒/帧) */
|
|
14
56
|
speed: number;
|
|
57
|
+
/** 是否在最后一帧停止(forwards 模式) */
|
|
15
58
|
forwards: boolean;
|
|
59
|
+
/** 动画引擎实例 */
|
|
16
60
|
_animate: SpriteAnimation_2;
|
|
61
|
+
/** 等待播放标志(资源加载前调用 play) */
|
|
17
62
|
private waitPlay;
|
|
63
|
+
/** 等待停止标志 */
|
|
18
64
|
private waitStop;
|
|
65
|
+
/** 播放次数 */
|
|
19
66
|
private times;
|
|
67
|
+
/** 当前已播放次数 */
|
|
20
68
|
private count;
|
|
69
|
+
/** 是否播放完成 */
|
|
21
70
|
private complete;
|
|
71
|
+
/**
|
|
72
|
+
* 初始化组件
|
|
73
|
+
* @param obj - 初始化参数
|
|
74
|
+
* @param obj.resource - 帧动画资源名称
|
|
75
|
+
* @param obj.autoPlay - 是否自动播放
|
|
76
|
+
* @param obj.speed - 播放速度
|
|
77
|
+
* @param obj.forwards - 是否停在最后一帧
|
|
78
|
+
*/
|
|
22
79
|
init(obj?: SpriteAnimationParams): void;
|
|
23
80
|
play(times?: number): void;
|
|
24
81
|
stop(): void;
|
|
@@ -34,6 +91,7 @@ export declare interface SpriteAnimationParams {
|
|
|
34
91
|
resource: string;
|
|
35
92
|
autoPlay?: boolean;
|
|
36
93
|
speed?: number;
|
|
94
|
+
/** Stop at last frame */
|
|
37
95
|
forwards?: boolean;
|
|
38
96
|
}
|
|
39
97
|
|
|
@@ -35,19 +35,74 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* 精灵动画组件
|
|
40
|
+
*
|
|
41
|
+
* SpriteAnimation 组件用于播放帧动画(序列帧动画)。
|
|
42
|
+
* 通过快速切换精灵图集中的不同帧,实现动画效果,
|
|
43
|
+
* 适用于角色动作、特效、UI 动画等场景。
|
|
44
|
+
*
|
|
45
|
+
* 主要功能:
|
|
46
|
+
* - 支持自动播放和手动控制
|
|
47
|
+
* - 可配置播放速度和循环次数
|
|
48
|
+
* - 支持跳转到指定帧
|
|
49
|
+
* - 提供播放完成事件
|
|
50
|
+
* - 支持 forwards 模式(停在最后一帧)
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* // 基础用法 - 自动播放
|
|
55
|
+
* const character = new GameObject('character');
|
|
56
|
+
* character.addComponent(new SpriteAnimation({
|
|
57
|
+
* resource: 'walkAnimation', // 帧动画资源
|
|
58
|
+
* autoPlay: true,
|
|
59
|
+
* speed: 100 // 播放速度(毫秒/帧)
|
|
60
|
+
* }));
|
|
61
|
+
*
|
|
62
|
+
* // 控制播放
|
|
63
|
+
* const anim = character.getComponent('SpriteAnimation');
|
|
64
|
+
* anim.play(3); // 播放 3 次
|
|
65
|
+
* anim.play(Infinity); // 无限循环
|
|
66
|
+
* anim.stop(); // 停止播放
|
|
67
|
+
* anim.gotoAndPlay(10); // 跳到第 10 帧并播放
|
|
68
|
+
* anim.gotoAndStop(0); // 跳到第 0 帧并停止
|
|
69
|
+
*
|
|
70
|
+
* // 监听播放完成
|
|
71
|
+
* anim.on('complete', () => {
|
|
72
|
+
* console.log('动画播放完成');
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
38
76
|
class SpriteAnimation$2 extends Component {
|
|
39
77
|
constructor() {
|
|
40
78
|
super(...arguments);
|
|
79
|
+
/** 帧动画资源名称 */
|
|
41
80
|
this.resource = '';
|
|
81
|
+
/** 是否自动播放 */
|
|
42
82
|
this.autoPlay = true;
|
|
83
|
+
/** 播放速度(毫秒/帧) */
|
|
43
84
|
this.speed = 100;
|
|
85
|
+
/** 是否在最后一帧停止(forwards 模式) */
|
|
44
86
|
this.forwards = false;
|
|
87
|
+
/** 等待播放标志(资源加载前调用 play) */
|
|
45
88
|
this.waitPlay = false;
|
|
89
|
+
/** 等待停止标志 */
|
|
46
90
|
this.waitStop = false;
|
|
91
|
+
/** 播放次数 */
|
|
47
92
|
this.times = Infinity;
|
|
93
|
+
/** 当前已播放次数 */
|
|
48
94
|
this.count = 0;
|
|
95
|
+
/** 是否播放完成 */
|
|
49
96
|
this.complete = false;
|
|
50
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* 初始化组件
|
|
100
|
+
* @param obj - 初始化参数
|
|
101
|
+
* @param obj.resource - 帧动画资源名称
|
|
102
|
+
* @param obj.autoPlay - 是否自动播放
|
|
103
|
+
* @param obj.speed - 播放速度
|
|
104
|
+
* @param obj.forwards - 是否停在最后一帧
|
|
105
|
+
*/
|
|
51
106
|
init(obj) {
|
|
52
107
|
obj && Object.assign(this, obj);
|
|
53
108
|
this.on('loop', () => {
|
|
@@ -117,6 +172,7 @@ class SpriteAnimation$2 extends Component {
|
|
|
117
172
|
return (_b = (_a = this.animate) === null || _a === void 0 ? void 0 : _a.animatedSprite) === null || _b === void 0 ? void 0 : _b.totalFrames;
|
|
118
173
|
}
|
|
119
174
|
}
|
|
175
|
+
/** 组件名称 */
|
|
120
176
|
SpriteAnimation$2.componentName = 'SpriteAnimation';
|
|
121
177
|
__decorate([
|
|
122
178
|
type('string')
|
|
@@ -139,6 +195,7 @@ resource.registerInstance(RESOURCE_TYPE.SPRITE_ANIMATION, ({ name, data }) => {
|
|
|
139
195
|
const texture = data.image instanceof Texture ? data.image : Texture.from(data.image);
|
|
140
196
|
const frames = textureObj.frames || {};
|
|
141
197
|
const animations = textureObj.animations || {};
|
|
198
|
+
const newAnimations = {};
|
|
142
199
|
const newFrames = {};
|
|
143
200
|
for (const key in frames) {
|
|
144
201
|
const newKey = name + resourceKeySplit + key;
|
|
@@ -152,10 +209,9 @@ resource.registerInstance(RESOURCE_TYPE.SPRITE_ANIMATION, ({ name, data }) => {
|
|
|
152
209
|
spriteList.push(newSpriteName);
|
|
153
210
|
}
|
|
154
211
|
}
|
|
155
|
-
|
|
212
|
+
newAnimations[key] = spriteList;
|
|
156
213
|
}
|
|
157
|
-
textureObj
|
|
158
|
-
const spriteSheet = new Spritesheet(texture, textureObj);
|
|
214
|
+
const spriteSheet = new Spritesheet(texture, Object.assign(Object.assign({}, textureObj), { frames: newFrames, animations: newAnimations }));
|
|
159
215
|
spriteSheet.parse().then(() => {
|
|
160
216
|
const { textures } = spriteSheet;
|
|
161
217
|
const spriteFrames = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eva/plugin-renderer-sprite-animation",
|
|
3
|
-
"version": "2.0.1
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "@eva/plugin-renderer-sprite-animation",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "dist/plugin-renderer-sprite-animation.esm.js",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"homepage": "https://eva.js.org",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@eva/inspector-decorator": "^0.0.5",
|
|
22
|
-
"@eva/plugin-renderer": "2.0.1
|
|
23
|
-
"@eva/renderer-adapter": "2.0.1
|
|
24
|
-
"@eva/eva.js": "2.0.1
|
|
25
|
-
"pixi.js": "^8.
|
|
22
|
+
"@eva/plugin-renderer": "2.0.1",
|
|
23
|
+
"@eva/renderer-adapter": "2.0.1",
|
|
24
|
+
"@eva/eva.js": "2.0.1",
|
|
25
|
+
"pixi.js": "^8.17.0"
|
|
26
26
|
}
|
|
27
27
|
}
|