Wall.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import { _decorator, Component, Node, Label, find, JsonAsset } 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. @property({
  18. type: JsonAsset,
  19. tooltip: '墙体配置文件'
  20. })
  21. public wallConfigAsset: JsonAsset = null;
  22. // === 私有属性 ===
  23. private currentHealth: number = 100;
  24. private heartLabel: Label = null;
  25. private saveDataManager: SaveDataManager = null;
  26. // 墙体配置数据
  27. private wallConfig: any = null;
  28. private wallHpMap: Record<number, number> = {};
  29. start() {
  30. this.initializeWall();
  31. }
  32. /**
  33. * 加载墙体配置
  34. */
  35. private loadWallConfig(): void {
  36. if (this.wallConfigAsset) {
  37. try {
  38. this.wallConfig = this.wallConfigAsset.json;
  39. if (this.wallConfig && this.wallConfig.wallConfig && this.wallConfig.wallConfig.healthByLevel) {
  40. // 转换字符串键为数字键
  41. const healthByLevel = this.wallConfig.wallConfig.healthByLevel;
  42. this.wallHpMap = {};
  43. for (const level in healthByLevel) {
  44. this.wallHpMap[parseInt(level)] = healthByLevel[level];
  45. }
  46. console.log('[Wall] 墙体配置加载成功:', this.wallHpMap);
  47. } else {
  48. console.warn('[Wall] 配置文件格式错误,使用默认配置');
  49. this.useDefaultConfig();
  50. }
  51. } catch (parseErr) {
  52. console.error('[Wall] 解析墙体配置失败:', parseErr);
  53. this.useDefaultConfig();
  54. }
  55. } else {
  56. console.warn('[Wall] 未挂载墙体配置文件,使用默认配置');
  57. this.useDefaultConfig();
  58. }
  59. }
  60. /**
  61. * 使用默认配置
  62. */
  63. private useDefaultConfig(): void {
  64. this.wallHpMap = {
  65. 1: 100,
  66. 2: 500,
  67. 3: 1200,
  68. 4: 1500,
  69. 5: 2000
  70. };
  71. }
  72. /**
  73. * 初始化墙体
  74. */
  75. private initializeWall() {
  76. // 初始化存档管理器
  77. this.saveDataManager = SaveDataManager.getInstance();
  78. if (!this.saveDataManager) {
  79. console.error('[Wall] SaveDataManager not found');
  80. return;
  81. }
  82. // 加载墙体配置
  83. this.loadWallConfig();
  84. // 查找血量显示节点
  85. this.findHeartLabelNode();
  86. // 从存档读取墙体血量
  87. this.loadWallHealthFromSave();
  88. // 初始化血量显示
  89. this.updateHealthDisplay();
  90. // 监听治疗技能变化
  91. this.setupSkillListeners();
  92. // 设置事件监听器
  93. this.setupEventListeners();
  94. }
  95. /**
  96. * 查找血量显示节点
  97. */
  98. private findHeartLabelNode() {
  99. // 查找心血显示节点
  100. if (!this.heartLabelNode) {
  101. this.heartLabelNode = find('Canvas-001/TopArea/HeartNode/HeartLabel') || find('Canvas/GameLevelUI/HeartNode/HeartLabel');
  102. }
  103. if (this.heartLabelNode) {
  104. this.heartLabel = this.heartLabelNode.getComponent(Label);
  105. }
  106. }
  107. /**
  108. * 从存档加载墙体血量
  109. */
  110. private loadWallHealthFromSave() {
  111. // 直接从SaveDataManager获取当前墙体血量
  112. this.currentHealth = this.saveDataManager.getWallHealth();
  113. console.log('[Wall] 从配置加载墙体血量:', this.currentHealth);
  114. // 确保当前血量不超过最大血量(考虑技能加成)
  115. const maxHealth = this.getMaxHealth();
  116. if (this.currentHealth > maxHealth) {
  117. this.currentHealth = maxHealth;
  118. }
  119. }
  120. /**
  121. * 墙体受到伤害
  122. */
  123. public takeDamage(damage: number) {
  124. if (damage <= 0) return;
  125. const previousHealth = this.currentHealth;
  126. this.currentHealth = Math.max(0, this.currentHealth - damage);
  127. // 触发受到伤害事件
  128. const eventBus = EventBus.getInstance();
  129. eventBus.emit(GameEvents.WALL_TAKE_DAMAGE, {
  130. damage: damage,
  131. previousHealth: previousHealth,
  132. currentHealth: this.currentHealth
  133. });
  134. // 触发血量变化事件
  135. eventBus.emit(GameEvents.WALL_HEALTH_CHANGED, {
  136. previousHealth: previousHealth,
  137. currentHealth: this.currentHealth,
  138. maxHealth: this.getMaxHealth()
  139. });
  140. // 更新血量显示
  141. this.updateHealthDisplay();
  142. console.log(`[Wall] 墙体受到伤害: ${damage}, 当前血量: ${this.currentHealth}`);
  143. // 检查墙体是否被摧毁
  144. if (this.currentHealth <= 0) {
  145. this.onWallDestroyed();
  146. }
  147. }
  148. /**
  149. * 墙体被摧毁时的处理
  150. * 统一与菜单退出的失败处理流程,直接触发GAME_DEFEAT事件
  151. */
  152. private onWallDestroyed() {
  153. console.log('[Wall] 墙体被摧毁,触发游戏失败');
  154. // 通过事件系统触发墙体被摧毁事件(保留用于其他监听器)
  155. const eventBus = EventBus.getInstance();
  156. eventBus.emit(GameEvents.WALL_DESTROYED, {
  157. finalHealth: this.currentHealth,
  158. maxHealth: this.getMaxHealth()
  159. });
  160. // 统一失败处理:直接触发GAME_DEFEAT事件,与菜单退出处理保持一致
  161. console.log('[Wall] 直接触发GAME_DEFEAT事件,与菜单退出失败处理流程一致');
  162. eventBus.emit(GameEvents.GAME_DEFEAT);
  163. }
  164. /**
  165. * 更新血量显示
  166. */
  167. public updateHealthDisplay() {
  168. if (this.heartLabel) {
  169. this.heartLabel.string = Math.floor(this.currentHealth).toString();
  170. }
  171. }
  172. /**
  173. * 设置墙体血量
  174. */
  175. public setHealth(health: number) {
  176. const previousHealth = this.currentHealth;
  177. this.currentHealth = Math.max(0, health);
  178. // 如果血量发生变化,触发血量变化事件
  179. if (previousHealth !== this.currentHealth) {
  180. const eventBus = EventBus.getInstance();
  181. eventBus.emit(GameEvents.WALL_HEALTH_CHANGED, {
  182. previousHealth: previousHealth,
  183. currentHealth: this.currentHealth,
  184. maxHealth: this.getMaxHealth()
  185. });
  186. }
  187. this.updateHealthDisplay();
  188. }
  189. /**
  190. * 获取当前墙体血量
  191. */
  192. public getCurrentHealth(): number {
  193. return this.currentHealth;
  194. }
  195. /**
  196. * 获取最大血量(基于当前等级和技能加成)
  197. */
  198. public getMaxHealth(): number {
  199. const currentLevel = this.getCurrentWallLevel();
  200. const baseMaxHealth = this.getWallHealthByLevel(currentLevel);
  201. // 应用治疗技能的最大血量加成
  202. const skillManager = SkillManager.getInstance();
  203. if (skillManager) {
  204. const healSkillLevel = skillManager.getSkillLevel('heal');
  205. const healthBonus = SkillManager.getHealSkillHealthBonus(healSkillLevel);
  206. return Math.floor(baseMaxHealth * (1 + healthBonus));
  207. }
  208. return baseMaxHealth;
  209. }
  210. /**
  211. * 根据等级获取墙体血量
  212. */
  213. public getWallHealthByLevel(level: number): number {
  214. // 使用本地配置
  215. return this.wallHpMap[level] || (100 + (level - 1) * 200);
  216. }
  217. /**
  218. * 获取当前墙壁等级
  219. */
  220. public getCurrentWallLevel(): number {
  221. return this.saveDataManager?.getWallLevel() || 1;
  222. }
  223. /**
  224. * 恢复墙体血量
  225. */
  226. public heal(amount: number) {
  227. const previousHealth = this.currentHealth;
  228. const maxHealth = this.getMaxHealth();
  229. this.currentHealth = Math.min(maxHealth, this.currentHealth + amount);
  230. // 如果血量发生变化,触发血量变化事件
  231. if (previousHealth !== this.currentHealth) {
  232. const eventBus = EventBus.getInstance();
  233. eventBus.emit(GameEvents.WALL_HEALTH_CHANGED, {
  234. previousHealth: previousHealth,
  235. currentHealth: this.currentHealth,
  236. maxHealth: maxHealth,
  237. healAmount: this.currentHealth - previousHealth
  238. });
  239. }
  240. this.updateHealthDisplay();
  241. }
  242. /**
  243. * 重置墙体血量到满血
  244. */
  245. public resetToFullHealth() {
  246. this.currentHealth = this.getMaxHealth();
  247. this.updateHealthDisplay();
  248. }
  249. /**
  250. * 获取血量百分比
  251. */
  252. public getHealthPercentage(): number {
  253. const maxHealth = this.getMaxHealth();
  254. return maxHealth > 0 ? this.currentHealth / maxHealth : 0;
  255. }
  256. /**
  257. * 检查墙体是否存活
  258. */
  259. public isAlive(): boolean {
  260. return this.currentHealth > 0;
  261. }
  262. /**
  263. * 设置事件监听器
  264. */
  265. private setupEventListeners() {
  266. const eventBus = EventBus.getInstance();
  267. // 监听重置墙体血量事件
  268. eventBus.on(GameEvents.RESET_WALL_HEALTH, this.onResetWallHealthEvent, this);
  269. // 监听墙体血量变化事件(用于升级后更新显示)
  270. eventBus.on(GameEvents.WALL_HEALTH_CHANGED, this.onWallHealthChangedEvent, this);
  271. }
  272. /**
  273. * 处理重置墙体血量事件
  274. */
  275. private onResetWallHealthEvent() {
  276. console.log('[Wall] 接收到重置墙体血量事件,重置到满血');
  277. this.resetToFullHealth();
  278. }
  279. /**
  280. * 处理墙体血量变化事件(用于升级后更新)
  281. */
  282. private onWallHealthChangedEvent(eventData?: any) {
  283. // 只有在特定情况下才重新加载存档数据,避免覆盖受伤后的血量
  284. // 如果事件数据包含isUpgrade标志,说明是升级触发的,需要重新加载
  285. if (eventData && eventData.isUpgrade) {
  286. console.log('[Wall] 接收到墙体升级事件,重新加载血量数据');
  287. this.loadWallHealthFromSave();
  288. this.updateHealthDisplay();
  289. }
  290. // 其他情况(如受伤、治疗)不需要重新加载存档,血量已经在相应方法中更新
  291. }
  292. /**
  293. * 设置技能监听器
  294. */
  295. private setupSkillListeners() {
  296. const skillManager = SkillManager.getInstance();
  297. if (skillManager) {
  298. // 监听治疗技能变化
  299. skillManager.onSkillChanged('heal', this.onHealSkillChanged.bind(this));
  300. }
  301. }
  302. /**
  303. * 治疗技能变化回调
  304. */
  305. private onHealSkillChanged(skillId: string, level: number) {
  306. console.log(`[Wall] 治疗技能等级变化: ${level}`);
  307. // 技能升级时,墙体最大血量可能增加,需要更新显示
  308. this.updateHealthDisplay();
  309. // 如果当前血量低于新的最大血量,可以考虑给予一些额外治疗
  310. // 这里暂时不做额外处理,因为SkillSelectionController已经处理了即时治疗
  311. }
  312. /**
  313. * 清理技能监听器
  314. */
  315. private cleanupSkillListeners() {
  316. const skillManager = SkillManager.getInstance();
  317. if (skillManager) {
  318. skillManager.offSkillChanged('heal', this.onHealSkillChanged.bind(this));
  319. }
  320. }
  321. onDestroy() {
  322. // 清理事件监听
  323. const eventBus = EventBus.getInstance();
  324. eventBus.off(GameEvents.RESET_WALL_HEALTH, this.onResetWallHealthEvent, this);
  325. eventBus.off(GameEvents.WALL_HEALTH_CHANGED, this.onWallHealthChangedEvent, this);
  326. this.cleanupSkillListeners();
  327. }
  328. }