GameStartup.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { _decorator, Component, Node, find, Label, UITransform, Vec3, PhysicsSystem2D, EPhysics2DDrawFlags, Vec2 } from 'cc';
  2. import { GameManager } from './GameManager';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('GameStartup')
  5. export class GameStartup extends Component {
  6. @property({
  7. tooltip: '墙体初始血量'
  8. })
  9. public wallHealth: number = 1200;
  10. @property({
  11. type: Node,
  12. tooltip: '拖拽GameManager节点到这里'
  13. })
  14. public gameManager: Node = null;
  15. start() {
  16. console.log('GameStartup: 初始化游戏');
  17. // 启用物理系统
  18. this.initPhysicsSystem();
  19. // 初始化游戏管理器
  20. this.initGameManager();
  21. // 初始化墙体血量显示
  22. this.setupWallHealthDisplay();
  23. // 初始化敌人控制器
  24. this.setupEnemyController();
  25. console.log('游戏初始化完成');
  26. }
  27. // 初始化物理系统
  28. initPhysicsSystem() {
  29. // 启用物理系统
  30. PhysicsSystem2D.instance.enable = true;
  31. // 设置物理系统参数
  32. PhysicsSystem2D.instance.gravity = new Vec2(0, 0); // 无重力
  33. // 调试模式下显示碰撞框(开发时可用)
  34. PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
  35. EPhysics2DDrawFlags.Pair |
  36. EPhysics2DDrawFlags.CenterOfMass |
  37. EPhysics2DDrawFlags.Joint |
  38. EPhysics2DDrawFlags.Shape;
  39. console.log('物理系统已启用');
  40. }
  41. // 初始化游戏管理器
  42. initGameManager() {
  43. console.log('开始查找GameManager节点...');
  44. // 查找GameManager节点
  45. const gameManagerNode = find('Canvas/GameLevelUI/GameManager');
  46. if (!gameManagerNode) {
  47. console.error('找不到GameManager节点,路径:Canvas/GameLevelUI/GameManager');
  48. // 尝试查找其他可能的位置
  49. const altNode = find('Canvas/GameManager');
  50. if (altNode) {
  51. console.log('找到了Canvas/GameManager节点');
  52. this.tryInitGameManager(altNode);
  53. return;
  54. }
  55. // 查找所有可能的GameManager节点
  56. this.findAllGameManagerNodes();
  57. return;
  58. }
  59. console.log('找到了GameManager节点:', gameManagerNode.name);
  60. this.tryInitGameManager(gameManagerNode);
  61. }
  62. // 尝试初始化GameManager
  63. tryInitGameManager(node: Node) {
  64. console.log(`尝试从节点 ${node.name} 获取GameManager组件...`);
  65. // 获取节点上的所有组件
  66. const allComps = node.components;
  67. console.log(`节点 ${node.name} 上有 ${allComps.length} 个组件:`);
  68. allComps.forEach((comp, index) => {
  69. console.log(` ${index + 1}. ${comp.constructor.name}`);
  70. });
  71. // 获取GameManager组件 - 使用导入的类
  72. const gameManagerComp = node.getComponent(GameManager);
  73. if (!gameManagerComp) {
  74. console.error(`${node.name} 节点上没有GameManager组件`);
  75. return;
  76. }
  77. console.log('成功获取GameManager组件');
  78. // 调用GameManager的方法
  79. gameManagerComp.calculateGameBounds();
  80. gameManagerComp.initWallHealthDisplay();
  81. console.log('游戏管理器已初始化');
  82. }
  83. // 查找所有可能的GameManager节点
  84. findAllGameManagerNodes() {
  85. console.log('尝试查找所有可能的GameManager节点...');
  86. const canvas = find('Canvas');
  87. if (!canvas) {
  88. console.error('找不到Canvas节点');
  89. return;
  90. }
  91. // 递归查找所有名为GameManager的节点
  92. this.findNodesByName(canvas, 'GameManager');
  93. }
  94. // 递归查找指定名称的节点
  95. findNodesByName(rootNode: Node, name: string) {
  96. // 检查当前节点名称
  97. if (rootNode.name === name) {
  98. console.log(`找到名为 ${name} 的节点,路径: ${this.getNodePath(rootNode)}`);
  99. this.tryInitGameManager(rootNode);
  100. }
  101. // 递归检查子节点
  102. for (let i = 0; i < rootNode.children.length; i++) {
  103. this.findNodesByName(rootNode.children[i], name);
  104. }
  105. }
  106. // 获取节点路径
  107. getNodePath(node: Node): string {
  108. let path = node.name;
  109. let current = node;
  110. while (current.parent) {
  111. current = current.parent;
  112. path = current.name + '/' + path;
  113. }
  114. return path;
  115. }
  116. // 设置墙体血量显示
  117. setupWallHealthDisplay() {
  118. // 查找是否已经存在墙体血量显示节点
  119. let wallHealthNode = find('Canvas/GameLevelUI/HeartNode');
  120. // 更新墙体血量显示
  121. const label = wallHealthNode.getComponent(Label);
  122. if (label) {
  123. label.string = this.wallHealth.toString();
  124. }
  125. console.log('已更新墙体血量显示');
  126. // 设置EnemyController的墙体血量
  127. this.updateEnemyControllerWallHealth(wallHealthNode);
  128. }
  129. // 更新EnemyController的墙体血量
  130. updateEnemyControllerWallHealth(wallHealthNode: Node) {
  131. const enemyController = find('Canvas/GameLevelUI/EnemyController');
  132. if (enemyController) {
  133. const enemyControllerComp = enemyController.getComponent('EnemyController');
  134. if (enemyControllerComp) {
  135. // 设置墙体血量和显示节点
  136. (enemyControllerComp as any).wallHealth = this.wallHealth;
  137. (enemyControllerComp as any).wallHealthNode = wallHealthNode;
  138. console.log('已设置EnemyController的墙体血量');
  139. }
  140. }
  141. }
  142. // 设置敌人控制器
  143. setupEnemyController() {
  144. // 查找EnemyController节点
  145. let enemyController = find('Canvas/GameLevelUI/EnemyController');
  146. // 如果节点不存在,创建一个
  147. if (!enemyController) {
  148. // 获取GameLevelUI节点
  149. const gameLevelUI = find('Canvas/GameLevelUI');
  150. if (!gameLevelUI) {
  151. console.error('找不到GameLevelUI节点,无法创建EnemyController');
  152. return;
  153. }
  154. // 创建EnemyController节点
  155. enemyController = new Node('EnemyController');
  156. gameLevelUI.addChild(enemyController);
  157. console.log('已创建EnemyController节点');
  158. }
  159. // 添加EnemyController组件
  160. let enemyControllerComp = enemyController.getComponent('EnemyController');
  161. if (!enemyControllerComp) {
  162. enemyControllerComp = enemyController.addComponent('EnemyController');
  163. // 设置初始属性
  164. (enemyControllerComp as any).wallHealth = this.wallHealth;
  165. console.log('已在EnemyController节点上添加EnemyController组件');
  166. } else {
  167. console.log('EnemyController节点上已存在EnemyController组件');
  168. }
  169. }
  170. }