| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- import { _decorator, Component, Node, find, Label, UITransform, Vec3, PhysicsSystem2D, EPhysics2DDrawFlags, Vec2 } from 'cc';
- import { GameManager } from './GameManager';
- const { ccclass, property } = _decorator;
- @ccclass('GameStartup')
- export class GameStartup extends Component {
- @property({
- tooltip: '墙体初始血量'
- })
- public wallHealth: number = 1200;
-
- @property({
- type: Node,
- tooltip: '拖拽GameManager节点到这里'
- })
- public gameManager: Node = null;
-
- start() {
- console.log('GameStartup: 初始化游戏');
-
- // 启用物理系统
- this.initPhysicsSystem();
-
- // 初始化游戏管理器
- this.initGameManager();
-
- // 初始化墙体血量显示
- this.setupWallHealthDisplay();
-
- // 初始化敌人控制器
- this.setupEnemyController();
-
- console.log('游戏初始化完成');
- }
-
- // 初始化物理系统
- initPhysicsSystem() {
- // 启用物理系统
- PhysicsSystem2D.instance.enable = true;
-
- // 设置物理系统参数
- PhysicsSystem2D.instance.gravity = new Vec2(0, 0); // 无重力
-
- // 调试模式下显示碰撞框(开发时可用)
- PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
- EPhysics2DDrawFlags.Pair |
- EPhysics2DDrawFlags.CenterOfMass |
- EPhysics2DDrawFlags.Joint |
- EPhysics2DDrawFlags.Shape;
-
- console.log('物理系统已启用');
- }
-
- // 初始化游戏管理器
- initGameManager() {
- console.log('开始查找GameManager节点...');
-
- // 查找GameManager节点
- const gameManagerNode = find('Canvas/GameLevelUI/GameManager');
- if (!gameManagerNode) {
- console.error('找不到GameManager节点,路径:Canvas/GameLevelUI/GameManager');
-
- // 尝试查找其他可能的位置
- const altNode = find('Canvas/GameManager');
- if (altNode) {
- console.log('找到了Canvas/GameManager节点');
- this.tryInitGameManager(altNode);
- return;
- }
-
- // 查找所有可能的GameManager节点
- this.findAllGameManagerNodes();
- return;
- }
-
- console.log('找到了GameManager节点:', gameManagerNode.name);
- this.tryInitGameManager(gameManagerNode);
- }
-
- // 尝试初始化GameManager
- tryInitGameManager(node: Node) {
- console.log(`尝试从节点 ${node.name} 获取GameManager组件...`);
-
- // 获取节点上的所有组件
- const allComps = node.components;
- console.log(`节点 ${node.name} 上有 ${allComps.length} 个组件:`);
- allComps.forEach((comp, index) => {
- console.log(` ${index + 1}. ${comp.constructor.name}`);
- });
-
- // 获取GameManager组件 - 使用导入的类
- const gameManagerComp = node.getComponent(GameManager);
- if (!gameManagerComp) {
- console.error(`${node.name} 节点上没有GameManager组件`);
- return;
- }
-
- console.log('成功获取GameManager组件');
-
- // 调用GameManager的方法
- gameManagerComp.calculateGameBounds();
- gameManagerComp.initWallHealthDisplay();
-
- console.log('游戏管理器已初始化');
- }
-
- // 查找所有可能的GameManager节点
- findAllGameManagerNodes() {
- console.log('尝试查找所有可能的GameManager节点...');
-
- const canvas = find('Canvas');
- if (!canvas) {
- console.error('找不到Canvas节点');
- return;
- }
-
- // 递归查找所有名为GameManager的节点
- this.findNodesByName(canvas, 'GameManager');
- }
-
- // 递归查找指定名称的节点
- findNodesByName(rootNode: Node, name: string) {
- // 检查当前节点名称
- if (rootNode.name === name) {
- console.log(`找到名为 ${name} 的节点,路径: ${this.getNodePath(rootNode)}`);
- this.tryInitGameManager(rootNode);
- }
-
- // 递归检查子节点
- for (let i = 0; i < rootNode.children.length; i++) {
- this.findNodesByName(rootNode.children[i], name);
- }
- }
-
- // 获取节点路径
- getNodePath(node: Node): string {
- let path = node.name;
- let current = node;
-
- while (current.parent) {
- current = current.parent;
- path = current.name + '/' + path;
- }
-
- return path;
- }
-
- // 设置墙体血量显示
- setupWallHealthDisplay() {
- // 查找是否已经存在墙体血量显示节点
- let wallHealthNode = find('Canvas/GameLevelUI/HeartNode');
-
- // 更新墙体血量显示
- const label = wallHealthNode.getComponent(Label);
- if (label) {
- label.string = this.wallHealth.toString();
- }
-
- console.log('已更新墙体血量显示');
-
-
- // 设置EnemyController的墙体血量
- this.updateEnemyControllerWallHealth(wallHealthNode);
- }
-
- // 更新EnemyController的墙体血量
- updateEnemyControllerWallHealth(wallHealthNode: Node) {
- const enemyController = find('Canvas/GameLevelUI/EnemyController');
- if (enemyController) {
- const enemyControllerComp = enemyController.getComponent('EnemyController');
- if (enemyControllerComp) {
- // 设置墙体血量和显示节点
- (enemyControllerComp as any).wallHealth = this.wallHealth;
- (enemyControllerComp as any).wallHealthNode = wallHealthNode;
-
- console.log('已设置EnemyController的墙体血量');
- }
- }
- }
-
- // 设置敌人控制器
- setupEnemyController() {
- // 查找EnemyController节点
- let enemyController = find('Canvas/GameLevelUI/EnemyController');
-
- // 如果节点不存在,创建一个
- if (!enemyController) {
- // 获取GameLevelUI节点
- const gameLevelUI = find('Canvas/GameLevelUI');
- if (!gameLevelUI) {
- console.error('找不到GameLevelUI节点,无法创建EnemyController');
- return;
- }
-
- // 创建EnemyController节点
- enemyController = new Node('EnemyController');
- gameLevelUI.addChild(enemyController);
- console.log('已创建EnemyController节点');
- }
-
- // 添加EnemyController组件
- let enemyControllerComp = enemyController.getComponent('EnemyController');
- if (!enemyControllerComp) {
- enemyControllerComp = enemyController.addComponent('EnemyController');
-
- // 设置初始属性
- (enemyControllerComp as any).wallHealth = this.wallHealth;
-
- console.log('已在EnemyController节点上添加EnemyController组件');
- } else {
- console.log('EnemyController节点上已存在EnemyController组件');
- }
- }
- }
|