GameManager.ts 6.8 KB

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