feat: 支持关卡配置分享

This commit is contained in:
richarjiang
2026-04-06 17:32:32 +08:00
parent c7f52ab032
commit b489ab40f5
8 changed files with 392 additions and 24 deletions

View File

@@ -7,6 +7,7 @@ import { WxSDK } from 'db://assets/scripts/utils/WxSDK';
import { LevelDataManager } from 'db://assets/scripts/utils/LevelDataManager';
import { RuntimeLevelConfig } from 'db://assets/scripts/types/LevelTypes';
import { ToastManager } from 'db://assets/scripts/utils/ToastManager';
import { ShareManager } from 'db://assets/scripts/utils/ShareManager';
import { PassModal } from 'db://assets/prefabs/PassModal';
const { ccclass, property } = _decorator;
@@ -104,14 +105,26 @@ export class PageLevel extends BaseView {
/** 通关弹窗实例 */
private _passModalNode: Node | null = null;
/** 是否处于分享挑战模式 */
private _isShareMode: boolean = false;
/**
* 页面首次加载时调用
*/
onViewLoad(): void {
console.log('[PageLevel] onViewLoad');
// 从本地存储恢复关卡进度
this.currentLevelIndex = StorageManager.getCurrentLevelIndex();
console.log(`[PageLevel] 恢复关卡进度: 第 ${this.currentLevelIndex + 1}`);
const params = this.getParams();
this._isShareMode = params?.shareMode === true;
if (this._isShareMode) {
this.currentLevelIndex = 0;
console.log('[PageLevel] 进入分享挑战模式');
} else {
// 从本地存储恢复关卡进度
this.currentLevelIndex = StorageManager.getCurrentLevelIndex();
console.log(`[PageLevel] 恢复关卡进度: 第 ${this.currentLevelIndex + 1}`);
}
this.updatePointsLabel();
this.initIconSetting();
this.initUnlockButtons();
@@ -160,13 +173,18 @@ export class PageLevel extends BaseView {
* 初始化关卡(从 API 数据加载,异步确保资源就绪)
*/
private async initLevel(): Promise<void> {
// 先尝试从缓存获取
let config = LevelDataManager.instance.getLevelConfig(this.currentLevelIndex);
let config: RuntimeLevelConfig | null = null;
if (!config) {
// 缓存中没有,异步加载
console.log(`[PageLevel] 关卡 ${this.currentLevelIndex + 1} 资源未缓存,开始加载...`);
config = await LevelDataManager.instance.ensureLevelReady(this.currentLevelIndex);
if (this._isShareMode) {
// 分享模式:从 ShareManager 获取关卡
config = await ShareManager.instance.ensureShareLevelReady(this.currentLevelIndex);
} else {
// 正常模式:先尝试缓存,再异步加载
config = LevelDataManager.instance.getLevelConfig(this.currentLevelIndex);
if (!config) {
console.log(`[PageLevel] 关卡 ${this.currentLevelIndex + 1} 资源未缓存,开始加载...`);
config = await LevelDataManager.instance.ensureLevelReady(this.currentLevelIndex);
}
}
if (!config) {
@@ -212,7 +230,14 @@ export class PageLevel extends BaseView {
this.updateClockLabel();
// 预加载下一关图片(静默加载,不阻塞)
LevelDataManager.instance.preloadNextLevel(this.currentLevelIndex);
if (this._isShareMode) {
const nextIndex = this.currentLevelIndex + 1;
if (nextIndex < ShareManager.instance.getShareLevelCount()) {
ShareManager.instance.ensureShareLevelReady(nextIndex).catch(() => {});
}
} else {
LevelDataManager.instance.preloadNextLevel(this.currentLevelIndex);
}
console.log(`[PageLevel] 初始化关卡 ${this.currentLevelIndex + 1}, 答案长度: ${config.answer.length}`);
}
@@ -356,7 +381,14 @@ export class PageLevel extends BaseView {
private onIconSettingClick(): void {
console.log('[PageLevel] IconSetting 点击,返回主页');
this.playClickSound();
ViewManager.instance.back();
// 分享模式下栈中没有 PageHome需要清除分享状态并直接打开首页
if (this._isShareMode) {
ShareManager.instance.clearShareMode();
ViewManager.instance.replace('PageHome');
} else {
ViewManager.instance.back();
}
}
// ========== 线索相关方法 ==========
@@ -656,10 +688,12 @@ export class PageLevel extends BaseView {
// 播放成功音效
this.playSuccessSound();
// 通关奖励:通过服务端增加积分
const levelId = this._currentConfig?.id ?? '';
await UserAssetsManager.instance.earnPoint(levelId);
this.updatePointsLabel();
// 通关奖励:分享模式下不增加积分
if (!this._isShareMode) {
const levelId = this._currentConfig?.id ?? '';
await UserAssetsManager.instance.earnPoint(levelId);
this.updatePointsLabel();
}
// 显示通关弹窗
this._showPassModal();
@@ -748,19 +782,29 @@ export class PageLevel extends BaseView {
* 进入下一关
*/
private async nextLevel(): Promise<void> {
// 保存当前关卡进度
StorageManager.onLevelCompleted(this.currentLevelIndex);
// 分享模式不保存本地进度
if (!this._isShareMode) {
StorageManager.onLevelCompleted(this.currentLevelIndex);
}
this.currentLevelIndex++;
// 检查是否还有关卡
const totalLevels = LevelDataManager.instance.getLevelCount();
const totalLevels = this._isShareMode
? ShareManager.instance.getShareLevelCount()
: LevelDataManager.instance.getLevelCount();
if (this.currentLevelIndex >= totalLevels) {
// 所有关卡完成
console.log('[PageLevel] 恭喜通关!');
this.stopCountdown();
ViewManager.instance.back();
if (this._isShareMode) {
ShareManager.instance.clearShareMode();
ViewManager.instance.replace('PageHome');
} else {
ViewManager.instance.back();
}
return;
}