GameLifeManager.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { _decorator, Component, Node, Label, Sprite, Button } from 'cc';
  2. import { SummaryManager } from './SummaryManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 游戏生命值管理器,处理生命值和挑战失败、当天总结UI
  6. */
  7. @ccclass('GameLifeManager')
  8. export class GameLifeManager extends Component {
  9. @property({
  10. tooltip: '最大生命值',
  11. min: 0,
  12. max: 3
  13. })
  14. maxLife: number = 3;
  15. @property({
  16. type: [Sprite],
  17. tooltip: '生命值图标数组'
  18. })
  19. lifeIcons: Sprite[] = [];
  20. @property({
  21. type: Node,
  22. tooltip: '挑战失败UI面板'
  23. })
  24. failurePanel: Node = null;
  25. @property({
  26. type: Button,
  27. tooltip: '复活按钮'
  28. })
  29. reviveButton: Button = null;
  30. @property({
  31. type: Button,
  32. tooltip: '取消按钮'
  33. })
  34. cancelButton: Button = null;
  35. @property({
  36. type: SummaryManager,
  37. tooltip: '总结管理器引用'
  38. })
  39. summaryManager: SummaryManager = null;
  40. // 当前生命值
  41. private currentLife: number = 3;
  42. // 回调函数
  43. private onReviveCallback: () => void = null;
  44. private onGameOverCallback: () => void = null;
  45. // 是否已经显示过总结面板
  46. private hasSummaryShown: boolean = false;
  47. start() {
  48. // 初始化生命值
  49. this.currentLife = this.maxLife;
  50. this.updateLifeDisplay();
  51. // 隐藏面板
  52. if (this.failurePanel) {
  53. this.failurePanel.active = false;
  54. }
  55. // 重置显示标志
  56. this.hasSummaryShown = false;
  57. // 注册按钮事件
  58. this.setupButtons();
  59. }
  60. /**
  61. * 设置按钮事件
  62. */
  63. private setupButtons() {
  64. if (this.reviveButton) {
  65. this.reviveButton.node.on(Button.EventType.CLICK, this.handleRevive, this);
  66. }
  67. if (this.cancelButton) {
  68. this.cancelButton.node.on(Button.EventType.CLICK, this.handleCancel, this);
  69. }
  70. }
  71. /**
  72. * 设置回调函数
  73. * @param onRevive 复活后的回调
  74. * @param onGameOver 游戏结束的回调
  75. */
  76. public setCallbacks(onRevive: () => void, onGameOver: () => void) {
  77. this.onReviveCallback = onRevive;
  78. this.onGameOverCallback = onGameOver;
  79. }
  80. /**
  81. * 减少生命值
  82. * @param amount 减少的数量,默认为1
  83. * @returns 剩余生命值
  84. */
  85. public decreaseLife(amount: number = 1): number {
  86. this.currentLife = Math.max(0, this.currentLife - amount);
  87. console.log(`扣除${amount}点生命值,当前生命值: ${this.currentLife}`);
  88. // 更新生命值显示
  89. this.updateLifeDisplay();
  90. // 检查是否游戏结束
  91. if (this.currentLife <= 0) {
  92. this.showFailurePanel();
  93. return 0;
  94. }
  95. return this.currentLife;
  96. }
  97. /**
  98. * 增加生命值
  99. * @param amount 增加的数量,默认为1
  100. * @returns 剩余生命值
  101. */
  102. public increaseLife(amount: number = 1): number {
  103. this.currentLife = Math.min(this.maxLife, this.currentLife + amount);
  104. console.log(`恢复${amount}点生命值,当前生命值: ${this.currentLife}`);
  105. // 更新生命值显示
  106. this.updateLifeDisplay();
  107. return this.currentLife;
  108. }
  109. /**
  110. * 更新生命值显示
  111. */
  112. private updateLifeDisplay() {
  113. if (this.lifeIcons.length === 0) return;
  114. // 更新每个生命值图标的显示状态
  115. for (let i = 0; i < this.lifeIcons.length; i++) {
  116. if (i < this.currentLife) {
  117. // 显示生命值图标
  118. this.lifeIcons[i].node.active = true;
  119. } else {
  120. // 隐藏生命值图标
  121. this.lifeIcons[i].node.active = false;
  122. }
  123. }
  124. }
  125. /**
  126. * 显示挑战失败面板
  127. */
  128. private showFailurePanel() {
  129. if (this.failurePanel) {
  130. this.failurePanel.active = true;
  131. this.failurePanel.setSiblingIndex(999); // 确保显示在最前面
  132. }
  133. }
  134. /**
  135. * 隐藏挑战失败面板
  136. */
  137. private hideFailurePanel() {
  138. if (this.failurePanel) {
  139. this.failurePanel.active = false;
  140. }
  141. }
  142. /**
  143. * 显示当天总结面板
  144. */
  145. public showSummaryPanel() {
  146. // 标记已显示总结面板
  147. this.hasSummaryShown = true;
  148. console.log('GameLifeManager.showSummaryPanel被调用,准备显示总结面板');
  149. // 使用SummaryManager显示总结面板
  150. if (this.summaryManager) {
  151. console.log('summaryManager引用存在,调用showSummaryPanel方法');
  152. this.summaryManager.showSummaryPanel();
  153. } else {
  154. console.error('summaryManager引用不存在,无法显示总结面板');
  155. }
  156. }
  157. /**
  158. * 处理复活按钮点击
  159. */
  160. private handleRevive() {
  161. // 隐藏失败面板
  162. this.hideFailurePanel();
  163. // 恢复1点生命值
  164. this.increaseLife(1);
  165. // 调用复活回调
  166. if (this.onReviveCallback) {
  167. this.onReviveCallback();
  168. }
  169. }
  170. /**
  171. * 处理取消按钮点击
  172. */
  173. private handleCancel() {
  174. // 隐藏失败面板
  175. this.hideFailurePanel();
  176. // 显示当天总结面板
  177. this.showSummaryPanel();
  178. // 调用游戏结束回调
  179. if (this.onGameOverCallback) {
  180. this.onGameOverCallback();
  181. }
  182. }
  183. /**
  184. * 获取当前生命值
  185. */
  186. public getCurrentLife(): number {
  187. return this.currentLife;
  188. }
  189. /**
  190. * 获取最大生命值
  191. */
  192. public getMaxLife(): number {
  193. return this.maxLife;
  194. }
  195. /**
  196. * 检查是否已显示总结面板
  197. */
  198. public hasSummaryPanelShown(): boolean {
  199. return this.hasSummaryShown;
  200. }
  201. /**
  202. * 重置总结面板显示状态
  203. */
  204. public resetSummaryPanelState(): void {
  205. this.hasSummaryShown = false;
  206. }
  207. /**
  208. * 重置生命值
  209. * 在关卡切换时调用
  210. */
  211. public resetLife(): void {
  212. console.log('GameLifeManager.resetLife被调用,重置生命值');
  213. // 恢复到最大生命值
  214. this.currentLife = this.maxLife;
  215. // 更新生命值显示
  216. this.updateLifeDisplay();
  217. console.log(`生命值已重置为 ${this.currentLife}`);
  218. }
  219. onDestroy() {
  220. // 清理按钮事件
  221. if (this.reviveButton) {
  222. this.reviveButton.node.off(Button.EventType.CLICK, this.handleRevive, this);
  223. }
  224. if (this.cancelButton) {
  225. this.cancelButton.node.off(Button.EventType.CLICK, this.handleCancel, this);
  226. }
  227. }
  228. }