|
|
@@ -1,4 +1,5 @@
|
|
|
-import { _decorator, Component, Node, Prefab, instantiate, Vec3, EventTouch, Vec2, UITransform, find, Rect, Label, Color, Size } from 'cc';
|
|
|
+import { _decorator, Component, Node, Prefab, instantiate, Vec3, EventTouch, Vec2, UITransform, find, Rect, Label, Color, Size, Sprite, SpriteFrame, resources } from 'cc';
|
|
|
+import { ConfigManager, WeaponConfig } from '../Core/ConfigManager';
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
@ccclass('BlockManager')
|
|
|
@@ -28,6 +29,13 @@ export class BlockManager extends Component {
|
|
|
})
|
|
|
public coinLabelNode: Node = null;
|
|
|
|
|
|
+ // 已放置方块容器节点
|
|
|
+ @property({
|
|
|
+ type: Node,
|
|
|
+ tooltip: '拖拽PlacedBlocks节点到这里(Canvas/GameLevelUI/PlacedBlocks)'
|
|
|
+ })
|
|
|
+ public placedBlocksContainer: Node = null;
|
|
|
+
|
|
|
// 游戏是否已开始
|
|
|
public gameStarted: boolean = false;
|
|
|
|
|
|
@@ -75,6 +83,12 @@ export class BlockManager extends Component {
|
|
|
private blockCooldowns: Map<Node, number> = new Map(); // 存储每个方块的冷却结束时间
|
|
|
private globalCooldownEndTime: number = 0; // 全局冷却结束时间
|
|
|
|
|
|
+ // 配置管理器
|
|
|
+ private configManager: ConfigManager = null;
|
|
|
+
|
|
|
+ // 方块武器配置映射
|
|
|
+ private blockWeaponConfigs: Map<Node, WeaponConfig> = new Map();
|
|
|
+
|
|
|
// 检查方块是否可以移动(冷却检查)
|
|
|
private canMoveBlock(block: Node): boolean {
|
|
|
if (!this.gameStarted) {
|
|
|
@@ -114,6 +128,12 @@ export class BlockManager extends Component {
|
|
|
}
|
|
|
|
|
|
start() {
|
|
|
+ // 获取配置管理器
|
|
|
+ this.configManager = ConfigManager.getInstance();
|
|
|
+ if (!this.configManager) {
|
|
|
+ console.error('无法获取ConfigManager实例');
|
|
|
+ }
|
|
|
+
|
|
|
// 如果没有指定GridContainer,尝试找到它
|
|
|
if (!this.gridContainer) {
|
|
|
this.gridContainer = find('Canvas/GameLevelUI/GameArea/GridContainer');
|
|
|
@@ -141,6 +161,14 @@ export class BlockManager extends Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 如果没有指定placedBlocksContainer,尝试找到它
|
|
|
+ if (!this.placedBlocksContainer) {
|
|
|
+ this.placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
|
|
|
+ if (!this.placedBlocksContainer) {
|
|
|
+ console.warn('找不到PlacedBlocks节点,将尝试创建');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 确保有PlacedBlocks节点用于存放已放置的方块
|
|
|
this.ensurePlacedBlocksNode();
|
|
|
|
|
|
@@ -153,27 +181,39 @@ export class BlockManager extends Component {
|
|
|
// 初始化网格占用情况
|
|
|
this.initGridOccupationMap();
|
|
|
|
|
|
- // 在kuang下随机生成三个方块
|
|
|
- this.generateRandomBlocksInKuang();
|
|
|
+ // 等待配置加载完成后生成方块
|
|
|
+ this.scheduleOnce(() => {
|
|
|
+ this.generateRandomBlocksInKuang();
|
|
|
+ }, 0.5);
|
|
|
}
|
|
|
|
|
|
// 确保有PlacedBlocks节点
|
|
|
ensurePlacedBlocksNode() {
|
|
|
- const canvas = find('Canvas');
|
|
|
- if (!canvas) {
|
|
|
- console.error('找不到Canvas节点');
|
|
|
+ // 如果已经通过拖拽设置了节点,直接使用
|
|
|
+ if (this.placedBlocksContainer && this.placedBlocksContainer.isValid) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- let placedBlocksNode = find('Canvas/PlacedBlocks');
|
|
|
- if (!placedBlocksNode) {
|
|
|
- placedBlocksNode = new Node('PlacedBlocks');
|
|
|
- canvas.addChild(placedBlocksNode);
|
|
|
- if (!placedBlocksNode.getComponent(UITransform)) {
|
|
|
- placedBlocksNode.addComponent(UITransform);
|
|
|
- }
|
|
|
- console.log('已创建PlacedBlocks节点');
|
|
|
+ // 尝试查找节点
|
|
|
+ this.placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
|
|
|
+ if (this.placedBlocksContainer) {
|
|
|
+ console.log('找到已存在的PlacedBlocks节点');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果找不到,创建新节点
|
|
|
+ const gameLevelUI = find('Canvas/GameLevelUI');
|
|
|
+ if (!gameLevelUI) {
|
|
|
+ console.error('找不到GameLevelUI节点,无法创建PlacedBlocks');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.placedBlocksContainer = new Node('PlacedBlocks');
|
|
|
+ gameLevelUI.addChild(this.placedBlocksContainer);
|
|
|
+ if (!this.placedBlocksContainer.getComponent(UITransform)) {
|
|
|
+ this.placedBlocksContainer.addComponent(UITransform);
|
|
|
}
|
|
|
+ console.log('已在GameLevelUI下创建PlacedBlocks节点');
|
|
|
}
|
|
|
|
|
|
// 初始化网格信息
|
|
|
@@ -227,6 +267,15 @@ export class BlockManager extends Component {
|
|
|
private generateRandomBlocksInKuang() {
|
|
|
this.clearBlocks();
|
|
|
|
|
|
+ // 检查配置管理器是否可用
|
|
|
+ if (!this.configManager || !this.configManager.isConfigLoaded()) {
|
|
|
+ console.warn('配置管理器未初始化或配置未加载完成,延迟生成方块');
|
|
|
+ this.scheduleOnce(() => {
|
|
|
+ this.generateRandomBlocksInKuang();
|
|
|
+ }, 1.0);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (this.blockPrefabs.length === 0) {
|
|
|
console.error('没有可用的预制体');
|
|
|
return;
|
|
|
@@ -250,14 +299,20 @@ export class BlockManager extends Component {
|
|
|
kuangNode.getChildByName('db03')
|
|
|
];
|
|
|
|
|
|
- console.log('开始在kuang容器中生成随机方块');
|
|
|
+ console.log('开始在kuang容器中生成随机武器方块');
|
|
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
- const randomIndex = Math.floor(Math.random() * this.blockPrefabs.length);
|
|
|
- const prefab = this.blockPrefabs[randomIndex];
|
|
|
+ // 获取随机武器配置
|
|
|
+ const weaponConfig = this.configManager.getRandomWeapon();
|
|
|
+ if (!weaponConfig) {
|
|
|
+ console.error(`无法获取第 ${i + 1} 个武器配置`);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
+ // 基于武器配置选择合适的预制体
|
|
|
+ const prefab = this.selectPrefabForWeapon(weaponConfig);
|
|
|
if (!prefab) {
|
|
|
- console.error(`方块预制体索引 ${randomIndex} 无效`);
|
|
|
+ console.error(`无法为武器 ${weaponConfig.name} 选择合适的预制体`);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -266,6 +321,14 @@ export class BlockManager extends Component {
|
|
|
|
|
|
block.position = offsets[i];
|
|
|
|
|
|
+ // 设置方块名称
|
|
|
+ block.name = `WeaponBlock_${weaponConfig.id}`;
|
|
|
+
|
|
|
+ // 保存武器配置到方块
|
|
|
+ this.blockWeaponConfigs.set(block, weaponConfig);
|
|
|
+ block['weaponConfig'] = weaponConfig;
|
|
|
+ block['weaponId'] = weaponConfig.id;
|
|
|
+
|
|
|
this.originalPositions.set(block, offsets[i].clone());
|
|
|
this.blockLocations.set(block, 'kuang');
|
|
|
this.blocks.push(block);
|
|
|
@@ -275,17 +338,23 @@ export class BlockManager extends Component {
|
|
|
if (priceNode) {
|
|
|
this.blockPriceMap.set(block, priceNode);
|
|
|
priceNode.active = true;
|
|
|
+
|
|
|
+ // 根据武器稀有度设置价格
|
|
|
+ this.setBlockPriceByRarity(priceNode, weaponConfig.rarity);
|
|
|
}
|
|
|
|
|
|
this.associateDbNodeWithBlock(block, dbNodes[i]);
|
|
|
}
|
|
|
|
|
|
+ // 设置方块的武器外观
|
|
|
+ this.setupBlockWeaponVisual(block, weaponConfig);
|
|
|
+
|
|
|
this.setupDragEvents(block);
|
|
|
|
|
|
- console.log(`生成方块 ${i + 1}/3: ${prefab.name} 在位置 (${offsets[i].x.toFixed(2)}, ${offsets[i].y.toFixed(2)})`);
|
|
|
+ console.log(`✅ 生成武器方块 ${i + 1}/3: ${weaponConfig.name} (${weaponConfig.rarity}) 在位置 (${offsets[i].x.toFixed(2)}, ${offsets[i].y.toFixed(2)})`);
|
|
|
}
|
|
|
|
|
|
- console.log(`成功在kuang容器中生成了 ${this.blocks.length} 个方块`);
|
|
|
+ console.log(`🎉 成功在kuang容器中生成了 ${this.blocks.length} 个武器方块`);
|
|
|
this.updateCoinDisplay();
|
|
|
}
|
|
|
|
|
|
@@ -851,6 +920,8 @@ export class BlockManager extends Component {
|
|
|
this.originalPositions.delete(block);
|
|
|
this.blockLocations.delete(block);
|
|
|
this.blockPriceMap.delete(block);
|
|
|
+ // 清理武器配置映射
|
|
|
+ this.blockWeaponConfigs.delete(block);
|
|
|
|
|
|
block.destroy();
|
|
|
}
|
|
|
@@ -903,9 +974,13 @@ export class BlockManager extends Component {
|
|
|
|
|
|
// 将方块移动到PlacedBlocks节点下
|
|
|
moveBlockToPlacedBlocks(block: Node) {
|
|
|
- const placedBlocksNode = find('Canvas/PlacedBlocks');
|
|
|
- if (!placedBlocksNode) {
|
|
|
- console.error('找不到PlacedBlocks节点');
|
|
|
+ if (!this.placedBlocksContainer) {
|
|
|
+ console.error('PlacedBlocks容器未设置');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!this.placedBlocksContainer.isValid) {
|
|
|
+ console.error('PlacedBlocks容器已失效');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -913,8 +988,162 @@ export class BlockManager extends Component {
|
|
|
block.getWorldPosition(worldPosition);
|
|
|
|
|
|
block.removeFromParent();
|
|
|
- placedBlocksNode.addChild(block);
|
|
|
+ this.placedBlocksContainer.addChild(block);
|
|
|
block.setWorldPosition(worldPosition);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据武器配置选择合适的预制体
|
|
|
+ private selectPrefabForWeapon(weaponConfig: WeaponConfig): Prefab | null {
|
|
|
+ if (this.blockPrefabs.length === 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据武器类型或稀有度选择预制体
|
|
|
+ // 这里可以根据实际需求来选择不同的预制体
|
|
|
+ // 目前简单地随机选择一个预制体
|
|
|
+ const randomIndex = Math.floor(Math.random() * this.blockPrefabs.length);
|
|
|
+ return this.blockPrefabs[randomIndex];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据稀有度设置方块价格
|
|
|
+ private setBlockPriceByRarity(priceNode: Node, rarity: string) {
|
|
|
+ const label = priceNode.getComponent(Label);
|
|
|
+ if (!label) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
+ let price: number;
|
|
|
+ switch (rarity) {
|
|
|
+ case 'common':
|
|
|
+ price = 50;
|
|
|
+ break;
|
|
|
+ case 'uncommon':
|
|
|
+ price = 100;
|
|
|
+ break;
|
|
|
+ case 'rare':
|
|
|
+ price = 200;
|
|
|
+ break;
|
|
|
+ case 'epic':
|
|
|
+ price = 350;
|
|
|
+ break;
|
|
|
+ case 'legendary':
|
|
|
+ price = 500;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ price = 50;
|
|
|
+ }
|
|
|
+
|
|
|
+ label.string = price.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置方块的武器外观
|
|
|
+ private setupBlockWeaponVisual(block: Node, weaponConfig: WeaponConfig) {
|
|
|
+ // 设置方块的稀有度颜色
|
|
|
+ this.setBlockRarityColor(block, weaponConfig.rarity);
|
|
|
+
|
|
|
+ // 加载武器图标
|
|
|
+ this.loadWeaponIcon(block, weaponConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置方块稀有度颜色
|
|
|
+ private setBlockRarityColor(block: Node, rarity: string) {
|
|
|
+ const sprite = block.getComponent(Sprite);
|
|
|
+ if (!sprite) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据稀有度设置颜色
|
|
|
+ let color: Color;
|
|
|
+ switch (rarity) {
|
|
|
+ case 'common':
|
|
|
+ color = new Color(255, 255, 255); // 白色
|
|
|
+ break;
|
|
|
+ case 'uncommon':
|
|
|
+ color = new Color(0, 255, 0); // 绿色
|
|
|
+ break;
|
|
|
+ case 'rare':
|
|
|
+ color = new Color(0, 100, 255); // 蓝色
|
|
|
+ break;
|
|
|
+ case 'epic':
|
|
|
+ color = new Color(160, 32, 240); // 紫色
|
|
|
+ break;
|
|
|
+ case 'legendary':
|
|
|
+ color = new Color(255, 165, 0); // 橙色
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ color = new Color(255, 255, 255); // 默认白色
|
|
|
+ }
|
|
|
+
|
|
|
+ sprite.color = color;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加载武器图标
|
|
|
+ private loadWeaponIcon(block: Node, weaponConfig: WeaponConfig) {
|
|
|
+ // 根据预制体结构:WeaponBlock -> B1 -> Weapon
|
|
|
+ const b1Node = block.getChildByName('B1');
|
|
|
+ if (!b1Node) {
|
|
|
+ console.warn('找不到B1节点');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const weaponNode = b1Node.getChildByName('Weapon');
|
|
|
+ if (!weaponNode) {
|
|
|
+ console.warn('找不到Weapon节点');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const weaponSprite = weaponNode.getComponent(Sprite);
|
|
|
+ if (!weaponSprite) {
|
|
|
+ console.warn('Weapon节点上没有Sprite组件');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取武器配置中的图片路径
|
|
|
+ const spriteConfig = weaponConfig.visualConfig?.weaponSprites;
|
|
|
+ if (!spriteConfig) {
|
|
|
+ console.warn(`武器 ${weaponConfig.name} 没有配置图片信息`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 选择合适的图片路径(这里默认使用1x1)
|
|
|
+ const spritePath = spriteConfig['1x1'] || spriteConfig['1x2'] || spriteConfig['2x1'] || spriteConfig['2x2'];
|
|
|
+ if (!spritePath) {
|
|
|
+ console.warn(`武器 ${weaponConfig.name} 没有可用的图片路径`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 正确的SpriteFrame子资源路径
|
|
|
+ const spriteFramePath = `${spritePath}/spriteFrame`;
|
|
|
+ console.log(`正在加载武器图片: ${spriteFramePath}`);
|
|
|
+
|
|
|
+ // 加载SpriteFrame子资源
|
|
|
+ resources.load(spriteFramePath, SpriteFrame, (err, spriteFrame) => {
|
|
|
+ if (err) {
|
|
|
+ console.warn(`加载武器图片失败: ${spriteFramePath}`, err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (weaponSprite && spriteFrame) {
|
|
|
+ weaponSprite.spriteFrame = spriteFrame;
|
|
|
+ console.log(`✅ 武器图片加载成功: ${weaponConfig.name} -> ${spriteFramePath}`);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据方块获取武器配置
|
|
|
+ public getBlockWeaponConfig(block: Node): WeaponConfig | null {
|
|
|
+ return this.blockWeaponConfigs.get(block) || block['weaponConfig'] || null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取方块的武器ID
|
|
|
+ public getBlockWeaponId(block: Node): string | null {
|
|
|
+ const weaponConfig = this.getBlockWeaponConfig(block);
|
|
|
+ return weaponConfig ? weaponConfig.id : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 刷新方块 - 重新生成三个新的武器方块
|
|
|
+ public refreshBlocks() {
|
|
|
+ console.log('刷新方块,生成新的武器方块');
|
|
|
+ this.generateRandomBlocksInKuang();
|
|
|
}
|
|
|
}
|