GameManager.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { _decorator, Component, Node, Prefab, instantiate, Vec3, find, director, Canvas, UITransform, Button, Label } 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. @property({
  21. type: Node,
  22. tooltip: '拖拽EnemyController节点到这里'
  23. })
  24. public enemyManager: Node = null;
  25. @property({
  26. type: Node,
  27. tooltip: '拖拽墙体血量显示节点到这里'
  28. })
  29. public wallHealthNode: Node = null;
  30. // 游戏是否已经开始
  31. private gameStarted: boolean = false;
  32. // 游戏区域的边界
  33. private gameBounds = {
  34. left: 0,
  35. right: 0,
  36. top: 0,
  37. bottom: 0
  38. };
  39. start() {
  40. // 计算游戏区域边界
  41. this.calculateGameBounds();
  42. // 初始化墙体血量显示
  43. this.initWallHealthDisplay();
  44. }
  45. // 计算游戏区域边界
  46. calculateGameBounds() {
  47. // 获取屏幕尺寸
  48. const canvas = find('Canvas');
  49. if (!canvas) {
  50. console.error('找不到Canvas节点');
  51. return;
  52. }
  53. const canvasUI = canvas.getComponent(UITransform);
  54. if (!canvasUI) {
  55. console.error('Canvas节点没有UITransform组件');
  56. return;
  57. }
  58. // 获取屏幕的尺寸
  59. const screenWidth = canvasUI.width;
  60. const screenHeight = canvasUI.height;
  61. // 获取屏幕的世界坐标位置
  62. const worldPos = canvas.worldPosition;
  63. // 计算屏幕的世界坐标边界
  64. this.gameBounds.left = worldPos.x - screenWidth / 2;
  65. this.gameBounds.right = worldPos.x + screenWidth / 2;
  66. this.gameBounds.bottom = worldPos.y - screenHeight / 2;
  67. this.gameBounds.top = worldPos.y + screenHeight / 2;
  68. console.log('Screen Bounds:', this.gameBounds);
  69. }
  70. // 初始化墙体血量显示
  71. initWallHealthDisplay() {
  72. if (!this.wallHealthNode) {
  73. // 尝试查找墙体血量显示节点
  74. this.wallHealthNode = find('Canvas/GameLevelUI/WallHealth');
  75. }
  76. if (this.wallHealthNode) {
  77. // 获取EnemyController组件
  78. const enemyControllerComp = this.getEnemyController();
  79. if (enemyControllerComp) {
  80. // 设置墙体血量显示节点
  81. (enemyControllerComp as any).wallHealthNode = this.wallHealthNode;
  82. }
  83. }
  84. }
  85. // 获取EnemyController组件
  86. getEnemyController() {
  87. if (!this.enemyManager) {
  88. this.enemyManager = find('Canvas/GameLevelUI/EnemyController');
  89. }
  90. if (this.enemyManager) {
  91. return this.enemyManager.getComponent('EnemyController');
  92. }
  93. return null;
  94. }
  95. // 点击确认按钮的回调
  96. onConfirmButtonClicked() {
  97. console.log('确认按钮被点击');
  98. // 隐藏BlockSelectionUI,但保留已放置的方块
  99. if (this.blockSelectionUI) {
  100. this.blockSelectionUI.active = false;
  101. // 查找GridContainer节点,确保它保持可见
  102. const gridContainer = find('Canvas/GameLevelUI/GameArea/GridContainer');
  103. if (gridContainer) {
  104. gridContainer.active = true;
  105. }
  106. // 确保已放置的方块保持可见
  107. this.preservePlacedBlocks();
  108. }
  109. // 开始游戏
  110. this.startGame();
  111. }
  112. // 保存已放置的方块
  113. preservePlacedBlocks() {
  114. // 查找BlockManager组件
  115. const blockController = find('Canvas/GameLevelUI/BlockController');
  116. if (blockController) {
  117. const blockManager = blockController.getComponent('BlockManager');
  118. if (blockManager) {
  119. // 调用BlockManager的onGameStart方法
  120. (blockManager as any).onGameStart();
  121. console.log('已调用BlockManager.onGameStart(),确保已放置的方块正确显示');
  122. } else {
  123. console.warn('BlockController节点上没有BlockManager组件');
  124. }
  125. } else {
  126. console.warn('找不到BlockController节点,尝试在Canvas上查找BlockManager组件');
  127. // 尝试在Canvas上查找
  128. const canvasBlockManager = find('Canvas').getComponent('BlockManager');
  129. if (canvasBlockManager) {
  130. // 调用BlockManager的onGameStart方法
  131. (canvasBlockManager as any).onGameStart();
  132. console.log('已调用Canvas上的BlockManager.onGameStart()');
  133. } else {
  134. console.warn('找不到BlockManager组件,无法保存已放置的方块状态');
  135. }
  136. }
  137. }
  138. // 开始游戏
  139. startGame() {
  140. if (this.gameStarted) return;
  141. console.log('游戏开始');
  142. this.gameStarted = true;
  143. // 使用BallController生成小球
  144. this.spawnBall();
  145. // 启动敌人生成
  146. const enemyControllerComp = this.getEnemyController();
  147. if (enemyControllerComp) {
  148. (enemyControllerComp as any).startGame();
  149. console.log('开始生成敌人');
  150. } else {
  151. console.warn('找不到EnemyController组件,无法开始生成敌人');
  152. }
  153. }
  154. // 生成小球
  155. spawnBall() {
  156. if (!this.ballController) {
  157. console.error('未设置BallController节点');
  158. return;
  159. }
  160. // 获取BallController组件
  161. const ballControllerComp = this.ballController.getComponent('BallController');
  162. if (ballControllerComp) {
  163. // 调用initialize方法创建小球
  164. (ballControllerComp as any).initialize();
  165. } else {
  166. console.error('BallController节点没有BallController组件');
  167. }
  168. }
  169. // 游戏结束
  170. gameOver() {
  171. if (!this.gameStarted) return;
  172. this.gameStarted = false;
  173. console.log('游戏结束');
  174. // 停止敌人生成
  175. const enemyControllerComp = this.getEnemyController();
  176. if (enemyControllerComp) {
  177. (enemyControllerComp as any).stopGame();
  178. console.log('停止生成敌人');
  179. }
  180. // 显示BlockSelectionUI
  181. if (this.blockSelectionUI) {
  182. this.blockSelectionUI.active = true;
  183. }
  184. }
  185. }