feat: 添加生命值系统
- 新增 StorageManager 本地存储管理器,管理用户生命值 - 新用户默认 10 点生命值,存储在 localStorage - 查看提示消耗 1 点生命值 - 通关奖励 1 点生命值 - PageLevel 集成生命值显示和消耗逻辑
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { _decorator, Node, EditBox, instantiate, Vec3, Button, Label, Sprite, SpriteFrame, AudioClip, AudioSource } from 'cc';
|
||||
import { BaseView } from 'db://assets/scripts/core/BaseView';
|
||||
import { ViewManager } from 'db://assets/scripts/core/ViewManager';
|
||||
import { StorageManager } from 'db://assets/scripts/utils/StorageManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
@@ -70,6 +71,9 @@ export class PageLevel extends BaseView {
|
||||
@property(Label)
|
||||
clockLabel: Label | null = null;
|
||||
|
||||
@property(Label)
|
||||
liveLabel: Label | null = null;
|
||||
|
||||
// ========== 配置属性 ==========
|
||||
@property([LevelConfig])
|
||||
levelConfigs: LevelConfig[] = [];
|
||||
@@ -104,6 +108,7 @@ export class PageLevel extends BaseView {
|
||||
*/
|
||||
onViewLoad(): void {
|
||||
console.log('[PageLevel] onViewLoad');
|
||||
this.updateLiveLabel();
|
||||
this.initLevel();
|
||||
this.initIconSetting();
|
||||
this.initUnlockButtons();
|
||||
@@ -116,6 +121,7 @@ export class PageLevel extends BaseView {
|
||||
*/
|
||||
onViewShow(): void {
|
||||
console.log('[PageLevel] onViewShow');
|
||||
this.updateLiveLabel();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -480,6 +486,17 @@ export class PageLevel extends BaseView {
|
||||
* 点击解锁线索
|
||||
*/
|
||||
private onUnlockClue(index: number): void {
|
||||
// 检查生命值是否足够
|
||||
if (!this.hasLives()) {
|
||||
console.warn('[PageLevel] 生命值不足,无法解锁线索');
|
||||
return;
|
||||
}
|
||||
|
||||
// 消耗一颗生命值
|
||||
if (!this.consumeLife()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 播放点击音效
|
||||
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.addLife();
|
||||
|
||||
// 延迟后进入下一关
|
||||
this.scheduleOnce(() => {
|
||||
this.nextLevel();
|
||||
|
||||
Reference in New Issue
Block a user