Sfoglia il codice sorgente

修改随机运动 逃离 添加speed

woso_javan 2 mesi fa
parent
commit
a4acd05b40

+ 21 - 16
assets/module_storm_sunder/Script/Component/TornadoAIComponent.ts

@@ -64,7 +64,6 @@ export class TornadoAIComponent extends TornadoComponent {
         } else {
             const closestItem = PropMgr.inst.getNearestProp(this.node);
             if (closestItem) {
-                // this.moveToTarget(closestItem, this.moveDuration);
                 this.chaseTarget(closestItem);
             } else {
                 this.randomMove();
@@ -76,8 +75,18 @@ export class TornadoAIComponent extends TornadoComponent {
     private randomMove() {
         if (this.isChasing || this.isEscaping) return; // 如果正在追击或逃跑,则不进行随机移动
 
-        const randomDirection = new Vec3(randomRange(-50, 50), 0, randomRange(-50, 50));
+        // 计算最大可移动距离 = 速度 * 持续时间
+        const maxDistance = this.speed * this.moveDuration;
+
+        // 随机方向的单位向量
+        let randomDirection = new Vec3(randomRange(-1, 1), 0, randomRange(-1, 1));
+        randomDirection.normalize(); // 归一化,保持方向不变但长度为 1
+
+        // 计算最终的移动向量
+        randomDirection.multiplyScalar(maxDistance);
+
         const targetPosition = this.node.position.clone().add(randomDirection);
+
         PathfindingManager.getInstance().moveTo(this.node, targetPosition, this.moveDuration);
         console.log(`AI 触发随机移动行为!`);
 
@@ -88,14 +97,6 @@ export class TornadoAIComponent extends TornadoComponent {
         }, this.moveDuration);
     }
 
-    /** 移动到目标 */
-    private moveToTarget(target: Node, duration: number) {
-        PathfindingManager.getInstance().moveTo(this.node, target.position, duration);
-
-        this.scheduleOnce(() => {
-            this.decideAction();
-        }, duration);
-    }
 
     protected onTriggerEnter(event: ITriggerEvent): void {
         // super.onTriggerEnter(event);
@@ -117,7 +118,6 @@ export class TornadoAIComponent extends TornadoComponent {
             if (!targetTornado) return;
 
             if (this.currentLv > targetTornado.currentLv) {
-                console.log('AI 撒日朗++++++++');
                 GameMgr.inst.setGameStatus(GameStatus.End);
             }
         }
@@ -137,7 +137,6 @@ export class TornadoAIComponent extends TornadoComponent {
 
             if (targetLv > this.currentLv) {
                 // 目标等级比自己高 → 逃跑
-                console.log(`AI 逃离行为!`)
                 this.escapeFrom(targetTornado.node);
             } else if (targetLv < this.currentLv) {
                 // 目标等级比自己低 → 先判断是否追击
@@ -173,13 +172,13 @@ export class TornadoAIComponent extends TornadoComponent {
     /** 追击目标 */
     private chaseTarget(target: Node) {
         if (this.isChasing) return;
-        console.log(`AI 追击目标->>>>>>>>>>>>>`);
+        // console.log(`AI 追击目标->>>>>>>>>>>>>`);
 
         this.isChasing = true;
         this.targetNode = target;
 
         PathfindingManager.getInstance().followTarget(this, target, this.speed, () => {
-            console.log(`AI 追击目标到达`);
+            // console.log(`AI 追击目标到达`);
             this.unscheduleAllCallbacks();
             this.isChasing = false;
             this.targetNode = null;
@@ -197,8 +196,13 @@ export class TornadoAIComponent extends TornadoComponent {
     /** 逃离目标 */
     private escapeFrom(target: Node) {
         this.isEscaping = true;
-        console.log(`AI 逃离行为->>>>>>>>>>>>>>`);
-        const direction = this.node.position.clone().subtract(target.position).normalize().multiplyScalar(100);
+        // console.log(`AI 逃离行为->>>>>>>>>>>>>>`);
+
+        // 计算最大可逃离距离 = 速度 * 持续时间
+        const maxDistance = this.speed * this.escapeDuration;
+
+        // 计算逃离方向,指向目标的反方向
+        const direction = this.node.position.clone().subtract(target.position).normalize().multiplyScalar(maxDistance);
         const escapePosition = this.node.position.clone().add(direction);
 
         PathfindingManager.getInstance().moveTo(this.node, escapePosition, this.escapeDuration);
@@ -208,4 +212,5 @@ export class TornadoAIComponent extends TornadoComponent {
             this.decideAction();
         }, this.escapeDuration);
     }
+
 }