Files
mayi_coco/assets/scripts/MainController.ts
T

124 lines
4.2 KiB
TypeScript

import { Component, Game, game, Node, view, _decorator } from 'cc';
import { AtelierService } from './core/AtelierService';
import { SoundService } from './core/SoundService';
import { StorageService } from './core/StorageService';
import { EmbroideryRenderer } from './render/EmbroideryRenderer';
import { LobbyView } from './ui/LobbyView';
import { PlayView } from './ui/PlayView';
import { applyResponsivePolicy, computeScreenMetrics, ScreenMetrics } from './ui/ScreenAdapter';
import { createLabel, createPanel, createUINode } from './ui/UiBuilder';
const { ccclass } = _decorator;
@ccclass('MainController')
export class MainController extends Component {
private storage!: StorageService;
private sound!: SoundService;
private atelier!: AtelierService;
private renderer!: EmbroideryRenderer;
private lobbyView!: LobbyView;
private playView!: PlayView;
private mockCapsuleNode: Node | null = null;
protected onLoad(): void {
applyResponsivePolicy();
this.storage = new StorageService();
this.sound = new SoundService(this.storage);
this.atelier = new AtelierService(
() => this.storage.readAtelierRaw(),
raw => this.storage.saveAtelierRaw(raw),
);
this.atelier.syncFromLegacyRecords(lv => this.storage.getRecord(lv));
this.renderer = new EmbroideryRenderer(1.5);
const lobbyNode = createUINode('LobbyView', 750, 1334, this.node);
this.lobbyView = lobbyNode.addComponent(LobbyView);
this.lobbyView.init(this.storage, this.sound, this.atelier, this.renderer, index => this.enterLevel(index));
const playNode = createUINode('PlayView', 750, 1334, this.node);
this.playView = playNode.addComponent(PlayView);
this.playView.init(
this.storage,
this.sound,
this.atelier,
this.renderer,
() => this.showLobby(),
(tab, chapterIndex, focusLevelIndex) => {
this.showLobby();
this.lobbyView.openAtelier(tab, chapterIndex, focusLevelIndex);
},
);
game.on(Game.EVENT_HIDE, this.onAppHide, this);
game.on(Game.EVENT_SHOW, this.onAppShow, this);
view.on('canvas-resize', this.onCanvasResize, this);
this.applyLayoutMetrics();
this.showLobby();
}
protected onDestroy(): void {
game.off(Game.EVENT_HIDE, this.onAppHide, this);
game.off(Game.EVENT_SHOW, this.onAppShow, this);
view.off('canvas-resize', this.onCanvasResize, this);
}
private onCanvasResize(): void {
applyResponsivePolicy();
this.applyLayoutMetrics();
}
private applyLayoutMetrics(): void {
const metrics = computeScreenMetrics();
this.lobbyView?.applyScreenMetrics(metrics);
this.playView?.applyScreenMetrics(metrics);
this.syncMockWeChatCapsule(metrics);
}
/** 仅在浏览器 URL 带 ?wxsim=iphone15 或 ?wxsim=iphonese 时绘制模拟微信胶囊,方便真机前目视验收 */
private syncMockWeChatCapsule(m: ScreenMetrics): void {
if (!m.mockCapsule) {
if (this.mockCapsuleNode) this.mockCapsuleNode.active = false;
return;
}
const { top, height, width, right } = m.mockCapsule;
if (!this.mockCapsuleNode) {
const cap = createPanel('MockWeChatCapsule', width, height, {
fill: '#00000088', stroke: '#ffffff88', strokeWidth: 2, radius: height / 2,
}, this.node);
createLabel('CapGlyph', '••• | ◎', width - 14, height - 8, {
size: 22, color: '#ffffff', bold: true,
}, cap.node);
this.mockCapsuleNode = cap.node;
}
this.mockCapsuleNode.active = true;
this.mockCapsuleNode.setPosition(
m.visibleW / 2 - right - width / 2,
m.visibleH / 2 - top - height / 2,
0,
);
}
private onAppHide(): void {
this.playView?.cancelDrag(false);
}
private onAppShow(): void {
this.atelier?.settleIdle(Date.now());
this.applyLayoutMetrics();
if (this.lobbyView?.node.active) this.lobbyView.refresh();
}
private showLobby(): void {
this.atelier?.settleIdle(Date.now());
this.playView.node.active = false;
this.lobbyView.node.active = true;
this.lobbyView.refresh();
}
private enterLevel(index: number): void {
this.lobbyView.node.active = false;
this.playView.node.active = true;
this.playView.loadLevel(index);
}
}