mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-05-30 18:39:18 +08:00
特效管理添加自动回收播放完的特效资源到对象池中
This commit is contained in:
@@ -1,30 +1,53 @@
|
||||
/*
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-12-30 19:16:47
|
||||
* @Date: 2022-08-19 15:36:08
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-08-05 09:39:33
|
||||
* @LastEditTime: 2022-08-19 16:38:15
|
||||
*/
|
||||
|
||||
import { Animation, Component, _decorator } from 'cc';
|
||||
import { Animation, Component, ParticleSystem, _decorator } from 'cc';
|
||||
import { EffectSingleCase } from './EffectSingleCase';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('EffectFinishedRelease')
|
||||
/** 动画播放完释放特效 */
|
||||
@ccclass('EffectFinishedRelease')
|
||||
export class EffectFinishedRelease extends Component {
|
||||
@property({ type: Animation, tooltip: '资源对象池类型名' })
|
||||
anim: Animation = null!;
|
||||
private maxDuration: number = 0;
|
||||
|
||||
onLoad() {
|
||||
this.anim.on(Animation.EventType.FINISHED, this.onFinished, this);
|
||||
this.anim.on(Animation.EventType.LASTFRAME, this.onFinished, this);
|
||||
let arrAni: Animation[] = this.node.getComponentsInChildren(Animation);
|
||||
|
||||
arrAni.forEach((element: Animation, idx: number) => {
|
||||
element.play();
|
||||
|
||||
let aniName = element?.defaultClip?.name;
|
||||
if (aniName) {
|
||||
let aniState = element.getState(aniName);
|
||||
if (aniState) {
|
||||
let duration = aniState.duration;
|
||||
this.maxDuration = duration > this.maxDuration ? duration : this.maxDuration;
|
||||
aniState.speed = 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let arrParticle: ParticleSystem[] = this.node.getComponentsInChildren(ParticleSystem);
|
||||
arrParticle.forEach((element: ParticleSystem) => {
|
||||
element.simulationSpeed = 1;
|
||||
// element.clear(); // cc3.6 执行报错
|
||||
element.stop();
|
||||
element.play()
|
||||
|
||||
let duration: number = element.duration;
|
||||
this.maxDuration = duration > this.maxDuration ? duration : this.maxDuration;
|
||||
});
|
||||
}
|
||||
|
||||
protected onEnable() {
|
||||
this.anim.play();
|
||||
this.scheduleOnce(this.onRecovery.bind(this), this.maxDuration);
|
||||
}
|
||||
|
||||
private onFinished() {
|
||||
EffectSingleCase.instance.put(this.node);
|
||||
private onRecovery() {
|
||||
if (this.node.parent) EffectSingleCase.instance.put(this.node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,27 @@
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-10-12 14:00:43
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-07-25 11:51:21
|
||||
* @LastEditTime: 2022-08-19 16:34:48
|
||||
*/
|
||||
|
||||
import { Component, Node, NodePool, Vec3 } from 'cc';
|
||||
import { Component, Node, NodePool, Prefab, Vec3 } from 'cc';
|
||||
import { resLoader } from '../../core/common/loader/ResLoader';
|
||||
import { ViewUtil } from '../../core/utils/ViewUtil';
|
||||
import { EffectFinishedRelease } from './EffectFinishedRelease';
|
||||
|
||||
/** 效果数据 */
|
||||
class EffectData extends Component {
|
||||
type: string = null!;
|
||||
}
|
||||
|
||||
/** 特效参数 */
|
||||
interface IEffectParams {
|
||||
/** 初始位置 */
|
||||
pos?: Vec3,
|
||||
/** 是否播放完成后删除 */
|
||||
isPlayFinishedRelease?: boolean
|
||||
}
|
||||
|
||||
/** 全局单例特效 */
|
||||
export class EffectSingleCase {
|
||||
private static _instance: EffectSingleCase;
|
||||
@@ -24,13 +35,35 @@ export class EffectSingleCase {
|
||||
|
||||
private effects: Map<string, NodePool> = new Map();
|
||||
|
||||
/** 加载资源并现实特效 */
|
||||
loadAndShow(name: string, parent: Node, params?: IEffectParams): Promise<Node> {
|
||||
return new Promise((resolve, reject) => {
|
||||
var np = this.effects.get(name);
|
||||
if (np == undefined) {
|
||||
resLoader.load(name, Prefab, (err: Error | null, prefab: Prefab) => {
|
||||
if (err) {
|
||||
console.error(`名为【${name}】的特效资源加载失败`);
|
||||
return;
|
||||
}
|
||||
|
||||
var node = this.show(name, parent, params);
|
||||
resolve(node);
|
||||
});
|
||||
}
|
||||
else {
|
||||
var node = this.show(name, parent, params);
|
||||
resolve(node);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示预制对象
|
||||
* @param name 预制对象名称
|
||||
* @param parent 父节点
|
||||
* @param pos 位置
|
||||
*/
|
||||
show(name: string, parent: Node, pos?: Vec3): Node {
|
||||
show(name: string, parent: Node, params?: IEffectParams): Node {
|
||||
var np = this.effects.get(name);
|
||||
if (np == null) {
|
||||
np = new NodePool();
|
||||
@@ -41,12 +74,16 @@ export class EffectSingleCase {
|
||||
if (np.size() == 0) {
|
||||
node = ViewUtil.createPrefabNode(name);
|
||||
node.addComponent(EffectData).type = name;
|
||||
if (params && params.isPlayFinishedRelease) {
|
||||
node.addComponent(EffectFinishedRelease);
|
||||
}
|
||||
}
|
||||
else {
|
||||
node = np.get()!;
|
||||
}
|
||||
node.parent = parent;
|
||||
if (pos) node.position = pos;
|
||||
|
||||
if (params && params.pos) node.position = params.pos;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user