| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- import { _decorator, Component, Node, Button, Label, find, UITransform, Sprite, Color } from 'cc';
- import { LevelSessionManager } from '../../Core/LevelSessionManager';
- import { BallController } from '../BallController';
- import { BlockManager } from '../BlockManager';
- import { GameStartMove } from '../../Animations/GameStartMove';
- const { ccclass, property } = _decorator;
- @ccclass('GameBlockSelection')
- export class GameBlockSelection extends Component {
-
- @property({
- type: Node,
- tooltip: '拖拽BlockSelectionUI/diban/ann001按钮节点到这里'
- })
- public addBallButton: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽BlockSelectionUI/diban/ann002按钮节点到这里'
- })
- public addCoinButton: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽BlockSelectionUI/diban/ann003按钮节点到这里'
- })
- public refreshButton: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽Canvas-001/TopArea/CoinNode/CoinLabel节点到这里'
- })
- public coinLabelNode: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽Canvas/InsufficientCoinsUI节点到这里'
- })
- public insufficientCoinsUI: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽Canvas/GameLevelUI/BallController节点到这里'
- })
- public ballControllerNode: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽Canvas/GameLevelUI/BlockController节点到这里'
- })
- public blockManagerNode: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽Canvas/Camera节点到这里'
- })
- public cameraNode: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽Canvas/GameLevelUI/GameArea/GridContainer节点到这里'
- })
- public gridContainer: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽confirm按钮节点到这里'
- })
- public confirmButton: Node = null;
- // 按钮费用配置
- private readonly ADD_BALL_COST = 80;
- private readonly ADD_COIN_AMOUNT = 80;
- private readonly REFRESH_COST = 5;
- private session: LevelSessionManager = null;
- private ballController: BallController = null;
- private blockManager: BlockManager = null;
- private gameStartMove: GameStartMove = null;
- // 回调函数,用于通知GameManager
- public onConfirmCallback: () => void = null;
- start() {
- // 获取管理器实例
- this.session = LevelSessionManager.inst;
-
- // 获取BallController
- if (this.ballControllerNode) {
- this.ballController = this.ballControllerNode.getComponent(BallController);
- } else {
- console.warn('BallController节点未绑定,请在Inspector中拖拽Canvas/GameLevelUI/BallController节点');
- }
- // 获取BlockManager
- if (this.blockManagerNode) {
- this.blockManager = this.blockManagerNode.getComponent(BlockManager);
- } else {
- console.warn('BlockManager节点未绑定,请在Inspector中拖拽Canvas/GameLevelUI/BlockController节点');
- }
- // 获取GameStartMove组件
- if (this.cameraNode) {
- this.gameStartMove = this.cameraNode.getComponent(GameStartMove);
- }
- // 如果没有指定coinLabelNode,尝试找到它
- if (!this.coinLabelNode) {
- this.coinLabelNode = find('Canvas-001/TopArea/CoinNode/CoinLabel');
- }
- // 绑定按钮事件
- this.bindButtonEvents();
- }
- // 绑定按钮事件
- private bindButtonEvents() {
- // 绑定新增小球按钮
- if (this.addBallButton) {
- const btn = this.addBallButton.getComponent(Button);
- if (btn) {
- this.addBallButton.on(Button.EventType.CLICK, this.onAddBallClicked, this);
- } else {
- this.addBallButton.on(Node.EventType.TOUCH_END, this.onAddBallClicked, this);
- }
- }
- // 绑定增加金币按钮
- if (this.addCoinButton) {
- const btn = this.addCoinButton.getComponent(Button);
- if (btn) {
- this.addCoinButton.on(Button.EventType.CLICK, this.onAddCoinClicked, this);
- } else {
- this.addCoinButton.on(Node.EventType.TOUCH_END, this.onAddCoinClicked, this);
- }
- }
- // 绑定刷新方块按钮
- if (this.refreshButton) {
- const btn = this.refreshButton.getComponent(Button);
- if (btn) {
- this.refreshButton.on(Button.EventType.CLICK, this.onRefreshClicked, this);
- } else {
- this.refreshButton.on(Node.EventType.TOUCH_END, this.onRefreshClicked, this);
- }
- }
- // 绑定确认按钮
- if (this.confirmButton) {
- const btn = this.confirmButton.getComponent(Button);
- if (btn) {
- this.confirmButton.on(Button.EventType.CLICK, this.onConfirmButtonClicked, this);
- } else {
- this.confirmButton.on(Node.EventType.TOUCH_END, this.onConfirmButtonClicked, this);
- }
- }
- }
- // 新增小球按钮点击
- private onAddBallClicked() {
- if (!this.canSpendCoins(this.ADD_BALL_COST)) {
- this.showInsufficientCoinsUI();
- return;
- }
- // 扣除金币
- if (this.session.spendCoins(this.ADD_BALL_COST)) {
- this.updateCoinDisplay();
-
- // 创建新的小球
- if (this.ballController) {
- this.ballController.createBall();
- console.log(`新增小球成功,扣除${this.ADD_BALL_COST}金币`);
- } else {
- console.error('找不到BallController,无法创建小球');
- }
- }
- }
- // 增加金币按钮点击
- private onAddCoinClicked() {
- this.session.addCoins(this.ADD_COIN_AMOUNT);
- this.updateCoinDisplay();
- console.log(`增加${this.ADD_COIN_AMOUNT}金币`);
- }
- // 刷新方块按钮点击
- private onRefreshClicked() {
- if (!this.canSpendCoins(this.REFRESH_COST)) {
- this.showInsufficientCoinsUI();
- return;
- }
- // 扣除金币
- if (this.session.spendCoins(this.REFRESH_COST)) {
- this.updateCoinDisplay();
-
- // 刷新方块
- if (this.blockManager) {
- this.blockManager.refreshBlocks();
- console.log(`刷新方块成功,扣除${this.REFRESH_COST}金币`);
- } else {
- console.error('找不到BlockManager,无法刷新方块');
- }
- }
- }
- // 确认按钮点击(从GameManager迁移的onConfirmButtonClicked)
- public onConfirmButtonClicked() {
- // 执行相机动画和UI关闭
- if (this.gameStartMove) {
- this.gameStartMove.slideDibanDownAndHide();
- } else {
- // Fallback: 若未挂载 GameStartMove,直接关闭
- this.node.active = false;
- }
- // Camera move back up with smooth animation
- if (this.gameStartMove) {
- this.gameStartMove.moveUpSmooth();
- }
- // 显示GridContainer
- if (this.gridContainer) {
- this.gridContainer.active = true;
- }
-
- // 保存已放置的方块
- this.preservePlacedBlocks();
- // 回调通知GameManager
- if (this.onConfirmCallback) {
- this.onConfirmCallback();
- }
- }
- // 保存已放置的方块(从GameManager迁移)
- private preservePlacedBlocks() {
- if (this.blockManager) {
- this.blockManager.onGameStart();
- }
- }
- // 检查是否有足够金币
- private canSpendCoins(amount: number): boolean {
- return this.session.getCoins() >= amount;
- }
- // 更新金币显示
- private updateCoinDisplay() {
- if (this.coinLabelNode) {
- const label = this.coinLabelNode.getComponent(Label);
- if (label) {
- label.string = this.session.getCoins().toString();
- }
- }
- }
- // 显示金币不足UI
- private showInsufficientCoinsUI() {
- if (!this.insufficientCoinsUI) {
- console.error('金币不足UI节点未绑定,请在Inspector中拖拽Canvas/InsufficientCoinsUI节点');
- return;
- }
- // 显示金币不足UI
- this.insufficientCoinsUI.active = true;
- // 3秒后自动隐藏
- this.scheduleOnce(() => {
- if (this.insufficientCoinsUI && this.insufficientCoinsUI.isValid) {
- this.insufficientCoinsUI.active = false;
- }
- }, 3.0);
- console.log('金币不足!');
- }
- // === 公共方法:供GameManager调用 ===
- // 显示方块选择UI(用于游戏开始或下一波)
- public showBlockSelection(isNextWave: boolean = false) {
- this.node.active = true;
-
- if (this.gridContainer) {
- this.gridContainer.active = true;
- }
- console.log(isNextWave ? '显示下一波方块选择UI' : '显示游戏开始方块选择UI');
- }
- // 隐藏方块选择UI
- public hideBlockSelection() {
- this.node.active = false;
-
- if (this.gridContainer) {
- this.gridContainer.active = false;
- }
- console.log('隐藏方块选择UI');
- }
- // 设置确认回调
- public setConfirmCallback(callback: () => void) {
- this.onConfirmCallback = callback;
- }
- }
|