|
|
@@ -136,7 +136,7 @@ export class BallController extends Component {
|
|
|
this.node.addChild(this.activeBall);
|
|
|
}
|
|
|
|
|
|
- // 随机位置(在GameArea范围内)
|
|
|
+ // 随机位置小球
|
|
|
this.positionBallRandomly();
|
|
|
|
|
|
// 设置球的半径
|
|
|
@@ -175,7 +175,106 @@ export class BallController extends Component {
|
|
|
const maxX = this.gameBounds.right - ballRadius - 20;
|
|
|
const minY = this.gameBounds.bottom + ballRadius + 20;
|
|
|
const maxY = this.gameBounds.top - ballRadius - 20;
|
|
|
+
|
|
|
+ // 获取GameArea节点
|
|
|
+ const gameArea = find('Canvas/GameLevelUI/GameArea');
|
|
|
+ if (!gameArea) {
|
|
|
+ console.error('找不到GameArea节点');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找GridContainer节点,它包含所有放置的方块
|
|
|
+ const gridContainer = gameArea.getChildByName('GridContainer');
|
|
|
+ if (!gridContainer) {
|
|
|
+ console.log('找不到GridContainer节点,使用默认随机位置');
|
|
|
+ this.setRandomPositionDefault(minX, maxX, minY, maxY);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取所有已放置的方块
|
|
|
+ const placedBlocks = [];
|
|
|
+ for (let i = 0; i < gridContainer.children.length; i++) {
|
|
|
+ const cell = gridContainer.children[i];
|
|
|
+ // 检查单元格是否有子节点(放置的方块)
|
|
|
+ if (cell.children.length > 0) {
|
|
|
+ placedBlocks.push(cell);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(`找到 ${placedBlocks.length} 个已放置的方块`);
|
|
|
+
|
|
|
+ // 如果没有方块,使用默认随机位置
|
|
|
+ if (placedBlocks.length === 0) {
|
|
|
+ this.setRandomPositionDefault(minX, maxX, minY, maxY);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试找到一个不与任何方块重叠的位置
|
|
|
+ let validPosition = false;
|
|
|
+ let attempts = 0;
|
|
|
+ const maxAttempts = 50; // 最大尝试次数
|
|
|
+ let randomX, randomY;
|
|
|
+
|
|
|
+ while (!validPosition && attempts < maxAttempts) {
|
|
|
+ // 随机生成位置
|
|
|
+ randomX = Math.random() * (maxX - minX) + minX;
|
|
|
+ randomY = Math.random() * (maxY - minY) + minY;
|
|
|
+
|
|
|
+ // 检查是否与任何方块重叠
|
|
|
+ let overlapping = false;
|
|
|
+ for (const block of placedBlocks) {
|
|
|
+ // 获取方块的世界坐标
|
|
|
+ const blockWorldPos = block.worldPosition;
|
|
|
+
|
|
|
+ // 计算小球与方块的距离
|
|
|
+ const distance = Math.sqrt(
|
|
|
+ Math.pow(randomX - blockWorldPos.x, 2) +
|
|
|
+ Math.pow(randomY - blockWorldPos.y, 2)
|
|
|
+ );
|
|
|
+
|
|
|
+ // 获取方块的尺寸
|
|
|
+ const blockTransform = block.getComponent(UITransform);
|
|
|
+ const blockSize = blockTransform ?
|
|
|
+ Math.max(blockTransform.width, blockTransform.height) / 2 : 50;
|
|
|
+
|
|
|
+ // 如果距离小于小球半径+方块尺寸的一半+安全距离,认为重叠
|
|
|
+ const safeDistance = 20; // 额外安全距离
|
|
|
+ if (distance < ballRadius + blockSize + safeDistance) {
|
|
|
+ overlapping = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有重叠,找到了有效位置
|
|
|
+ if (!overlapping) {
|
|
|
+ validPosition = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ attempts++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果找不到有效位置,使用默认位置(游戏区域底部中心)
|
|
|
+ if (!validPosition) {
|
|
|
+ console.log(`尝试 ${maxAttempts} 次后未找到有效位置,使用默认位置`);
|
|
|
+ randomX = (this.gameBounds.left + this.gameBounds.right) / 2;
|
|
|
+ randomY = this.gameBounds.bottom + ballRadius + 50; // 底部上方50单位
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将世界坐标转换为相对于GameArea的本地坐标
|
|
|
+ const localPos = gameArea.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(randomX, randomY, 0));
|
|
|
+ this.activeBall.position = localPos;
|
|
|
+
|
|
|
+ console.log('小球位置已设置:', {
|
|
|
+ worldX: randomX,
|
|
|
+ worldY: randomY,
|
|
|
+ localX: localPos.x,
|
|
|
+ localY: localPos.y,
|
|
|
+ overlapsWithBlock: !validPosition
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
+ // 设置默认随机位置
|
|
|
+ setRandomPositionDefault(minX, maxX, minY, maxY) {
|
|
|
// 随机生成位置
|
|
|
const randomX = Math.random() * (maxX - minX) + minX;
|
|
|
const randomY = Math.random() * (maxY - minY) + minY;
|
|
|
@@ -189,6 +288,11 @@ export class BallController extends Component {
|
|
|
// 直接设置位置(不太准确,但作为后备)
|
|
|
this.activeBall.position = new Vec3(randomX - this.gameBounds.left, randomY - this.gameBounds.bottom, 0);
|
|
|
}
|
|
|
+
|
|
|
+ console.log('使用默认随机位置设置小球:', {
|
|
|
+ worldX: randomX,
|
|
|
+ worldY: randomY
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// 设置碰撞组件
|