import { _decorator, Component, Node, Label, Sprite, Button } from 'cc'; import { SummaryManager } from './SummaryManager'; const { ccclass, property } = _decorator; /** * 游戏生命值管理器,处理生命值和挑战失败、当天总结UI */ @ccclass('GameLifeManager') export class GameLifeManager extends Component { @property({ tooltip: '最大生命值', min: 0, max: 3 }) maxLife: number = 3; @property({ type: [Sprite], tooltip: '生命值图标数组' }) lifeIcons: Sprite[] = []; @property({ type: Node, tooltip: '挑战失败UI面板' }) failurePanel: Node = null; @property({ type: Button, tooltip: '复活按钮' }) reviveButton: Button = null; @property({ type: Button, tooltip: '取消按钮' }) cancelButton: Button = null; @property({ type: SummaryManager, tooltip: '总结管理器引用' }) summaryManager: SummaryManager = null; // 当前生命值 private currentLife: number = 3; // 回调函数 private onReviveCallback: () => void = null; private onGameOverCallback: () => void = null; // 是否已经显示过总结面板 private hasSummaryShown: boolean = false; start() { // 初始化生命值 this.currentLife = this.maxLife; this.updateLifeDisplay(); // 隐藏面板 if (this.failurePanel) { this.failurePanel.active = false; } // 重置显示标志 this.hasSummaryShown = false; // 注册按钮事件 this.setupButtons(); } /** * 设置按钮事件 */ private setupButtons() { if (this.reviveButton) { this.reviveButton.node.on(Button.EventType.CLICK, this.handleRevive, this); } if (this.cancelButton) { this.cancelButton.node.on(Button.EventType.CLICK, this.handleCancel, this); } } /** * 设置回调函数 * @param onRevive 复活后的回调 * @param onGameOver 游戏结束的回调 */ public setCallbacks(onRevive: () => void, onGameOver: () => void) { this.onReviveCallback = onRevive; this.onGameOverCallback = onGameOver; } /** * 减少生命值 * @param amount 减少的数量,默认为1 * @returns 剩余生命值 */ public decreaseLife(amount: number = 1): number { this.currentLife = Math.max(0, this.currentLife - amount); console.log(`扣除${amount}点生命值,当前生命值: ${this.currentLife}`); // 更新生命值显示 this.updateLifeDisplay(); // 检查是否游戏结束 if (this.currentLife <= 0) { this.showFailurePanel(); return 0; } return this.currentLife; } /** * 增加生命值 * @param amount 增加的数量,默认为1 * @returns 剩余生命值 */ public increaseLife(amount: number = 1): number { this.currentLife = Math.min(this.maxLife, this.currentLife + amount); console.log(`恢复${amount}点生命值,当前生命值: ${this.currentLife}`); // 更新生命值显示 this.updateLifeDisplay(); return this.currentLife; } /** * 更新生命值显示 */ private updateLifeDisplay() { if (this.lifeIcons.length === 0) return; // 更新每个生命值图标的显示状态 for (let i = 0; i < this.lifeIcons.length; i++) { if (i < this.currentLife) { // 显示生命值图标 this.lifeIcons[i].node.active = true; } else { // 隐藏生命值图标 this.lifeIcons[i].node.active = false; } } } /** * 显示挑战失败面板 */ private showFailurePanel() { if (this.failurePanel) { this.failurePanel.active = true; this.failurePanel.setSiblingIndex(999); // 确保显示在最前面 } } /** * 隐藏挑战失败面板 */ private hideFailurePanel() { if (this.failurePanel) { this.failurePanel.active = false; } } /** * 显示当天总结面板 */ public showSummaryPanel() { // 标记已显示总结面板 this.hasSummaryShown = true; console.log('GameLifeManager.showSummaryPanel被调用,准备显示总结面板'); // 使用SummaryManager显示总结面板 if (this.summaryManager) { console.log('summaryManager引用存在,调用showSummaryPanel方法'); this.summaryManager.showSummaryPanel(); } else { console.error('summaryManager引用不存在,无法显示总结面板'); } } /** * 处理复活按钮点击 */ private handleRevive() { // 隐藏失败面板 this.hideFailurePanel(); // 恢复1点生命值 this.increaseLife(1); // 调用复活回调 if (this.onReviveCallback) { this.onReviveCallback(); } } /** * 处理取消按钮点击 */ private handleCancel() { // 隐藏失败面板 this.hideFailurePanel(); // 显示当天总结面板 this.showSummaryPanel(); // 调用游戏结束回调 if (this.onGameOverCallback) { this.onGameOverCallback(); } } /** * 获取当前生命值 */ public getCurrentLife(): number { return this.currentLife; } /** * 获取最大生命值 */ public getMaxLife(): number { return this.maxLife; } /** * 检查是否已显示总结面板 */ public hasSummaryPanelShown(): boolean { return this.hasSummaryShown; } /** * 重置总结面板显示状态 */ public resetSummaryPanelState(): void { this.hasSummaryShown = false; } /** * 重置生命值 * 在关卡切换时调用 */ public resetLife(): void { console.log('GameLifeManager.resetLife被调用,重置生命值'); // 恢复到最大生命值 this.currentLife = this.maxLife; // 更新生命值显示 this.updateLifeDisplay(); console.log(`生命值已重置为 ${this.currentLife}`); } onDestroy() { // 清理按钮事件 if (this.reviveButton) { this.reviveButton.node.off(Button.EventType.CLICK, this.handleRevive, this); } if (this.cancelButton) { this.cancelButton.node.off(Button.EventType.CLICK, this.handleCancel, this); } } }