@eva/plugin-renderer-particle 2.1.0-beta.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.
@@ -0,0 +1 @@
1
+ function _extends(){return _extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[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_particle=function(e,t,r,n){"use strict";function i(e,t,r,n){var i,a=arguments.length,s=a<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,n);else for(var o=e.length-1;o>=0;o--)(i=e[o])&&(s=(a<3?i(s):a>3?i(t,r,s):i(t,r))||s);return a>3&&s&&Object.defineProperty(t,r,s),s}function a(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function s(e,t,r,n){return new(r||(r=Promise))(function(i,a){function s(e){try{l(n.next(e))}catch(e){a(e)}}function o(e){try{l(n.throw(e))}catch(e){a(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(s,o)}l((n=n.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class o extends Error{constructor(){super("Symbol keys are not supported yet!"),Object.setPrototypeOf(this,new.target.prototype)}}const l="IDE_PROPERTY_METADATA";function c(e,t,r,n){let i=Reflect.getMetadata("design:type",e,t),a=i===Array;const s=function(e){return e===String?"string":e===Number?"number":e===Boolean?"boolean":"unknown"}(i);if("unknown"!==s&&(i=s),n){const e=n();Array.isArray(e)?(a=!0,i=e[0]):i=e}const o=Reflect.getMetadata(l,e.constructor)||{},c=_extends(_extends(_extends({},o[t]||{}),{type:i,isArray:a}),r);o[t]=c,Reflect.defineMetadata(l,o,e.constructor);!function(e,t,r){const n=e.constructor,i=n.IDEProps||{};i[t]=_extends(_extends({key:t},i[t]),r),n.IDEProps=i}(e,t,function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(e);i<n.length;i++)t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i])&&(r[n[i]]=e[n[i]])}return r}(c,["isArray"]))}function u(e){return t={type:e},(e,n)=>{if("symbol"==typeof n)throw new o;const{options:i,returnTypeFunc:a}=function(e,t){return"function"==typeof e?{returnTypeFunc:e,options:t||{}}:{options:e||{}}}(t,r);c(e,n,i,a)};var t,r}var d;!function(e){e[e.Edit=2]="Edit",e[e.Game=4]="Game",e[e.All=6]="All"}(d||(d={}));class h extends t.Component{constructor(){super(...arguments),this.resource="",this.auto=!0,this.duration=-1,this.frequency=250,this.quantity=1,this.maxParticles=500,this.lifespan=1e3,this.gravityX=0,this.gravityY=0,this.paused=!1}init(e){e&&_extends(this,e)}stop(){this.paused=!0}start(){this.paused=!1}}h.componentName="ParticleEmitter",i([u("string"),a("design:type",String)],h.prototype,"resource",void 0),i([u("boolean"),a("design:type",Boolean)],h.prototype,"auto",void 0),i([u("number"),a("design:type",Number)],h.prototype,"duration",void 0),i([u("number"),a("design:type",Number)],h.prototype,"frequency",void 0),i([u("number"),a("design:type",Number)],h.prototype,"quantity",void 0),i([u("number"),a("design:type",Number)],h.prototype,"maxParticles",void 0),i([u("number"),a("design:type",Number)],h.prototype,"gravityX",void 0),i([u("number"),a("design:type",Number)],h.prototype,"gravityY",void 0);const p=2*Math.PI,y={linear:e=>e,"sine.in":e=>1-Math.cos(e*Math.PI/2),"sine.out":e=>Math.sin(e*Math.PI/2),"sine.inout":e=>-(Math.cos(Math.PI*e)-1)/2,"quad.in":e=>e*e,"quad.out":e=>1-(1-e)*(1-e),"quad.inout":e=>e<.5?2*e*e:1-Math.pow(-2*e+2,2)/2,"cubic.in":e=>e*e*e,"cubic.out":e=>1-Math.pow(1-e,3),"expo.out":e=>1===e?1:1-Math.pow(2,-10*e)};function f(e){if(!e)return y.linear;const t=e.toLowerCase();return y[t]||y.linear}function m(e,t){return e+Math.random()*(t-e)}function v(e,t=0){return null==e?t:"number"==typeof e?e:Array.isArray(e)?e[Math.floor(Math.random()*e.length)]:"min"in e&&"max"in e?m(e.min,e.max):"start"in e&&"end"in e?e.start:t}function x(e){return null==e||"number"==typeof e||Array.isArray(e)?null:"start"in e&&"end"in e?e:null}function g(e){return e?function(e,t){var r,n,i,a,s,o,l,c;switch(e.type){case"point":return{x:null!==(r=e.x)&&void 0!==r?r:0,y:null!==(n=e.y)&&void 0!==n?n:0};case"rect":{const r=null!==(i=e.x)&&void 0!==i?i:0,n=null!==(a=e.y)&&void 0!==a?a:0;if(t){const t=2*(e.width+e.height),i=Math.random()*t;return i<e.width?{x:r+i,y:n}:i<e.width+e.height?{x:r+e.width,y:n+(i-e.width)}:i<2*e.width+e.height?{x:r+e.width-(i-e.width-e.height),y:n+e.height}:{x:r,y:n+e.height-(i-2*e.width-e.height)}}return{x:m(r,r+e.width),y:m(n,n+e.height)}}case"circle":{const r=null!==(s=e.x)&&void 0!==s?s:0,n=null!==(o=e.y)&&void 0!==o?o:0,i=Math.random()*p,a=t?e.radius:e.radius*Math.sqrt(Math.random());return{x:r+Math.cos(i)*a,y:n+Math.sin(i)*a}}case"ellipse":{const r=null!==(l=e.x)&&void 0!==l?l:0,n=null!==(c=e.y)&&void 0!==c?c:0,i=Math.random()*p,a=t?1:Math.sqrt(Math.random());return{x:r+Math.cos(i)*e.rx*a,y:n+Math.sin(i)*e.ry*a}}case"line":{const t=Math.random();return{x:e.x1+(e.x2-e.x1)*t,y:e.y1+(e.y2-e.y1)*t}}}}(e.shape,"edge"===e.type):{x:0,y:0}}class E{constructor(e,t,r){this.frameTextures=null,this.particles=[],this.elapsed=0,this.emittedTotal=0,this.emitTimer=0,this.explodeDone=!1,this.params=e,this.container=t,this.texture=r}setTexture(e){this.texture=e}setFrameTextures(e){this.frameTextures=e&&e.length>0?e:null}setParams(e){this.params=e}destroy(){this.particles.length=0,this.container.particleChildren.length=0,this.container.update()}update(e){this.elapsed+=e,this.advanceParticles(e),this.spawn(e),this.container.update()}advanceParticles(e){var t,r;const n=e/1e3,i=[];for(const a of this.particles){if(a.age+=e,a.age>a.lifespan)continue;a.vx+=a.ax*n+(null!==(t=this.params.gravityX)&&void 0!==t?t:0)*n,a.vy+=a.ay*n+(null!==(r=this.params.gravityY)&&void 0!==r?r:0)*n,a.particle.x+=a.vx*n,a.particle.y+=a.vy*n,a.particle.rotation+=a.rotateSpeed*n;const s=a.age/a.lifespan;if(a.scaleStart&&a.scaleEnd){const e=(a.scaleEase||y.linear)(s);a.particle.scaleX=a.scaleStart.x+(a.scaleEnd.x-a.scaleStart.x)*e,a.particle.scaleY=a.scaleStart.y+(a.scaleEnd.y-a.scaleStart.y)*e}if(null!=a.alphaStart&&null!=a.alphaEnd){const e=(a.alphaEase||y.linear)(s);a.particle.alpha=a.alphaStart+(a.alphaEnd-a.alphaStart)*e}this.params.deathZone&&this.shouldDie(a.particle.x,a.particle.y)||i.push(a)}this.particles=i,this.container.particleChildren.length=0;for(const e of this.particles)this.container.particleChildren.push(e.particle)}shouldDie(e,t){const r=this.params.deathZone;if(!r)return!1;const n=function(e,t,r){var n,i,a,s,o,l;switch(e.type){case"rect":{const a=null!==(n=e.x)&&void 0!==n?n:0,s=null!==(i=e.y)&&void 0!==i?i:0;return t>=a&&t<=a+e.width&&r>=s&&r<=s+e.height}case"circle":{const n=t-(null!==(a=e.x)&&void 0!==a?a:0),i=r-(null!==(s=e.y)&&void 0!==s?s:0);return n*n+i*i<=e.radius*e.radius}case"ellipse":{const n=(t-(null!==(o=e.x)&&void 0!==o?o:0))/e.rx,i=(r-(null!==(l=e.y)&&void 0!==l?l:0))/e.ry;return n*n+i*i<=1}case"point":case"line":return!1}}(r.shape,e,t);return"onLeave"===r.mode?!n:n}spawn(e){var t,r,n;const i=this.params;if(null!=i.duration&&i.duration>0&&this.elapsed>i.duration)return;if(null!=i.stopAfter&&this.emittedTotal>=i.stopAfter)return;if(i.paused)return;const a=null!==(t=i.maxParticles)&&void 0!==t?t:500;if(this.particles.length>=a)return;if(null!=i.explode&&!this.explodeDone){const e=Math.min(i.explode,a-this.particles.length);for(let t=0;t<e;t++)this.emitOne();return void(this.explodeDone=!0)}if(null!=i.explode)return;if(!1===i.auto)return;this.emitTimer+=e;const s=null!==(r=i.frequency)&&void 0!==r?r:250,o=null!==(n=i.quantity)&&void 0!==n?n:1;for(;this.emitTimer>=s&&this.particles.length<a;){for(let e=0;e<o&&this.particles.length<a&&!(null!=i.stopAfter&&this.emittedTotal>=i.stopAfter);e++)this.emitOne();this.emitTimer-=s}}emitOne(){var e,t,r,i,a,s,o,l,c,u;const d=this.params,h=g(d.emitZone),p=v(d.speed,0);let y,m;if(d.moveTo){const e=d.moveTo.x-h.x,t=d.moveTo.y-h.y,r=Math.hypot(e,t)||1;y=e/r*(p||100),m=t/r*(p||100)}else if(null!=d.speedX||null!=d.speedY)y=v(d.speedX,0),m=v(d.speedY,0);else{const e=v(d.angle,0)*Math.PI/180;y=Math.cos(e)*p,m=Math.sin(e)*p}const E=v(d.accelerationX,0),b=v(d.accelerationY,0),T=null!=d.scaleX?v(d.scaleX,1):v(d.scale,1),M=null!=d.scaleY?v(d.scaleY,1):v(d.scale,1),A=v(d.alpha,1),P=v(d.rotate,0),w=Array.isArray(d.tint)?d.tint[Math.floor(Math.random()*d.tint.length)]:null!==(e=d.tint)&&void 0!==e?e:16777215,O=this.frameTextures?this.frameTextures[Math.floor(Math.random()*this.frameTextures.length)]:this.texture,S=new n.Particle({texture:O,x:h.x,y:h.y,scaleX:T,scaleY:M,alpha:A,rotation:P,tint:w,anchorX:.5,anchorY:.5}),_={particle:S,age:0,lifespan:v(d.lifespan,1e3),vx:y,vy:m,ax:E,ay:b,rotateSpeed:0},R=x(d.scale),I=x(d.scaleX),Y=x(d.scaleY);if(R||I||Y){const e=null!==(r=null!==(t=null==I?void 0:I.start)&&void 0!==t?t:null==R?void 0:R.start)&&void 0!==r?r:T,n=null!==(a=null!==(i=null==I?void 0:I.end)&&void 0!==i?i:null==R?void 0:R.end)&&void 0!==a?a:T,d=null!==(o=null!==(s=null==Y?void 0:Y.start)&&void 0!==s?s:null==R?void 0:R.start)&&void 0!==o?o:M,h=null!==(c=null!==(l=null==Y?void 0:Y.end)&&void 0!==l?l:null==R?void 0:R.end)&&void 0!==c?c:M;_.scaleStart={x:e,y:d},_.scaleEnd={x:n,y:h},_.scaleEase=f(null!==(u=null==I?void 0:I.ease)&&void 0!==u?u:null==R?void 0:R.ease),S.scaleX=e,S.scaleY=d}const j=x(d.alpha);j&&(_.alphaStart=j.start,_.alphaEnd=j.end,_.alphaEase=f(j.ease),S.alpha=j.start),this.particles.push(_),this.container.particleChildren.push(S),this.emittedTotal++}}function b(e){return!!e&&!0===e.isTexture&&!!e.orig&&!!e.uvs}function T(e,t,r){if(b(e))return{texture:e,frameTextures:null};if(e&&"object"==typeof e){const n=r=>{var n;const i=null!==(n=e[t+"_s|r|c_"+r])&&void 0!==n?n:e[r];return b(i)?i:null},i=Array.isArray(r)?r:r?[r]:[],a=[];for(const e of i){const t=n(e);t&&a.push(t)}if(a.length>0)return{texture:a[0],frameTextures:a};for(const t in e){const r=e[t];if(b(r))return{texture:r,frameTextures:null}}}return{texture:null,frameTextures:null}}let M=class extends r.Renderer{constructor(){super(...arguments),this.name="ParticleEmitter",this.records={},this.lastDeltaTime=1e3/60}init(){this.renderSystem=this.game.getSystem(r.RendererSystem),this.renderSystem.rendererManager.register(this)}update(e){(null==e?void 0:e.deltaTime)&&(this.lastDeltaTime=e.deltaTime),super.update(e)}rendererUpdate(e){const t=this.records[e.id];t&&(t.emitter.setParams(t.component),t.emitter.update(this.lastDeltaTime))}componentChanged(e){var r,i,a,o;return s(this,void 0,void 0,function*(){if("ParticleEmitter"!==e.componentName)return;const s=e.component,l=e.gameObject.id;if(e.type===t.OBSERVER_TYPE.ADD){const r=this.increaseAsyncId(l);let i=null,a=null;if(s.resource){const{instance:n}=yield t.resource.getResource(s.resource);if(!this.validateAsyncId(l,r))return;if(!n)return void console.error(`GameObject:${e.gameObject.name}'s ParticleEmitter resource load error`);const o=T(n,s.resource,s.frame);i=o.texture,a=o.frameTextures}if(!i)return;const o=new n.ParticleContainer({dynamicProperties:{position:!0,rotation:!0,scale:!0,color:!0,uvs:!0}});o.texture=i,this.containerManager.getContainer(l).addChildAt(o,0);const c=new E(s,o,i);c.setFrameTextures(a),this.records[l]={container:o,emitter:c,component:s}}else if(e.type===t.OBSERVER_TYPE.CHANGE){const n=this.records[l];if(!n)return;if("resource"===(null===(i=null===(r=e.prop)||void 0===r?void 0:r.prop)||void 0===i?void 0:i[0])){const e=this.increaseAsyncId(l),{instance:r}=yield t.resource.getResource(s.resource);if(!this.validateAsyncId(l,e))return;if(r){const e=T(r,s.resource,s.frame);e.texture&&(n.container.texture=e.texture,n.emitter.setTexture(e.texture)),n.emitter.setFrameTextures(e.frameTextures)}}n.component=s,n.emitter.setParams(s)}else if(e.type===t.OBSERVER_TYPE.REMOVE){this.increaseAsyncId(l);const e=this.records[l];if(!e)return;e.emitter.destroy(),null===(o=null===(a=this.containerManager)||void 0===a?void 0:a.getContainer(l))||void 0===o||o.removeChild(e.container),e.container.destroy({children:!0}),delete this.records[l]}})}destroy(){var e,t;for(const r in this.records){const n=parseInt(r),i=this.records[n];i.emitter.destroy(),null===(t=null===(e=this.containerManager)||void 0===e?void 0:e.getContainer(n))||void 0===t||t.removeChild(i.container),i.container.destroy({children:!0}),delete this.records[n]}}};M.systemName="ParticleEmitter",M=i([t.decorators.componentObserver({ParticleEmitter:[{prop:["resource"],deep:!1}]})],M);var A=M;return e.Emitter=E,e.ParticleEmitter=h,e.ParticleEmitterSystem=A,Object.defineProperty(e,"__esModule",{value:!0}),e}({},EVA,EVA.plugin.renderer,PIXI);globalThis.EVA.plugin.renderer.particle=globalThis.EVA.plugin.renderer.particle||_EVA_IIFE_particle;
@@ -0,0 +1,573 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var eva_js = require('@eva/eva.js');
6
+ var inspectorDecorator = require('@eva/inspector-decorator');
7
+ var pluginRenderer = require('@eva/plugin-renderer');
8
+ var pixi_js = require('pixi.js');
9
+
10
+ /******************************************************************************
11
+ Copyright (c) Microsoft Corporation.
12
+
13
+ Permission to use, copy, modify, and/or distribute this software for any
14
+ purpose with or without fee is hereby granted.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
17
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
19
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
20
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22
+ PERFORMANCE OF THIS SOFTWARE.
23
+ ***************************************************************************** */
24
+
25
+ function __decorate(decorators, target, key, desc) {
26
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
27
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
28
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
29
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
30
+ }
31
+
32
+ function __metadata(metadataKey, metadataValue) {
33
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
34
+ }
35
+
36
+ function __awaiter(thisArg, _arguments, P, generator) {
37
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
+ return new (P || (P = Promise))(function (resolve, reject) {
39
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ }
45
+
46
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
47
+ var e = new Error(message);
48
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
49
+ };
50
+
51
+ class ParticleEmitter extends eva_js.Component {
52
+ constructor() {
53
+ super(...arguments);
54
+ this.resource = '';
55
+ this.auto = true;
56
+ this.duration = -1;
57
+ this.frequency = 250;
58
+ this.quantity = 1;
59
+ this.maxParticles = 500;
60
+ this.lifespan = 1000;
61
+ this.gravityX = 0;
62
+ this.gravityY = 0;
63
+ /** Runtime 标记:由 System 设置,触发时立即停止发射。 */
64
+ this.paused = false;
65
+ }
66
+ init(obj) {
67
+ if (obj)
68
+ Object.assign(this, obj);
69
+ }
70
+ /** 立刻停止发射(已存在的粒子继续运行直至 lifespan 到期)。 */
71
+ stop() {
72
+ this.paused = true;
73
+ }
74
+ /** 恢复发射。 */
75
+ start() {
76
+ this.paused = false;
77
+ }
78
+ }
79
+ ParticleEmitter.componentName = 'ParticleEmitter';
80
+ __decorate([
81
+ inspectorDecorator.type('string'),
82
+ __metadata("design:type", String)
83
+ ], ParticleEmitter.prototype, "resource", void 0);
84
+ __decorate([
85
+ inspectorDecorator.type('boolean'),
86
+ __metadata("design:type", Boolean)
87
+ ], ParticleEmitter.prototype, "auto", void 0);
88
+ __decorate([
89
+ inspectorDecorator.type('number'),
90
+ __metadata("design:type", Number)
91
+ ], ParticleEmitter.prototype, "duration", void 0);
92
+ __decorate([
93
+ inspectorDecorator.type('number'),
94
+ __metadata("design:type", Number)
95
+ ], ParticleEmitter.prototype, "frequency", void 0);
96
+ __decorate([
97
+ inspectorDecorator.type('number'),
98
+ __metadata("design:type", Number)
99
+ ], ParticleEmitter.prototype, "quantity", void 0);
100
+ __decorate([
101
+ inspectorDecorator.type('number'),
102
+ __metadata("design:type", Number)
103
+ ], ParticleEmitter.prototype, "maxParticles", void 0);
104
+ __decorate([
105
+ inspectorDecorator.type('number'),
106
+ __metadata("design:type", Number)
107
+ ], ParticleEmitter.prototype, "gravityX", void 0);
108
+ __decorate([
109
+ inspectorDecorator.type('number'),
110
+ __metadata("design:type", Number)
111
+ ], ParticleEmitter.prototype, "gravityY", void 0);
112
+
113
+ const TAU = Math.PI * 2;
114
+ const easings = {
115
+ linear: t => t,
116
+ 'sine.in': t => 1 - Math.cos((t * Math.PI) / 2),
117
+ 'sine.out': t => Math.sin((t * Math.PI) / 2),
118
+ 'sine.inout': t => -(Math.cos(Math.PI * t) - 1) / 2,
119
+ 'quad.in': t => t * t,
120
+ 'quad.out': t => 1 - (1 - t) * (1 - t),
121
+ 'quad.inout': t => (t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2),
122
+ 'cubic.in': t => t * t * t,
123
+ 'cubic.out': t => 1 - Math.pow(1 - t, 3),
124
+ 'expo.out': t => (t === 1 ? 1 : 1 - Math.pow(2, -10 * t)),
125
+ };
126
+ function resolveEase(name) {
127
+ if (!name)
128
+ return easings.linear;
129
+ const k = name.toLowerCase();
130
+ return easings[k] || easings.linear;
131
+ }
132
+ function rand(min, max) {
133
+ return min + Math.random() * (max - min);
134
+ }
135
+ /** Range 取值:每次 emit 时算一次具体值。`{start, end}` 留给粒子自己每帧插值。 */
136
+ function sampleRange(value, fallback = 0) {
137
+ if (value == null)
138
+ return fallback;
139
+ if (typeof value === 'number')
140
+ return value;
141
+ if (Array.isArray(value))
142
+ return value[Math.floor(Math.random() * value.length)];
143
+ if ('min' in value && 'max' in value)
144
+ return rand(value.min, value.max);
145
+ if ('start' in value && 'end' in value)
146
+ return value.start;
147
+ return fallback;
148
+ }
149
+ function getRangeBounds(value) {
150
+ if (value == null || typeof value === 'number' || Array.isArray(value))
151
+ return null;
152
+ if ('start' in value && 'end' in value)
153
+ return value;
154
+ return null;
155
+ }
156
+ function samplePosition(zone) {
157
+ if (!zone)
158
+ return { x: 0, y: 0 };
159
+ return sampleShape(zone.shape, zone.type === 'edge');
160
+ }
161
+ function sampleShape(shape, edgeOnly) {
162
+ var _a, _b, _c, _d, _e, _f, _g, _h;
163
+ switch (shape.type) {
164
+ case 'point':
165
+ return { x: (_a = shape.x) !== null && _a !== void 0 ? _a : 0, y: (_b = shape.y) !== null && _b !== void 0 ? _b : 0 };
166
+ case 'rect': {
167
+ const x0 = (_c = shape.x) !== null && _c !== void 0 ? _c : 0;
168
+ const y0 = (_d = shape.y) !== null && _d !== void 0 ? _d : 0;
169
+ if (edgeOnly) {
170
+ const perim = 2 * (shape.width + shape.height);
171
+ const t = Math.random() * perim;
172
+ if (t < shape.width)
173
+ return { x: x0 + t, y: y0 };
174
+ if (t < shape.width + shape.height)
175
+ return { x: x0 + shape.width, y: y0 + (t - shape.width) };
176
+ if (t < 2 * shape.width + shape.height)
177
+ return { x: x0 + shape.width - (t - shape.width - shape.height), y: y0 + shape.height };
178
+ return { x: x0, y: y0 + shape.height - (t - 2 * shape.width - shape.height) };
179
+ }
180
+ return { x: rand(x0, x0 + shape.width), y: rand(y0, y0 + shape.height) };
181
+ }
182
+ case 'circle': {
183
+ const cx = (_e = shape.x) !== null && _e !== void 0 ? _e : 0;
184
+ const cy = (_f = shape.y) !== null && _f !== void 0 ? _f : 0;
185
+ const angle = Math.random() * TAU;
186
+ const r = edgeOnly ? shape.radius : shape.radius * Math.sqrt(Math.random());
187
+ return { x: cx + Math.cos(angle) * r, y: cy + Math.sin(angle) * r };
188
+ }
189
+ case 'ellipse': {
190
+ const cx = (_g = shape.x) !== null && _g !== void 0 ? _g : 0;
191
+ const cy = (_h = shape.y) !== null && _h !== void 0 ? _h : 0;
192
+ const angle = Math.random() * TAU;
193
+ const k = edgeOnly ? 1 : Math.sqrt(Math.random());
194
+ return { x: cx + Math.cos(angle) * shape.rx * k, y: cy + Math.sin(angle) * shape.ry * k };
195
+ }
196
+ case 'line': {
197
+ const t = Math.random();
198
+ return { x: shape.x1 + (shape.x2 - shape.x1) * t, y: shape.y1 + (shape.y2 - shape.y1) * t };
199
+ }
200
+ }
201
+ }
202
+ function pointInShape(shape, px, py) {
203
+ var _a, _b, _c, _d, _e, _f;
204
+ switch (shape.type) {
205
+ case 'rect': {
206
+ const x0 = (_a = shape.x) !== null && _a !== void 0 ? _a : 0;
207
+ const y0 = (_b = shape.y) !== null && _b !== void 0 ? _b : 0;
208
+ return px >= x0 && px <= x0 + shape.width && py >= y0 && py <= y0 + shape.height;
209
+ }
210
+ case 'circle': {
211
+ const dx = px - ((_c = shape.x) !== null && _c !== void 0 ? _c : 0);
212
+ const dy = py - ((_d = shape.y) !== null && _d !== void 0 ? _d : 0);
213
+ return dx * dx + dy * dy <= shape.radius * shape.radius;
214
+ }
215
+ case 'ellipse': {
216
+ const dx = (px - ((_e = shape.x) !== null && _e !== void 0 ? _e : 0)) / shape.rx;
217
+ const dy = (py - ((_f = shape.y) !== null && _f !== void 0 ? _f : 0)) / shape.ry;
218
+ return dx * dx + dy * dy <= 1;
219
+ }
220
+ case 'point':
221
+ case 'line':
222
+ return false;
223
+ }
224
+ }
225
+ class Emitter {
226
+ constructor(params, container, texture) {
227
+ this.frameTextures = null;
228
+ this.particles = [];
229
+ this.elapsed = 0;
230
+ this.emittedTotal = 0;
231
+ this.emitTimer = 0;
232
+ this.explodeDone = false;
233
+ this.params = params;
234
+ this.container = container;
235
+ this.texture = texture;
236
+ }
237
+ setTexture(texture) {
238
+ this.texture = texture;
239
+ }
240
+ /** Provide a texture pool when component.frame is set; emitOne samples from it. */
241
+ setFrameTextures(textures) {
242
+ this.frameTextures = textures && textures.length > 0 ? textures : null;
243
+ }
244
+ setParams(params) {
245
+ this.params = params;
246
+ }
247
+ destroy() {
248
+ this.particles.length = 0;
249
+ this.container.particleChildren.length = 0;
250
+ this.container.update();
251
+ }
252
+ update(dt) {
253
+ this.elapsed += dt;
254
+ this.advanceParticles(dt);
255
+ this.spawn(dt);
256
+ this.container.update();
257
+ }
258
+ advanceParticles(dt) {
259
+ var _a, _b;
260
+ const dtSec = dt / 1000;
261
+ const out = [];
262
+ for (const lp of this.particles) {
263
+ lp.age += dt;
264
+ if (lp.age > lp.lifespan) {
265
+ continue;
266
+ }
267
+ lp.vx += lp.ax * dtSec + ((_a = this.params.gravityX) !== null && _a !== void 0 ? _a : 0) * dtSec;
268
+ lp.vy += lp.ay * dtSec + ((_b = this.params.gravityY) !== null && _b !== void 0 ? _b : 0) * dtSec;
269
+ lp.particle.x += lp.vx * dtSec;
270
+ lp.particle.y += lp.vy * dtSec;
271
+ lp.particle.rotation += lp.rotateSpeed * dtSec;
272
+ const t = lp.age / lp.lifespan;
273
+ if (lp.scaleStart && lp.scaleEnd) {
274
+ const k = (lp.scaleEase || easings.linear)(t);
275
+ lp.particle.scaleX = lp.scaleStart.x + (lp.scaleEnd.x - lp.scaleStart.x) * k;
276
+ lp.particle.scaleY = lp.scaleStart.y + (lp.scaleEnd.y - lp.scaleStart.y) * k;
277
+ }
278
+ if (lp.alphaStart != null && lp.alphaEnd != null) {
279
+ const k = (lp.alphaEase || easings.linear)(t);
280
+ lp.particle.alpha = lp.alphaStart + (lp.alphaEnd - lp.alphaStart) * k;
281
+ }
282
+ if (this.params.deathZone && this.shouldDie(lp.particle.x, lp.particle.y)) {
283
+ continue;
284
+ }
285
+ out.push(lp);
286
+ }
287
+ this.particles = out;
288
+ this.container.particleChildren.length = 0;
289
+ for (const lp of this.particles)
290
+ this.container.particleChildren.push(lp.particle);
291
+ }
292
+ shouldDie(x, y) {
293
+ const dz = this.params.deathZone;
294
+ if (!dz)
295
+ return false;
296
+ const inside = pointInShape(dz.shape, x, y);
297
+ return dz.mode === 'onLeave' ? !inside : inside;
298
+ }
299
+ spawn(dt) {
300
+ var _a, _b, _c;
301
+ const p = this.params;
302
+ if (p.duration != null && p.duration > 0 && this.elapsed > p.duration)
303
+ return;
304
+ if (p.stopAfter != null && this.emittedTotal >= p.stopAfter)
305
+ return;
306
+ if (p.paused)
307
+ return;
308
+ const max = (_a = p.maxParticles) !== null && _a !== void 0 ? _a : 500;
309
+ if (this.particles.length >= max)
310
+ return;
311
+ if (p.explode != null && !this.explodeDone) {
312
+ const n = Math.min(p.explode, max - this.particles.length);
313
+ for (let i = 0; i < n; i++)
314
+ this.emitOne();
315
+ this.explodeDone = true;
316
+ return;
317
+ }
318
+ if (p.explode != null)
319
+ return;
320
+ if (p.auto === false)
321
+ return;
322
+ this.emitTimer += dt;
323
+ const freq = (_b = p.frequency) !== null && _b !== void 0 ? _b : 250;
324
+ const quantity = (_c = p.quantity) !== null && _c !== void 0 ? _c : 1;
325
+ while (this.emitTimer >= freq && this.particles.length < max) {
326
+ for (let i = 0; i < quantity && this.particles.length < max; i++) {
327
+ if (p.stopAfter != null && this.emittedTotal >= p.stopAfter)
328
+ break;
329
+ this.emitOne();
330
+ }
331
+ this.emitTimer -= freq;
332
+ }
333
+ }
334
+ emitOne() {
335
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
336
+ const p = this.params;
337
+ const pos = samplePosition(p.emitZone);
338
+ const speedScalar = sampleRange(p.speed, 0);
339
+ let vx;
340
+ let vy;
341
+ if (p.moveTo) {
342
+ const dx = p.moveTo.x - pos.x;
343
+ const dy = p.moveTo.y - pos.y;
344
+ const len = Math.hypot(dx, dy) || 1;
345
+ vx = (dx / len) * (speedScalar || 100);
346
+ vy = (dy / len) * (speedScalar || 100);
347
+ }
348
+ else if (p.speedX != null || p.speedY != null) {
349
+ vx = sampleRange(p.speedX, 0);
350
+ vy = sampleRange(p.speedY, 0);
351
+ }
352
+ else {
353
+ const angleDeg = sampleRange(p.angle, 0);
354
+ const angleRad = (angleDeg * Math.PI) / 180;
355
+ vx = Math.cos(angleRad) * speedScalar;
356
+ vy = Math.sin(angleRad) * speedScalar;
357
+ }
358
+ const ax = sampleRange(p.accelerationX, 0);
359
+ const ay = sampleRange(p.accelerationY, 0);
360
+ const scaleX = p.scaleX != null ? sampleRange(p.scaleX, 1) : sampleRange(p.scale, 1);
361
+ const scaleY = p.scaleY != null ? sampleRange(p.scaleY, 1) : sampleRange(p.scale, 1);
362
+ const alpha = sampleRange(p.alpha, 1);
363
+ const rotation = sampleRange(p.rotate, 0);
364
+ const tintNum = Array.isArray(p.tint) ? p.tint[Math.floor(Math.random() * p.tint.length)] : (_a = p.tint) !== null && _a !== void 0 ? _a : 0xffffff;
365
+ const tex = this.frameTextures
366
+ ? this.frameTextures[Math.floor(Math.random() * this.frameTextures.length)]
367
+ : this.texture;
368
+ const particle = new pixi_js.Particle({
369
+ texture: tex,
370
+ x: pos.x,
371
+ y: pos.y,
372
+ scaleX,
373
+ scaleY,
374
+ alpha,
375
+ rotation,
376
+ tint: tintNum,
377
+ anchorX: 0.5,
378
+ anchorY: 0.5,
379
+ });
380
+ const lp = {
381
+ particle,
382
+ age: 0,
383
+ lifespan: sampleRange(p.lifespan, 1000),
384
+ vx,
385
+ vy,
386
+ ax,
387
+ ay,
388
+ rotateSpeed: 0,
389
+ };
390
+ const scaleBounds = getRangeBounds(p.scale);
391
+ const scaleXBounds = getRangeBounds(p.scaleX);
392
+ const scaleYBounds = getRangeBounds(p.scaleY);
393
+ if (scaleBounds || scaleXBounds || scaleYBounds) {
394
+ const sxs = (_c = (_b = scaleXBounds === null || scaleXBounds === void 0 ? void 0 : scaleXBounds.start) !== null && _b !== void 0 ? _b : scaleBounds === null || scaleBounds === void 0 ? void 0 : scaleBounds.start) !== null && _c !== void 0 ? _c : scaleX;
395
+ const sxe = (_e = (_d = scaleXBounds === null || scaleXBounds === void 0 ? void 0 : scaleXBounds.end) !== null && _d !== void 0 ? _d : scaleBounds === null || scaleBounds === void 0 ? void 0 : scaleBounds.end) !== null && _e !== void 0 ? _e : scaleX;
396
+ const sys = (_g = (_f = scaleYBounds === null || scaleYBounds === void 0 ? void 0 : scaleYBounds.start) !== null && _f !== void 0 ? _f : scaleBounds === null || scaleBounds === void 0 ? void 0 : scaleBounds.start) !== null && _g !== void 0 ? _g : scaleY;
397
+ const sye = (_j = (_h = scaleYBounds === null || scaleYBounds === void 0 ? void 0 : scaleYBounds.end) !== null && _h !== void 0 ? _h : scaleBounds === null || scaleBounds === void 0 ? void 0 : scaleBounds.end) !== null && _j !== void 0 ? _j : scaleY;
398
+ lp.scaleStart = { x: sxs, y: sys };
399
+ lp.scaleEnd = { x: sxe, y: sye };
400
+ lp.scaleEase = resolveEase((_k = scaleXBounds === null || scaleXBounds === void 0 ? void 0 : scaleXBounds.ease) !== null && _k !== void 0 ? _k : scaleBounds === null || scaleBounds === void 0 ? void 0 : scaleBounds.ease);
401
+ particle.scaleX = sxs;
402
+ particle.scaleY = sys;
403
+ }
404
+ const alphaBounds = getRangeBounds(p.alpha);
405
+ if (alphaBounds) {
406
+ lp.alphaStart = alphaBounds.start;
407
+ lp.alphaEnd = alphaBounds.end;
408
+ lp.alphaEase = resolveEase(alphaBounds.ease);
409
+ particle.alpha = alphaBounds.start;
410
+ }
411
+ this.particles.push(lp);
412
+ this.container.particleChildren.push(particle);
413
+ this.emittedTotal++;
414
+ }
415
+ }
416
+
417
+ const SPRITE_KEY_SEP = '_s|r|c_'; // Sync with plugin-renderer-sprite resourceKeySplit.
418
+ /**
419
+ * Resolve resource instance to a primary texture + optional frame texture pool.
420
+ * - IMAGE resource → instance is a Texture, return single.
421
+ * - SPRITE resource → instance is `{[fullKey]: Texture}`. Pick frames listed in `frame`
422
+ * (string or array); fall back to first frame as the primary texture.
423
+ */
424
+ /**
425
+ * Check if a value is a PixiJS Texture. Uses duck typing because separate copies of pixi.js
426
+ * may be loaded across plugins (ESM module duplication breaks `instanceof`).
427
+ */
428
+ function isTexture(v) {
429
+ return !!v && v.isTexture === true && !!v.orig && !!v.uvs;
430
+ }
431
+ function pickFrameTextures(instance, resourceName, frame) {
432
+ if (isTexture(instance)) {
433
+ return { texture: instance, frameTextures: null };
434
+ }
435
+ if (instance && typeof instance === 'object') {
436
+ const lookup = (name) => {
437
+ var _a;
438
+ const fullKey = resourceName + SPRITE_KEY_SEP + name;
439
+ const v = (_a = instance[fullKey]) !== null && _a !== void 0 ? _a : instance[name];
440
+ return isTexture(v) ? v : null;
441
+ };
442
+ const frameNames = Array.isArray(frame) ? frame : frame ? [frame] : [];
443
+ const frameTextures = [];
444
+ for (const f of frameNames) {
445
+ const t = lookup(f);
446
+ if (t)
447
+ frameTextures.push(t);
448
+ }
449
+ if (frameTextures.length > 0) {
450
+ return { texture: frameTextures[0], frameTextures };
451
+ }
452
+ // No frame requested → fall back to first texture in the map.
453
+ for (const k in instance) {
454
+ const v = instance[k];
455
+ if (isTexture(v))
456
+ return { texture: v, frameTextures: null };
457
+ }
458
+ }
459
+ return { texture: null, frameTextures: null };
460
+ }
461
+ let ParticleEmitterSystem = class ParticleEmitterSystem extends pluginRenderer.Renderer {
462
+ constructor() {
463
+ super(...arguments);
464
+ this.name = 'ParticleEmitter';
465
+ this.records = {};
466
+ this.lastDeltaTime = 1000 / 60;
467
+ }
468
+ init() {
469
+ this.renderSystem = this.game.getSystem(pluginRenderer.RendererSystem);
470
+ this.renderSystem.rendererManager.register(this);
471
+ }
472
+ update(e) {
473
+ if (e === null || e === void 0 ? void 0 : e.deltaTime)
474
+ this.lastDeltaTime = e.deltaTime;
475
+ super.update(e);
476
+ }
477
+ rendererUpdate(gameObject) {
478
+ const record = this.records[gameObject.id];
479
+ if (!record)
480
+ return;
481
+ record.emitter.setParams(record.component);
482
+ record.emitter.update(this.lastDeltaTime);
483
+ }
484
+ componentChanged(changed) {
485
+ var _a, _b, _c, _d;
486
+ return __awaiter(this, void 0, void 0, function* () {
487
+ if (changed.componentName !== 'ParticleEmitter')
488
+ return;
489
+ const component = changed.component;
490
+ const gameObjectId = changed.gameObject.id;
491
+ if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
492
+ const asyncId = this.increaseAsyncId(gameObjectId);
493
+ let texture = null;
494
+ let frameTextures = null;
495
+ if (component.resource) {
496
+ const { instance } = yield eva_js.resource.getResource(component.resource);
497
+ if (!this.validateAsyncId(gameObjectId, asyncId))
498
+ return;
499
+ if (!instance) {
500
+ console.error(`GameObject:${changed.gameObject.name}'s ParticleEmitter resource load error`);
501
+ return;
502
+ }
503
+ const picked = pickFrameTextures(instance, component.resource, component.frame);
504
+ texture = picked.texture;
505
+ frameTextures = picked.frameTextures;
506
+ }
507
+ if (!texture)
508
+ return;
509
+ const pc = new pixi_js.ParticleContainer({
510
+ dynamicProperties: { position: true, rotation: true, scale: true, color: true, uvs: true },
511
+ });
512
+ pc.texture = texture;
513
+ this.containerManager.getContainer(gameObjectId).addChildAt(pc, 0);
514
+ const emitter = new Emitter(component, pc, texture);
515
+ emitter.setFrameTextures(frameTextures);
516
+ this.records[gameObjectId] = { container: pc, emitter, component };
517
+ }
518
+ else if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) {
519
+ const record = this.records[gameObjectId];
520
+ if (!record)
521
+ return;
522
+ if (((_b = (_a = changed.prop) === null || _a === void 0 ? void 0 : _a.prop) === null || _b === void 0 ? void 0 : _b[0]) === 'resource') {
523
+ const asyncId = this.increaseAsyncId(gameObjectId);
524
+ const { instance } = yield eva_js.resource.getResource(component.resource);
525
+ if (!this.validateAsyncId(gameObjectId, asyncId))
526
+ return;
527
+ if (instance) {
528
+ const picked = pickFrameTextures(instance, component.resource, component.frame);
529
+ if (picked.texture) {
530
+ record.container.texture = picked.texture;
531
+ record.emitter.setTexture(picked.texture);
532
+ }
533
+ record.emitter.setFrameTextures(picked.frameTextures);
534
+ }
535
+ }
536
+ record.component = component;
537
+ record.emitter.setParams(component);
538
+ }
539
+ else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
540
+ this.increaseAsyncId(gameObjectId);
541
+ const record = this.records[gameObjectId];
542
+ if (!record)
543
+ return;
544
+ record.emitter.destroy();
545
+ (_d = (_c = this.containerManager) === null || _c === void 0 ? void 0 : _c.getContainer(gameObjectId)) === null || _d === void 0 ? void 0 : _d.removeChild(record.container);
546
+ record.container.destroy({ children: true });
547
+ delete this.records[gameObjectId];
548
+ }
549
+ });
550
+ }
551
+ destroy() {
552
+ var _a, _b;
553
+ for (const key in this.records) {
554
+ const id = parseInt(key);
555
+ const record = this.records[id];
556
+ record.emitter.destroy();
557
+ (_b = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(id)) === null || _b === void 0 ? void 0 : _b.removeChild(record.container);
558
+ record.container.destroy({ children: true });
559
+ delete this.records[id];
560
+ }
561
+ }
562
+ };
563
+ ParticleEmitterSystem.systemName = 'ParticleEmitter';
564
+ ParticleEmitterSystem = __decorate([
565
+ eva_js.decorators.componentObserver({
566
+ ParticleEmitter: [{ prop: ['resource'], deep: false }],
567
+ })
568
+ ], ParticleEmitterSystem);
569
+ var ParticleEmitterSystem$1 = ParticleEmitterSystem;
570
+
571
+ exports.Emitter = Emitter;
572
+ exports.ParticleEmitter = ParticleEmitter;
573
+ exports.ParticleEmitterSystem = ParticleEmitterSystem$1;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/inspector-decorator"),r=require("@eva/plugin-renderer"),i=require("pixi.js");function n(e,t,r,i){var n,a=arguments.length,s=a<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,i);else for(var o=e.length-1;o>=0;o--)(n=e[o])&&(s=(a<3?n(s):a>3?n(t,r,s):n(t,r))||s);return a>3&&s&&Object.defineProperty(t,r,s),s}function a(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function s(e,t,r,i){return new(r||(r=Promise))(function(n,a){function s(e){try{l(i.next(e))}catch(e){a(e)}}function o(e){try{l(i.throw(e))}catch(e){a(e)}}function l(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(s,o)}l((i=i.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class o extends e.Component{constructor(){super(...arguments),this.resource="",this.auto=!0,this.duration=-1,this.frequency=250,this.quantity=1,this.maxParticles=500,this.lifespan=1e3,this.gravityX=0,this.gravityY=0,this.paused=!1}init(e){e&&Object.assign(this,e)}stop(){this.paused=!0}start(){this.paused=!1}}o.componentName="ParticleEmitter",n([t.type("string"),a("design:type",String)],o.prototype,"resource",void 0),n([t.type("boolean"),a("design:type",Boolean)],o.prototype,"auto",void 0),n([t.type("number"),a("design:type",Number)],o.prototype,"duration",void 0),n([t.type("number"),a("design:type",Number)],o.prototype,"frequency",void 0),n([t.type("number"),a("design:type",Number)],o.prototype,"quantity",void 0),n([t.type("number"),a("design:type",Number)],o.prototype,"maxParticles",void 0),n([t.type("number"),a("design:type",Number)],o.prototype,"gravityX",void 0),n([t.type("number"),a("design:type",Number)],o.prototype,"gravityY",void 0);const l=2*Math.PI,c={linear:e=>e,"sine.in":e=>1-Math.cos(e*Math.PI/2),"sine.out":e=>Math.sin(e*Math.PI/2),"sine.inout":e=>-(Math.cos(Math.PI*e)-1)/2,"quad.in":e=>e*e,"quad.out":e=>1-(1-e)*(1-e),"quad.inout":e=>e<.5?2*e*e:1-Math.pow(-2*e+2,2)/2,"cubic.in":e=>e*e*e,"cubic.out":e=>1-Math.pow(1-e,3),"expo.out":e=>1===e?1:1-Math.pow(2,-10*e)};function u(e){if(!e)return c.linear;const t=e.toLowerCase();return c[t]||c.linear}function d(e,t){return e+Math.random()*(t-e)}function h(e,t=0){return null==e?t:"number"==typeof e?e:Array.isArray(e)?e[Math.floor(Math.random()*e.length)]:"min"in e&&"max"in e?d(e.min,e.max):"start"in e&&"end"in e?e.start:t}function p(e){return null==e||"number"==typeof e||Array.isArray(e)?null:"start"in e&&"end"in e?e:null}function m(e){return e?function(e,t){var r,i,n,a,s,o,c,u;switch(e.type){case"point":return{x:null!==(r=e.x)&&void 0!==r?r:0,y:null!==(i=e.y)&&void 0!==i?i:0};case"rect":{const r=null!==(n=e.x)&&void 0!==n?n:0,i=null!==(a=e.y)&&void 0!==a?a:0;if(t){const t=2*(e.width+e.height),n=Math.random()*t;return n<e.width?{x:r+n,y:i}:n<e.width+e.height?{x:r+e.width,y:i+(n-e.width)}:n<2*e.width+e.height?{x:r+e.width-(n-e.width-e.height),y:i+e.height}:{x:r,y:i+e.height-(n-2*e.width-e.height)}}return{x:d(r,r+e.width),y:d(i,i+e.height)}}case"circle":{const r=null!==(s=e.x)&&void 0!==s?s:0,i=null!==(o=e.y)&&void 0!==o?o:0,n=Math.random()*l,a=t?e.radius:e.radius*Math.sqrt(Math.random());return{x:r+Math.cos(n)*a,y:i+Math.sin(n)*a}}case"ellipse":{const r=null!==(c=e.x)&&void 0!==c?c:0,i=null!==(u=e.y)&&void 0!==u?u:0,n=Math.random()*l,a=t?1:Math.sqrt(Math.random());return{x:r+Math.cos(n)*e.rx*a,y:i+Math.sin(n)*e.ry*a}}case"line":{const t=Math.random();return{x:e.x1+(e.x2-e.x1)*t,y:e.y1+(e.y2-e.y1)*t}}}}(e.shape,"edge"===e.type):{x:0,y:0}}class y{constructor(e,t,r){this.frameTextures=null,this.particles=[],this.elapsed=0,this.emittedTotal=0,this.emitTimer=0,this.explodeDone=!1,this.params=e,this.container=t,this.texture=r}setTexture(e){this.texture=e}setFrameTextures(e){this.frameTextures=e&&e.length>0?e:null}setParams(e){this.params=e}destroy(){this.particles.length=0,this.container.particleChildren.length=0,this.container.update()}update(e){this.elapsed+=e,this.advanceParticles(e),this.spawn(e),this.container.update()}advanceParticles(e){var t,r;const i=e/1e3,n=[];for(const a of this.particles){if(a.age+=e,a.age>a.lifespan)continue;a.vx+=a.ax*i+(null!==(t=this.params.gravityX)&&void 0!==t?t:0)*i,a.vy+=a.ay*i+(null!==(r=this.params.gravityY)&&void 0!==r?r:0)*i,a.particle.x+=a.vx*i,a.particle.y+=a.vy*i,a.particle.rotation+=a.rotateSpeed*i;const s=a.age/a.lifespan;if(a.scaleStart&&a.scaleEnd){const e=(a.scaleEase||c.linear)(s);a.particle.scaleX=a.scaleStart.x+(a.scaleEnd.x-a.scaleStart.x)*e,a.particle.scaleY=a.scaleStart.y+(a.scaleEnd.y-a.scaleStart.y)*e}if(null!=a.alphaStart&&null!=a.alphaEnd){const e=(a.alphaEase||c.linear)(s);a.particle.alpha=a.alphaStart+(a.alphaEnd-a.alphaStart)*e}this.params.deathZone&&this.shouldDie(a.particle.x,a.particle.y)||n.push(a)}this.particles=n,this.container.particleChildren.length=0;for(const e of this.particles)this.container.particleChildren.push(e.particle)}shouldDie(e,t){const r=this.params.deathZone;if(!r)return!1;const i=function(e,t,r){var i,n,a,s,o,l;switch(e.type){case"rect":{const a=null!==(i=e.x)&&void 0!==i?i:0,s=null!==(n=e.y)&&void 0!==n?n:0;return t>=a&&t<=a+e.width&&r>=s&&r<=s+e.height}case"circle":{const i=t-(null!==(a=e.x)&&void 0!==a?a:0),n=r-(null!==(s=e.y)&&void 0!==s?s:0);return i*i+n*n<=e.radius*e.radius}case"ellipse":{const i=(t-(null!==(o=e.x)&&void 0!==o?o:0))/e.rx,n=(r-(null!==(l=e.y)&&void 0!==l?l:0))/e.ry;return i*i+n*n<=1}case"point":case"line":return!1}}(r.shape,e,t);return"onLeave"===r.mode?!i:i}spawn(e){var t,r,i;const n=this.params;if(null!=n.duration&&n.duration>0&&this.elapsed>n.duration)return;if(null!=n.stopAfter&&this.emittedTotal>=n.stopAfter)return;if(n.paused)return;const a=null!==(t=n.maxParticles)&&void 0!==t?t:500;if(this.particles.length>=a)return;if(null!=n.explode&&!this.explodeDone){const e=Math.min(n.explode,a-this.particles.length);for(let t=0;t<e;t++)this.emitOne();return void(this.explodeDone=!0)}if(null!=n.explode)return;if(!1===n.auto)return;this.emitTimer+=e;const s=null!==(r=n.frequency)&&void 0!==r?r:250,o=null!==(i=n.quantity)&&void 0!==i?i:1;for(;this.emitTimer>=s&&this.particles.length<a;){for(let e=0;e<o&&this.particles.length<a&&!(null!=n.stopAfter&&this.emittedTotal>=n.stopAfter);e++)this.emitOne();this.emitTimer-=s}}emitOne(){var e,t,r,n,a,s,o,l,c,d;const y=this.params,f=m(y.emitZone),v=h(y.speed,0);let x,g;if(y.moveTo){const e=y.moveTo.x-f.x,t=y.moveTo.y-f.y,r=Math.hypot(e,t)||1;x=e/r*(v||100),g=t/r*(v||100)}else if(null!=y.speedX||null!=y.speedY)x=h(y.speedX,0),g=h(y.speedY,0);else{const e=h(y.angle,0)*Math.PI/180;x=Math.cos(e)*v,g=Math.sin(e)*v}const M=h(y.accelerationX,0),T=h(y.accelerationY,0),E=null!=y.scaleX?h(y.scaleX,1):h(y.scale,1),P=null!=y.scaleY?h(y.scaleY,1):h(y.scale,1),b=h(y.alpha,1),w=h(y.rotate,0),S=Array.isArray(y.tint)?y.tint[Math.floor(Math.random()*y.tint.length)]:null!==(e=y.tint)&&void 0!==e?e:16777215,A=this.frameTextures?this.frameTextures[Math.floor(Math.random()*this.frameTextures.length)]:this.texture,R=new i.Particle({texture:A,x:f.x,y:f.y,scaleX:E,scaleY:P,alpha:b,rotation:w,tint:S,anchorX:.5,anchorY:.5}),O={particle:R,age:0,lifespan:h(y.lifespan,1e3),vx:x,vy:g,ax:M,ay:T,rotateSpeed:0},Y=p(y.scale),q=p(y.scaleX),C=p(y.scaleY);if(Y||q||C){const e=null!==(r=null!==(t=null==q?void 0:q.start)&&void 0!==t?t:null==Y?void 0:Y.start)&&void 0!==r?r:E,i=null!==(a=null!==(n=null==q?void 0:q.end)&&void 0!==n?n:null==Y?void 0:Y.end)&&void 0!==a?a:E,h=null!==(o=null!==(s=null==C?void 0:C.start)&&void 0!==s?s:null==Y?void 0:Y.start)&&void 0!==o?o:P,p=null!==(c=null!==(l=null==C?void 0:C.end)&&void 0!==l?l:null==Y?void 0:Y.end)&&void 0!==c?c:P;O.scaleStart={x:e,y:h},O.scaleEnd={x:i,y:p},O.scaleEase=u(null!==(d=null==q?void 0:q.ease)&&void 0!==d?d:null==Y?void 0:Y.ease),R.scaleX=e,R.scaleY=h}const X=p(y.alpha);X&&(O.alphaStart=X.start,O.alphaEnd=X.end,O.alphaEase=u(X.ease),R.alpha=X.start),this.particles.push(O),this.container.particleChildren.push(R),this.emittedTotal++}}function f(e){return!!e&&!0===e.isTexture&&!!e.orig&&!!e.uvs}function v(e,t,r){if(f(e))return{texture:e,frameTextures:null};if(e&&"object"==typeof e){const i=r=>{var i;const n=null!==(i=e[t+"_s|r|c_"+r])&&void 0!==i?i:e[r];return f(n)?n:null},n=Array.isArray(r)?r:r?[r]:[],a=[];for(const e of n){const t=i(e);t&&a.push(t)}if(a.length>0)return{texture:a[0],frameTextures:a};for(const t in e){const r=e[t];if(f(r))return{texture:r,frameTextures:null}}}return{texture:null,frameTextures:null}}let x=class extends r.Renderer{constructor(){super(...arguments),this.name="ParticleEmitter",this.records={},this.lastDeltaTime=1e3/60}init(){this.renderSystem=this.game.getSystem(r.RendererSystem),this.renderSystem.rendererManager.register(this)}update(e){(null==e?void 0:e.deltaTime)&&(this.lastDeltaTime=e.deltaTime),super.update(e)}rendererUpdate(e){const t=this.records[e.id];t&&(t.emitter.setParams(t.component),t.emitter.update(this.lastDeltaTime))}componentChanged(t){var r,n,a,o;return s(this,void 0,void 0,function*(){if("ParticleEmitter"!==t.componentName)return;const s=t.component,l=t.gameObject.id;if(t.type===e.OBSERVER_TYPE.ADD){const r=this.increaseAsyncId(l);let n=null,a=null;if(s.resource){const{instance:i}=yield e.resource.getResource(s.resource);if(!this.validateAsyncId(l,r))return;if(!i)return void console.error(`GameObject:${t.gameObject.name}'s ParticleEmitter resource load error`);const o=v(i,s.resource,s.frame);n=o.texture,a=o.frameTextures}if(!n)return;const o=new i.ParticleContainer({dynamicProperties:{position:!0,rotation:!0,scale:!0,color:!0,uvs:!0}});o.texture=n,this.containerManager.getContainer(l).addChildAt(o,0);const c=new y(s,o,n);c.setFrameTextures(a),this.records[l]={container:o,emitter:c,component:s}}else if(t.type===e.OBSERVER_TYPE.CHANGE){const i=this.records[l];if(!i)return;if("resource"===(null===(n=null===(r=t.prop)||void 0===r?void 0:r.prop)||void 0===n?void 0:n[0])){const t=this.increaseAsyncId(l),{instance:r}=yield e.resource.getResource(s.resource);if(!this.validateAsyncId(l,t))return;if(r){const e=v(r,s.resource,s.frame);e.texture&&(i.container.texture=e.texture,i.emitter.setTexture(e.texture)),i.emitter.setFrameTextures(e.frameTextures)}}i.component=s,i.emitter.setParams(s)}else if(t.type===e.OBSERVER_TYPE.REMOVE){this.increaseAsyncId(l);const e=this.records[l];if(!e)return;e.emitter.destroy(),null===(o=null===(a=this.containerManager)||void 0===a?void 0:a.getContainer(l))||void 0===o||o.removeChild(e.container),e.container.destroy({children:!0}),delete this.records[l]}})}destroy(){var e,t;for(const r in this.records){const i=parseInt(r),n=this.records[i];n.emitter.destroy(),null===(t=null===(e=this.containerManager)||void 0===e?void 0:e.getContainer(i))||void 0===t||t.removeChild(n.container),n.container.destroy({children:!0}),delete this.records[i]}}};x.systemName="ParticleEmitter",x=n([e.decorators.componentObserver({ParticleEmitter:[{prop:["resource"],deep:!1}]})],x);var g=x;exports.Emitter=y,exports.ParticleEmitter=o,exports.ParticleEmitterSystem=g;