GameBlockSelection.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. import { _decorator, Component, Node, Button, Label, find, UITransform, Sprite, Color, tween, Tween, Prefab, instantiate } 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 { GamePause } from '../GamePause';
  7. import { BlockTag } from './BlockTag';
  8. import EventBus, { GameEvents } from '../../Core/EventBus';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('GameBlockSelection')
  11. export class GameBlockSelection extends Component {
  12. @property({
  13. type: Node,
  14. tooltip: '拖拽BlockSelectionUI/diban/ann001按钮节点到这里'
  15. })
  16. public addBallButton: Node = null;
  17. @property({
  18. type: Node,
  19. tooltip: '拖拽BlockSelectionUI/diban/ann002按钮节点到这里'
  20. })
  21. public addCoinButton: Node = null;
  22. @property({
  23. type: Node,
  24. tooltip: '拖拽BlockSelectionUI/diban/ann003按钮节点到这里'
  25. })
  26. public refreshButton: Node = null;
  27. @property({
  28. type: Node,
  29. tooltip: '拖拽Canvas-001/TopArea/CoinNode/CoinLabel节点到这里'
  30. })
  31. public coinLabelNode: Node = null;
  32. @property({
  33. type: Prefab,
  34. tooltip: '拖拽Toast预制体到这里'
  35. })
  36. public toastPrefab: Prefab = null;
  37. @property({
  38. type: Node,
  39. tooltip: '拖拽Canvas/GameLevelUI/BallController节点到这里'
  40. })
  41. public ballControllerNode: Node = null;
  42. @property({
  43. type: Node,
  44. tooltip: '拖拽Canvas/GameLevelUI/BlockController节点到这里'
  45. })
  46. public blockManagerNode: Node = null;
  47. @property({
  48. type: Node,
  49. tooltip: '拖拽Canvas/Camera节点到这里'
  50. })
  51. public cameraNode: Node = null;
  52. @property({
  53. type: Node,
  54. tooltip: '拖拽Canvas/GameLevelUI/GameArea/GridContainer节点到这里'
  55. })
  56. public gridContainer: Node = null;
  57. @property({
  58. type: Node,
  59. tooltip: '拖拽confirm按钮节点到这里'
  60. })
  61. public confirmButton: Node = null;
  62. @property({
  63. type: Node,
  64. tooltip: '拖拽BlockSelectionUI根节点到这里'
  65. })
  66. public blockSelectionUINode: Node = null;
  67. @property({
  68. type: Node,
  69. tooltip: '拖拽BlockSelectionUI/diban节点到这里'
  70. })
  71. public dibanNode: Node = null;
  72. // 按钮费用配置
  73. private readonly ADD_BALL_COST = 80;
  74. private readonly ADD_COIN_AMOUNT = 80;
  75. private readonly REFRESH_COST = 5;
  76. private session: LevelSessionManager = null;
  77. private ballController: BallController = null;
  78. private blockManager: BlockManager = null;
  79. private gameStartMove: GameStartMove = 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. // 绑定按钮事件
  128. this.bindButtonEvents();
  129. console.log('GameBlockSelection.start() 初始化完成');
  130. }
  131. // 绑定按钮事件
  132. private bindButtonEvents() {
  133. // 绑定新增小球按钮
  134. if (this.addBallButton) {
  135. const btn = this.addBallButton.getComponent(Button);
  136. if (btn) {
  137. this.addBallButton.on(Button.EventType.CLICK, this.onAddBallClicked, this);
  138. } else {
  139. this.addBallButton.on(Node.EventType.TOUCH_END, this.onAddBallClicked, this);
  140. }
  141. }
  142. // 绑定增加金币按钮
  143. if (this.addCoinButton) {
  144. const btn = this.addCoinButton.getComponent(Button);
  145. if (btn) {
  146. this.addCoinButton.on(Button.EventType.CLICK, this.onAddCoinClicked, this);
  147. } else {
  148. this.addCoinButton.on(Node.EventType.TOUCH_END, this.onAddCoinClicked, this);
  149. }
  150. }
  151. // 绑定刷新方块按钮
  152. if (this.refreshButton) {
  153. const btn = this.refreshButton.getComponent(Button);
  154. if (btn) {
  155. this.refreshButton.on(Button.EventType.CLICK, this.onRefreshClicked, this);
  156. } else {
  157. this.refreshButton.on(Node.EventType.TOUCH_END, this.onRefreshClicked, this);
  158. }
  159. }
  160. // 绑定确认按钮
  161. if (this.confirmButton) {
  162. const btn = this.confirmButton.getComponent(Button);
  163. if (btn) {
  164. this.confirmButton.on(Button.EventType.CLICK, this.onConfirmButtonClicked, this);
  165. } else {
  166. this.confirmButton.on(Node.EventType.TOUCH_END, this.onConfirmButtonClicked, this);
  167. }
  168. }
  169. }
  170. // 新增小球按钮点击
  171. private onAddBallClicked() {
  172. if (!this.canSpendCoins(this.ADD_BALL_COST)) {
  173. this.showInsufficientCoinsUI();
  174. return;
  175. }
  176. // 扣除金币
  177. if (this.session.spendCoins(this.ADD_BALL_COST)) {
  178. this.updateCoinDisplay();
  179. // 创建新的小球
  180. if (this.ballControllerNode) {
  181. const ballController = this.ballControllerNode.getComponent(BallController);
  182. if (ballController) {
  183. // 使用createAdditionalBall方法创建额外的小球,而不是替换现有的小球
  184. ballController.createAdditionalBall();
  185. console.log(`新增小球成功,扣除${this.ADD_BALL_COST}金币`);
  186. } else {
  187. console.error('找不到BallController组件');
  188. }
  189. } else {
  190. console.error('找不到BallController节点,无法创建小球');
  191. }
  192. }
  193. }
  194. // 增加金币按钮点击
  195. private onAddCoinClicked() {
  196. this.session.addCoins(this.ADD_COIN_AMOUNT);
  197. this.updateCoinDisplay();
  198. console.log(`增加${this.ADD_COIN_AMOUNT}金币`);
  199. }
  200. // 刷新方块按钮点击
  201. private onRefreshClicked() {
  202. if (!this.canSpendCoins(this.REFRESH_COST)) {
  203. this.showInsufficientCoinsUI();
  204. return;
  205. }
  206. // 扣除金币
  207. if (this.session.spendCoins(this.REFRESH_COST)) {
  208. this.updateCoinDisplay();
  209. // 刷新方块
  210. if (this.blockManager) {
  211. // 找到PlacedBlocks容器
  212. const placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
  213. if (placedBlocksContainer) {
  214. // 移除已放置方块的标签
  215. BlockTag.removeTagsInContainer(placedBlocksContainer);
  216. }
  217. // 刷新方块
  218. this.blockManager.refreshBlocks();
  219. console.log(`刷新方块成功,扣除${this.REFRESH_COST}金币`);
  220. } else {
  221. console.error('找不到BlockManager,无法刷新方块');
  222. }
  223. }
  224. }
  225. // 确认按钮点击(从GameManager迁移的onConfirmButtonClicked)
  226. public onConfirmButtonClicked() {
  227. // 检查是否有上阵方块
  228. const hasBlocks = this.hasPlacedBlocks();
  229. console.log(`[GameBlockSelection] 检查上阵方块: ${hasBlocks ? '有' : '无'}`);
  230. if (!hasBlocks) {
  231. this.showNoPlacedBlocksToast();
  232. return;
  233. }
  234. // 使用统一的隐藏方法,包含动画
  235. this.hideBlockSelection();
  236. // 保存已放置的方块
  237. this.preservePlacedBlocks();
  238. // 回调通知GameManager
  239. if (this.onConfirmCallback) {
  240. this.onConfirmCallback();
  241. }
  242. // 恢复游戏
  243. const gamePause = GamePause.getInstance();
  244. if (gamePause) {
  245. gamePause.resumeGame();
  246. }
  247. }
  248. // 保存已放置的方块(从GameManager迁移)
  249. private preservePlacedBlocks() {
  250. if (this.blockManager) {
  251. this.blockManager.onGameStart();
  252. }
  253. }
  254. // 检查是否有足够金币
  255. private canSpendCoins(amount: number): boolean {
  256. return this.session.getCoins() >= amount;
  257. }
  258. // 更新金币显示
  259. private updateCoinDisplay() {
  260. if (this.coinLabelNode) {
  261. const label = this.coinLabelNode.getComponent(Label);
  262. if (label) {
  263. label.string = this.session.getCoins().toString();
  264. }
  265. }
  266. }
  267. // 显示金币不足UI
  268. private showInsufficientCoinsUI() {
  269. if (!this.toastPrefab) {
  270. console.error('Toast预制体未绑定,请在Inspector中拖拽Toast预制体');
  271. return;
  272. }
  273. // 实例化Toast预制体
  274. const toastNode = instantiate(this.toastPrefab);
  275. // 设置Toast的文本为"金币不足!"
  276. const labelNode = toastNode.getChildByName('label');
  277. if (labelNode) {
  278. const label = labelNode.getComponent(Label);
  279. if (label) {
  280. label.string = '金币不足!';
  281. }
  282. }
  283. // 将Toast添加到Canvas下
  284. const canvas = find('Canvas');
  285. if (canvas) {
  286. canvas.addChild(toastNode);
  287. }
  288. // 3秒后自动销毁Toast
  289. this.scheduleOnce(() => {
  290. if (toastNode && toastNode.isValid) {
  291. toastNode.destroy();
  292. }
  293. }, 3.0);
  294. console.log('金币不足!');
  295. }
  296. // === 公共方法:供GameManager调用 ===
  297. // 显示方块选择UI(用于游戏开始或下一波)
  298. public showBlockSelection(isNextWave: boolean = false) {
  299. // 检查游戏是否已结束,使用GamePause来检查状态
  300. const gamePause = GamePause.getInstance();
  301. if (gamePause && gamePause.isGameOver()) {
  302. console.warn('[GameBlockSelection] 游戏已经结束,不显示方块选择UI');
  303. return;
  304. }
  305. // 首先显示UI节点
  306. this.node.active = true;
  307. if (this.gridContainer) {
  308. this.gridContainer.active = true;
  309. }
  310. // 如果有BlockSelectionUI节点,确保它可见
  311. if (this.blockSelectionUINode) {
  312. this.blockSelectionUINode.active = true;
  313. }
  314. // 每次显示方块选择UI时,生成新的随机方块
  315. if (this.blockManager) {
  316. this.blockManager.refreshBlocks();
  317. console.log('[GameBlockSelection] 生成新的随机方块');
  318. } else {
  319. console.warn('[GameBlockSelection] BlockManager未找到,无法生成随机方块');
  320. }
  321. // 播放BlockSelectionUI出现动画
  322. this.playShowAnimation();
  323. // 派发事件
  324. EventBus.getInstance().emit(GameEvents.BLOCK_SELECTION_OPEN);
  325. console.log(`[GameBlockSelection] ${isNextWave ? '显示下一波方块选择UI' : '显示游戏开始方块选择UI'}`);
  326. }
  327. // 隐藏方块选择UI
  328. public hideBlockSelection() {
  329. // 播放隐藏动画,动画完成后隐藏UI
  330. this.playHideAnimation(() => {
  331. this.node.active = false;
  332. // 移除隐藏GridContainer的代码,因为GridContainer应该在游戏过程中保持可见
  333. // if (this.gridContainer) {
  334. // this.gridContainer.active = false;
  335. // }
  336. // 派发事件
  337. EventBus.getInstance().emit(GameEvents.BLOCK_SELECTION_CLOSE);
  338. console.log('隐藏方块选择UI');
  339. });
  340. }
  341. // 播放显示动画
  342. private playShowAnimation() {
  343. console.log('播放显示动画playShowAnimation');
  344. if (this.gameStartMove && this.blockSelectionUINode && this.dibanNode) {
  345. // 设置GameStartMove的blockSelectionUI和dibanNode引用
  346. this.gameStartMove.blockSelectionUI = this.blockSelectionUINode;
  347. this.gameStartMove.dibanNode = this.dibanNode;
  348. // 每次显示BlockSelectionUI时,摄像头下移182px
  349. this.gameStartMove.moveDownInstant();
  350. console.log('摄像头下移182px');
  351. // 使用GameStartMove的上滑入场动画
  352. this.gameStartMove.slideUpFromBottom(300, 0.3);
  353. }
  354. }
  355. // 播放隐藏动画
  356. private playHideAnimation(onComplete?: () => void) {
  357. console.log('播放隐藏动画playHideAnimation');
  358. console.log('GameBlockSelection 引用检查:', {
  359. gameStartMove: !!this.gameStartMove,
  360. blockSelectionUINode: !!this.blockSelectionUINode,
  361. dibanNode: !!this.dibanNode,
  362. blockSelectionUINodeName: this.blockSelectionUINode?.name,
  363. dibanNodeName: this.dibanNode?.name
  364. });
  365. if (this.gameStartMove && this.blockSelectionUINode && this.dibanNode) {
  366. // 设置GameStartMove的blockSelectionUI和dibanNode引用
  367. this.gameStartMove.blockSelectionUI = this.blockSelectionUINode;
  368. this.gameStartMove.dibanNode = this.dibanNode;
  369. console.log('GameStartMove 引用设置后检查:', {
  370. gameStartMoveBlockSelectionUI: !!this.gameStartMove.blockSelectionUI,
  371. gameStartMoveDibanNode: !!this.gameStartMove.dibanNode
  372. });
  373. // 每次隐藏BlockSelectionUI时,摄像头上移182px
  374. this.gameStartMove.moveUpSmooth();
  375. console.log('摄像头上移182px');
  376. // 播放下滑隐藏动画,动画完成后执行回调
  377. this.gameStartMove.slideDibanDownAndHide(300, 0.3);
  378. // 由于slideDibanDownAndHide会自动隐藏blockSelectionUI和重置位置,我们需要在动画完成后执行回调
  379. if (onComplete) {
  380. this.scheduleOnce(() => {
  381. onComplete();
  382. }, 0.3); // 与动画时长一致
  383. }
  384. } else {
  385. console.log('GameBlockSelection 条件检查失败,无法播放动画');
  386. if (onComplete) {
  387. // 如果没有动画组件,直接执行回调
  388. onComplete();
  389. }
  390. }
  391. }
  392. // 设置确认回调
  393. public setConfirmCallback(callback: () => void) {
  394. this.onConfirmCallback = callback;
  395. }
  396. // 检查是否有上阵方块
  397. private hasPlacedBlocks(): boolean {
  398. // 优先使用BlockManager的方法检查
  399. if (this.blockManager) {
  400. return this.blockManager.hasPlacedBlocks();
  401. }
  402. // 备用方案:直接检查PlacedBlocks容器
  403. const placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
  404. if (!placedBlocksContainer) {
  405. console.warn('找不到PlacedBlocks容器');
  406. return false;
  407. }
  408. // 检查容器中是否有子节点(方块)
  409. return placedBlocksContainer.children.length > 0;
  410. }
  411. // 显示没有上阵方块的Toast提示
  412. private showNoPlacedBlocksToast() {
  413. if (!this.toastPrefab) {
  414. console.error('Toast预制体未绑定,请在Inspector中拖拽Toast预制体');
  415. return;
  416. }
  417. // 实例化Toast预制体
  418. const toastNode = instantiate(this.toastPrefab);
  419. // 设置Toast的文本为"请至少上阵一个植物!"
  420. const labelNode = toastNode.getChildByName('label');
  421. if (labelNode) {
  422. const label = labelNode.getComponent(Label);
  423. if (label) {
  424. label.string = '请至少上阵一个植物!';
  425. }
  426. }
  427. // 将Toast添加到Canvas下
  428. const canvas = find('Canvas');
  429. if (canvas) {
  430. canvas.addChild(toastNode);
  431. }
  432. // 3秒后自动销毁Toast
  433. this.scheduleOnce(() => {
  434. if (toastNode && toastNode.isValid) {
  435. toastNode.destroy();
  436. }
  437. }, 3.0);
  438. console.log('请至少上阵一个植物!');
  439. }
  440. }