|
|
@@ -269,5 +269,36 @@ export class BulletController extends Component {
|
|
|
if (this.lifeTimer <= 0) {
|
|
|
this.node.destroy();
|
|
|
}
|
|
|
+
|
|
|
+ // 检查是否飞出游戏区域
|
|
|
+ this.checkOutOfBounds();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 如果子弹飞出 GameArea 边界(留有一定安全距离)则自动销毁
|
|
|
+ */
|
|
|
+ private checkOutOfBounds() {
|
|
|
+ const gameArea = find('Canvas/GameLevelUI/GameArea');
|
|
|
+ if (!gameArea || !this.node || !this.node.isValid) return;
|
|
|
+
|
|
|
+ const uiTransform = gameArea.getComponent(UITransform);
|
|
|
+ if (!uiTransform) return;
|
|
|
+
|
|
|
+ const halfWidth = uiTransform.width / 2;
|
|
|
+ const halfHeight = uiTransform.height / 2;
|
|
|
+ const center = gameArea.worldPosition;
|
|
|
+
|
|
|
+ const left = center.x - halfWidth;
|
|
|
+ const right = center.x + halfWidth;
|
|
|
+ const bottom = center.y - halfHeight;
|
|
|
+ const top = center.y + halfHeight;
|
|
|
+
|
|
|
+ // 允许子弹稍微超出边界后再销毁,避免边缘误差
|
|
|
+ const margin = 100;
|
|
|
+
|
|
|
+ const pos = this.node.worldPosition;
|
|
|
+ if (pos.x < left - margin || pos.x > right + margin || pos.y < bottom - margin || pos.y > top + margin) {
|
|
|
+ this.node.destroy();
|
|
|
+ }
|
|
|
}
|
|
|
}
|