/** * 手电筒激光武器持续攻击 */ import { _decorator, Component, isValid, Node, sp, UITransform, Vec3 } from 'cc'; import { IWeaponProperty } from '../BattleDataManager'; import { BattleType } from '../../../manager/ChapterDataManager'; import { Monster } from '../Monster'; const { ccclass, property } = _decorator; @ccclass('FlashlightSchedule') export class FlashlightSchedule extends Component { _dt: number = 0; _dtAll: number = 0; //累计时间 _weaponid: number = 0; _damageInterval: number = 1; //伤害间隔 _weaponContinueTime: number = 0; //持续时间 isCanAtk: boolean = false; //是否可以攻击造成伤害 isStartGame: boolean = false; //是否开始游戏 _updatePosiTime: number = 0//0.05; //更新位置时间 _updatePosiInterval: number = 0.03; //更新位置间隔 private _scale: number; private _weaponLaunchNode: Node | null = null; protected onLoad(): void { //console.log('手电筒激光武器持续攻击 onLoad') this.isStartGame = false this.isCanAtk = false } start() { //console.log('手电筒激光武器持续攻击 start') } update(deltaTime: number) { //this.node.worldPosition = gg.game.CurentBattle.playerController.playerNode.getChildByName('weaponLaunchNode').worldPosition.clone() if (!gg.game.CurentBattle.canUpdateFrame()) return; const scaledDt = deltaTime * gg.game.CurentBattle.TimeScale; // 与伤害 CD、持续时间同一套时间,避免 TimeScale 下不同步 this._updatePosiTime += scaledDt; if (this._updatePosiTime >= this._updatePosiInterval) { this._updatePosiTime = 0; if (!this._weaponLaunchNode || !this._weaponLaunchNode.isValid) { this._weaponLaunchNode = gg.game.CurentBattle.PlayerController.playerNode.getChildByName('weaponLaunchNode'); } const launchPos = this._weaponLaunchNode!.worldPosition; this.node.setWorldPosition(launchPos.x, launchPos.y, launchPos.z); // 仅同步位置;伤害只由 _damageInterval 触发,避免与 CD 同一帧打两次 } if (!this.isStartGame) { return; } this._dtAll += scaledDt; this._dt += scaledDt; if (this._dtAll >= this._weaponContinueTime) { this.isStartGame = false; this.isCanAtk = false; this.destroyNode(); } else if (this._damageInterval > 0 && this._dt >= this._damageInterval) { this._dt -= this._damageInterval; this.isCanAtk = true; this.attackMonstersInFlashlightRange(); } //可以攻击的状态,间隔多少秒刷新, 持续时间到了会自动销毁 } destroyNode() { this.unscheduleAllCallbacks(); //移除当前脚本 this.removeSelf(); gg.res.putNode(this.node); } /**刷新激光武器ui */ removeSelf() { this.destroy() } onDestroy() { this.unscheduleAllCallbacks(); // console.log('LaserContinueSchedule组件已被移除'); } atkToMonster(weaponId: number, index: number, weaponProperty: IWeaponProperty, scale: number, isGold) { //console.log('手电筒激光武器持续攻击 atkToMonster') this.unscheduleAllCallbacks(); this.isStartGame = false; this._dtAll = 0; this._updatePosiTime = 0; this._weaponid = weaponId; this._scale = scale; this.isCanAtk = false; this._damageInterval = weaponProperty.damageInterval; this._weaponContinueTime = weaponProperty.weaponContinueTime; let playAnimStart = this.node.getChildByName('手电筒技能特效3'); this.node.getChildByName('手电筒技能特效1').active = false this.node.getChildByName('手电筒技能特效2').active = false playAnimStart.active = true playAnimStart.getComponent(sp.Skeleton).setAnimation(0, '效果示意', false) if (isGold) { playAnimStart.getComponent(sp.Skeleton).setSkin('橙色手电筒') } else { playAnimStart.getComponent(sp.Skeleton).setSkin('蓝色手电筒') } this.scheduleOnce(() => { if (!isValid(this.node)) return; playAnimStart.active = false; if (isGold) { if (index == 1) { this.node.getChildByName('手电筒技能特效1').active = true; this.node.getChildByName('手电筒技能特效2').active = true; } else { this.node.getChildByName('手电筒技能特效2').active = true; } } else { this.node.getChildByName('手电筒技能特效1').active = true; this.node.getChildByName('手电筒技能特效2').active = true; } if (isGold) { this.node.getChildByName('手电筒技能特效1').getComponent(sp.Skeleton).setSkin('橙色手电筒'); this.node.getChildByName('手电筒技能特效2').getComponent(sp.Skeleton).setSkin('橙色手电筒'); } else { this.node.getChildByName('手电筒技能特效1').getComponent(sp.Skeleton).setSkin('蓝色手电筒'); this.node.getChildByName('手电筒技能特效2').getComponent(sp.Skeleton).setSkin('蓝色手电筒'); } }, 1.5); this.scheduleOnce(() => { if (!isValid(this.node)) return; //可以开始攻击 this.isStartGame = true; this.isCanAtk = true; // 与首开对齐:下一帧 _dt 累加后即可满足 >= _damageInterval(原为位置分支抢先打第一下) this._dt = this._damageInterval; }, 1.2); //this.isCanAtk = true this._dt = 0; //console.log('手电筒激光武器攻击怪物atkToMonster',weaponId,index,this._damageInterval,this._weaponContinueTime) } attackMonstersInFlashlightRange() { //找出怪物坐标在激光武器范围的怪物 if (!this.isCanAtk) return; let selectMonster = gg.game.CurentBattle.MonsterSpawner.getFlashlighttMonster(this.node, this._scale) //console.log('手电筒激光武器攻击怪物',selectMonster.length) if (selectMonster.length > 0) { for (let i = 0; i < selectMonster.length; i++) { selectMonster[i].getComponent(Monster).attackByWeapon(this._weaponid) } } // 勿与武器发射共用 audio_timeStamp;节流由 SoundManager.shouldPlayWeaponSfx 按名处理 gg.audio.playEffect({ name: '彩虹飞弹命中', path: 'sound/武器音效/' }); this.isCanAtk = false; //this._targetMonster.getComponent(Monster).attackByWeapon(this._weaponid) } }