GameBlockSelection.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import { _decorator, Component, Node, Button, Label, find, UITransform, Sprite, Color, tween, Tween } from 'cc';
  2. import { LevelSessionManager } from '../../Core/LevelSessionManager';
  3. import { BallController } from '../BallController';
  4. import { BlockManager } from '../BlockManager';
  5. import { GameStartMove } from '../../Animations/GameStartMove';
  6. import { GameManager } from '../../LevelSystem/GameManager';
  7. import { GamePause } from '../GamePause';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('GameBlockSelection')
  10. export class GameBlockSelection extends Component {
  11. @property({
  12. type: Node,
  13. tooltip: '拖拽BlockSelectionUI/diban/ann001按钮节点到这里'
  14. })
  15. public addBallButton: Node = null;
  16. @property({
  17. type: Node,
  18. tooltip: '拖拽BlockSelectionUI/diban/ann002按钮节点到这里'
  19. })
  20. public addCoinButton: Node = null;
  21. @property({
  22. type: Node,
  23. tooltip: '拖拽BlockSelectionUI/diban/ann003按钮节点到这里'
  24. })
  25. public refreshButton: Node = null;
  26. @property({
  27. type: Node,
  28. tooltip: '拖拽Canvas-001/TopArea/CoinNode/CoinLabel节点到这里'
  29. })
  30. public coinLabelNode: Node = null;
  31. @property({
  32. type: Node,
  33. tooltip: '拖拽Canvas/InsufficientCoinsUI节点到这里'
  34. })
  35. public insufficientCoinsUI: Node = null;
  36. @property({
  37. type: Node,
  38. tooltip: '拖拽Canvas/GameLevelUI/BallController节点到这里'
  39. })
  40. public ballControllerNode: Node = null;
  41. @property({
  42. type: Node,
  43. tooltip: '拖拽Canvas/GameLevelUI/BlockController节点到这里'
  44. })
  45. public blockManagerNode: Node = null;
  46. @property({
  47. type: Node,
  48. tooltip: '拖拽Canvas/Camera节点到这里'
  49. })
  50. public cameraNode: Node = null;
  51. @property({
  52. type: Node,
  53. tooltip: '拖拽Canvas/GameLevelUI/GameArea/GridContainer节点到这里'
  54. })
  55. public gridContainer: Node = null;
  56. @property({
  57. type: Node,
  58. tooltip: '拖拽confirm按钮节点到这里'
  59. })
  60. public confirmButton: Node = null;
  61. @property({
  62. type: Node,
  63. tooltip: '拖拽BlockSelectionUI根节点到这里'
  64. })
  65. public blockSelectionUINode: Node = null;
  66. @property({
  67. type: Node,
  68. tooltip: '拖拽BlockSelectionUI/diban节点到这里'
  69. })
  70. public dibanNode: Node = null;
  71. // 按钮费用配置
  72. private readonly ADD_BALL_COST = 80;
  73. private readonly ADD_COIN_AMOUNT = 80;
  74. private readonly REFRESH_COST = 5;
  75. private session: LevelSessionManager = null;
  76. private ballController: BallController = null;
  77. private blockManager: BlockManager = null;
  78. private gameStartMove: GameStartMove = null;
  79. private gameManager: GameManager = null;
  80. // 回调函数,用于通知GameManager
  81. public onConfirmCallback: () => void = null;
  82. start() {
  83. console.log('GameBlockSelection.start() 开始初始化');
  84. // 获取管理器实例
  85. this.session = LevelSessionManager.inst;
  86. // 获取BallController
  87. if (this.ballControllerNode) {
  88. this.ballController = this.ballControllerNode.getComponent(BallController);
  89. } else {
  90. console.warn('BallController节点未绑定,请在Inspector中拖拽Canvas/GameLevelUI/BallController节点');
  91. }
  92. // 获取BlockManager
  93. if (this.blockManagerNode) {
  94. this.blockManager = this.blockManagerNode.getComponent(BlockManager);
  95. } else {
  96. console.warn('BlockManager节点未绑定,请在Inspector中拖拽Canvas/GameLevelUI/BlockController节点');
  97. }
  98. // 获取GameStartMove组件
  99. if (this.cameraNode) {
  100. this.gameStartMove = this.cameraNode.getComponent(GameStartMove);
  101. console.log('GameStartMove组件获取结果:', !!this.gameStartMove);
  102. // 如果GameStartMove存在,设置BlockSelectionUI和diban引用,并更新原始位置
  103. if (this.gameStartMove && this.blockSelectionUINode && this.dibanNode) {
  104. this.gameStartMove.blockSelectionUI = this.blockSelectionUINode;
  105. this.gameStartMove.dibanNode = this.dibanNode;
  106. this.gameStartMove.updateDibanOriginalPosition();
  107. console.log('GameStartMove引用设置完成:', {
  108. blockSelectionUISet: !!this.gameStartMove.blockSelectionUI,
  109. dibanNodeSet: !!this.gameStartMove.dibanNode,
  110. blockSelectionUINodeName: this.blockSelectionUINode.name,
  111. dibanNodeName: this.dibanNode.name
  112. });
  113. } else {
  114. console.warn('GameStartMove引用设置失败:', {
  115. gameStartMove: !!this.gameStartMove,
  116. blockSelectionUINode: !!this.blockSelectionUINode,
  117. dibanNode: !!this.dibanNode
  118. });
  119. }
  120. } else {
  121. console.warn('Camera节点未绑定,请在Inspector中拖拽Canvas/Camera节点');
  122. }
  123. // 如果没有指定coinLabelNode,尝试找到它
  124. if (!this.coinLabelNode) {
  125. this.coinLabelNode = find('Canvas-001/TopArea/CoinNode/CoinLabel');
  126. }
  127. // 获取GameManager实例
  128. const gameManagerNode = find('Canvas/GameManager');
  129. if (gameManagerNode) {
  130. this.gameManager = gameManagerNode.getComponent(GameManager);
  131. }
  132. // 绑定按钮事件
  133. this.bindButtonEvents();
  134. console.log('GameBlockSelection.start() 初始化完成');
  135. }
  136. // 绑定按钮事件
  137. private bindButtonEvents() {
  138. // 绑定新增小球按钮
  139. if (this.addBallButton) {
  140. const btn = this.addBallButton.getComponent(Button);
  141. if (btn) {
  142. this.addBallButton.on(Button.EventType.CLICK, this.onAddBallClicked, this);
  143. } else {
  144. this.addBallButton.on(Node.EventType.TOUCH_END, this.onAddBallClicked, this);
  145. }
  146. }
  147. // 绑定增加金币按钮
  148. if (this.addCoinButton) {
  149. const btn = this.addCoinButton.getComponent(Button);
  150. if (btn) {
  151. this.addCoinButton.on(Button.EventType.CLICK, this.onAddCoinClicked, this);
  152. } else {
  153. this.addCoinButton.on(Node.EventType.TOUCH_END, this.onAddCoinClicked, this);
  154. }
  155. }
  156. // 绑定刷新方块按钮
  157. if (this.refreshButton) {
  158. const btn = this.refreshButton.getComponent(Button);
  159. if (btn) {
  160. this.refreshButton.on(Button.EventType.CLICK, this.onRefreshClicked, this);
  161. } else {
  162. this.refreshButton.on(Node.EventType.TOUCH_END, this.onRefreshClicked, this);
  163. }
  164. }
  165. // 绑定确认按钮
  166. if (this.confirmButton) {
  167. const btn = this.confirmButton.getComponent(Button);
  168. if (btn) {
  169. this.confirmButton.on(Button.EventType.CLICK, this.onConfirmButtonClicked, this);
  170. } else {
  171. this.confirmButton.on(Node.EventType.TOUCH_END, this.onConfirmButtonClicked, this);
  172. }
  173. }
  174. }
  175. // 新增小球按钮点击
  176. private onAddBallClicked() {
  177. if (!this.canSpendCoins(this.ADD_BALL_COST)) {
  178. this.showInsufficientCoinsUI();
  179. return;
  180. }
  181. // 扣除金币
  182. if (this.session.spendCoins(this.ADD_BALL_COST)) {
  183. this.updateCoinDisplay();
  184. // 创建新的小球
  185. if (this.ballController) {
  186. this.ballController.createBall();
  187. console.log(`新增小球成功,扣除${this.ADD_BALL_COST}金币`);
  188. } else {
  189. console.error('找不到BallController,无法创建小球');
  190. }
  191. }
  192. }
  193. // 增加金币按钮点击
  194. private onAddCoinClicked() {
  195. this.session.addCoins(this.ADD_COIN_AMOUNT);
  196. this.updateCoinDisplay();
  197. console.log(`增加${this.ADD_COIN_AMOUNT}金币`);
  198. }
  199. // 刷新方块按钮点击
  200. private onRefreshClicked() {
  201. if (!this.canSpendCoins(this.REFRESH_COST)) {
  202. this.showInsufficientCoinsUI();
  203. return;
  204. }
  205. // 扣除金币
  206. if (this.session.spendCoins(this.REFRESH_COST)) {
  207. this.updateCoinDisplay();
  208. // 刷新方块
  209. if (this.blockManager) {
  210. this.blockManager.refreshBlocks();
  211. console.log(`刷新方块成功,扣除${this.REFRESH_COST}金币`);
  212. } else {
  213. console.error('找不到BlockManager,无法刷新方块');
  214. }
  215. }
  216. }
  217. // 确认按钮点击(从GameManager迁移的onConfirmButtonClicked)
  218. public onConfirmButtonClicked() {
  219. // 使用统一的隐藏方法,包含动画
  220. this.hideBlockSelection();
  221. // 保存已放置的方块
  222. this.preservePlacedBlocks();
  223. // 回调通知GameManager
  224. if (this.onConfirmCallback) {
  225. this.onConfirmCallback();
  226. }
  227. // 恢复游戏
  228. const gamePause = GamePause.getInstance();
  229. if (gamePause) {
  230. gamePause.resumeGame();
  231. }
  232. }
  233. // 保存已放置的方块(从GameManager迁移)
  234. private preservePlacedBlocks() {
  235. if (this.blockManager) {
  236. this.blockManager.onGameStart();
  237. }
  238. }
  239. // 检查是否有足够金币
  240. private canSpendCoins(amount: number): boolean {
  241. return this.session.getCoins() >= amount;
  242. }
  243. // 更新金币显示
  244. private updateCoinDisplay() {
  245. if (this.coinLabelNode) {
  246. const label = this.coinLabelNode.getComponent(Label);
  247. if (label) {
  248. label.string = this.session.getCoins().toString();
  249. }
  250. }
  251. }
  252. // 显示金币不足UI
  253. private showInsufficientCoinsUI() {
  254. if (!this.insufficientCoinsUI) {
  255. console.error('金币不足UI节点未绑定,请在Inspector中拖拽Canvas/InsufficientCoinsUI节点');
  256. return;
  257. }
  258. // 显示金币不足UI
  259. this.insufficientCoinsUI.active = true;
  260. // 3秒后自动隐藏
  261. this.scheduleOnce(() => {
  262. if (this.insufficientCoinsUI && this.insufficientCoinsUI.isValid) {
  263. this.insufficientCoinsUI.active = false;
  264. }
  265. }, 3.0);
  266. console.log('金币不足!');
  267. }
  268. // === 公共方法:供GameManager调用 ===
  269. // 显示方块选择UI(用于游戏开始或下一波)
  270. public showBlockSelection(isNextWave: boolean = false) {
  271. // 检查游戏是否已结束(胜利或失败),如果是则不显示方块选择UI
  272. if (this.gameManager && this.gameManager.isGameOver()) {
  273. console.warn('[GameBlockSelection] 游戏已经结束(胜利或失败),不显示方块选择UI');
  274. return;
  275. }
  276. // 首先显示UI节点
  277. this.node.active = true;
  278. if (this.gridContainer) {
  279. this.gridContainer.active = true;
  280. }
  281. // 如果有BlockSelectionUI节点,确保它可见
  282. if (this.blockSelectionUINode) {
  283. this.blockSelectionUINode.active = true;
  284. }
  285. // 每次显示方块选择UI时,生成新的随机方块
  286. if (this.blockManager) {
  287. this.blockManager.refreshBlocks();
  288. console.log('[GameBlockSelection] 生成新的随机方块');
  289. } else {
  290. console.warn('[GameBlockSelection] BlockManager未找到,无法生成随机方块');
  291. }
  292. // 播放BlockSelectionUI出现动画
  293. this.playShowAnimation();
  294. console.log(`[GameBlockSelection] ${isNextWave ? '显示下一波方块选择UI' : '显示游戏开始方块选择UI'}`);
  295. }
  296. // 隐藏方块选择UI
  297. public hideBlockSelection() {
  298. // 播放隐藏动画,动画完成后隐藏UI
  299. this.playHideAnimation(() => {
  300. this.node.active = false;
  301. // 移除隐藏GridContainer的代码,因为GridContainer应该在游戏过程中保持可见
  302. // if (this.gridContainer) {
  303. // this.gridContainer.active = false;
  304. // }
  305. console.log('隐藏方块选择UI');
  306. });
  307. }
  308. // 播放显示动画
  309. private playShowAnimation() {
  310. console.log('播放显示动画playShowAnimation');
  311. if (this.gameStartMove && this.blockSelectionUINode && this.dibanNode) {
  312. // 设置GameStartMove的blockSelectionUI和dibanNode引用
  313. this.gameStartMove.blockSelectionUI = this.blockSelectionUINode;
  314. this.gameStartMove.dibanNode = this.dibanNode;
  315. // 每次显示BlockSelectionUI时,摄像头下移182px
  316. this.gameStartMove.moveDownInstant();
  317. console.log('摄像头下移182px');
  318. // 使用GameStartMove的上滑入场动画
  319. this.gameStartMove.slideUpFromBottom(300, 0.3);
  320. }
  321. }
  322. // 播放隐藏动画
  323. private playHideAnimation(onComplete?: () => void) {
  324. console.log('播放隐藏动画playHideAnimation');
  325. console.log('GameBlockSelection 引用检查:', {
  326. gameStartMove: !!this.gameStartMove,
  327. blockSelectionUINode: !!this.blockSelectionUINode,
  328. dibanNode: !!this.dibanNode,
  329. blockSelectionUINodeName: this.blockSelectionUINode?.name,
  330. dibanNodeName: this.dibanNode?.name
  331. });
  332. if (this.gameStartMove && this.blockSelectionUINode && this.dibanNode) {
  333. // 设置GameStartMove的blockSelectionUI和dibanNode引用
  334. this.gameStartMove.blockSelectionUI = this.blockSelectionUINode;
  335. this.gameStartMove.dibanNode = this.dibanNode;
  336. console.log('GameStartMove 引用设置后检查:', {
  337. gameStartMoveBlockSelectionUI: !!this.gameStartMove.blockSelectionUI,
  338. gameStartMoveDibanNode: !!this.gameStartMove.dibanNode
  339. });
  340. // 每次隐藏BlockSelectionUI时,摄像头上移182px
  341. this.gameStartMove.moveUpSmooth();
  342. console.log('摄像头上移182px');
  343. // 播放下滑隐藏动画,动画完成后执行回调
  344. this.gameStartMove.slideDibanDownAndHide(300, 0.3);
  345. // 由于slideDibanDownAndHide会自动隐藏blockSelectionUI和重置位置,我们需要在动画完成后执行回调
  346. if (onComplete) {
  347. this.scheduleOnce(() => {
  348. onComplete();
  349. }, 0.3); // 与动画时长一致
  350. }
  351. } else {
  352. console.log('GameBlockSelection 条件检查失败,无法播放动画');
  353. if (onComplete) {
  354. // 如果没有动画组件,直接执行回调
  355. onComplete();
  356. }
  357. }
  358. }
  359. // 设置确认回调
  360. public setConfirmCallback(callback: () => void) {
  361. this.onConfirmCallback = callback;
  362. }
  363. }