|
@@ -1,4 +1,4 @@
|
|
|
-import { BoxCollider, Button, Collider, Component, ConeCollider, CylinderCollider, ITriggerEvent, Label, Node, NodeEventType, SphereCollider, Vec3, _decorator, find, game } from 'cc';
|
|
|
+import { BoxCollider, Button, Collider, Component, ConeCollider, CylinderCollider, ITriggerEvent, Label, Node, NodeEventType, RigidBody, SphereCollider, Vec3, _decorator, find, game } from 'cc';
|
|
|
import { StormSunderAudioMgr } from '../Manager/StormSunderAudioMgr';
|
|
|
import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
|
|
|
import { GameEvent } from '../Enum/GameEvent';
|
|
@@ -22,6 +22,7 @@ export class TornadoComponent extends Component {
|
|
|
points: Node[] = [];
|
|
|
|
|
|
tornado: Node = null!;
|
|
|
+ rigidBody: RigidBody = null!;
|
|
|
tigger: Collider = null!; //龙卷风触发器
|
|
|
radiusTigger: Collider = null!; //龙卷风半径检测触发器
|
|
|
|
|
@@ -33,6 +34,7 @@ export class TornadoComponent extends Component {
|
|
|
nickName: string = null;
|
|
|
playerStatus: PlayerStatus = PlayerStatus.LIFE;
|
|
|
playerInfo: PlayerInfo = null;
|
|
|
+ isColliding: boolean = false;
|
|
|
private _ai: boolean = false;
|
|
|
public get ai(): boolean {
|
|
|
return this._ai;
|
|
@@ -54,6 +56,7 @@ export class TornadoComponent extends Component {
|
|
|
if (!this.node) return;
|
|
|
|
|
|
this.tornado = this.node.getChildByName('tornado')!;
|
|
|
+ this.rigidBody = this.tornado.getComponent(RigidBody)!;
|
|
|
this.tigger = this.tornado.getComponent(Collider)!;
|
|
|
|
|
|
this.tigger.on('onTriggerEnter', this.onTriggerEnter, this);
|
|
@@ -84,11 +87,42 @@ export class TornadoComponent extends Component {
|
|
|
}
|
|
|
|
|
|
onTriggerEnter(event: ITriggerEvent): void {
|
|
|
- if (event.otherCollider.getGroup() == 1 << 2) {
|
|
|
-
|
|
|
+ let _originalPosition = this.node.position.clone();
|
|
|
+ if (event.otherCollider.getGroup() === 1 << 2) {
|
|
|
+ if (this.isColliding) return; // 防止重复触发
|
|
|
+
|
|
|
+ this.isColliding = true;
|
|
|
+ _originalPosition = this.node.position.clone(); // 记录碰撞前坐标
|
|
|
+
|
|
|
+ // 计算反方向移动(保持Y轴不变)
|
|
|
+ const reverseDirection = this.calculateReverseDirection(event);
|
|
|
+ const newPosition = _originalPosition.add(reverseDirection);
|
|
|
+
|
|
|
+ // 直接修改坐标
|
|
|
+ this.node.setPosition(newPosition);
|
|
|
+ setTimeout(() => {
|
|
|
+ this.isColliding = false;
|
|
|
+ }, 500);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 计算反方向(基于障碍物位置)
|
|
|
+ private calculateReverseDirection(event: ITriggerEvent): Vec3 {
|
|
|
+ // 获取障碍物节点位置
|
|
|
+ const obstaclePos = event.otherCollider.node.worldPosition;
|
|
|
+ const selfPos = this.node.worldPosition;
|
|
|
+
|
|
|
+ // 计算方向向量(从障碍物指向自己)
|
|
|
+ const direction = new Vec3(
|
|
|
+ selfPos.x - obstaclePos.x,
|
|
|
+ 0, // Y轴保持0
|
|
|
+ selfPos.z - obstaclePos.z
|
|
|
+ );
|
|
|
+
|
|
|
+ // 归一化并乘以反冲距离
|
|
|
+ return direction.normalize().multiplyScalar(5);
|
|
|
+ }
|
|
|
+
|
|
|
onTriggerStay(event: ITriggerEvent): void {
|
|
|
if (event.otherCollider.getGroup() == 1 << 4) {
|
|
|
const otherCollider = event.otherCollider;
|
|
@@ -142,6 +176,7 @@ export class TornadoComponent extends Component {
|
|
|
}
|
|
|
|
|
|
onMoveHandler(): void {
|
|
|
+ if (this.isColliding) return;
|
|
|
const playerDir = UIJoyStick.ins.dir;
|
|
|
const playerX = playerDir.x * this.speed * game.deltaTime;
|
|
|
const playerZ = playerDir.y * this.speed * game.deltaTime;
|