/** * 闪电劈障碍物 */ import { _decorator, Component, sp, Vec3 } from 'cc'; import { ChapterNewPlayTypeEnum } from '../../../game/ChapterPlayTypes'; import { GEvent } from 'db://assets/mx/module/event/GEvent'; const { ccclass, property } = _decorator; @ccclass('MechanismThunderSchedule') export class MechanismThunderSchedule extends Component { _dt: number = 0 start() { } protected update(dt: number): void { if (!gg.game.CurentBattle.canUpdateFrame()) return; this._dt += dt * gg.game.CurentBattle.TimeScale; if (this._dt >= 1) { this._dt = 0 this.checkThunderLaunchTime() } } /**检查是否到了攻击时间 */ checkThunderLaunchTime() { let thunderRemoveObstacleTimeArr = gg.data.project.getChapterNewPlayInfo(ChapterNewPlayTypeEnum.ThunderRemoveObstacle) if (thunderRemoveObstacleTimeArr && thunderRemoveObstacleTimeArr.length > 0 && gg.game.CurentBattle.BattleTime >= thunderRemoveObstacleTimeArr[0]) { thunderRemoveObstacleTimeArr.shift() this.atkToObstacles() } } /**攻击障碍物 */ atkToObstacles() { // 勿在雷云前摇阶段锁操作:此前会 IsLaunchThunder=true 约 4s,且与全图寻路同帧叠在一起易卡死主线程 let thunder = this.node.getChildByName('雷云') thunder.active = true thunder.getComponent(sp.Skeleton).setAnimation(0, '雷云出现', false) this.scheduleOnce(() => { thunder.getComponent(sp.Skeleton).setAnimation(0, '雷云', true) let randomObs = gg.game.CurentBattle.Map.randomThunderObstacle(); if (randomObs) { // gg.game.CurentBattle.IsLaunchThunder = true let startNode = this.node.getChildByName('雷起点') let endNode = this.node.getChildByName('雷末点') let lightNode = this.node.getChildByName('雷光柱') startNode.active = true endNode.active = true lightNode.active = true startNode.getComponent(sp.Skeleton).setAnimation(0, '发射点', false) endNode.getComponent(sp.Skeleton).setAnimation(0, '击中点', false) lightNode.getComponent(sp.Skeleton).setAnimation(0, '闪电柱', false) let pos = randomObs.node.position.clone(); startNode.position = new Vec3(pos.x, 520, 0) lightNode.position = new Vec3(pos.x, 520, 0) const totalDistance = Vec3.distance(startNode.worldPosition, randomObs.node.worldPosition); lightNode.scale = new Vec3(1, totalDistance / 500, 1) endNode.worldPosition = randomObs.node.worldPosition.clone(); // 下一帧再刷新全怪物路径:避免与 remove→重建距离场、Spine 同帧挤爆主线程 this.scheduleOnce(() => { GEvent.Ins.emit(GEvent.updateAgentPath); }, 0); this.scheduleOnce(() => { startNode.active = false endNode.active = false lightNode.active = false }, 1) } this.scheduleOnce(() => { // gg.game.CurentBattle.IsLaunchThunder = false thunder.getComponent(sp.Skeleton).setAnimation(0, '雷云消失', false) }, 1) }, 1.5) } protected onDisable(): void { // 防止组件回收后 scheduleOnce 回调继续持有引用 this.unscheduleAllCallbacks(); } protected onDestroy(): void { this.unscheduleAllCallbacks(); } }