GameManager.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. console.log('隐藏BlockSelectionUI...');
  101. this.blockSelectionUI.active = false;
  102. // 查找GridContainer节点,确保它保持可见
  103. const gridContainer = find('Canvas/GameLevelUI/GameArea/GridContainer');
  104. if (gridContainer) {
  105. gridContainer.active = true;
  106. console.log('GridContainer保持可见');
  107. }
  108. // 确保已放置的方块保持可见
  109. console.log('准备调用preservePlacedBlocks...');
  110. this.preservePlacedBlocks();
  111. } else {
  112. console.warn('blockSelectionUI未设置或为null');
  113. }
  114. // 开始游戏
  115. console.log('准备开始游戏...');
  116. this.startGame();
  117. }
  118. // 保存已放置的方块
  119. preservePlacedBlocks() {
  120. console.log('=== preservePlacedBlocks 开始执行 ===');
  121. // 查找BlockManager组件
  122. const blockController = find('Canvas/GameLevelUI/BlockController');
  123. console.log('BlockController节点查找结果:', blockController);
  124. if (blockController) {
  125. console.log('找到BlockController节点,开始查找BlockManager组件...');
  126. // 显示所有组件
  127. const allComponents = blockController.getComponents(Component);
  128. console.log('BlockController节点上的所有组件:', allComponents.map(comp => comp.constructor.name));
  129. const blockManager = blockController.getComponent('BlockManager');
  130. console.log('BlockManager组件查找结果:', blockManager);
  131. if (blockManager) {
  132. console.log('找到BlockManager组件,准备调用onGameStart方法...');
  133. // 调用BlockManager的onGameStart方法
  134. (blockManager as any).onGameStart();
  135. console.log('已调用BlockManager.onGameStart(),确保已放置的方块正确显示');
  136. } else {
  137. console.warn('BlockController节点上没有BlockManager组件');
  138. // 尝试通过索引获取第二个组件(第一个通常是UITransform)
  139. if (allComponents.length > 1) {
  140. const secondComponent = allComponents[1] as any;
  141. console.log('尝试使用第二个组件:', secondComponent.constructor.name);
  142. if (secondComponent && typeof secondComponent.onGameStart === 'function') {
  143. console.log('第二个组件有onGameStart方法,调用它...');
  144. secondComponent.onGameStart();
  145. console.log('通过索引调用了第二个组件的onGameStart方法');
  146. } else {
  147. console.log('第二个组件没有onGameStart方法');
  148. }
  149. }
  150. }
  151. } else {
  152. console.warn('找不到BlockController节点,尝试在Canvas上查找BlockManager组件');
  153. // 尝试在Canvas上查找
  154. const canvasBlockManager = find('Canvas').getComponent('BlockManager');
  155. if (canvasBlockManager) {
  156. // 调用BlockManager的onGameStart方法
  157. (canvasBlockManager as any).onGameStart();
  158. console.log('已调用Canvas上的BlockManager.onGameStart()');
  159. } else {
  160. console.warn('找不到BlockManager组件,无法保存已放置的方块状态');
  161. }
  162. }
  163. console.log('=== preservePlacedBlocks 执行完毕 ===');
  164. }
  165. // 开始游戏
  166. startGame() {
  167. if (this.gameStarted) return;
  168. console.log('游戏开始');
  169. this.gameStarted = true;
  170. // 使用BallController生成小球
  171. this.spawnBall();
  172. // 启动敌人生成
  173. const enemyControllerComp = this.getEnemyController();
  174. if (enemyControllerComp) {
  175. (enemyControllerComp as any).startGame();
  176. console.log('开始生成敌人');
  177. } else {
  178. console.warn('找不到EnemyController组件,无法开始生成敌人');
  179. }
  180. }
  181. // 生成小球
  182. spawnBall() {
  183. if (!this.ballController) {
  184. console.error('未设置BallController节点');
  185. return;
  186. }
  187. // 获取BallController组件
  188. const ballControllerComp = this.ballController.getComponent('BallController');
  189. if (ballControllerComp) {
  190. // 调用initialize方法创建小球
  191. (ballControllerComp as any).initialize();
  192. } else {
  193. console.error('BallController节点没有BallController组件');
  194. }
  195. }
  196. // 游戏结束
  197. gameOver() {
  198. if (!this.gameStarted) return;
  199. this.gameStarted = false;
  200. console.log('游戏结束');
  201. // 停止敌人生成
  202. const enemyControllerComp = this.getEnemyController();
  203. if (enemyControllerComp) {
  204. (enemyControllerComp as any).stopGame();
  205. console.log('停止生成敌人');
  206. }
  207. // 显示BlockSelectionUI
  208. if (this.blockSelectionUI) {
  209. this.blockSelectionUI.active = true;
  210. }
  211. }
  212. }