|
|
@@ -123,8 +123,38 @@ export class WeaponBullet extends Component {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
- // 计算子弹生成信息
|
|
|
- const direction = initData.direction || new Vec3(1, 0, 0);
|
|
|
+ // === 计算基础发射方向 ===
|
|
|
+ let direction: Vec3;
|
|
|
+ if (initData.direction) {
|
|
|
+ direction = initData.direction.clone();
|
|
|
+ } else if (initData.autoTarget) {
|
|
|
+ // 复制 calculateDirection 中的最近敌人算法,以避免新增方法
|
|
|
+ const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
|
|
|
+ if (enemyContainer) {
|
|
|
+ const enemies = enemyContainer.children.filter(child => child.active && !!child.name);
|
|
|
+ let nearestEnemy: Node = null;
|
|
|
+ let nearestDistance = Infinity;
|
|
|
+ for (const enemy of enemies) {
|
|
|
+ const dist = Vec3.distance(initData.firePosition, enemy.worldPosition);
|
|
|
+ if (dist < nearestDistance) {
|
|
|
+ nearestDistance = dist;
|
|
|
+ nearestEnemy = enemy;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (nearestEnemy) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ direction = new Vec3(1, 0, 0);
|
|
|
+ }
|
|
|
+
|
|
|
const spawnInfos = BulletCount.calculateBulletSpawns(
|
|
|
config.bulletConfig.count,
|
|
|
initData.firePosition,
|
|
|
@@ -293,8 +323,8 @@ export class WeaponBullet extends Component {
|
|
|
return new Vec3(Math.cos(angle), Math.sin(angle), 0);
|
|
|
}
|
|
|
|
|
|
- // 寻找最近敌人
|
|
|
- const nearestEnemy = this.findNearestEnemy();
|
|
|
+ // 寻找最近敌人(使用静态工具函数)
|
|
|
+ const nearestEnemy = WeaponBullet.findNearestEnemyGlobal(this.node.worldPosition);
|
|
|
if (nearestEnemy) {
|
|
|
const direction = nearestEnemy.worldPosition.clone()
|
|
|
.subtract(this.node.worldPosition)
|
|
|
@@ -308,44 +338,6 @@ export class WeaponBullet extends Component {
|
|
|
return new Vec3(Math.cos(angle), Math.sin(angle), 0);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 寻找最近敌人
|
|
|
- */
|
|
|
- private findNearestEnemy(): Node | null {
|
|
|
- const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
|
|
|
- if (!enemyContainer) return null;
|
|
|
-
|
|
|
- const enemies = enemyContainer.children.filter(child =>
|
|
|
- child.active && this.isEnemyNode(child)
|
|
|
- );
|
|
|
-
|
|
|
- if (enemies.length === 0) return null;
|
|
|
-
|
|
|
- let nearestEnemy: Node = null;
|
|
|
- let nearestDistance = Infinity;
|
|
|
- const bulletPos = this.node.worldPosition;
|
|
|
-
|
|
|
- for (const enemy of enemies) {
|
|
|
- const distance = Vec3.distance(bulletPos, enemy.worldPosition);
|
|
|
- if (distance < nearestDistance) {
|
|
|
- nearestDistance = distance;
|
|
|
- nearestEnemy = enemy;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nearestEnemy;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断是否为敌人节点
|
|
|
- */
|
|
|
- private isEnemyNode(node: Node): boolean {
|
|
|
- const name = node.name.toLowerCase();
|
|
|
- return name.includes('enemy') ||
|
|
|
- name.includes('敌人') ||
|
|
|
- node.getComponent('EnemyInstance') !== null;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 设置碰撞监听
|
|
|
*/
|
|
|
@@ -510,4 +502,33 @@ export class WeaponBullet extends Component {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 静态:根据给定世界坐标,寻找最近的敌人节点
|
|
|
+ */
|
|
|
+ private static findNearestEnemyGlobal(worldPos: Vec3): Node | null {
|
|
|
+ const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
|
|
|
+ if (!enemyContainer) return null;
|
|
|
+
|
|
|
+ const enemies = enemyContainer.children.filter(child => {
|
|
|
+ if (!child.active) return false;
|
|
|
+ const nameLower = child.name.toLowerCase();
|
|
|
+ if (nameLower.includes('enemy') || nameLower.includes('敌人')) return true;
|
|
|
+ if (child.getComponent('EnemyInstance')) return true;
|
|
|
+ return false;
|
|
|
+ });
|
|
|
+
|
|
|
+ if (enemies.length === 0) return null;
|
|
|
+
|
|
|
+ let nearest: Node = null;
|
|
|
+ let nearestDist = Infinity;
|
|
|
+ for (const enemy of enemies) {
|
|
|
+ const dist = Vec3.distance(worldPos, enemy.worldPosition);
|
|
|
+ if (dist < nearestDist) {
|
|
|
+ nearestDist = dist;
|
|
|
+ nearest = enemy;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nearest;
|
|
|
+ }
|
|
|
}
|