|
|
@@ -306,8 +306,11 @@ export class BulletTrajectory extends Component {
|
|
|
this.findTarget();
|
|
|
}
|
|
|
|
|
|
- // 检查是否到达目标位置(用于西瓜炸弹等爆炸武器)
|
|
|
- if (this.state.targetPosition) {
|
|
|
+ // 检查是否到达目标位置(仅用于西瓜炸弹等爆炸武器,回旋镖不适用)
|
|
|
+ const lifecycle = this.getComponent('BulletLifecycle') as any;
|
|
|
+ const isBoomerang = lifecycle && lifecycle.getState && lifecycle.getState().phase !== undefined;
|
|
|
+
|
|
|
+ if (this.state.targetPosition && !isBoomerang) {
|
|
|
const currentPos = this.node.worldPosition;
|
|
|
const distanceToTarget = Vec3.distance(currentPos, this.state.targetPosition);
|
|
|
|
|
|
@@ -326,7 +329,6 @@ export class BulletTrajectory extends Component {
|
|
|
}
|
|
|
|
|
|
// 然后通知生命周期组件触发爆炸
|
|
|
- const lifecycle = this.getComponent('BulletLifecycle') as any;
|
|
|
if (lifecycle && typeof lifecycle.onHit === 'function') {
|
|
|
lifecycle.onHit(this.node); // 触发爆炸逻辑
|
|
|
}
|
|
|
@@ -334,23 +336,57 @@ export class BulletTrajectory extends Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 每帧计算目标方向,使得弹道持续朝向敌人
|
|
|
- if (this.targetNode && this.targetNode.isValid) {
|
|
|
- const pos = this.node.worldPosition;
|
|
|
- const dirToTarget = this.targetNode.worldPosition.clone().subtract(pos).normalize();
|
|
|
- this.arcTargetDir.set(dirToTarget);
|
|
|
-
|
|
|
- // 更新目标位置为当前敌人位置
|
|
|
- this.state.targetPosition = this.targetNode.worldPosition.clone();
|
|
|
- } else if (!this.state.targetPosition) {
|
|
|
- // 如果没有目标且没有记录的目标位置,设置一个默认的前方位置
|
|
|
- const defaultTarget = this.node.worldPosition.clone().add(this.arcDir.clone().multiplyScalar(200));
|
|
|
- this.state.targetPosition = defaultTarget;
|
|
|
+ // 根据回旋镖状态决定目标方向
|
|
|
+ if (isBoomerang && lifecycle.getState().phase === 'returning') {
|
|
|
+ // 返回阶段:朝向起始位置
|
|
|
+ const pos = this.node.worldPosition;
|
|
|
+ const startPos = this.state.startPosition || pos; // 防止空指针
|
|
|
+ const dirToHome = startPos.clone().subtract(pos).normalize();
|
|
|
+ this.arcTargetDir.set(dirToHome);
|
|
|
+
|
|
|
+ // 更新目标位置为起始位置
|
|
|
+ this.state.targetPosition = startPos.clone();
|
|
|
+ } else {
|
|
|
+ // 主动追踪阶段:朝向敌人
|
|
|
+ if (this.targetNode && this.targetNode.isValid) {
|
|
|
+ const pos = this.node.worldPosition;
|
|
|
+ const dirToTarget = this.targetNode.worldPosition.clone().subtract(pos).normalize();
|
|
|
+ this.arcTargetDir.set(dirToTarget);
|
|
|
+
|
|
|
+ // 对于回旋镖,检查是否足够接近敌人来触发命中
|
|
|
+ if (isBoomerang) {
|
|
|
+ const distanceToEnemy = Vec3.distance(pos, this.targetNode.worldPosition);
|
|
|
+ if (distanceToEnemy <= 30) { // 30像素内算命中
|
|
|
+ // 触发命中效果
|
|
|
+ const hitEffect = this.getComponent('BulletHitEffect') as any;
|
|
|
+ if (hitEffect && typeof hitEffect.processHit === 'function') {
|
|
|
+ hitEffect.processHit(this.targetNode, pos);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 通知生命周期组件处理命中
|
|
|
+ if (lifecycle && typeof lifecycle.onHit === 'function') {
|
|
|
+ lifecycle.onHit(this.targetNode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新目标位置为当前敌人位置
|
|
|
+ this.state.targetPosition = this.targetNode.worldPosition.clone();
|
|
|
+ } else if (!this.state.targetPosition) {
|
|
|
+ // 如果没有目标且没有记录的目标位置,设置一个默认的前方位置
|
|
|
+ const defaultTarget = this.node.worldPosition.clone().add(this.arcDir.clone().multiplyScalar(200));
|
|
|
+ this.state.targetPosition = defaultTarget;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 根据与目标距离动态调整转向速率:越近转得越快,避免打圈
|
|
|
let rotateFactor = (this.config.rotateSpeed ?? 0.5) * dt;
|
|
|
- if (this.targetNode && this.targetNode.isValid) {
|
|
|
+
|
|
|
+ if (isBoomerang && lifecycle.getState().phase === 'returning') {
|
|
|
+ // 回旋镖返回阶段:使用固定的转向速率,确保平滑返回
|
|
|
+ rotateFactor *= 1.5; // 返回时适中的转向速率
|
|
|
+ } else if (this.targetNode && this.targetNode.isValid) {
|
|
|
+ // 主动追踪阶段:根据距离调整转向速率
|
|
|
const distance = Vec3.distance(this.node.worldPosition, this.targetNode.worldPosition);
|
|
|
rotateFactor *= (2000 / Math.max(distance, 50));
|
|
|
}
|