feat: 添加生命值系统
- 新增 StorageManager 本地存储管理器,管理用户生命值 - 新用户默认 10 点生命值,存储在 localStorage - 查看提示消耗 1 点生命值 - 通关奖励 1 点生命值 - PageLevel 集成生命值显示和消耗逻辑
This commit is contained in:
@@ -1612,7 +1612,7 @@
|
|||||||
},
|
},
|
||||||
"_lpos": {
|
"_lpos": {
|
||||||
"__type__": "cc.Vec3",
|
"__type__": "cc.Vec3",
|
||||||
"x": -220,
|
"x": -180,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
"z": 0
|
"z": 0
|
||||||
},
|
},
|
||||||
@@ -1653,7 +1653,7 @@
|
|||||||
},
|
},
|
||||||
"_contentSize": {
|
"_contentSize": {
|
||||||
"__type__": "cc.Size",
|
"__type__": "cc.Size",
|
||||||
"width": 160,
|
"width": 320,
|
||||||
"height": 100.8
|
"height": 100.8
|
||||||
},
|
},
|
||||||
"_anchorPoint": {
|
"_anchorPoint": {
|
||||||
@@ -1689,7 +1689,7 @@
|
|||||||
"b": 0,
|
"b": 0,
|
||||||
"a": 255
|
"a": 255
|
||||||
},
|
},
|
||||||
"_string": "这是",
|
"_string": "答案是:",
|
||||||
"_horizontalAlign": 1,
|
"_horizontalAlign": 1,
|
||||||
"_verticalAlign": 1,
|
"_verticalAlign": 1,
|
||||||
"_actualFontSize": 80,
|
"_actualFontSize": 80,
|
||||||
@@ -1771,7 +1771,7 @@
|
|||||||
},
|
},
|
||||||
"_lpos": {
|
"_lpos": {
|
||||||
"__type__": "cc.Vec3",
|
"__type__": "cc.Vec3",
|
||||||
"x": -150,
|
"x": -70,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
"z": 0
|
"z": 0
|
||||||
},
|
},
|
||||||
@@ -1904,7 +1904,7 @@
|
|||||||
},
|
},
|
||||||
"_lpos": {
|
"_lpos": {
|
||||||
"__type__": "cc.Vec3",
|
"__type__": "cc.Vec3",
|
||||||
"x": -110,
|
"x": -30,
|
||||||
"y": 41.081,
|
"y": 41.081,
|
||||||
"z": 0
|
"z": 0
|
||||||
},
|
},
|
||||||
@@ -6023,6 +6023,9 @@
|
|||||||
"clockLabel": {
|
"clockLabel": {
|
||||||
"__id__": 48
|
"__id__": 48
|
||||||
},
|
},
|
||||||
|
"liveLabel": {
|
||||||
|
"__id__": 30
|
||||||
|
},
|
||||||
"levelConfigs": [
|
"levelConfigs": [
|
||||||
{
|
{
|
||||||
"__id__": 250
|
"__id__": 250
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { _decorator, Node, EditBox, instantiate, Vec3, Button, Label, Sprite, SpriteFrame, AudioClip, AudioSource } from 'cc';
|
import { _decorator, Node, EditBox, instantiate, Vec3, Button, Label, Sprite, SpriteFrame, AudioClip, AudioSource } from 'cc';
|
||||||
import { BaseView } from 'db://assets/scripts/core/BaseView';
|
import { BaseView } from 'db://assets/scripts/core/BaseView';
|
||||||
import { ViewManager } from 'db://assets/scripts/core/ViewManager';
|
import { ViewManager } from 'db://assets/scripts/core/ViewManager';
|
||||||
|
import { StorageManager } from 'db://assets/scripts/utils/StorageManager';
|
||||||
const { ccclass, property } = _decorator;
|
const { ccclass, property } = _decorator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,6 +71,9 @@ export class PageLevel extends BaseView {
|
|||||||
@property(Label)
|
@property(Label)
|
||||||
clockLabel: Label | null = null;
|
clockLabel: Label | null = null;
|
||||||
|
|
||||||
|
@property(Label)
|
||||||
|
liveLabel: Label | null = null;
|
||||||
|
|
||||||
// ========== 配置属性 ==========
|
// ========== 配置属性 ==========
|
||||||
@property([LevelConfig])
|
@property([LevelConfig])
|
||||||
levelConfigs: LevelConfig[] = [];
|
levelConfigs: LevelConfig[] = [];
|
||||||
@@ -104,6 +108,7 @@ export class PageLevel extends BaseView {
|
|||||||
*/
|
*/
|
||||||
onViewLoad(): void {
|
onViewLoad(): void {
|
||||||
console.log('[PageLevel] onViewLoad');
|
console.log('[PageLevel] onViewLoad');
|
||||||
|
this.updateLiveLabel();
|
||||||
this.initLevel();
|
this.initLevel();
|
||||||
this.initIconSetting();
|
this.initIconSetting();
|
||||||
this.initUnlockButtons();
|
this.initUnlockButtons();
|
||||||
@@ -116,6 +121,7 @@ export class PageLevel extends BaseView {
|
|||||||
*/
|
*/
|
||||||
onViewShow(): void {
|
onViewShow(): void {
|
||||||
console.log('[PageLevel] onViewShow');
|
console.log('[PageLevel] onViewShow');
|
||||||
|
this.updateLiveLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -480,6 +486,17 @@ export class PageLevel extends BaseView {
|
|||||||
* 点击解锁线索
|
* 点击解锁线索
|
||||||
*/
|
*/
|
||||||
private onUnlockClue(index: number): void {
|
private onUnlockClue(index: number): void {
|
||||||
|
// 检查生命值是否足够
|
||||||
|
if (!this.hasLives()) {
|
||||||
|
console.warn('[PageLevel] 生命值不足,无法解锁线索');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 消耗一颗生命值
|
||||||
|
if (!this.consumeLife()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 播放点击音效
|
// 播放点击音效
|
||||||
this.playClickSound();
|
this.playClickSound();
|
||||||
|
|
||||||
@@ -607,6 +624,50 @@ export class PageLevel extends BaseView {
|
|||||||
// 可以在这里添加游戏结束逻辑
|
// 可以在这里添加游戏结束逻辑
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========== 生命值相关方法 ==========
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新生命值显示
|
||||||
|
*/
|
||||||
|
private updateLiveLabel(): void {
|
||||||
|
if (this.liveLabel) {
|
||||||
|
const lives = StorageManager.getLives();
|
||||||
|
this.liveLabel.string = `x ${lives}`;
|
||||||
|
console.log(`[PageLevel] 更新生命值显示: ${lives}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消耗一颗生命值(用于查看提示)
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
|
||||||
// ========== 答案提交与关卡切换 ==========
|
// ========== 答案提交与关卡切换 ==========
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -641,6 +702,9 @@ export class PageLevel extends BaseView {
|
|||||||
// 播放成功音效
|
// 播放成功音效
|
||||||
this.playSuccessSound();
|
this.playSuccessSound();
|
||||||
|
|
||||||
|
// 通关奖励:增加一颗生命值
|
||||||
|
this.addLife();
|
||||||
|
|
||||||
// 延迟后进入下一关
|
// 延迟后进入下一关
|
||||||
this.scheduleOnce(() => {
|
this.scheduleOnce(() => {
|
||||||
this.nextLevel();
|
this.nextLevel();
|
||||||
|
|||||||
87
assets/scripts/utils/StorageManager.ts
Normal file
87
assets/scripts/utils/StorageManager.ts
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import { sys } from 'cc';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地存储管理器
|
||||||
|
* 统一管理用户数据的本地持久化存储
|
||||||
|
*/
|
||||||
|
export class StorageManager {
|
||||||
|
/** 生命值存储键 */
|
||||||
|
private static readonly KEY_LIVES = 'game_lives';
|
||||||
|
|
||||||
|
/** 默认生命值 */
|
||||||
|
private static readonly DEFAULT_LIVES = 10;
|
||||||
|
|
||||||
|
/** 最小生命值 */
|
||||||
|
private static readonly MIN_LIVES = 0;
|
||||||
|
|
||||||
|
// ==================== 生命值管理 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前生命值
|
||||||
|
* @returns 当前生命值,新用户返回默认值 10
|
||||||
|
*/
|
||||||
|
static getLives(): number {
|
||||||
|
const stored = sys.localStorage.getItem(StorageManager.KEY_LIVES);
|
||||||
|
if (stored === null || stored === '') {
|
||||||
|
// 新用户,设置默认值
|
||||||
|
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
|
||||||
|
return StorageManager.DEFAULT_LIVES;
|
||||||
|
}
|
||||||
|
const lives = parseInt(stored, 10);
|
||||||
|
// 防止异常数据
|
||||||
|
if (isNaN(lives) || lives < 0) {
|
||||||
|
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
|
||||||
|
return StorageManager.DEFAULT_LIVES;
|
||||||
|
}
|
||||||
|
return lives;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置生命值
|
||||||
|
* @param lives 生命值
|
||||||
|
*/
|
||||||
|
static setLives(lives: number): void {
|
||||||
|
const validLives = Math.max(StorageManager.MIN_LIVES, lives);
|
||||||
|
sys.localStorage.setItem(StorageManager.KEY_LIVES, validLives.toString());
|
||||||
|
console.log(`[StorageManager] 生命值已更新: ${validLives}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消耗一颗生命
|
||||||
|
* @returns 是否消耗成功(生命值不足时返回 false)
|
||||||
|
*/
|
||||||
|
static consumeLife(): boolean {
|
||||||
|
const currentLives = StorageManager.getLives();
|
||||||
|
if (currentLives <= 0) {
|
||||||
|
console.warn('[StorageManager] 生命值不足,无法消耗');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
StorageManager.setLives(currentLives - 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加一颗生命
|
||||||
|
*/
|
||||||
|
static addLife(): void {
|
||||||
|
const currentLives = StorageManager.getLives();
|
||||||
|
StorageManager.setLives(currentLives + 1);
|
||||||
|
console.log(`[StorageManager] 获得一颗生命,当前生命值: ${currentLives + 1}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否有足够的生命值
|
||||||
|
* @returns 是否有生命值
|
||||||
|
*/
|
||||||
|
static hasLives(): boolean {
|
||||||
|
return StorageManager.getLives() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重置生命值为默认值
|
||||||
|
*/
|
||||||
|
static resetLives(): void {
|
||||||
|
StorageManager.setLives(StorageManager.DEFAULT_LIVES);
|
||||||
|
console.log('[StorageManager] 生命值已重置为默认值');
|
||||||
|
}
|
||||||
|
}
|
||||||
9
assets/scripts/utils/StorageManager.ts.meta
Normal file
9
assets/scripts/utils/StorageManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "4.0.24",
|
||||||
|
"importer": "typescript",
|
||||||
|
"imported": true,
|
||||||
|
"uuid": "3a09bd59-6481-4e26-8b61-c6e4b0003e59",
|
||||||
|
"files": [],
|
||||||
|
"subMetas": {},
|
||||||
|
"userData": {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user