|
|
@@ -187,10 +187,10 @@ export class WeaponBullet extends Component {
|
|
|
direction = nearestEnemy.worldPosition.clone().subtract(initData.firePosition).normalize();
|
|
|
}
|
|
|
|
|
|
- // 如果没有敌人或计算失败,则回退为随机方向
|
|
|
+ // 自动瞄准未找到有效目标时,不发射子弹
|
|
|
if (!direction) {
|
|
|
- const angleRand = Math.random() * Math.PI * 2;
|
|
|
- direction = new Vec3(Math.cos(angleRand), Math.sin(angleRand), 0);
|
|
|
+ console.log('[WeaponBullet] 自动瞄准未找到有效目标,取消发射子弹');
|
|
|
+ return [];
|
|
|
}
|
|
|
} else {
|
|
|
direction = new Vec3(1, 0, 0);
|
|
|
@@ -226,7 +226,7 @@ export class WeaponBullet extends Component {
|
|
|
return null;
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 处理延迟发射 - 对于burst模式,所有子弹都应该被创建并返回
|
|
|
if (spawnInfo.delay > 0) {
|
|
|
// 延迟发射:先创建子弹,然后延迟激活
|
|
|
@@ -341,6 +341,13 @@ export class WeaponBullet extends Component {
|
|
|
// 计算方向
|
|
|
const direction = initData.direction || this.calculateDirection(initData.autoTarget);
|
|
|
|
|
|
+ // 自动瞄准且未找到目标时,取消初始化子弹
|
|
|
+ if (!direction && initData.autoTarget) {
|
|
|
+ console.log('[WeaponBullet] 自动瞄准未找到目标,取消初始化子弹');
|
|
|
+ this.node.destroy();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
// === 根据发射方向调整朝向 ===
|
|
|
// 说明:
|
|
|
// - 当 shouldRotate !== false 时,保持父节点按发射方向旋转,并启用持续自旋转(原逻辑)。
|
|
|
@@ -375,10 +382,8 @@ export class WeaponBullet extends Component {
|
|
|
);
|
|
|
|
|
|
// --- 额外偏移 ---
|
|
|
- // 若子弹在生成时与发射方块碰撞(位置重叠), 会立刻触发碰撞事件导致被销毁。
|
|
|
- // 因此在初始化完弹道后, 将子弹沿发射方向平移一小段距离(默认 30 像素左右)。
|
|
|
const dir = direction.clone().normalize();
|
|
|
- const spawnOffset = 30; // 可根据子弹半径或方块大小调整
|
|
|
+ const spawnOffset = 30;
|
|
|
if (!Number.isNaN(dir.x) && !Number.isNaN(dir.y)) {
|
|
|
const newLocalPos = this.node.position.clone().add(new Vec3(dir.x * spawnOffset, dir.y * spawnOffset, 0));
|
|
|
this.node.setPosition(newLocalPos);
|
|
|
@@ -438,7 +443,7 @@ export class WeaponBullet extends Component {
|
|
|
/**
|
|
|
* 计算子弹方向
|
|
|
*/
|
|
|
- private calculateDirection(autoTarget: boolean = false): Vec3 {
|
|
|
+ private calculateDirection(autoTarget: boolean = false): Vec3 | null {
|
|
|
if (!autoTarget) {
|
|
|
// 随机方向
|
|
|
const angle = Math.random() * Math.PI * 2;
|
|
|
@@ -454,9 +459,8 @@ export class WeaponBullet extends Component {
|
|
|
return direction;
|
|
|
}
|
|
|
|
|
|
- // 若没有敌人则随机方向发射
|
|
|
- const angle = Math.random() * Math.PI * 2;
|
|
|
- return new Vec3(Math.cos(angle), Math.sin(angle), 0);
|
|
|
+ // 自动瞄准模式下,无目标则返回 null,不再随机发射
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|