|
|
@@ -28,8 +28,11 @@ export class BlockManager extends Component {
|
|
|
})
|
|
|
public coinLabelNode: Node = null;
|
|
|
|
|
|
+ // 游戏是否已开始
|
|
|
+ public gameStarted: boolean = false;
|
|
|
+
|
|
|
// 玩家金币数量
|
|
|
- private playerCoins: number = 200;
|
|
|
+ private playerCoins: number = 699;
|
|
|
|
|
|
// 方块价格标签映射
|
|
|
private blockPriceMap: Map<Node, Node> = new Map();
|
|
|
@@ -90,6 +93,9 @@ export class BlockManager extends Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 确保有PlacedBlocks节点用于存放已放置的方块
|
|
|
+ this.ensurePlacedBlocksNode();
|
|
|
+
|
|
|
// 初始化玩家金币显示
|
|
|
this.updateCoinDisplay();
|
|
|
|
|
|
@@ -103,6 +109,28 @@ export class BlockManager extends Component {
|
|
|
this.generateRandomBlocks();
|
|
|
}
|
|
|
|
|
|
+ // 确保有PlacedBlocks节点
|
|
|
+ ensurePlacedBlocksNode() {
|
|
|
+ // 查找Canvas节点
|
|
|
+ const canvas = find('Canvas');
|
|
|
+ if (!canvas) {
|
|
|
+ console.error('找不到Canvas节点');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找或创建PlacedBlocks节点
|
|
|
+ let placedBlocksNode = find('Canvas/PlacedBlocks');
|
|
|
+ if (!placedBlocksNode) {
|
|
|
+ placedBlocksNode = new Node('PlacedBlocks');
|
|
|
+ canvas.addChild(placedBlocksNode);
|
|
|
+ // 确保PlacedBlocks节点有UITransform组件
|
|
|
+ if (!placedBlocksNode.getComponent(UITransform)) {
|
|
|
+ placedBlocksNode.addComponent(UITransform);
|
|
|
+ }
|
|
|
+ console.log('已创建PlacedBlocks节点');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 更新金币显示
|
|
|
updateCoinDisplay() {
|
|
|
if (this.coinLabelNode) {
|
|
|
@@ -165,7 +193,7 @@ export class BlockManager extends Component {
|
|
|
}
|
|
|
|
|
|
generateRandomBlocks() {
|
|
|
- // 清除现有的块
|
|
|
+ // 如果游戏已开始,只清除kuang区域的块,否则清除所有块
|
|
|
this.clearBlocks();
|
|
|
|
|
|
// 检查是否有预制体可用
|
|
|
@@ -174,6 +202,13 @@ export class BlockManager extends Component {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 获取kuang节点
|
|
|
+ const kuangNode = find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang');
|
|
|
+ if (!kuangNode) {
|
|
|
+ console.error('找不到kuang节点');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
// 位置偏移量
|
|
|
const offsets = [
|
|
|
new Vec3(-200, 0, 0),
|
|
|
@@ -183,9 +218,9 @@ export class BlockManager extends Component {
|
|
|
|
|
|
// 获取kuang节点下的db01、db02、db03节点
|
|
|
const dbNodes = [
|
|
|
- find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang/db01'),
|
|
|
- find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang/db02'),
|
|
|
- find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang/db03')
|
|
|
+ kuangNode.getChildByName('db01'),
|
|
|
+ kuangNode.getChildByName('db02'),
|
|
|
+ kuangNode.getChildByName('db03')
|
|
|
];
|
|
|
|
|
|
// 生成三个随机块
|
|
|
@@ -196,7 +231,7 @@ export class BlockManager extends Component {
|
|
|
|
|
|
// 实例化预制体
|
|
|
const block = instantiate(prefab);
|
|
|
- this.node.addChild(block);
|
|
|
+ kuangNode.addChild(block); // 直接添加到kuang节点下
|
|
|
|
|
|
// 设置位置
|
|
|
block.position = offsets[i];
|
|
|
@@ -316,7 +351,7 @@ export class BlockManager extends Component {
|
|
|
this.currentDragBlock['startLocation'] = this.blockLocations.get(block);
|
|
|
|
|
|
// 将块置于顶层
|
|
|
- block.setSiblingIndex(this.node.children.length - 1);
|
|
|
+ block.setSiblingIndex(block.parent.children.length - 1);
|
|
|
|
|
|
// 临时保存方块占用的网格,而不是直接移除
|
|
|
this.tempStoreBlockOccupiedGrids(block);
|
|
|
@@ -352,6 +387,16 @@ export class BlockManager extends Component {
|
|
|
// 如果拖回kuang区域,恢复到原始位置
|
|
|
const originalPos = this.originalPositions.get(this.currentDragBlock);
|
|
|
if (originalPos) {
|
|
|
+ // 确保方块在kuang节点下
|
|
|
+ const kuangNode = find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang');
|
|
|
+ if (kuangNode && this.currentDragBlock.parent !== kuangNode) {
|
|
|
+ // 将方块从当前父节点移除
|
|
|
+ this.currentDragBlock.removeFromParent();
|
|
|
+ // 添加到kuang节点下
|
|
|
+ kuangNode.addChild(this.currentDragBlock);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置位置
|
|
|
this.currentDragBlock.position = originalPos.clone();
|
|
|
}
|
|
|
|
|
|
@@ -398,6 +443,17 @@ export class BlockManager extends Component {
|
|
|
|
|
|
// 隐藏价格标签
|
|
|
this.hidePriceLabel(this.currentDragBlock);
|
|
|
+
|
|
|
+ // 隐藏db节点
|
|
|
+ const dbNode = this.currentDragBlock['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ dbNode.active = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果游戏已经开始,将方块移动到PlacedBlocks节点下
|
|
|
+ if (this.gameStarted) {
|
|
|
+ this.moveBlockToPlacedBlocks(this.currentDragBlock);
|
|
|
+ }
|
|
|
} else {
|
|
|
// 尝试扣除金币
|
|
|
if (this.deductPlayerCoins(price)) {
|
|
|
@@ -410,8 +466,19 @@ export class BlockManager extends Component {
|
|
|
// 隐藏价格标签
|
|
|
this.hidePriceLabel(this.currentDragBlock);
|
|
|
|
|
|
+ // 隐藏db节点
|
|
|
+ const dbNode = this.currentDragBlock['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ dbNode.active = false;
|
|
|
+ }
|
|
|
+
|
|
|
// 标记方块已经放置过
|
|
|
this.currentDragBlock['placedBefore'] = true;
|
|
|
+
|
|
|
+ // 如果游戏已经开始,将方块移动到PlacedBlocks节点下
|
|
|
+ if (this.gameStarted) {
|
|
|
+ this.moveBlockToPlacedBlocks(this.currentDragBlock);
|
|
|
+ }
|
|
|
} else {
|
|
|
// 金币不足,放置失败,返回原位
|
|
|
const originalPos = this.originalPositions.get(this.currentDragBlock);
|
|
|
@@ -478,6 +545,17 @@ export class BlockManager extends Component {
|
|
|
// 触摸取消,返回原位
|
|
|
this.currentDragBlock.position = this.blockStartPos.clone();
|
|
|
|
|
|
+ // 如果当前在kuang区域,确保方块在kuang节点下
|
|
|
+ if (this.blockLocations.get(this.currentDragBlock) === 'kuang') {
|
|
|
+ const kuangNode = find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang');
|
|
|
+ if (kuangNode && this.currentDragBlock.parent !== kuangNode) {
|
|
|
+ // 将方块从当前父节点移除
|
|
|
+ this.currentDragBlock.removeFromParent();
|
|
|
+ // 添加到kuang节点下
|
|
|
+ kuangNode.addChild(this.currentDragBlock);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 恢复方块原来的占用状态
|
|
|
this.restoreBlockOccupiedGrids(this.currentDragBlock);
|
|
|
|
|
|
@@ -909,45 +987,106 @@ export class BlockManager extends Component {
|
|
|
}
|
|
|
|
|
|
clearBlocks() {
|
|
|
- // 移除所有已经生成的块
|
|
|
+ // 只移除kuang区域的块,保留已放置在网格中的块
|
|
|
+ const blocksToRemove = [];
|
|
|
+
|
|
|
+ // 找出所有在kuang区域的块
|
|
|
+ for (const block of this.blocks) {
|
|
|
+ if (block.isValid) {
|
|
|
+ const location = this.blockLocations.get(block);
|
|
|
+ if (location === 'kuang') {
|
|
|
+ blocksToRemove.push(block);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除kuang区域的块
|
|
|
+ for (const block of blocksToRemove) {
|
|
|
+ // 如果有关联的db节点,恢复其位置
|
|
|
+ const dbNode = block['dbNode'];
|
|
|
+ if (dbNode && dbNode.isValid) {
|
|
|
+ // 移除方块移动时的监听
|
|
|
+ block.off(Node.EventType.TRANSFORM_CHANGED);
|
|
|
+
|
|
|
+ // 恢复db节点到原始位置
|
|
|
+ const kuangNode = find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang');
|
|
|
+ if (kuangNode) {
|
|
|
+ // 确保db节点回到原来的父节点下
|
|
|
+ const dbName = dbNode.name;
|
|
|
+ if (!kuangNode.getChildByName(dbName)) {
|
|
|
+ dbNode.parent = kuangNode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从blocks数组中移除
|
|
|
+ const index = this.blocks.indexOf(block);
|
|
|
+ if (index !== -1) {
|
|
|
+ this.blocks.splice(index, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除相关映射
|
|
|
+ this.originalPositions.delete(block);
|
|
|
+ this.blockLocations.delete(block);
|
|
|
+ this.blockPriceMap.delete(block);
|
|
|
+
|
|
|
+ // 销毁方块
|
|
|
+ block.destroy();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注意:不重置网格占用情况,保留已放置的方块占用信息
|
|
|
+ }
|
|
|
+
|
|
|
+ // 游戏开始时,确保已放置的方块正确显示
|
|
|
+ onGameStart() {
|
|
|
+ // 遍历所有方块
|
|
|
for (const block of this.blocks) {
|
|
|
if (block.isValid) {
|
|
|
- // 移除占用的格子
|
|
|
- this.removeBlockFromGrid(block);
|
|
|
+ const location = this.blockLocations.get(block);
|
|
|
|
|
|
- // 如果有关联的db节点,恢复其位置
|
|
|
- const dbNode = block['dbNode'];
|
|
|
- if (dbNode && dbNode.isValid) {
|
|
|
- // 移除方块移动时的监听
|
|
|
- block.off(Node.EventType.TRANSFORM_CHANGED);
|
|
|
+ // 如果方块在网格中,将其移动到PlacedBlocks节点下
|
|
|
+ if (location === 'grid') {
|
|
|
+ // 隐藏价格标签
|
|
|
+ this.hidePriceLabel(block);
|
|
|
|
|
|
- // 恢复db节点到原始位置
|
|
|
- const originalParent = find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang');
|
|
|
- if (originalParent) {
|
|
|
- // 确保db节点回到原来的父节点下
|
|
|
- const dbName = dbNode.name;
|
|
|
- if (!originalParent.getChildByName(dbName)) {
|
|
|
- dbNode.parent = originalParent;
|
|
|
- }
|
|
|
+ // 隐藏db节点
|
|
|
+ const dbNode = block['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ dbNode.active = false;
|
|
|
}
|
|
|
+
|
|
|
+ // 将方块移动到PlacedBlocks节点下
|
|
|
+ this.moveBlockToPlacedBlocks(block);
|
|
|
}
|
|
|
-
|
|
|
- // 销毁方块
|
|
|
- block.destroy();
|
|
|
}
|
|
|
}
|
|
|
- this.blocks = [];
|
|
|
|
|
|
- // 清空原始位置记录
|
|
|
- this.originalPositions.clear();
|
|
|
+ // 设置游戏已开始标志
|
|
|
+ this.gameStarted = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将方块移动到PlacedBlocks节点下
|
|
|
+ moveBlockToPlacedBlocks(block: Node) {
|
|
|
+ // 查找PlacedBlocks节点
|
|
|
+ const placedBlocksNode = find('Canvas/PlacedBlocks');
|
|
|
+ if (!placedBlocksNode) {
|
|
|
+ console.error('找不到PlacedBlocks节点');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存方块的世界位置
|
|
|
+ const worldPosition = new Vec3();
|
|
|
+ block.getWorldPosition(worldPosition);
|
|
|
|
|
|
- // 清空位置标记
|
|
|
- this.blockLocations.clear();
|
|
|
+ // 将方块从当前父节点移除
|
|
|
+ block.removeFromParent();
|
|
|
|
|
|
- // 清空价格标签映射
|
|
|
- this.blockPriceMap.clear();
|
|
|
+ // 添加到PlacedBlocks节点下
|
|
|
+ placedBlocksNode.addChild(block);
|
|
|
|
|
|
- // 重置网格占用情况
|
|
|
- this.initGridOccupationMap();
|
|
|
+ // 设置方块的世界位置,确保在屏幕上的位置不变
|
|
|
+ block.setWorldPosition(worldPosition);
|
|
|
+
|
|
|
+ console.log(`已将方块 ${block.name} 移动到PlacedBlocks节点下`);
|
|
|
}
|
|
|
}
|