|
|
@@ -181,6 +181,13 @@ export class BlockManager extends Component {
|
|
|
new Vec3(200, 0, 0)
|
|
|
];
|
|
|
|
|
|
+ // 获取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')
|
|
|
+ ];
|
|
|
+
|
|
|
// 生成三个随机块
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
// 随机选择一个预制体
|
|
|
@@ -203,60 +210,54 @@ export class BlockManager extends Component {
|
|
|
// 添加到块数组
|
|
|
this.blocks.push(block);
|
|
|
|
|
|
- // 查找并保存价格标签节点
|
|
|
- this.findAndSavePriceNode(block);
|
|
|
+ // 将对应的db节点与方块关联
|
|
|
+ if (dbNodes[i]) {
|
|
|
+ // 保存价格节点引用 (Price是db节点的子节点)
|
|
|
+ const priceNode = dbNodes[i].getChildByName('Price');
|
|
|
+ if (priceNode) {
|
|
|
+ this.blockPriceMap.set(block, priceNode);
|
|
|
+ priceNode.active = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将db节点作为方块的子节点
|
|
|
+ this.associateDbNodeWithBlock(block, dbNodes[i]);
|
|
|
+ }
|
|
|
|
|
|
// 添加触摸事件
|
|
|
this.setupDragEvents(block);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 查找并保存价格标签节点
|
|
|
- findAndSavePriceNode(block: Node) {
|
|
|
- // 查找db01节点
|
|
|
- let db01 = block.getChildByName('db01');
|
|
|
-
|
|
|
- // 如果没有db01节点,创建一个
|
|
|
- if (!db01) {
|
|
|
- db01 = new Node('db01');
|
|
|
- block.addChild(db01);
|
|
|
- db01.position = new Vec3(0, -80, 0); // 放在方块下方
|
|
|
- }
|
|
|
-
|
|
|
- // 查找Price节点
|
|
|
- let priceNode = db01.getChildByName('Price');
|
|
|
-
|
|
|
- // 如果没有Price节点,创建一个
|
|
|
- if (!priceNode) {
|
|
|
- priceNode = new Node('Price');
|
|
|
- db01.addChild(priceNode);
|
|
|
-
|
|
|
- // 添加UI变换组件
|
|
|
- const transform = priceNode.addComponent(UITransform);
|
|
|
- transform.contentSize = new Size(80, 30);
|
|
|
-
|
|
|
- // 添加标签组件
|
|
|
- const label = priceNode.addComponent(Label);
|
|
|
- label.string = this.generateRandomPrice().toString();
|
|
|
- label.fontSize = 20;
|
|
|
- label.color = new Color(255, 255, 0, 255); // 黄色
|
|
|
-
|
|
|
- // 居中显示
|
|
|
- label.horizontalAlign = Label.HorizontalAlign.CENTER;
|
|
|
- label.verticalAlign = Label.VerticalAlign.CENTER;
|
|
|
- }
|
|
|
+ // 将db节点与方块关联
|
|
|
+ associateDbNodeWithBlock(block: Node, dbNode: Node) {
|
|
|
+ // 记录db节点与方块的关联
|
|
|
+ block['dbNode'] = dbNode;
|
|
|
|
|
|
- // 保存价格节点引用
|
|
|
- this.blockPriceMap.set(block, priceNode);
|
|
|
-
|
|
|
- // 确保价格标签可见
|
|
|
- priceNode.active = true;
|
|
|
- }
|
|
|
-
|
|
|
- // 生成随机价格
|
|
|
- generateRandomPrice(): number {
|
|
|
- // 生成50-100之间的随机价格
|
|
|
- return Math.floor(Math.random() * 51) + 50;
|
|
|
+ // 当方块移动时,db节点也跟随移动
|
|
|
+ block.on(Node.EventType.TRANSFORM_CHANGED, () => {
|
|
|
+ if (dbNode && block.parent) {
|
|
|
+ // 检查方块当前位置
|
|
|
+ const location = this.blockLocations.get(block);
|
|
|
+
|
|
|
+ // 如果方块在网格中,不需要显示db节点
|
|
|
+ if (location === 'grid') {
|
|
|
+ dbNode.active = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保db节点可见
|
|
|
+ dbNode.active = true;
|
|
|
+
|
|
|
+ // 获取方块在世界坐标系中的位置
|
|
|
+ const worldPos = block.parent.getComponent(UITransform).convertToWorldSpaceAR(block.position);
|
|
|
+
|
|
|
+ // 将世界坐标转换为db节点父节点的本地坐标
|
|
|
+ const localPos = dbNode.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
|
|
|
+
|
|
|
+ // 设置db节点位置,放在方块下方
|
|
|
+ dbNode.position = new Vec3(localPos.x, localPos.y - 80, localPos.z);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// 获取方块价格
|
|
|
@@ -311,6 +312,8 @@ export class BlockManager extends Component {
|
|
|
this.startPos = event.getUILocation();
|
|
|
// 记录块的起始位置
|
|
|
this.blockStartPos.set(block.position);
|
|
|
+ // 记录块的起始位置类型
|
|
|
+ this.currentDragBlock['startLocation'] = this.blockLocations.get(block);
|
|
|
|
|
|
// 将块置于顶层
|
|
|
block.setSiblingIndex(this.node.children.length - 1);
|
|
|
@@ -341,6 +344,8 @@ export class BlockManager extends Component {
|
|
|
if (this.currentDragBlock) {
|
|
|
// 获取当前触摸位置
|
|
|
const touchPos = event.getUILocation();
|
|
|
+ // 获取起始位置类型
|
|
|
+ const startLocation = this.currentDragBlock['startLocation'];
|
|
|
|
|
|
// 检查是否拖到了kuang区域
|
|
|
if (this.isInKuangArea(touchPos)) {
|
|
|
@@ -358,13 +363,34 @@ export class BlockManager extends Component {
|
|
|
|
|
|
// 确保价格标签显示
|
|
|
this.showPriceLabel(this.currentDragBlock);
|
|
|
+
|
|
|
+ // 如果方块之前在网格中,需要恢复金币
|
|
|
+ if (startLocation === 'grid') {
|
|
|
+ // 获取方块价格
|
|
|
+ const price = this.getBlockPrice(this.currentDragBlock);
|
|
|
+ // 恢复金币
|
|
|
+ this.playerCoins += price;
|
|
|
+ // 更新金币显示
|
|
|
+ this.updateCoinDisplay();
|
|
|
+ // 重置已放置标记
|
|
|
+ this.currentDragBlock['placedBefore'] = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保db节点可见并回到正确位置
|
|
|
+ const dbNode = this.currentDragBlock['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ dbNode.active = true;
|
|
|
+
|
|
|
+ // 触发一次位置更新,确保db节点位置正确
|
|
|
+ this.currentDragBlock.emit(Node.EventType.TRANSFORM_CHANGED);
|
|
|
+ }
|
|
|
} else if (this.tryPlaceBlockToGrid(this.currentDragBlock)) {
|
|
|
// 获取方块价格
|
|
|
const price = this.getBlockPrice(this.currentDragBlock);
|
|
|
|
|
|
- // 尝试扣除金币
|
|
|
- if (this.deductPlayerCoins(price)) {
|
|
|
- // 扣除成功,清除临时保存的占用状态
|
|
|
+ // 如果方块之前在网格中,不需要重复扣除金币
|
|
|
+ if (startLocation === 'grid') {
|
|
|
+ // 清除临时保存的占用状态
|
|
|
this.clearTempStoredOccupiedGrids(this.currentDragBlock);
|
|
|
|
|
|
// 更新方块位置标记
|
|
|
@@ -373,17 +399,41 @@ export class BlockManager extends Component {
|
|
|
// 隐藏价格标签
|
|
|
this.hidePriceLabel(this.currentDragBlock);
|
|
|
} else {
|
|
|
- // 金币不足,放置失败,返回原位
|
|
|
- const originalPos = this.originalPositions.get(this.currentDragBlock);
|
|
|
- if (originalPos) {
|
|
|
- this.currentDragBlock.position = originalPos.clone();
|
|
|
+ // 尝试扣除金币
|
|
|
+ if (this.deductPlayerCoins(price)) {
|
|
|
+ // 扣除成功,清除临时保存的占用状态
|
|
|
+ this.clearTempStoredOccupiedGrids(this.currentDragBlock);
|
|
|
+
|
|
|
+ // 更新方块位置标记
|
|
|
+ this.blockLocations.set(this.currentDragBlock, 'grid');
|
|
|
+
|
|
|
+ // 隐藏价格标签
|
|
|
+ this.hidePriceLabel(this.currentDragBlock);
|
|
|
+
|
|
|
+ // 标记方块已经放置过
|
|
|
+ this.currentDragBlock['placedBefore'] = true;
|
|
|
+ } else {
|
|
|
+ // 金币不足,放置失败,返回原位
|
|
|
+ const originalPos = this.originalPositions.get(this.currentDragBlock);
|
|
|
+ if (originalPos) {
|
|
|
+ this.currentDragBlock.position = originalPos.clone();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 恢复方块原来的占用状态
|
|
|
+ this.restoreBlockOccupiedGrids(this.currentDragBlock);
|
|
|
+
|
|
|
+ // 确保价格标签显示
|
|
|
+ this.showPriceLabel(this.currentDragBlock);
|
|
|
+
|
|
|
+ // 确保db节点可见并回到正确位置
|
|
|
+ const dbNode = this.currentDragBlock['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ dbNode.active = true;
|
|
|
+
|
|
|
+ // 触发一次位置更新,确保db节点位置正确
|
|
|
+ this.currentDragBlock.emit(Node.EventType.TRANSFORM_CHANGED);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 恢复方块原来的占用状态
|
|
|
- this.restoreBlockOccupiedGrids(this.currentDragBlock);
|
|
|
-
|
|
|
- // 确保价格标签显示
|
|
|
- this.showPriceLabel(this.currentDragBlock);
|
|
|
}
|
|
|
} else {
|
|
|
// 放置失败,返回原位
|
|
|
@@ -404,6 +454,15 @@ export class BlockManager extends Component {
|
|
|
|
|
|
// 确保价格标签显示
|
|
|
this.showPriceLabel(this.currentDragBlock);
|
|
|
+
|
|
|
+ // 确保db节点可见并回到正确位置
|
|
|
+ const dbNode = this.currentDragBlock['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ dbNode.active = true;
|
|
|
+
|
|
|
+ // 触发一次位置更新,确保db节点位置正确
|
|
|
+ this.currentDragBlock.emit(Node.EventType.TRANSFORM_CHANGED);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
this.currentDragBlock = null;
|
|
|
@@ -413,6 +472,9 @@ export class BlockManager extends Component {
|
|
|
// 添加触摸取消事件
|
|
|
block.on(Node.EventType.TOUCH_CANCEL, () => {
|
|
|
if (this.currentDragBlock) {
|
|
|
+ // 获取起始位置类型
|
|
|
+ const startLocation = this.currentDragBlock['startLocation'];
|
|
|
+
|
|
|
// 触摸取消,返回原位
|
|
|
this.currentDragBlock.position = this.blockStartPos.clone();
|
|
|
|
|
|
@@ -422,6 +484,28 @@ export class BlockManager extends Component {
|
|
|
// 确保价格标签显示
|
|
|
this.showPriceLabel(this.currentDragBlock);
|
|
|
|
|
|
+ // 如果方块之前在网格中且现在在kuang区域,需要恢复金币
|
|
|
+ if (startLocation === 'grid' &&
|
|
|
+ this.blockLocations.get(this.currentDragBlock) === 'kuang') {
|
|
|
+ // 获取方块价格
|
|
|
+ const price = this.getBlockPrice(this.currentDragBlock);
|
|
|
+ // 恢复金币
|
|
|
+ this.playerCoins += price;
|
|
|
+ // 更新金币显示
|
|
|
+ this.updateCoinDisplay();
|
|
|
+ // 重置已放置标记
|
|
|
+ this.currentDragBlock['placedBefore'] = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保db节点可见并回到正确位置
|
|
|
+ const dbNode = this.currentDragBlock['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ dbNode.active = true;
|
|
|
+
|
|
|
+ // 触发一次位置更新,确保db节点位置正确
|
|
|
+ this.currentDragBlock.emit(Node.EventType.TRANSFORM_CHANGED);
|
|
|
+ }
|
|
|
+
|
|
|
this.currentDragBlock = null;
|
|
|
}
|
|
|
}, this);
|
|
|
@@ -557,6 +641,9 @@ export class BlockManager extends Component {
|
|
|
tryPlaceBlockToGrid(block: Node): boolean {
|
|
|
if (!this.gridContainer || !this.gridInitialized) return false;
|
|
|
|
|
|
+ // 记录方块之前的位置
|
|
|
+ const previousLocation = this.blockLocations.get(block);
|
|
|
+
|
|
|
// 获取B1节点
|
|
|
let b1Node = block;
|
|
|
if (block.name !== 'B1') {
|
|
|
@@ -596,14 +683,46 @@ export class BlockManager extends Component {
|
|
|
if (row >= 0 && row < this.GRID_ROWS && col >= 0 && col < this.GRID_COLS) {
|
|
|
const grid = this.gridNodes[row][col];
|
|
|
if (grid) {
|
|
|
- return this.tryPlaceBlockToSpecificGrid(block, grid);
|
|
|
+ const result = this.tryPlaceBlockToSpecificGrid(block, grid);
|
|
|
+
|
|
|
+ // 如果放置成功,处理db节点
|
|
|
+ if (result) {
|
|
|
+ const dbNode = block['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ // 隐藏db节点
|
|
|
+ dbNode.active = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果方块之前在网格中,不需要重复扣除金币
|
|
|
+ if (previousLocation === 'grid') {
|
|
|
+ block['placedBefore'] = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- return this.tryPlaceBlockToSpecificGrid(block, nearestGrid);
|
|
|
+ const result = this.tryPlaceBlockToSpecificGrid(block, nearestGrid);
|
|
|
+
|
|
|
+ // 如果放置成功,处理db节点
|
|
|
+ if (result) {
|
|
|
+ const dbNode = block['dbNode'];
|
|
|
+ if (dbNode) {
|
|
|
+ // 隐藏db节点
|
|
|
+ dbNode.active = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果方块之前在网格中,不需要重复扣除金币
|
|
|
+ if (previousLocation === 'grid') {
|
|
|
+ block['placedBefore'] = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
// 尝试将方块放置到指定的网格节点
|
|
|
@@ -795,6 +914,24 @@ export class BlockManager extends Component {
|
|
|
if (block.isValid) {
|
|
|
// 移除占用的格子
|
|
|
this.removeBlockFromGrid(block);
|
|
|
+
|
|
|
+ // 如果有关联的db节点,恢复其位置
|
|
|
+ const dbNode = block['dbNode'];
|
|
|
+ if (dbNode && dbNode.isValid) {
|
|
|
+ // 移除方块移动时的监听
|
|
|
+ block.off(Node.EventType.TRANSFORM_CHANGED);
|
|
|
+
|
|
|
+ // 恢复db节点到原始位置
|
|
|
+ const originalParent = find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang');
|
|
|
+ if (originalParent) {
|
|
|
+ // 确保db节点回到原来的父节点下
|
|
|
+ const dbName = dbNode.name;
|
|
|
+ if (!originalParent.getChildByName(dbName)) {
|
|
|
+ dbNode.parent = originalParent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 销毁方块
|
|
|
block.destroy();
|
|
|
}
|