| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281 |
- import { _decorator, Component, Node, Prefab, instantiate, Vec3, EventTouch, Vec2, UITransform, find, Rect, Label, Color, Size, Sprite, SpriteFrame, resources, Button } from 'cc';
- import { ConfigManager, WeaponConfig } from '../Core/ConfigManager';
- import { SaveDataManager } from '../LevelSystem/SaveDataManager';
- import { LevelSessionManager } from '../Core/LevelSessionManager';
- import { sp } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('BlockManager')
- export class BlockManager extends Component {
- // 预制体数组,存储5个预制体
- @property([Prefab])
- public blockPrefabs: Prefab[] = [];
-
- // 网格容器节点
- @property({
- type: Node,
- tooltip: '拖拽GridContainer节点到这里'
- })
- public gridContainer: Node = null;
-
- // 方块容器节点(kuang)
- @property({
- type: Node,
- tooltip: '拖拽kuang节点到这里'
- })
- public kuangContainer: Node = null;
-
- // 金币标签节点
- @property({
- type: Node,
- tooltip: '拖拽CoinLabel节点到这里'
- })
- public coinLabelNode: Node = null;
-
- // 已放置方块容器节点
- @property({
- type: Node,
- tooltip: '拖拽PlacedBlocks节点到这里(Canvas/GameLevelUI/PlacedBlocks)'
- })
- public placedBlocksContainer: Node = null;
-
- // 游戏是否已开始
- public gameStarted: boolean = false;
-
- // 移除移动冷却:相关属性/逻辑已废弃
-
- // SaveDataManager 单例
- private saveDataManager: SaveDataManager = null; // 仅用于局外数据
- private session: LevelSessionManager = null;
-
- // 方块价格标签映射
- private blockPriceMap: Map<Node, Node> = new Map();
-
- // 已经生成的块
- private blocks: Node[] = [];
- // 当前拖拽的块
- private currentDragBlock: Node | null = null;
- // 拖拽起始位置
- private startPos = new Vec2();
- // 块的起始位置
- private blockStartPos: Vec3 = new Vec3();
- // 网格占用情况,用于控制台输出
- private gridOccupationMap: number[][] = [];
- // 网格行数和列数
- private readonly GRID_ROWS = 6;
- private readonly GRID_COLS = 11;
- // 是否已初始化网格信息
- private gridInitialized = false;
- // 存储网格节点信息
- private gridNodes: Node[][] = [];
- // 网格间距
- private gridSpacing = 54;
- // 不参与占用的节点名称列表
- private readonly NON_BLOCK_NODES: string[] = ['Weapon', 'Price'];
- // 临时保存方块的原始占用格子
- private tempRemovedOccupiedGrids: { block: Node, occupiedGrids: { row: number, col: number }[] }[] = [];
- // 方块原始位置(在kuang中的位置)
- private originalPositions: Map<Node, Vec3> = new Map();
- // 方块当前所在的区域
- private blockLocations: Map<Node, string> = new Map();
-
- // 配置管理器
- private configManager: ConfigManager = null;
-
- // 方块武器配置映射
- private blockWeaponConfigs: Map<Node, WeaponConfig> = new Map();
-
- // 刷新方块按钮节点(ann003)
- @property({
- type: Node,
- tooltip: '拖拽BlockSelectionUI/diban/ann003按钮节点到这里'
- })
- public refreshButton: Node = null;
-
- /** 冷却机制已废弃,方块随时可移动 */
- private canMoveBlock(block: Node): boolean { return true; }
- // 清除所有冷却(游戏重置时调用)
- public clearAllCooldowns() { /* no-op */ }
- start() {
- // 获取配置管理器
- this.configManager = ConfigManager.getInstance();
- if (!this.configManager) {
- console.error('无法获取ConfigManager实例');
- }
- // 如果没有指定GridContainer,尝试找到它
- if (!this.gridContainer) {
- this.gridContainer = find('Canvas/GameLevelUI/GameArea/GridContainer');
- if (!this.gridContainer) {
- console.error('找不到GridContainer节点');
- return;
- }
- }
-
- // 如果没有指定kuangContainer,尝试找到它
- if (!this.kuangContainer) {
- this.kuangContainer = find('Canvas/GameLevelUI/BlockSelectionUI/diban/kuang');
- if (!this.kuangContainer) {
- console.error('找不到kuang节点');
- return;
- }
- }
-
- // 如果没有指定coinLabelNode,尝试找到它
- if (!this.coinLabelNode) {
- this.coinLabelNode = find('Canvas/GameLevelUI/CoinNode/CoinLabel');
- if (!this.coinLabelNode) {
- console.error('找不到CoinLabel节点');
- return;
- }
- }
-
- // 如果没有指定placedBlocksContainer,尝试找到它
- if (!this.placedBlocksContainer) {
- this.placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
- }
-
- // 确保有PlacedBlocks节点用于存放已放置的方块
- this.ensurePlacedBlocksNode();
-
- // 获取数据管理器
- this.saveDataManager = SaveDataManager.getInstance();
- this.session = LevelSessionManager.inst;
-
- // 初始化玩家金币显示
- this.updateCoinDisplay();
-
- // 初始化网格信息
- this.initGridInfo();
-
- // 初始化网格占用情况
- this.initGridOccupationMap();
-
- if (this.refreshButton) {
- const btn = this.refreshButton.getComponent(Button);
- if (btn) {
- this.refreshButton.on(Button.EventType.CLICK, this.onRefreshButtonClicked, this);
- } else {
- // 兼容没有 Button 组件的情况
- this.refreshButton.on(Node.EventType.TOUCH_END, this.onRefreshButtonClicked, this);
- }
- }
- // 等待配置加载完成后生成方块
- this.scheduleOnce(() => {
- this.generateRandomBlocksInKuang();
- }, 0.5);
- }
-
- // 确保有PlacedBlocks节点
- ensurePlacedBlocksNode() {
- // 如果已经通过拖拽设置了节点,直接使用
- if (this.placedBlocksContainer && this.placedBlocksContainer.isValid) {
- return;
- }
-
- // 尝试查找节点
- this.placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
- if (this.placedBlocksContainer) {
- 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节点');
- }
-
- // 初始化网格信息
- initGridInfo() {
- if (!this.gridContainer || this.gridInitialized) return;
-
- this.gridNodes = [];
- for (let row = 0; row < this.GRID_ROWS; row++) {
- this.gridNodes[row] = [];
- }
-
- for (let i = 0; i < this.gridContainer.children.length; i++) {
- const grid = this.gridContainer.children[i];
- if (grid.name.startsWith('Grid_')) {
- const parts = grid.name.split('_');
- if (parts.length === 3) {
- const row = parseInt(parts[1]);
- const col = parseInt(parts[2]);
-
- if (row >= 0 && row < this.GRID_ROWS && col >= 0 && col < this.GRID_COLS) {
- this.gridNodes[row][col] = grid;
- }
- }
- }
- }
-
- if (this.GRID_ROWS > 1 && this.GRID_COLS > 0) {
- if (this.gridNodes[0][0] && this.gridNodes[1][0]) {
- const pos1 = this.gridNodes[0][0].position;
- const pos2 = this.gridNodes[1][0].position;
- this.gridSpacing = Math.abs(pos2.y - pos1.y);
- }
- }
-
- this.gridInitialized = true;
- }
-
- // 初始化网格占用情况
- initGridOccupationMap() {
- this.gridOccupationMap = [];
- for (let row = 0; row < this.GRID_ROWS; row++) {
- const rowArray: number[] = [];
- for (let col = 0; col < this.GRID_COLS; col++) {
- rowArray.push(0);
- }
- this.gridOccupationMap.push(rowArray);
- }
- }
-
- // 在kuang下随机生成三个方块
- 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;
- }
-
- const kuangNode = this.kuangContainer;
- if (!kuangNode) {
- console.error('找不到kuang节点');
- return;
- }
-
- const offsets = [
- new Vec3(-200, 0, 0),
- new Vec3(0, 0, 0),
- new Vec3(200, 0, 0)
- ];
-
- const dbNodes = [
- kuangNode.getChildByName('db01'),
- kuangNode.getChildByName('db02'),
- kuangNode.getChildByName('db03')
- ];
-
- console.log('开始在kuang容器中生成随机武器方块');
-
- for (let i = 0; i < 3; i++) {
- // 获取随机武器配置
- const weaponConfig = this.configManager.getRandomWeapon();
- if (!weaponConfig) {
- console.error(`无法获取第 ${i + 1} 个武器配置`);
- continue;
- }
-
- // 基于武器配置选择合适的预制体
- const prefab = this.selectPrefabForWeapon(weaponConfig);
- if (!prefab) {
- console.error(`无法为武器 ${weaponConfig.name} 选择合适的预制体`);
- continue;
- }
-
- const block = instantiate(prefab);
- kuangNode.addChild(block);
-
- 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);
-
- if (dbNodes[i]) {
- const priceNode = dbNodes[i].getChildByName('Price');
- 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);
- }
-
- this.updateCoinDisplay();
- }
-
- // 将db节点与方块关联
- associateDbNodeWithBlock(block: Node, dbNode: Node) {
- block['dbNode'] = dbNode;
-
- block.on(Node.EventType.TRANSFORM_CHANGED, () => {
- if (dbNode && block.parent) {
- const location = this.blockLocations.get(block);
-
- if (location === 'grid') {
- dbNode.active = false;
- return;
- }
-
- dbNode.active = true;
-
- const worldPos = block.parent.getComponent(UITransform).convertToWorldSpaceAR(block.position);
- const localPos = dbNode.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
-
- dbNode.position = new Vec3(localPos.x, localPos.y - 80, localPos.z);
- }
- });
- }
-
- // 更新金币显示
- updateCoinDisplay() {
- if (this.coinLabelNode) {
- const label = this.coinLabelNode.getComponent(Label);
- if (label) {
- const coins = this.session.getCoins();
- label.string = coins.toString();
- }
- }
- }
-
- // 获取方块价格
- getBlockPrice(block: Node): number {
- const priceNode = this.blockPriceMap.get(block);
- if (priceNode) {
- const label = priceNode.getComponent(Label);
- if (label) {
- const price = parseInt(label.string);
- if (!isNaN(price)) {
- return price;
- }
- }
- }
- return 50;
- }
-
- // 隐藏价格标签
- hidePriceLabel(block: Node) {
- const priceNode = this.blockPriceMap.get(block);
- if (priceNode) {
- priceNode.active = false;
- }
- }
-
- // 显示价格标签
- showPriceLabel(block: Node) {
- const priceNode = this.blockPriceMap.get(block);
- if (priceNode) {
- priceNode.active = true;
- }
- }
-
- // 扣除玩家金币
- deductPlayerCoins(amount: number): boolean {
- const success = this.session.spendCoins(amount);
- if (success) {
- this.updateCoinDisplay();
- }
- return success;
- }
-
- // 归还玩家金币
- refundPlayerCoins(amount: number) {
- this.session.addCoins(amount);
- this.updateCoinDisplay();
- }
-
- // 设置拖拽事件
- setupDragEvents(block: Node) {
- block.on(Node.EventType.TOUCH_START, (event: EventTouch) => {
- if (this.gameStarted && this.blockLocations.get(block) === 'grid') {
- if (!this.canMoveBlock(block)) {
- return;
- }
- }
-
- this.currentDragBlock = block;
- this.startPos = event.getUILocation();
- this.blockStartPos.set(block.position);
- this.currentDragBlock['startLocation'] = this.blockLocations.get(block);
-
- block.setSiblingIndex(block.parent.children.length - 1);
- this.tempStoreBlockOccupiedGrids(block);
- }, this);
-
- block.on(Node.EventType.TOUCH_MOVE, (event: EventTouch) => {
- if (this.gameStarted && this.blockLocations.get(block) === 'grid') {
- if (!this.canMoveBlock(block)) {
- return;
- }
- }
-
- if (!this.currentDragBlock) return;
-
- const location = event.getUILocation();
- const deltaX = location.x - this.startPos.x;
- const deltaY = location.y - this.startPos.y;
-
- this.currentDragBlock.position = new Vec3(
- this.blockStartPos.x + deltaX,
- this.blockStartPos.y + deltaY,
- this.blockStartPos.z
- );
- }, this);
-
- block.on(Node.EventType.TOUCH_END, (event: EventTouch) => {
- if (this.gameStarted && this.blockLocations.get(block) === 'grid') {
- if (!this.canMoveBlock(block)) {
- return;
- }
- }
-
- if (this.currentDragBlock) {
- this.handleBlockDrop(event);
-
-
- this.currentDragBlock = null;
- }
- }, this);
-
- block.on(Node.EventType.TOUCH_CANCEL, () => {
- if (this.currentDragBlock) {
- this.returnBlockToOriginalPosition();
- this.currentDragBlock = null;
- }
- }, this);
- }
-
- // 处理方块放下
- handleBlockDrop(event: EventTouch) {
- const touchPos = event.getUILocation();
- const startLocation = this.currentDragBlock['startLocation'];
-
- if (this.isInKuangArea(touchPos)) {
- this.returnBlockToKuang(startLocation);
- } else if (this.tryPlaceBlockToGrid(this.currentDragBlock)) {
- this.handleSuccessfulPlacement(startLocation);
- } else {
- // 放置失败,尝试直接与重叠方块合成
- if (this.tryMergeOnOverlap(this.currentDragBlock)) {
- // 合成成功时,若来自 kuang 则扣费
- if (startLocation !== 'grid') {
- const price = this.getBlockPrice(this.currentDragBlock);
- this.deductPlayerCoins(price);
- }
- // 当前拖拽块已被销毁(合并时),无需复位
- } else {
- this.returnBlockToOriginalPosition();
- }
- }
- }
-
- // 返回方块到kuang区域
- returnBlockToKuang(startLocation: string) {
- const originalPos = this.originalPositions.get(this.currentDragBlock);
- if (originalPos) {
- const kuangNode = this.kuangContainer;
- if (kuangNode && this.currentDragBlock.parent !== kuangNode) {
- this.currentDragBlock.removeFromParent();
- kuangNode.addChild(this.currentDragBlock);
- }
-
- this.currentDragBlock.position = originalPos.clone();
- }
-
- this.restoreBlockOccupiedGrids(this.currentDragBlock);
- this.blockLocations.set(this.currentDragBlock, 'kuang');
- this.showPriceLabel(this.currentDragBlock);
-
- if (startLocation === 'grid') {
- const price = this.getBlockPrice(this.currentDragBlock);
- this.refundPlayerCoins(price);
- this.currentDragBlock['placedBefore'] = false;
- }
-
- const dbNode = this.currentDragBlock['dbNode'];
- if (dbNode) {
- dbNode.active = true;
- this.currentDragBlock.emit(Node.EventType.TRANSFORM_CHANGED);
- }
- }
-
- // 处理成功放置
- handleSuccessfulPlacement(startLocation: string) {
- const price = this.getBlockPrice(this.currentDragBlock);
-
- if (startLocation === 'grid') {
- this.clearTempStoredOccupiedGrids(this.currentDragBlock);
- this.blockLocations.set(this.currentDragBlock, 'grid');
- this.hidePriceLabel(this.currentDragBlock);
-
- const dbNode = this.currentDragBlock['dbNode'];
- if (dbNode) {
- dbNode.active = false;
- }
-
- // 立即将方块移动到PlacedBlocks节点下,不等游戏开始
- this.moveBlockToPlacedBlocks(this.currentDragBlock);
-
- // 如果游戏已开始,添加锁定视觉提示
- if (this.gameStarted) {
- this.addLockedVisualHint(this.currentDragBlock);
- }
- +
- + // 检查并执行合成
- + this.tryMergeBlock(this.currentDragBlock);
- } else {
- if (this.deductPlayerCoins(price)) {
- this.clearTempStoredOccupiedGrids(this.currentDragBlock);
- this.blockLocations.set(this.currentDragBlock, 'grid');
- this.hidePriceLabel(this.currentDragBlock);
-
- const dbNode = this.currentDragBlock['dbNode'];
- if (dbNode) {
- dbNode.active = false;
- }
-
- this.currentDragBlock['placedBefore'] = true;
-
- // 立即将方块移动到PlacedBlocks节点下,不等游戏开始
- this.moveBlockToPlacedBlocks(this.currentDragBlock);
-
- // 如果游戏已开始,添加锁定视觉提示
- if (this.gameStarted) {
- this.addLockedVisualHint(this.currentDragBlock);
- }
- +
- + // 检查并执行合成
- + this.tryMergeBlock(this.currentDragBlock);
- } else {
- this.returnBlockToOriginalPosition();
- }
- }
- }
-
- // 返回方块到原位置
- returnBlockToOriginalPosition() {
- const currentLocation = this.blockLocations.get(this.currentDragBlock);
- if (currentLocation === 'kuang') {
- const originalPos = this.originalPositions.get(this.currentDragBlock);
- if (originalPos) {
- this.currentDragBlock.position = originalPos.clone();
- }
- } else {
- this.currentDragBlock.position = this.blockStartPos.clone();
- }
-
- this.restoreBlockOccupiedGrids(this.currentDragBlock);
- this.showPriceLabel(this.currentDragBlock);
-
- const dbNode = this.currentDragBlock['dbNode'];
- if (dbNode) {
- dbNode.active = true;
- this.currentDragBlock.emit(Node.EventType.TRANSFORM_CHANGED);
- }
- }
-
- // 检查是否在kuang区域内
- isInKuangArea(touchPos: Vec2): boolean {
- if (!this.kuangContainer) return false;
-
- const kuangTransform = this.kuangContainer.getComponent(UITransform);
- if (!kuangTransform) return false;
-
- const kuangBoundingBox = new Rect(
- this.kuangContainer.worldPosition.x - kuangTransform.width * kuangTransform.anchorX,
- this.kuangContainer.worldPosition.y - kuangTransform.height * kuangTransform.anchorY,
- kuangTransform.width,
- kuangTransform.height
- );
-
- return kuangBoundingBox.contains(new Vec2(touchPos.x, touchPos.y));
- }
-
- // 临时保存方块占用的网格
- tempStoreBlockOccupiedGrids(block: Node) {
- const occupiedGrids = block['occupiedGrids'];
- if (!occupiedGrids || occupiedGrids.length === 0) return;
-
- this.tempRemovedOccupiedGrids.push({
- block: block,
- occupiedGrids: [...occupiedGrids]
- });
-
- for (const grid of occupiedGrids) {
- if (grid.row >= 0 && grid.row < this.GRID_ROWS &&
- grid.col >= 0 && grid.col < this.GRID_COLS) {
- this.gridOccupationMap[grid.row][grid.col] = 0;
- }
- }
-
- block['occupiedGrids'] = [];
- }
-
- // 恢复方块原来的占用状态
- restoreBlockOccupiedGrids(block: Node) {
- const index = this.tempRemovedOccupiedGrids.findIndex(item => item.block === block);
- if (index === -1) return;
-
- const savedItem = this.tempRemovedOccupiedGrids[index];
-
- for (const grid of savedItem.occupiedGrids) {
- if (grid.row >= 0 && grid.row < this.GRID_ROWS &&
- grid.col >= 0 && grid.col < this.GRID_COLS) {
- this.gridOccupationMap[grid.row][grid.col] = 1;
- }
- }
-
- block['occupiedGrids'] = [...savedItem.occupiedGrids];
- this.tempRemovedOccupiedGrids.splice(index, 1);
- }
-
- // 清除临时保存的占用状态
- clearTempStoredOccupiedGrids(block: Node) {
- const index = this.tempRemovedOccupiedGrids.findIndex(item => item.block === block);
- if (index === -1) return;
-
- this.tempRemovedOccupiedGrids.splice(index, 1);
- }
-
- // 尝试将方块放置到网格中
- tryPlaceBlockToGrid(block: Node): boolean {
- if (!this.gridContainer || !this.gridInitialized) return false;
-
- let b1Node = block;
- if (block.name !== 'B1') {
- b1Node = block.getChildByName('B1');
- if (!b1Node) {
- return false;
- }
- }
-
- const b1WorldPos = b1Node.parent.getComponent(UITransform).convertToWorldSpaceAR(b1Node.position);
- const gridPos = this.gridContainer.getComponent(UITransform).convertToNodeSpaceAR(b1WorldPos);
-
- const gridSize = this.gridContainer.getComponent(UITransform).contentSize;
- const halfWidth = gridSize.width / 2;
- const halfHeight = gridSize.height / 2;
-
- const tolerance = this.gridSpacing * 0.5;
- if (gridPos.x < -halfWidth - tolerance || gridPos.x > halfWidth + tolerance ||
- gridPos.y < -halfHeight - tolerance || gridPos.y > halfHeight + tolerance) {
- return false;
- }
-
- const nearestGrid = this.findNearestGridNode(gridPos);
- if (!nearestGrid) {
- return false;
- }
-
- return this.tryPlaceBlockToSpecificGrid(block, nearestGrid);
- }
-
- // 找到最近的网格节点
- findNearestGridNode(position: Vec3): Node {
- if (!this.gridContainer || !this.gridInitialized) return null;
-
- let nearestNode: Node = null;
- let minDistance = Number.MAX_VALUE;
-
- for (let row = 0; row < this.GRID_ROWS; row++) {
- for (let col = 0; col < this.GRID_COLS; col++) {
- const grid = this.gridNodes[row][col];
- if (grid) {
- const distance = Vec3.distance(position, grid.position);
- if (distance < minDistance) {
- minDistance = distance;
- nearestNode = grid;
- }
- }
- }
- }
-
- if (minDistance > this.gridSpacing * 2) {
- return null;
- }
-
- return nearestNode;
- }
-
- // 尝试将方块放置到指定的网格节点
- tryPlaceBlockToSpecificGrid(block: Node, targetGrid: Node): boolean {
- let b1Node = block;
- if (block.name !== 'B1') {
- b1Node = block.getChildByName('B1');
- if (!b1Node) {
- return false;
- }
- }
-
- if (!this.canPlaceBlockAt(block, targetGrid)) {
- return false;
- }
-
- const gridCenterWorldPos = this.gridContainer.getComponent(UITransform).convertToWorldSpaceAR(targetGrid.position);
- const targetWorldPos = gridCenterWorldPos.clone();
-
- const b1LocalPos = b1Node.position.clone();
-
- let rootTargetWorldPos;
- if (b1Node === block) {
- rootTargetWorldPos = targetWorldPos.clone();
- } else {
- rootTargetWorldPos = new Vec3(
- targetWorldPos.x - b1LocalPos.x,
- targetWorldPos.y - b1LocalPos.y,
- targetWorldPos.z
- );
- }
-
- const rootTargetLocalPos = block.parent.getComponent(UITransform).convertToNodeSpaceAR(rootTargetWorldPos);
-
- block.position = rootTargetLocalPos;
- this.markOccupiedPositions(block, targetGrid);
-
- return true;
- }
-
- // 检查方块是否可以放置在指定位置
- canPlaceBlockAt(block: Node, targetGrid: Node): boolean {
- if (!this.gridInitialized) return false;
-
- const targetRowCol = this.getGridRowCol(targetGrid);
- if (!targetRowCol) return false;
-
- const parts = this.getBlockParts(block);
-
- for (const part of parts) {
- const row = targetRowCol.row - part.y;
- const col = targetRowCol.col + part.x;
-
- if (row < 0 || row >= this.GRID_ROWS || col < 0 || col >= this.GRID_COLS) {
- return false;
- }
-
- if (this.gridOccupationMap[row][col] === 1) {
- return false;
- }
- }
-
- return true;
- }
-
- // 获取网格行列索引
- getGridRowCol(gridNode: Node): { row: number, col: number } | null {
- if (!gridNode || !gridNode.name.startsWith('Grid_')) return null;
-
- const parts = gridNode.name.split('_');
- if (parts.length === 3) {
- const row = parseInt(parts[1]);
- const col = parseInt(parts[2]);
-
- if (row >= 0 && row < this.GRID_ROWS && col >= 0 && col < this.GRID_COLS) {
- return { row, col };
- }
- }
-
- return null;
- }
-
- // 获取方块的所有部分节点及其相对坐标
- getBlockParts(block: Node): { node: Node, x: number, y: number }[] {
- const parts: { node: Node, x: number, y: number }[] = [];
-
- parts.push({ node: block, x: 0, y: 0 });
- this.findBlockParts(block, parts, 0, 0);
-
- return parts;
- }
-
- // 递归查找方块的所有部分
- findBlockParts(node: Node, result: { node: Node, x: number, y: number }[], parentX: number, parentY: number) {
- for (let i = 0; i < node.children.length; i++) {
- const child = node.children[i];
-
- if (this.NON_BLOCK_NODES.indexOf(child.name) !== -1) {
- continue;
- }
-
- let x = parentX;
- let y = parentY;
-
- const match = child.name.match(/^\((-?\d+),(-?\d+)\)$/);
- if (match) {
- x = parseInt(match[1]);
- y = parseInt(match[2]);
- result.push({ node: child, x, y });
- } else if (child.name.startsWith('B')) {
- const relativeX = Math.round(child.position.x / this.gridSpacing);
- const relativeY = -Math.round(child.position.y / this.gridSpacing);
-
- x = parentX + relativeX;
- y = parentY + relativeY;
-
- result.push({ node: child, x, y });
- }
-
- this.findBlockParts(child, result, x, y);
- }
- }
-
- // 标记方块占用的格子
- markOccupiedPositions(block: Node, targetGrid: Node) {
- if (!this.gridInitialized) return;
-
- const targetRowCol = this.getGridRowCol(targetGrid);
- if (!targetRowCol) return;
-
- const parts = this.getBlockParts(block);
-
- block['occupiedGrids'] = [];
-
- for (const part of parts) {
- const row = targetRowCol.row - part.y;
- const col = targetRowCol.col + part.x;
-
- if (row >= 0 && row < this.GRID_ROWS && col >= 0 && col < this.GRID_COLS) {
- this.gridOccupationMap[row][col] = 1;
-
- block['occupiedGrids'] = block['occupiedGrids'] || [];
- block['occupiedGrids'].push({ row, col });
- }
- }
- }
-
- // 清除方块
- clearBlocks() {
- const blocksToRemove = [];
-
- for (const block of this.blocks) {
- if (block.isValid) {
- const location = this.blockLocations.get(block);
- if (location === 'kuang') {
- blocksToRemove.push(block);
- }
- }
- }
-
- for (const block of blocksToRemove) {
- const dbNode = block['dbNode'];
- if (dbNode && dbNode.isValid) {
- block.off(Node.EventType.TRANSFORM_CHANGED);
-
- const kuangNode = this.kuangContainer;
- if (kuangNode) {
- const dbName = dbNode.name;
- if (!kuangNode.getChildByName(dbName)) {
- dbNode.parent = kuangNode;
- }
- }
- }
-
- 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);
- // 清理武器配置映射
- this.blockWeaponConfigs.delete(block);
-
- block.destroy();
- }
- }
-
- // 游戏开始时调用
- onGameStart() {
- this.gameStarted = true;
-
- for (const block of this.blocks) {
- if (block.isValid) {
- const location = this.blockLocations.get(block);
-
- if (location === 'grid') {
- this.hidePriceLabel(block);
-
- const dbNode = block['dbNode'];
- if (dbNode) {
- dbNode.active = false;
- }
-
- this.moveBlockToPlacedBlocks(block);
- this.addLockedVisualHint(block);
- }
- }
- }
- }
-
- // 游戏重置时调用
- onGameReset() {
- this.gameStarted = false;
- this.clearAllCooldowns();
- }
-
- // 添加视觉提示,表明方块已锁定
- addLockedVisualHint(block: Node) {
- const children = block.children;
- for (let i = 0; i < children.length; i++) {
- const child = children[i];
- if (this.NON_BLOCK_NODES.indexOf(child.name) !== -1) {
- continue;
- }
-
- child.setScale(new Vec3(0.95, 0.95, 1));
- }
-
- }
-
- // 将方块移动到PlacedBlocks节点下
- moveBlockToPlacedBlocks(block: Node) {
- if (!this.placedBlocksContainer) {
- console.error('PlacedBlocks容器未设置');
- return;
- }
-
- if (!this.placedBlocksContainer.isValid) {
- console.error('PlacedBlocks容器已失效');
- return;
- }
-
- const worldPosition = new Vec3();
- block.getWorldPosition(worldPosition);
-
- // 移除旧的触摸事件监听器
- block.off(Node.EventType.TOUCH_START);
- block.off(Node.EventType.TOUCH_MOVE);
- block.off(Node.EventType.TOUCH_END);
- block.off(Node.EventType.TOUCH_CANCEL);
-
- block.removeFromParent();
- this.placedBlocksContainer.addChild(block);
- block.setWorldPosition(worldPosition);
-
- // 重新设置触摸事件监听器
- this.setupDragEvents(block);
- }
-
- // 根据武器配置选择合适的预制体
- 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 = 10;
- break;
- case 'uncommon':
- price = 20;
- break;
- case 'rare':
- price = 50;
- break;
- case 'epic':
- price = 100;
- break;
- case 'legendary':
- price = 150;
- break;
- default:
- price = 200;
- }
-
- 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`;
-
- // 加载SpriteFrame子资源
- resources.load(spriteFramePath, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.warn(`加载武器图片失败: ${spriteFramePath}`, err);
- return;
- }
-
- if (weaponSprite && spriteFrame) {
- weaponSprite.spriteFrame = spriteFrame;
- }
- });
- }
-
- // 根据方块获取武器配置
- 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() {
- this.generateRandomBlocksInKuang();
- }
- // 刷新按钮点击回调
- private onRefreshButtonClicked() {
- this.refreshBlocks();
- }
- /* =================== 合成逻辑 =================== */
- private readonly rarityOrder: string[] = ['common','uncommon','rare','epic','legendary'];
- /** 检查当前放置的方块能否与相同稀有度的方块合成 */
- private tryMergeBlock(block: Node) {
- const rarity = this.getBlockRarity(block);
- if (!rarity) return;
- // 在 placedBlocksContainer 中寻找与之重叠且同稀有度的其他方块
- if (!this.placedBlocksContainer) return;
- const weaponId = this.getBlockWeaponId(block);
- const blockBB = this.getWorldAABB(block);
- for (const other of this.placedBlocksContainer.children) {
- if (other === block) continue;
- if (this.getBlockRarity(other) !== rarity) continue;
- if (this.getBlockWeaponId(other) !== weaponId) continue;
- const otherBB = this.getWorldAABB(other);
- if (this.rectIntersects(blockBB, otherBB)) {
- // 找到合成目标
- this.performMerge(block, other, rarity);
- break;
- }
- }
- }
- private performMerge(target: Node, source: Node, rarity: string) {
- // 隐藏价格标签并处理 db 关联
- this.hidePriceLabel(source);
- const srcDb = source['dbNode'];
- if (srcDb) srcDb.active = false;
- // 销毁被合并方块
- source.destroy();
- // 升级稀有度
- const nextRarity = this.getNextRarity(rarity);
- if (nextRarity) {
- this.setBlockRarityColor(target, nextRarity);
- // 如果有武器配置,也升级
- const cfg = this.blockWeaponConfigs.get(target);
- if (cfg) {
- cfg.rarity = nextRarity;
- this.blockWeaponConfigs.set(target, cfg);
- }
- }
- // 播放烟雾动画
- const worldPos = new Vec3();
- target.getWorldPosition(worldPos);
- this.spawnMergeSmoke(worldPos);
- // 递归检查是否还能继续合成
- if (nextRarity) {
- this.tryMergeBlock(target);
- }
- }
- private getBlockRarity(block: Node): string | null {
- const cfg = this.blockWeaponConfigs.get(block);
- if (cfg) return cfg.rarity;
- // fallback based on sprite color maybe
- return null;
- }
- private getNextRarity(rarity: string): string | null {
- const idx = this.rarityOrder.indexOf(rarity);
- if (idx === -1 || idx >= this.rarityOrder.length - 1) return null;
- return this.rarityOrder[idx + 1];
- }
- private getWorldAABB(node: Node): Rect {
- const ui = node.getComponent(UITransform);
- if (!ui) return new Rect();
- const pos = node.worldPosition;
- const width = ui.width;
- const height = ui.height;
- return new Rect(pos.x - width/2, pos.y - height/2, width, height);
- }
- private rectIntersects(a: Rect, b: Rect): boolean {
- return a.x < b.x + b.width &&
- a.x + a.width > b.x &&
- a.y < b.y + b.height &&
- a.y + a.height > b.y;
- }
- /** 生成烟雾特效 */
- private spawnMergeSmoke(worldPos: Vec3) {
- const path = 'Animation/WeaponTx/tx0003/tx0003';
- resources.load(path, sp.SkeletonData, (err, sData: sp.SkeletonData) => {
- if (err || !sData) {
- console.warn('加载合成烟雾动画失败', err);
- return;
- }
- const node = new Node('MergeSmoke');
- const skeleton = node.addComponent(sp.Skeleton);
- skeleton.skeletonData = sData;
- skeleton.premultipliedAlpha = false;
- skeleton.setAnimation(0, 'animation', false);
- skeleton.setCompleteListener(() => node.destroy());
- const canvas = find('Canvas');
- if (canvas) canvas.addChild(node);
- node.setWorldPosition(worldPos);
- });
- }
- /** 在放置失败时尝试与现有方块进行合成 */
- private tryMergeOnOverlap(draggedBlock: Node): boolean {
- if (!this.placedBlocksContainer) return false;
- const rarity = this.getBlockRarity(draggedBlock);
- if (!rarity) return false;
- const weaponId = this.getBlockWeaponId(draggedBlock);
- const dragBB = this.getWorldAABB(draggedBlock);
- for (const target of this.placedBlocksContainer.children) {
- if (target === draggedBlock) continue;
- if (this.getBlockRarity(target) !== rarity) continue;
- if (this.getBlockWeaponId(target) !== weaponId) continue;
- const targetBB = this.getWorldAABB(target);
- if (this.rectIntersects(dragBB, targetBB)) {
- // 执行合并:目标保留,拖拽方块销毁
- this.performMerge(target, draggedBlock, rarity);
- return true;
- }
- }
- return false;
- }
- }
|