Files
mayi_coco/assets/scripts/MainController.ts
T

208 lines
7.3 KiB
TypeScript

import { Component, Game, game, Node, view, _decorator } from 'cc';
import { PREVIEW } from 'cc/env';
import { AtelierService } from './core/AtelierService';
import { RELEASE_LEVELS } from './core/ReleaseLevels';
import { SoundService } from './core/SoundService';
import { StorageService } from './core/StorageService';
import { findValidationSample, VALIDATION_SAMPLES, ValidationSampleEntry } from './core/ValidationSamples';
import { EmbroideryRenderer } from './render/EmbroideryRenderer';
import { LobbyView } from './ui/LobbyView';
import { PlayView } from './ui/PlayView';
import { applyResponsivePolicy, computeScreenMetrics, ScreenMetrics } from './ui/ScreenAdapter';
import { createButton, createLabel, createPanel, createUINode, hex, StyledButton } 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;
private sampleBarNode: Node | null = null;
private sampleBarBtns: { key: string; btn: StyledButton }[] = [];
private activeSampleKey: string | 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();
const sampleParam = PREVIEW && typeof window !== 'undefined'
? new URLSearchParams(window.location?.search ?? '').get('sample')
: null;
const sampleEntry = findValidationSample(sampleParam);
if (sampleEntry) {
this.enterValidationSample(sampleEntry);
} else if (sampleParam === '024') {
this.lobbyView.node.active = false;
this.playView.node.active = true;
this.playView.loadCustomLevel(RELEASE_LEVELS[23], '样板 024 · 晨露叶桥');
} else {
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);
this.syncSampleBarPosition(metrics);
}
/** 仅在 PREVIEW 且 URL 带 ?sample= 时显示底部紧凑样板切换条,便于手机一指切换对照测试 */
private ensureSampleBar(): void {
if (!PREVIEW || this.sampleBarNode) return;
const bar = createPanel('SampleSwitcherBar', 726, 48, {
fill: '#2f1e10e8',
stroke: '#d8b06a',
strokeWidth: 1.6,
radius: 16,
depth: 3,
depthColor: '#1a0f07',
}, this.node);
this.sampleBarNode = bar.node;
this.sampleBarBtns = [];
const count = VALIDATION_SAMPLES.length;
const btnW = 134;
const gap = 8;
const totalW = count * btnW + (count - 1) * gap;
VALIDATION_SAMPLES.forEach((entry, idx) => {
const x = -totalW / 2 + btnW / 2 + idx * (btnW + gap);
const btn = createButton({
name: `SampleBtn_${entry.key}`,
text: entry.shortLabel,
w: btnW,
h: 34,
radius: 12,
fill: '#f3e5c7',
stroke: '#c9a367',
depth: 2,
depthColor: '#9c7843',
textColor: '#482e14',
fontSize: 15,
onClick: () => this.enterValidationSample(entry),
}, bar.node);
btn.node.setPosition(x, 0, 0);
this.sampleBarBtns.push({ key: entry.key, btn });
});
this.syncSampleBarPosition(computeScreenMetrics());
}
private syncSampleBarPosition(m: ScreenMetrics): void {
if (!this.sampleBarNode) return;
const bottomSafe = Math.min(54, Math.max(0, m.bottomInset));
this.sampleBarNode.setPosition(0, -m.visibleH / 2 + bottomSafe + 28, 0);
}
private refreshSampleBarHighlight(): void {
for (const item of this.sampleBarBtns) {
const active = item.key === this.activeSampleKey;
item.btn.label.color = hex(active ? '#9f2f23' : '#482e14');
item.btn.node.setScale(active ? 1.05 : 1, active ? 1.05 : 1, 1);
}
}
private enterValidationSample(entry: ValidationSampleEntry): void {
this.activeSampleKey = entry.key;
this.ensureSampleBar();
if (this.sampleBarNode) this.sampleBarNode.active = true;
this.refreshSampleBarHighlight();
this.lobbyView.node.active = false;
this.playView.node.active = true;
this.playView.loadCustomLevel(entry.level, entry.headerTitle);
}
/** 仅在浏览器 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());
if (this.sampleBarNode) this.sampleBarNode.active = false;
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);
}
}