Wall.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { _decorator, Component, Node, Label, find } from 'cc';
  2. import { SaveDataManager } from '../LevelSystem/SaveDataManager';
  3. import EventBus, { GameEvents } from '../Core/EventBus';
  4. import { SkillManager } from './SkillSelection/SkillManager';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 墙体组件
  8. * 负责管理墙体的血量、伤害处理、等级升级等功能
  9. */
  10. @ccclass('Wall')
  11. export class Wall extends Component {
  12. @property({
  13. type: Node,
  14. tooltip: '血量显示节点 (HeartLabel)'
  15. })
  16. public heartLabelNode: Node = null;
  17. // === 私有属性 ===
  18. private currentHealth: number = 100;
  19. private heartLabel: Label = null;
  20. private saveDataManager: SaveDataManager = null;
  21. // 墙体等级血量映射
  22. private wallHpMap: Record<number, number> = {
  23. 1: 100,
  24. 2: 1000,
  25. 3: 1200,
  26. 4: 1500,
  27. 5: 2000
  28. };
  29. start() {
  30. this.initializeWall();
  31. }
  32. /**
  33. * 初始化墙体
  34. */
  35. private initializeWall() {
  36. // 初始化存档管理器
  37. this.saveDataManager = SaveDataManager.getInstance();
  38. if (!this.saveDataManager) {
  39. console.error('[Wall] SaveDataManager not found');
  40. return;
  41. }
  42. // 查找血量显示节点
  43. this.findHeartLabelNode();
  44. // 从存档读取墙体血量
  45. this.loadWallHealthFromSave();
  46. // 初始化血量显示
  47. this.updateHealthDisplay();
  48. // 监听治疗技能变化
  49. this.setupSkillListeners();
  50. // 设置事件监听器
  51. this.setupEventListeners();
  52. }
  53. /**
  54. * 查找血量显示节点
  55. */
  56. private findHeartLabelNode() {
  57. // 查找心血显示节点
  58. if (!this.heartLabelNode) {
  59. this.heartLabelNode = find('Canvas-001/TopArea/HeartNode/HeartLabeld') || find('Canvas/GameLevelUI/HeartNode/HeartLabeld');
  60. }
  61. if (this.heartLabelNode) {
  62. this.heartLabel = this.heartLabelNode.getComponent(Label);
  63. }
  64. }
  65. /**
  66. * 从存档加载墙体血量
  67. */
  68. private loadWallHealthFromSave() {
  69. const pd = this.saveDataManager.getPlayerData();
  70. if (pd && typeof pd.wallBaseHealth === 'number') {
  71. this.currentHealth = pd.wallBaseHealth;
  72. } else {
  73. // 如果没有存档数据,使用默认血量
  74. this.currentHealth = this.getWallHealthByLevel(1);
  75. }
  76. // 确保当前血量不超过最大血量(考虑技能加成)
  77. const maxHealth = this.getMaxHealth();
  78. if (this.currentHealth > maxHealth) {
  79. this.currentHealth = maxHealth;
  80. }
  81. }
  82. /**
  83. * 墙体受到伤害
  84. */
  85. public takeDamage(damage: number) {
  86. if (damage <= 0) return;
  87. this.currentHealth = Math.max(0, this.currentHealth - damage);
  88. // 更新血量显示
  89. this.updateHealthDisplay();
  90. console.log(`[Wall] 墙体受到伤害: ${damage}, 当前血量: ${this.currentHealth}`);
  91. // 检查墙体是否被摧毁
  92. if (this.currentHealth <= 0) {
  93. this.onWallDestroyed();
  94. }
  95. }
  96. /**
  97. * 墙体被摧毁时的处理
  98. */
  99. private onWallDestroyed() {
  100. console.log('[Wall] 墙体被摧毁,触发游戏失败');
  101. // 通过事件系统触发游戏失败
  102. const eventBus = EventBus.getInstance();
  103. eventBus.emit(GameEvents.GAME_DEFEAT);
  104. }
  105. /**
  106. * 更新血量显示
  107. */
  108. public updateHealthDisplay() {
  109. if (this.heartLabel) {
  110. this.heartLabel.string = this.currentHealth.toString();
  111. }
  112. }
  113. /**
  114. * 设置墙体血量
  115. */
  116. public setHealth(health: number) {
  117. this.currentHealth = Math.max(0, health);
  118. this.updateHealthDisplay();
  119. }
  120. /**
  121. * 获取当前墙体血量
  122. */
  123. public getCurrentHealth(): number {
  124. return this.currentHealth;
  125. }
  126. /**
  127. * 获取最大血量(基于当前等级和技能加成)
  128. */
  129. public getMaxHealth(): number {
  130. const currentLevel = this.getCurrentWallLevel();
  131. const baseMaxHealth = this.getWallHealthByLevel(currentLevel);
  132. // 应用治疗技能的最大血量加成
  133. const skillManager = SkillManager.getInstance();
  134. if (skillManager) {
  135. const healSkillLevel = skillManager.getSkillLevel('heal');
  136. const healthBonus = SkillManager.getHealSkillHealthBonus(healSkillLevel);
  137. return baseMaxHealth * (1 + healthBonus);
  138. }
  139. return baseMaxHealth;
  140. }
  141. /**
  142. * 根据等级获取墙体血量
  143. */
  144. public getWallHealthByLevel(level: number): number {
  145. return this.wallHpMap[level] || (100 + (level - 1) * 200);
  146. }
  147. /**
  148. * 获取当前墙壁等级
  149. */
  150. public getCurrentWallLevel(): number {
  151. return this.saveDataManager?.getWallLevel() || 1;
  152. }
  153. /**
  154. * 升级墙体等级
  155. * 返回升级后信息,失败返回null
  156. */
  157. public upgradeWallLevel(): { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null {
  158. if (!this.saveDataManager) return null;
  159. // 检查是否可以升级
  160. if (!this.saveDataManager.canUpgradeWall()) return null;
  161. // 获取升级费用并扣除金币
  162. const cost = this.saveDataManager.getWallUpgradeCost();
  163. if (!this.saveDataManager.spendCoins(cost)) return null;
  164. // 执行升级
  165. if (!this.saveDataManager.upgradeWallLevel()) return null;
  166. const newLvl = this.saveDataManager.getWallLevel();
  167. const baseNewHp = this.getWallHealthByLevel(newLvl);
  168. const actualNewHp = this.getWallHealthByLevel(newLvl); // 使用当前等级的基础血量
  169. // 更新内存中的数值为当前等级的基础血量
  170. this.currentHealth = actualNewHp;
  171. this.updateHealthDisplay();
  172. return {
  173. currentLevel: newLvl,
  174. currentHp: actualNewHp,
  175. nextLevel: newLvl + 1,
  176. nextHp: this.getWallHealthByLevel(newLvl + 1) // 这里返回基础血量用于显示
  177. };
  178. }
  179. /**
  180. * 恢复墙体血量
  181. */
  182. public heal(amount: number) {
  183. const maxHealth = this.getMaxHealth();
  184. this.currentHealth = Math.min(maxHealth, this.currentHealth + amount);
  185. this.updateHealthDisplay();
  186. }
  187. /**
  188. * 重置墙体血量到满血
  189. */
  190. public resetToFullHealth() {
  191. this.currentHealth = this.getMaxHealth();
  192. this.updateHealthDisplay();
  193. }
  194. /**
  195. * 获取血量百分比
  196. */
  197. public getHealthPercentage(): number {
  198. const maxHealth = this.getMaxHealth();
  199. return maxHealth > 0 ? this.currentHealth / maxHealth : 0;
  200. }
  201. /**
  202. * 检查墙体是否存活
  203. */
  204. public isAlive(): boolean {
  205. return this.currentHealth > 0;
  206. }
  207. /**
  208. * 设置事件监听器
  209. */
  210. private setupEventListeners() {
  211. const eventBus = EventBus.getInstance();
  212. // 监听重置墙体血量事件
  213. eventBus.on(GameEvents.RESET_WALL_HEALTH, this.onResetWallHealthEvent, this);
  214. }
  215. /**
  216. * 处理重置墙体血量事件
  217. */
  218. private onResetWallHealthEvent() {
  219. console.log('[Wall] 接收到重置墙体血量事件,重置到满血');
  220. this.resetToFullHealth();
  221. }
  222. /**
  223. * 设置技能监听器
  224. */
  225. private setupSkillListeners() {
  226. const skillManager = SkillManager.getInstance();
  227. if (skillManager) {
  228. // 监听治疗技能变化
  229. skillManager.onSkillChanged('heal', this.onHealSkillChanged.bind(this));
  230. }
  231. }
  232. /**
  233. * 治疗技能变化回调
  234. */
  235. private onHealSkillChanged(skillId: string, level: number) {
  236. console.log(`[Wall] 治疗技能等级变化: ${level}`);
  237. // 技能升级时,墙体最大血量可能增加,需要更新显示
  238. this.updateHealthDisplay();
  239. // 如果当前血量低于新的最大血量,可以考虑给予一些额外治疗
  240. // 这里暂时不做额外处理,因为SkillSelectionController已经处理了即时治疗
  241. }
  242. /**
  243. * 清理技能监听器
  244. */
  245. private cleanupSkillListeners() {
  246. const skillManager = SkillManager.getInstance();
  247. if (skillManager) {
  248. skillManager.offSkillChanged('heal', this.onHealSkillChanged.bind(this));
  249. }
  250. }
  251. onDestroy() {
  252. // 清理事件监听
  253. const eventBus = EventBus.getInstance();
  254. eventBus.off(GameEvents.RESET_WALL_HEALTH, this.onResetWallHealthEvent, this);
  255. this.cleanupSkillListeners();
  256. }
  257. }