| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import { _decorator, Component, Node, Prefab, instantiate, Vec3, find, director, Canvas, UITransform, Button, Label } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('GameManager')
- export class GameManager extends Component {
- @property({
- type: Node,
- tooltip: '拖拽BallController节点到这里'
- })
- public ballController: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽BlockSelectionUI节点到这里'
- })
- public blockSelectionUI: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽GameArea节点到这里'
- })
- public gameArea: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽EnemyController节点到这里'
- })
- public enemyManager: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽墙体血量显示节点到这里'
- })
- public wallHealthNode: Node = null;
- // 游戏是否已经开始
- private gameStarted: boolean = false;
- // 游戏区域的边界
- private gameBounds = {
- left: 0,
- right: 0,
- top: 0,
- bottom: 0
- };
- start() {
- // 计算游戏区域边界
- this.calculateGameBounds();
- // 初始化墙体血量显示
- this.initWallHealthDisplay();
- }
- // 计算游戏区域边界
- calculateGameBounds() {
- // 获取屏幕尺寸
- const canvas = find('Canvas');
- if (!canvas) {
- console.error('找不到Canvas节点');
- return;
- }
- const canvasUI = canvas.getComponent(UITransform);
- if (!canvasUI) {
- console.error('Canvas节点没有UITransform组件');
- return;
- }
- // 获取屏幕的尺寸
- const screenWidth = canvasUI.width;
- const screenHeight = canvasUI.height;
-
- // 获取屏幕的世界坐标位置
- const worldPos = canvas.worldPosition;
- // 计算屏幕的世界坐标边界
- this.gameBounds.left = worldPos.x - screenWidth / 2;
- this.gameBounds.right = worldPos.x + screenWidth / 2;
- this.gameBounds.bottom = worldPos.y - screenHeight / 2;
- this.gameBounds.top = worldPos.y + screenHeight / 2;
- console.log('Screen Bounds:', this.gameBounds);
- }
- // 初始化墙体血量显示
- initWallHealthDisplay() {
- if (!this.wallHealthNode) {
- // 尝试查找墙体血量显示节点
- this.wallHealthNode = find('Canvas/GameLevelUI/WallHealth');
- }
- if (this.wallHealthNode) {
- // 获取EnemyController组件
- const enemyControllerComp = this.getEnemyController();
- if (enemyControllerComp) {
- // 设置墙体血量显示节点
- (enemyControllerComp as any).wallHealthNode = this.wallHealthNode;
- }
- }
- }
- // 获取EnemyController组件
- getEnemyController() {
- if (!this.enemyManager) {
- this.enemyManager = find('Canvas/GameLevelUI/EnemyController');
- }
- if (this.enemyManager) {
- return this.enemyManager.getComponent('EnemyController');
- }
- return null;
- }
- // 点击确认按钮的回调
- onConfirmButtonClicked() {
- console.log('=== 确认按钮被点击 ===');
-
- // 隐藏BlockSelectionUI,但保留已放置的方块
- if (this.blockSelectionUI) {
- console.log('隐藏BlockSelectionUI...');
- this.blockSelectionUI.active = false;
-
- // 查找GridContainer节点,确保它保持可见
- const gridContainer = find('Canvas/GameLevelUI/GameArea/GridContainer');
- if (gridContainer) {
- gridContainer.active = true;
- console.log('GridContainer保持可见');
- }
-
- // 确保已放置的方块保持可见
- console.log('准备调用preservePlacedBlocks...');
- this.preservePlacedBlocks();
- } else {
- console.warn('blockSelectionUI未设置或为null');
- }
- // 开始游戏
- console.log('准备开始游戏...');
- this.startGame();
- }
-
- // 保存已放置的方块
- preservePlacedBlocks() {
- console.log('=== preservePlacedBlocks 开始执行 ===');
-
- // 查找BlockManager组件
- const blockController = find('Canvas/GameLevelUI/BlockController');
- console.log('BlockController节点查找结果:', blockController);
-
- if (blockController) {
- console.log('找到BlockController节点,开始查找BlockManager组件...');
-
- // 显示所有组件
- const allComponents = blockController.getComponents(Component);
- console.log('BlockController节点上的所有组件:', allComponents.map(comp => comp.constructor.name));
-
- const blockManager = blockController.getComponent('BlockManager');
- console.log('BlockManager组件查找结果:', blockManager);
-
- if (blockManager) {
- console.log('找到BlockManager组件,准备调用onGameStart方法...');
- // 调用BlockManager的onGameStart方法
- (blockManager as any).onGameStart();
- console.log('已调用BlockManager.onGameStart(),确保已放置的方块正确显示');
- } else {
- console.warn('BlockController节点上没有BlockManager组件');
-
- // 尝试通过索引获取第二个组件(第一个通常是UITransform)
- if (allComponents.length > 1) {
- const secondComponent = allComponents[1] as any;
- console.log('尝试使用第二个组件:', secondComponent.constructor.name);
-
- if (secondComponent && typeof secondComponent.onGameStart === 'function') {
- console.log('第二个组件有onGameStart方法,调用它...');
- secondComponent.onGameStart();
- console.log('通过索引调用了第二个组件的onGameStart方法');
- } else {
- console.log('第二个组件没有onGameStart方法');
- }
- }
- }
- } else {
- console.warn('找不到BlockController节点,尝试在Canvas上查找BlockManager组件');
-
- // 尝试在Canvas上查找
- const canvasBlockManager = find('Canvas').getComponent('BlockManager');
- if (canvasBlockManager) {
- // 调用BlockManager的onGameStart方法
- (canvasBlockManager as any).onGameStart();
- console.log('已调用Canvas上的BlockManager.onGameStart()');
- } else {
- console.warn('找不到BlockManager组件,无法保存已放置的方块状态');
- }
- }
-
- console.log('=== preservePlacedBlocks 执行完毕 ===');
- }
- // 开始游戏
- startGame() {
- if (this.gameStarted) return;
-
- console.log('游戏开始');
- this.gameStarted = true;
- // 使用BallController生成小球
- this.spawnBall();
- // 启动敌人生成
- const enemyControllerComp = this.getEnemyController();
- if (enemyControllerComp) {
- (enemyControllerComp as any).startGame();
- console.log('开始生成敌人');
- } else {
- console.warn('找不到EnemyController组件,无法开始生成敌人');
- }
- }
- // 生成小球
- spawnBall() {
- if (!this.ballController) {
- console.error('未设置BallController节点');
- return;
- }
- // 获取BallController组件
- const ballControllerComp = this.ballController.getComponent('BallController');
- if (ballControllerComp) {
- // 调用initialize方法创建小球
- (ballControllerComp as any).initialize();
- } else {
- console.error('BallController节点没有BallController组件');
- }
- }
- // 游戏结束
- gameOver() {
- if (!this.gameStarted) return;
-
- this.gameStarted = false;
- console.log('游戏结束');
-
- // 停止敌人生成
- const enemyControllerComp = this.getEnemyController();
- if (enemyControllerComp) {
- (enemyControllerComp as any).stopGame();
- console.log('停止生成敌人');
- }
-
- // 显示BlockSelectionUI
- if (this.blockSelectionUI) {
- this.blockSelectionUI.active = true;
- }
- }
- }
|