import { _decorator, Component, Node } from 'cc'; import EventBus, { GameEvents } from '../../Core/EventBus'; const { ccclass, property } = _decorator; /** * 灼烧效果组件 * 用于管理敌人身上的持续灼烧效果 */ @ccclass('BurnEffect') export class BurnEffect extends Component { private _duration: number = 0; private _originalDuration: number = 0; private _damage: number = 0; private _tickInterval: number = 0; private _burnTimerId: string = ''; private _isActive: boolean = false; private _burnEffectNode: Node | null = null; // 灼烧特效节点 /** * 初始化灼烧效果 * @param duration 持续时间(秒) * @param damage 每次伤害值 * @param tickInterval 伤害间隔(秒) */ public initBurnEffect(duration: number, damage: number, tickInterval: number): void { this._duration = duration; this._originalDuration = duration; this._damage = damage; this._tickInterval = tickInterval; this._isActive = true; this._burnTimerId = `burn_${this.node.uuid}_${Date.now()}`; // 启动灼烧伤害循环 this.startBurnDamageLoop(); } /** * 刷新灼烧效果的持续时间 * @param newDuration 新的持续时间 */ public refreshDuration(newDuration: number): void { this._duration = newDuration; this._originalDuration = newDuration; console.log(`[BurnEffect] 刷新灼烧效果持续时间: ${newDuration}秒`); // 如果当前没有激活,重新启动 if (!this._isActive) { this._isActive = true; this.startBurnDamageLoop(); } } /** * 获取剩余持续时间 */ public getRemainingDuration(): number { return this._duration; } /** * 获取伤害值 */ public getDamage(): number { return this._damage; } /** * 获取伤害间隔 */ public getTickInterval(): number { return this._tickInterval; } /** * 减少持续时间 * @param deltaTime 减少的时间 * @returns 是否还有剩余时间 */ public reduceDuration(deltaTime: number): boolean { this._duration -= deltaTime; return this._duration > 0; } /** * 启动灼烧伤害循环 */ private startBurnDamageLoop(): void { if (!this._isActive || this._duration <= 0) { return; } console.log(`[BurnEffect] 启动灼烧伤害循环 - 持续时间: ${this._duration}秒, 伤害间隔: ${this._tickInterval}秒`); // 使用定时器进行伤害循环 this.scheduleOnce(() => { this.processBurnDamage(); }, this._tickInterval); } /** * 处理灼烧伤害 */ private processBurnDamage(): void { // 检查组件和节点是否仍然有效 if (!this._isActive || !this.node || !this.node.isValid) { console.log(`[BurnEffect] 灼烧效果已停止或节点无效`); return; } // 减少持续时间 this._duration -= this._tickInterval; console.log(`[BurnEffect] 处理灼烧伤害 - 伤害: ${this._damage}, 剩余时间: ${this._duration.toFixed(1)}秒`); // 通过事件系统发送伤害 const eventBus = EventBus.getInstance(); const damageData = { enemyNode: this.node, damage: this._damage, isCritical: false, source: 'BurnEffect', burnTimerId: this._burnTimerId }; eventBus.emit(GameEvents.APPLY_DAMAGE_TO_ENEMY, damageData); // 检查是否还有剩余时间 if (this._duration > 0) { // 继续下一次伤害 this.scheduleOnce(() => { this.processBurnDamage(); }, this._tickInterval); } else { // 灼烧效果结束 console.log(`[BurnEffect] 灼烧效果结束,移除组件`); this.stopBurnEffect(); } } /** * 设置灼烧特效节点 */ public setBurnEffectNode(effectNode: Node): void { this._burnEffectNode = effectNode; } /** * 停止灼烧效果 */ public stopBurnEffect(): void { this._isActive = false; this.unscheduleAllCallbacks(); // 销毁灼烧特效节点 if (this._burnEffectNode && this._burnEffectNode.isValid) { console.log(`[BurnEffect] 销毁灼烧特效节点`); this._burnEffectNode.destroy(); this._burnEffectNode = null; } // 通过事件通知灼烧效果结束 const eventBus = EventBus.getInstance(); eventBus.emit(GameEvents.BURN_EFFECT_ENDED, { enemyNode: this.node, burnTimerId: this._burnTimerId }); console.log(`[BurnEffect] 灼烧效果已停止`); // 移除组件 if (this.node && this.node.isValid) { this.node.removeComponent(BurnEffect); } } /** * 获取灼烧效果是否激活 */ public isActive(): boolean { return this._isActive; } /** * 组件销毁时清理定时器 */ protected onDestroy(): void { this._isActive = false; // 取消所有定时器 this.unscheduleAllCallbacks(); // 清理灼烧特效节点 if (this._burnEffectNode && this._burnEffectNode.isValid) { console.log(`[BurnEffect] 组件销毁时清理灼烧特效节点`); this._burnEffectNode.destroy(); this._burnEffectNode = null; } console.log(`[BurnEffect] 灼烧效果组件已销毁,清理定时器`); } }