feat: 进入关卡时 toast 提示体力消耗,修复 StorageManager 接口位置和 WxSDK 访问级别
- 进入关卡成功后显示 toast 提示消耗体力及剩余体力 - 将 StorageManager 中 UserInfo 接口移至模块顶层,修复嵌套接口语法问题 - WxSDK.getWx() 改为 static 公开方法,便于外部调用
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { sys } from 'cc';
|
||||
import { StaminaInfo } from '../types/ApiTypes';
|
||||
|
||||
/**
|
||||
* 用户进度数据结构
|
||||
@@ -10,13 +11,21 @@ interface UserProgress {
|
||||
maxUnlockedLevelIndex: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户信息结构
|
||||
*/
|
||||
interface UserInfo {
|
||||
avatarUrl: string;
|
||||
nickName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地存储管理器
|
||||
* 统一管理用户数据的本地持久化存储
|
||||
*/
|
||||
export class StorageManager {
|
||||
/** 积分存储键 */
|
||||
private static readonly KEY_POINTS = 'game_points';
|
||||
/** 体力值存储键 */
|
||||
private static readonly KEY_STAMINA = 'game_stamina';
|
||||
|
||||
/** 用户进度存储键 */
|
||||
private static readonly KEY_PROGRESS = 'game_progress';
|
||||
@@ -27,11 +36,12 @@ export class StorageManager {
|
||||
/** 用户信息存储键 */
|
||||
private static readonly KEY_USER_INFO = 'user_info';
|
||||
|
||||
/** 默认积分 */
|
||||
private static readonly DEFAULT_POINTS = 10;
|
||||
|
||||
/** 最小积分 */
|
||||
private static readonly MIN_POINTS = 0;
|
||||
/** 默认体力值 */
|
||||
private static readonly DEFAULT_STAMINA: StaminaInfo = {
|
||||
current: 5,
|
||||
max: 5,
|
||||
nextRecoverAt: null,
|
||||
};
|
||||
|
||||
/** 默认进度 */
|
||||
private static readonly DEFAULT_PROGRESS: UserProgress = {
|
||||
@@ -42,75 +52,52 @@ export class StorageManager {
|
||||
/** 进度缓存(避免重复读取 localStorage) */
|
||||
private static _progressCache: UserProgress | null = null;
|
||||
|
||||
// ==================== 积分管理 ====================
|
||||
/** 体力缓存(避免重复 JSON 解析) */
|
||||
private static _staminaCache: StaminaInfo | null = null;
|
||||
|
||||
// ==================== 体力值管理 ====================
|
||||
|
||||
/**
|
||||
* 获取当前积分
|
||||
* @returns 当前积分,新用户返回默认值 10
|
||||
* 获取当前体力信息(带内存缓存,避免重复 JSON 解析)
|
||||
*/
|
||||
static getPoints(): number {
|
||||
const stored = sys.localStorage.getItem(StorageManager.KEY_POINTS);
|
||||
static getStamina(): StaminaInfo {
|
||||
if (StorageManager._staminaCache) {
|
||||
return { ...StorageManager._staminaCache };
|
||||
}
|
||||
|
||||
const stored = sys.localStorage.getItem(StorageManager.KEY_STAMINA);
|
||||
if (stored === null || stored === '') {
|
||||
// 新用户,设置默认值
|
||||
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
|
||||
return StorageManager.DEFAULT_POINTS;
|
||||
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
|
||||
return { ...StorageManager.DEFAULT_STAMINA };
|
||||
}
|
||||
const points = parseInt(stored, 10);
|
||||
// 防止异常数据
|
||||
if (isNaN(points) || points < 0) {
|
||||
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
|
||||
return StorageManager.DEFAULT_POINTS;
|
||||
try {
|
||||
const stamina = JSON.parse(stored) as StaminaInfo;
|
||||
if (typeof stamina.current !== 'number' || typeof stamina.max !== 'number') {
|
||||
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
|
||||
return { ...StorageManager.DEFAULT_STAMINA };
|
||||
}
|
||||
StorageManager._staminaCache = stamina;
|
||||
return { ...stamina };
|
||||
} catch {
|
||||
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
|
||||
return { ...StorageManager.DEFAULT_STAMINA };
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置积分
|
||||
* @param points 积分
|
||||
* 设置体力信息(同时更新缓存)
|
||||
*/
|
||||
static setPoints(points: number): void {
|
||||
const validPoints = Math.max(StorageManager.MIN_POINTS, points);
|
||||
sys.localStorage.setItem(StorageManager.KEY_POINTS, validPoints.toString());
|
||||
console.log(`[StorageManager] 积分已更新: ${validPoints}`);
|
||||
static setStamina(stamina: StaminaInfo): void {
|
||||
StorageManager._staminaCache = stamina;
|
||||
sys.localStorage.setItem(StorageManager.KEY_STAMINA, JSON.stringify(stamina));
|
||||
console.log(`[StorageManager] 体力已更新: ${stamina.current}/${stamina.max}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗一个积分
|
||||
* @returns 是否消耗成功(积分不足时返回 false)
|
||||
* 检查是否有足够的体力
|
||||
*/
|
||||
static consumePoint(): boolean {
|
||||
const currentPoints = StorageManager.getPoints();
|
||||
if (currentPoints <= 0) {
|
||||
console.warn('[StorageManager] 积分不足,无法消耗');
|
||||
return false;
|
||||
}
|
||||
StorageManager.setPoints(currentPoints - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加一个积分
|
||||
*/
|
||||
static addPoint(): void {
|
||||
const currentPoints = StorageManager.getPoints();
|
||||
StorageManager.setPoints(currentPoints + 1);
|
||||
console.log(`[StorageManager] 获得一个积分,当前积分: ${currentPoints + 1}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有足够的积分
|
||||
* @returns 是否有积分
|
||||
*/
|
||||
static hasPoints(): boolean {
|
||||
return StorageManager.getPoints() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置积分为默认值
|
||||
*/
|
||||
static resetPoints(): void {
|
||||
StorageManager.setPoints(StorageManager.DEFAULT_POINTS);
|
||||
console.log('[StorageManager] 积分已重置为默认值');
|
||||
static hasStamina(): boolean {
|
||||
return StorageManager.getStamina().current > 0;
|
||||
}
|
||||
|
||||
// ==================== 认证 Token 管理 ====================
|
||||
@@ -261,10 +248,10 @@ export class StorageManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置所有数据(积分 + 进度)
|
||||
* 重置所有数据(体力 + 进度)
|
||||
*/
|
||||
static resetAll(): void {
|
||||
StorageManager.resetPoints();
|
||||
StorageManager.setStamina(StorageManager.DEFAULT_STAMINA);
|
||||
StorageManager.resetProgress();
|
||||
StorageManager.clearToken();
|
||||
StorageManager.clearUserInfo();
|
||||
@@ -273,14 +260,6 @@ export class StorageManager {
|
||||
|
||||
// ==================== 用户信息管理 ====================
|
||||
|
||||
/**
|
||||
* 用户信息结构
|
||||
*/
|
||||
interface UserInfo {
|
||||
avatarUrl: string;
|
||||
nickName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存用户信息(头像、昵称)
|
||||
* @param userInfo 用户信息对象
|
||||
|
||||
Reference in New Issue
Block a user