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

@@ -2,6 +2,7 @@ import { _decorator, Node, EditBox, instantiate, Vec3, Button, Label, Sprite, Sp
import { BaseView } from 'db://assets/scripts/core/BaseView';
import { ViewManager } from 'db://assets/scripts/core/ViewManager';
import { StorageManager } from 'db://assets/scripts/utils/StorageManager';
import { UserAssetsManager } from 'db://assets/scripts/utils/UserAssetsManager';
import { WxSDK } from 'db://assets/scripts/utils/WxSDK';
import { LevelDataManager } from 'db://assets/scripts/utils/LevelDataManager';
import { RuntimeLevelConfig } from 'db://assets/scripts/types/LevelTypes';
@@ -58,6 +59,7 @@ export class PageLevel extends BaseView {
@property(Label)
clockLabel: Label | null = null;
/** 积分显示标签prefab 中序列化名为 liveLabel保持兼容 */
@property(Label)
liveLabel: Label | null = null;
@@ -96,6 +98,9 @@ export class PageLevel extends BaseView {
/** 是否正在切换关卡(防止重复提交) */
private _isTransitioning: boolean = false;
/** 是否正在解锁提示(防止双击重复消耗积分) */
private _isUnlocking: boolean = false;
/** 通关弹窗实例 */
private _passModalNode: Node | null = null;
@@ -107,7 +112,7 @@ export class PageLevel extends BaseView {
// 从本地存储恢复关卡进度
this.currentLevelIndex = StorageManager.getCurrentLevelIndex();
console.log(`[PageLevel] 恢复关卡进度: 第 ${this.currentLevelIndex + 1}`);
this.updateLiveLabel();
this.updatePointsLabel();
this.initIconSetting();
this.initUnlockButtons();
this.initSubmitButton();
@@ -125,7 +130,7 @@ export class PageLevel extends BaseView {
*/
onViewShow(): void {
console.log('[PageLevel] onViewShow');
this.updateLiveLabel();
this.updatePointsLabel();
}
/**
@@ -143,6 +148,12 @@ export class PageLevel extends BaseView {
this.clearInputNodes();
this.stopCountdown();
this._closePassModal();
// 清理事件监听
this.iconSetting?.off(Node.EventType.TOUCH_END, this.onIconSettingClick, this);
this.unLockItem2?.off(Node.EventType.TOUCH_END);
this.unLockItem3?.off(Node.EventType.TOUCH_END);
this.submitButton?.off(Node.EventType.TOUCH_END, this.onSubmitAnswer, this);
}
/**
@@ -460,34 +471,39 @@ export class PageLevel extends BaseView {
/**
* 点击解锁线索
*/
private onUnlockClue(index: number): void {
// 检查生命值是否足够
if (!this.hasLives()) {
console.warn('[PageLevel] 生命值不足,无法解锁线索');
private async onUnlockClue(index: number): Promise<void> {
// 防止双击重复消耗
if (this._isUnlocking) return;
if (!this.hasPoints()) {
ToastManager.show('积分不足,无法解锁提示!');
return;
}
// 消耗一颗生命值
if (!this.consumeLife()) {
return;
this._isUnlocking = true;
try {
const levelId = this._currentConfig?.id;
const success = await UserAssetsManager.instance.consumePoint(levelId, index);
if (!success) {
ToastManager.show('积分不足,无法解锁提示!');
return;
}
this.updatePointsLabel();
this.playClickSound();
this.hideUnlockButton(index);
this.showClue(index);
if (this._currentConfig) {
const clueContent = index === 2 ? this._currentConfig.clue2 : this._currentConfig.clue3;
this.setClue(index, clueContent);
}
console.log(`[PageLevel] 解锁线索${index}`);
} finally {
this._isUnlocking = false;
}
// 播放点击音效
this.playClickSound();
// 隐藏解锁按钮
this.hideUnlockButton(index);
// 显示线索
this.showClue(index);
// 设置线索内容
if (this._currentConfig) {
const clueContent = index === 2 ? this._currentConfig.clue2 : this._currentConfig.clue3;
this.setClue(index, clueContent);
}
console.log(`[PageLevel] 解锁线索${index}`);
}
// ========== 主图相关方法 ==========
@@ -591,48 +607,17 @@ export class PageLevel extends BaseView {
// 可以在这里添加游戏结束逻辑
}
// ========== 生命值相关方法 ==========
// ========== 积分相关方法 ==========
/**
* 更新生命值显示
*/
private updateLiveLabel(): void {
private updatePointsLabel(): void {
if (this.liveLabel) {
const lives = StorageManager.getLives();
this.liveLabel.string = `x ${lives}`;
console.log(`[PageLevel] 更新生命值显示: ${lives}`);
const points = StorageManager.getPoints();
this.liveLabel.string = `x ${points}`;
}
}
/**
* 消耗一颗生命值(用于查看提示)
* @returns 是否消耗成功
*/
private consumeLife(): boolean {
const success = StorageManager.consumeLife();
if (success) {
this.updateLiveLabel();
console.log('[PageLevel] 消耗一颗生命');
} else {
console.warn('[PageLevel] 生命值不足,无法消耗');
}
return success;
}
/**
* 增加一颗生命值(用于通关奖励)
*/
private addLife(): void {
StorageManager.addLife();
this.updateLiveLabel();
console.log('[PageLevel] 获得一颗生命');
}
/**
* 检查是否有足够的生命值
*/
private hasLives(): boolean {
return StorageManager.hasLives();
private hasPoints(): boolean {
return StorageManager.hasPoints();
}
// ========== 答案提交与关卡切换 ==========
@@ -659,7 +644,7 @@ export class PageLevel extends BaseView {
/**
* 显示成功提示
*/
private showSuccess(): void {
private async showSuccess(): Promise<void> {
console.log('[PageLevel] 答案正确!');
// 标记正在切换关卡,防止重复提交
@@ -671,8 +656,10 @@ export class PageLevel extends BaseView {
// 播放成功音效
this.playSuccessSound();
// 通关奖励:增加一颗生命值
this.addLife();
// 通关奖励:通过服务端增加积分
const levelId = this._currentConfig?.id ?? '';
await UserAssetsManager.instance.earnPoint(levelId);
this.updatePointsLabel();
// 显示通关弹窗
this._showPassModal();