/** * 激光武器持续攻击 */ import { _decorator, Component, isValid, Node, UITransform, Vec3 } from 'cc'; import { IWeaponProperty } from '../BattleDataManager'; import { Monster } from '../Monster'; const { ccclass, property } = _decorator; @ccclass('LaserContinueSchedule') export class LaserContinueSchedule extends Component { _dt:number = 0; _weaponid:number = 0; _addBuffInterval:number = 1; _repeatCount:number = 1; curRepeatCount:number = 0; _targetMonster:Node = null;//目标怪物 _laserId:number = 0; lightNode:Node = null; private _startNode: Node | null = null; private _endNode: Node | null = null; private _tempDirX: number = 0; private _tempDirY: number = 0; // 目标移动很小的时候,跳过三角函数/开方计算,降低 update 热度 private _lastTargetWorldX: number = NaN; private _lastTargetWorldY: number = NaN; private _targetMoveEpsSq: number = 0.25; // (0.5px)^2 update(deltaTime: number) { if (!gg.game.CurentBattle.canUpdateFrame()) return; this._dt += deltaTime * gg.game.CurentBattle.TimeScale; if(!this.refreshLaserUi()){ return } if(this._dt >= this._addBuffInterval){ this._dt = 0 this.curRepeatCount++ // console.log('激光造成一次伤害') /**被武器攻击 */ this._targetMonster.getComponent(Monster).attackByWeapon(this._weaponid) if(this.curRepeatCount >= this._repeatCount){ this.destroyNode() } } } destroyNode(){ //清除怪物身上的锁定 //移除当前脚本 this.removeSelf() gg.res.putNode(this.node) } atkToMonster(weaponId,laserId,targetMonster,weaponProperty:IWeaponProperty){ this.node.getChildByName('蓝激光光柱').active = false this.node.getChildByName('紫激光光柱').active = false this.node.getChildByName('橙激光光柱').active = false if(weaponProperty.isHaveOrangeSkill){ this.lightNode = this.node.getChildByName('橙激光光柱') }else if(weaponProperty.isHavePurpleSkill){ this.lightNode = this.node.getChildByName('紫激光光柱') }else{ this.lightNode = this.node.getChildByName('蓝激光光柱') } this.lightNode.active = true // 缓存起点/终点节点:避免每帧 getChildByName this._startNode = this.node.getChildByName('起点'); this._endNode = this.node.getChildByName('末点'); if (this._startNode) { // 起点固定在局部原点:只设置一次 this._startNode.setPosition(0, 0, 0); } this._laserId = laserId this._weaponid = weaponId this._targetMonster = targetMonster this._dt = 0 this._addBuffInterval = weaponProperty.damageInterval this._repeatCount = Math.floor(weaponProperty.weaponContinueTime/weaponProperty.damageInterval) this._lastTargetWorldX = NaN; this._lastTargetWorldY = NaN; if(!this.refreshLaserUi()){ return } } /**找到攻击目标 */ getTargetMonster(){ // if(!this._targetMonster || !isValid(this._targetMonster) || this._targetMonster.x > 106 || !this._targetMonster.parent || !this._targetMonster.getComponent(MonsterOld).getIsLockByLaser(this._laserId)){ // let arr = gg.game.CurentBattle.MonsterSpawner.getLaserTargetMonster(this._laserId,1) // return arr[0] // } return this._targetMonster } /**刷新激光武器ui */ refreshLaserUi(){ this._targetMonster = this.getTargetMonster() if(!this._targetMonster || !isValid(this._targetMonster)){ this.destroyNode() return false } if (!this._endNode) this._endNode = this.node.getChildByName('末点'); // 避免 clone/new:直接用数值计算 const startPos = this.node.worldPosition; const targetPos = this._targetMonster.worldPosition; // 小幅移动时直接复用上一次的 angle/scale if (this._lastTargetWorldX === this._lastTargetWorldX && this._lastTargetWorldY === this._lastTargetWorldY) { const mdx = targetPos.x - this._lastTargetWorldX; const mdy = targetPos.y - this._lastTargetWorldY; if (mdx * mdx + mdy * mdy < this._targetMoveEpsSq) { this._endNode!.setWorldPosition(targetPos); return true; } } this._lastTargetWorldX = targetPos.x; this._lastTargetWorldY = targetPos.y; // if(targetPos.x < 0){ // console.log('激光武器攻击目标在左侧==',targetPos,this._targetMonster) // } const dx = targetPos.x - startPos.x; const dy = targetPos.y - startPos.y; const totalDistance = Math.sqrt(dx * dx + dy * dy); if (totalDistance <= 1e-6) return true; // atan2 直接得到角度 this.lightNode.angle = Math.atan2(dy, dx) * 180 / Math.PI; // scale:避免 new Vec3 const s = totalDistance / 433; this.lightNode.setScale(s, 1, 1); // 终点直接设置到目标世界坐标(不需要 clone) this._endNode!.setWorldPosition(targetPos); return true } removeSelf() { this.destroy() } onDestroy() { // console.log('LaserContinueSchedule组件已被移除'); } }