feat: 支持登录、个人信息存储

This commit is contained in:
richarjiang
2026-04-05 13:37:58 +08:00
parent e438f6fce4
commit b732e4d8f8
23 changed files with 3572 additions and 144 deletions

View File

@@ -1,11 +1,14 @@
import { _decorator, Component, ProgressBar, Label } from 'cc';
import { ViewManager } from './scripts/core/ViewManager';
import { LevelDataManager } from './scripts/utils/LevelDataManager';
import { AuthManager } from './scripts/utils/AuthManager';
import { StorageManager } from './scripts/utils/StorageManager';
const { ccclass, property } = _decorator;
/**
* 页面加载组件
* 负责预加载资源并显示加载进度
* 负责用户登录、预加载资源并显示加载进度
* 登录与关卡数据加载并行执行以减少等待时间
*/
@ccclass('PageLoading')
export class PageLoading extends Component {
@@ -19,26 +22,40 @@ export class PageLoading extends Component {
this._startPreload();
}
/**
* 开始预加载
*/
private async _startPreload(): Promise<void> {
// 初始化进度条
if (this.progressBar) {
this.progressBar.progress = 0;
}
// 阶段1: 初始化 LevelDataManager (0-80%)
const success = await LevelDataManager.instance.initialize((progress, message) => {
this._updateProgress(progress);
this._updateStatusLabel(message);
});
this._updateStatusLabel('正在加载...');
if (!success) {
// 登录和关卡数据并行加载
const [loginSuccess, levelSuccess] = await Promise.all([
AuthManager.instance.initialize(),
LevelDataManager.instance.initialize((progress, message) => {
// 关卡加载占 0-80% 进度
this._updateProgress(progress);
this._updateStatusLabel(message);
}),
]);
if (loginSuccess) {
console.log('[PageLoading] 用户登录成功');
} else {
console.warn('[PageLoading] 登录失败,继续离线模式');
}
if (!levelSuccess) {
this._updateStatusLabel('加载失败,请重新打开游戏');
return;
}
// 阶段2: 预加载 PageHome (80-100%)
// 登录 + 关卡数据都就绪后,用服务端进度覆盖本地进度
if (loginSuccess) {
this._syncProgressFromServer();
}
// 预加载 PageHome (80-100%)
ViewManager.instance.preload('PageHome',
(progress) => {
this._updateProgress(0.8 + progress * 0.2);
@@ -50,38 +67,52 @@ export class PageLoading extends Component {
);
}
/**
* 更新进度条
*/
private _updateProgress(progress: number): void {
if (this.progressBar) {
this.progressBar.progress = progress;
}
}
/**
* 更新状态标签
*/
private _updateStatusLabel(message: string): void {
if (this.statusLabel) {
this.statusLabel.string = message;
}
}
/**
* 预加载完成回调
*/
private _onPreloadComplete(): void {
// 确保进度条显示完成
this._updateProgress(1);
this._updateStatusLabel('加载完成');
// 打开 PageHome
ViewManager.instance.open('PageHome', {
onComplete: () => {
// PageHome 打开成功后,销毁自身
this.node.destroy();
}
});
}
/**
* 用服务端通关进度覆盖本地进度
* 将 completedLevelIds 转换为本地的 currentLevelIndex / maxUnlockedLevelIndex
*/
private _syncProgressFromServer(): void {
const completedIds = AuthManager.instance.completedLevelIds;
if (completedIds.length === 0) {
console.log('[PageLoading] 服务端无通关记录,使用本地进度');
return;
}
const maxCompletedIndex = LevelDataManager.instance.getMaxCompletedIndex(completedIds);
if (maxCompletedIndex < 0) {
return;
}
const localMax = StorageManager.getMaxUnlockedLevelIndex();
// 取服务端和本地的较大值,防止进度回退
if (maxCompletedIndex > localMax) {
// onLevelCompleted 会同时设置 currentLevelIndex = maxCompletedIndex + 1 和 maxUnlockedLevelIndex
StorageManager.onLevelCompleted(maxCompletedIndex);
console.log(`[PageLoading] 服务端进度同步:已通关到第 ${maxCompletedIndex + 1}`);
}
}
}