GameManager.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { _decorator, Component, Node, Prefab, instantiate, Vec3, find, director, Canvas, UITransform, Button } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('GameManager')
  4. export class GameManager extends Component {
  5. @property({
  6. type: Node,
  7. tooltip: '拖拽BallController节点到这里'
  8. })
  9. public ballController: Node = null;
  10. @property({
  11. type: Node,
  12. tooltip: '拖拽BlockSelectionUI节点到这里'
  13. })
  14. public blockSelectionUI: Node = null;
  15. @property({
  16. type: Node,
  17. tooltip: '拖拽GameArea节点到这里'
  18. })
  19. public gameArea: Node = null;
  20. // 游戏是否已经开始
  21. private gameStarted: boolean = false;
  22. // 游戏区域的边界
  23. private gameBounds = {
  24. left: 0,
  25. right: 0,
  26. top: 0,
  27. bottom: 0
  28. };
  29. start() {
  30. // 如果没有指定blockSelectionUI,尝试找到它
  31. if (!this.blockSelectionUI) {
  32. this.blockSelectionUI = find('Canvas/GameLevelUI/BlockSelectionUI');
  33. if (!this.blockSelectionUI) {
  34. console.error('找不到BlockSelectionUI节点');
  35. return;
  36. }
  37. }
  38. // 如果没有指定gameArea,尝试找到它
  39. if (!this.gameArea) {
  40. this.gameArea = find('Canvas/GameLevelUI/GameArea');
  41. if (!this.gameArea) {
  42. console.error('找不到GameArea节点');
  43. return;
  44. }
  45. }
  46. // 如果没有指定ballController,尝试找到它
  47. if (!this.ballController) {
  48. this.ballController = find('Canvas/GameLevelUI/BallController');
  49. if (!this.ballController) {
  50. console.error('找不到BallController节点');
  51. return;
  52. }
  53. }
  54. // 计算游戏区域边界
  55. this.calculateGameBounds();
  56. // 注意:不再需要手动添加按钮事件监听
  57. // 可以在编辑器中通过Button组件的Click Events直接绑定onConfirmButtonClicked方法
  58. }
  59. // 计算游戏区域边界
  60. calculateGameBounds() {
  61. // 获取屏幕尺寸
  62. const canvas = find('Canvas');
  63. if (!canvas) {
  64. console.error('找不到Canvas节点');
  65. return;
  66. }
  67. const canvasUI = canvas.getComponent(UITransform);
  68. if (!canvasUI) {
  69. console.error('Canvas节点没有UITransform组件');
  70. return;
  71. }
  72. // 获取屏幕的尺寸
  73. const screenWidth = canvasUI.width;
  74. const screenHeight = canvasUI.height;
  75. // 获取屏幕的世界坐标位置
  76. const worldPos = canvas.worldPosition;
  77. // 计算屏幕的世界坐标边界
  78. this.gameBounds.left = worldPos.x - screenWidth / 2;
  79. this.gameBounds.right = worldPos.x + screenWidth / 2;
  80. this.gameBounds.bottom = worldPos.y - screenHeight / 2;
  81. this.gameBounds.top = worldPos.y + screenHeight / 2;
  82. console.log('Screen Bounds:', this.gameBounds);
  83. }
  84. // 点击确认按钮的回调
  85. onConfirmButtonClicked() {
  86. console.log('确认按钮被点击');
  87. // 隐藏BlockSelectionUI,但保留已放置的方块
  88. if (this.blockSelectionUI) {
  89. this.blockSelectionUI.active = false;
  90. // 查找GridContainer节点,确保它保持可见
  91. const gridContainer = find('Canvas/GameLevelUI/GameArea/GridContainer');
  92. if (gridContainer) {
  93. gridContainer.active = true;
  94. }
  95. // 确保已放置的方块保持可见
  96. this.preservePlacedBlocks();
  97. }
  98. // 开始游戏
  99. this.startGame();
  100. }
  101. // 保存已放置的方块
  102. preservePlacedBlocks() {
  103. // 查找BlockManager组件
  104. const blockController = find('Canvas/GameLevelUI/BlockController');
  105. if (blockController) {
  106. const blockManager = blockController.getComponent('BlockManager');
  107. if (blockManager) {
  108. // 调用BlockManager的onGameStart方法
  109. (blockManager as any).onGameStart();
  110. console.log('已调用BlockManager.onGameStart(),确保已放置的方块正确显示');
  111. } else {
  112. console.warn('BlockController节点上没有BlockManager组件');
  113. }
  114. } else {
  115. console.warn('找不到BlockController节点,尝试在Canvas上查找BlockManager组件');
  116. // 尝试在Canvas上查找
  117. const canvasBlockManager = find('Canvas').getComponent('BlockManager');
  118. if (canvasBlockManager) {
  119. // 调用BlockManager的onGameStart方法
  120. (canvasBlockManager as any).onGameStart();
  121. console.log('已调用Canvas上的BlockManager.onGameStart()');
  122. } else {
  123. console.warn('找不到BlockManager组件,无法保存已放置的方块状态');
  124. }
  125. }
  126. }
  127. // 开始游戏
  128. startGame() {
  129. if (this.gameStarted) return;
  130. console.log('游戏开始');
  131. this.gameStarted = true;
  132. // 使用BallController生成小球
  133. this.spawnBall();
  134. }
  135. // 生成小球
  136. spawnBall() {
  137. if (!this.ballController) {
  138. console.error('未设置BallController节点');
  139. return;
  140. }
  141. // 获取BallController组件
  142. const ballControllerComp = this.ballController.getComponent('BallController');
  143. if (ballControllerComp) {
  144. // 调用initialize方法创建小球
  145. (ballControllerComp as any).initialize();
  146. } else {
  147. console.error('BallController节点没有BallController组件');
  148. }
  149. }
  150. // 游戏结束
  151. gameOver() {
  152. this.gameStarted = false;
  153. console.log('游戏结束');
  154. // 显示BlockSelectionUI
  155. if (this.blockSelectionUI) {
  156. this.blockSelectionUI.active = true;
  157. }
  158. }
  159. }