import { _decorator, Component, Node, Canvas, Camera, UITransform, view, Color, director, Label, find, } from 'cc'; import { GameController } from './GameController'; import { GameManager } from './manager/GameManager'; import { VisualAssets } from './visual/VisualAssets'; const { ccclass, executionOrder } = _decorator; /** 每格像素尺寸(UI 坐标) */ export const CELL_PIXEL = 56; @ccclass('AppBootstrap') @executionOrder(-100) export class AppBootstrap extends Component { async onLoad() { try { await this.bootstrap(); } catch (e) { console.error('[AppBootstrap] 初始化失败', e); } } private async bootstrap() { console.log('[AppBootstrap] 开始初始化…'); await VisualAssets.preload(); const scene = director.getScene()!; let mainCam = find('Main Camera', scene)?.getComponent(Camera) ?? null; if (!mainCam) { const camNode = new Node('Main Camera'); camNode.parent = scene; mainCam = camNode.addComponent(Camera); } this.setupCamera(mainCam); const light = find('Main Light', scene); if (light) light.active = false; let canvasNode = find('Canvas', scene); if (!canvasNode) { canvasNode = new Node('Canvas'); canvasNode.parent = scene; canvasNode.addComponent(Canvas); } const canvas = canvasNode.getComponent(Canvas)!; canvas.cameraComponent = mainCam; let canvasUi = canvasNode.getComponent(UITransform); if (!canvasUi) canvasUi = canvasNode.addComponent(UITransform); const size = view.getVisibleSize(); canvasUi.setContentSize(size.width, size.height); let gameRoot = canvasNode.getChildByName('GameRoot'); if (!gameRoot) { gameRoot = new Node('GameRoot'); gameRoot.parent = canvasNode; const grUi = gameRoot.addComponent(UITransform); grUi.setContentSize(size.width, size.height); } let gcNode = scene.getChildByName('GameController'); if (!gcNode) { gcNode = new Node('GameController'); gcNode.parent = scene; gcNode.addComponent(GameManager); gcNode.addComponent(GameController); } const gctl = gcNode.getComponent(GameController); const gm = gcNode.getComponent(GameManager)!; if (gctl) { if (gctl.mainLevelEntrance) gm.mainLevelEntrance = gctl.mainLevelEntrance; gm.initialLevelID = gctl.initialLevelID; gm.playerSkin = gctl.playerSkin; } let entrance = gameRoot.getChildByName('MainLevelEntrance'); if (!entrance) { entrance = new Node('MainLevelEntrance'); entrance.parent = gameRoot; const eUi = entrance.addComponent(UITransform); eUi.setContentSize(size.width, size.height); } gm.mainLevelEntrance = entrance; gm.initialLevelID = 1; this.ensureHint(canvasNode); await gm.createNewLevel(gm.initialLevelID); console.log('[AppBootstrap] 关卡已加载'); } private setupCamera(cam: Camera) { const camNode = cam.node; camNode.setPosition(0, 0, 1000); camNode.setRotationFromEuler(0, 0, 0); cam.projection = Camera.ProjectionType.ORTHO; cam.orthoHeight = 360; cam.near = 1; cam.far = 2000; cam.clearFlags = Camera.ClearFlag.SOLID_COLOR; cam.clearColor = new Color(30, 40, 60, 255); } private ensureHint(canvasNode: Node) { if (canvasNode.getChildByName('Hint')) return; const hint = new Node('Hint'); hint.parent = canvasNode; const ui = hint.addComponent(UITransform); ui.setContentSize(400, 40); hint.setPosition(0, 280, 0); const label = hint.addComponent(Label); label.string = '主站 Cocos · 关卡运行中'; label.fontSize = 22; label.color = new Color(200, 220, 255); } }