Wall.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. const pd = this.saveDataManager.getPlayerData();
  112. if (pd && typeof pd.wallBaseHealth === 'number') {
  113. this.currentHealth = pd.wallBaseHealth;
  114. } else {
  115. // 如果没有存档数据,使用默认血量
  116. this.currentHealth = this.getWallHealthByLevel(1);
  117. }
  118. // 确保当前血量不超过最大血量(考虑技能加成)
  119. const maxHealth = this.getMaxHealth();
  120. if (this.currentHealth > maxHealth) {
  121. this.currentHealth = maxHealth;
  122. }
  123. }
  124. /**
  125. * 墙体受到伤害
  126. */
  127. public takeDamage(damage: number) {
  128. if (damage <= 0) return;
  129. const previousHealth = this.currentHealth;
  130. this.currentHealth = Math.max(0, this.currentHealth - damage);
  131. // 触发受到伤害事件
  132. const eventBus = EventBus.getInstance();
  133. eventBus.emit(GameEvents.WALL_TAKE_DAMAGE, {
  134. damage: damage,
  135. previousHealth: previousHealth,
  136. currentHealth: this.currentHealth
  137. });
  138. // 触发血量变化事件
  139. eventBus.emit(GameEvents.WALL_HEALTH_CHANGED, {
  140. previousHealth: previousHealth,
  141. currentHealth: this.currentHealth,
  142. maxHealth: this.getMaxHealth()
  143. });
  144. // 更新血量显示
  145. this.updateHealthDisplay();
  146. console.log(`[Wall] 墙体受到伤害: ${damage}, 当前血量: ${this.currentHealth}`);
  147. // 检查墙体是否被摧毁
  148. if (this.currentHealth <= 0) {
  149. this.onWallDestroyed();
  150. }
  151. }
  152. /**
  153. * 墙体被摧毁时的处理
  154. * 统一与菜单退出的失败处理流程,直接触发GAME_DEFEAT事件
  155. */
  156. private onWallDestroyed() {
  157. console.log('[Wall] 墙体被摧毁,触发游戏失败');
  158. // 通过事件系统触发墙体被摧毁事件(保留用于其他监听器)
  159. const eventBus = EventBus.getInstance();
  160. eventBus.emit(GameEvents.WALL_DESTROYED, {
  161. finalHealth: this.currentHealth,
  162. maxHealth: this.getMaxHealth()
  163. });
  164. // 统一失败处理:直接触发GAME_DEFEAT事件,与菜单退出处理保持一致
  165. console.log('[Wall] 直接触发GAME_DEFEAT事件,与菜单退出失败处理流程一致');
  166. eventBus.emit(GameEvents.GAME_DEFEAT);
  167. }
  168. /**
  169. * 更新血量显示
  170. */
  171. public updateHealthDisplay() {
  172. if (this.heartLabel) {
  173. this.heartLabel.string = Math.floor(this.currentHealth).toString();
  174. }
  175. }
  176. /**
  177. * 设置墙体血量
  178. */
  179. public setHealth(health: number) {
  180. const previousHealth = this.currentHealth;
  181. this.currentHealth = Math.max(0, health);
  182. // 如果血量发生变化,触发血量变化事件
  183. if (previousHealth !== this.currentHealth) {
  184. const eventBus = EventBus.getInstance();
  185. eventBus.emit(GameEvents.WALL_HEALTH_CHANGED, {
  186. previousHealth: previousHealth,
  187. currentHealth: this.currentHealth,
  188. maxHealth: this.getMaxHealth()
  189. });
  190. }
  191. this.updateHealthDisplay();
  192. }
  193. /**
  194. * 获取当前墙体血量
  195. */
  196. public getCurrentHealth(): number {
  197. return this.currentHealth;
  198. }
  199. /**
  200. * 获取最大血量(基于当前等级和技能加成)
  201. */
  202. public getMaxHealth(): number {
  203. const currentLevel = this.getCurrentWallLevel();
  204. const baseMaxHealth = this.getWallHealthByLevel(currentLevel);
  205. // 应用治疗技能的最大血量加成
  206. const skillManager = SkillManager.getInstance();
  207. if (skillManager) {
  208. const healSkillLevel = skillManager.getSkillLevel('heal');
  209. const healthBonus = SkillManager.getHealSkillHealthBonus(healSkillLevel);
  210. return Math.floor(baseMaxHealth * (1 + healthBonus));
  211. }
  212. return baseMaxHealth;
  213. }
  214. /**
  215. * 根据等级获取墙体血量
  216. */
  217. public getWallHealthByLevel(level: number): number {
  218. // 使用本地配置
  219. return this.wallHpMap[level] || (100 + (level - 1) * 200);
  220. }
  221. /**
  222. * 获取当前墙壁等级
  223. */
  224. public getCurrentWallLevel(): number {
  225. return this.saveDataManager?.getWallLevel() || 1;
  226. }
  227. /**
  228. * 恢复墙体血量
  229. */
  230. public heal(amount: number) {
  231. const previousHealth = this.currentHealth;
  232. const maxHealth = this.getMaxHealth();
  233. this.currentHealth = Math.min(maxHealth, this.currentHealth + amount);
  234. // 如果血量发生变化,触发血量变化事件
  235. if (previousHealth !== this.currentHealth) {
  236. const eventBus = EventBus.getInstance();
  237. eventBus.emit(GameEvents.WALL_HEALTH_CHANGED, {
  238. previousHealth: previousHealth,
  239. currentHealth: this.currentHealth,
  240. maxHealth: maxHealth,
  241. healAmount: this.currentHealth - previousHealth
  242. });
  243. }
  244. this.updateHealthDisplay();
  245. }
  246. /**
  247. * 重置墙体血量到满血
  248. */
  249. public resetToFullHealth() {
  250. this.currentHealth = this.getMaxHealth();
  251. this.updateHealthDisplay();
  252. }
  253. /**
  254. * 获取血量百分比
  255. */
  256. public getHealthPercentage(): number {
  257. const maxHealth = this.getMaxHealth();
  258. return maxHealth > 0 ? this.currentHealth / maxHealth : 0;
  259. }
  260. /**
  261. * 检查墙体是否存活
  262. */
  263. public isAlive(): boolean {
  264. return this.currentHealth > 0;
  265. }
  266. /**
  267. * 设置事件监听器
  268. */
  269. private setupEventListeners() {
  270. const eventBus = EventBus.getInstance();
  271. // 监听重置墙体血量事件
  272. eventBus.on(GameEvents.RESET_WALL_HEALTH, this.onResetWallHealthEvent, this);
  273. // 监听墙体血量变化事件(用于升级后更新显示)
  274. eventBus.on(GameEvents.WALL_HEALTH_CHANGED, this.onWallHealthChangedEvent, this);
  275. }
  276. /**
  277. * 处理重置墙体血量事件
  278. */
  279. private onResetWallHealthEvent() {
  280. console.log('[Wall] 接收到重置墙体血量事件,重置到满血');
  281. this.resetToFullHealth();
  282. }
  283. /**
  284. * 处理墙体血量变化事件(用于升级后更新)
  285. */
  286. private onWallHealthChangedEvent(eventData?: any) {
  287. // 只有在特定情况下才重新加载存档数据,避免覆盖受伤后的血量
  288. // 如果事件数据包含isUpgrade标志,说明是升级触发的,需要重新加载
  289. if (eventData && eventData.isUpgrade) {
  290. console.log('[Wall] 接收到墙体升级事件,重新加载血量数据');
  291. this.loadWallHealthFromSave();
  292. this.updateHealthDisplay();
  293. }
  294. // 其他情况(如受伤、治疗)不需要重新加载存档,血量已经在相应方法中更新
  295. }
  296. /**
  297. * 设置技能监听器
  298. */
  299. private setupSkillListeners() {
  300. const skillManager = SkillManager.getInstance();
  301. if (skillManager) {
  302. // 监听治疗技能变化
  303. skillManager.onSkillChanged('heal', this.onHealSkillChanged.bind(this));
  304. }
  305. }
  306. /**
  307. * 治疗技能变化回调
  308. */
  309. private onHealSkillChanged(skillId: string, level: number) {
  310. console.log(`[Wall] 治疗技能等级变化: ${level}`);
  311. // 技能升级时,墙体最大血量可能增加,需要更新显示
  312. this.updateHealthDisplay();
  313. // 如果当前血量低于新的最大血量,可以考虑给予一些额外治疗
  314. // 这里暂时不做额外处理,因为SkillSelectionController已经处理了即时治疗
  315. }
  316. /**
  317. * 清理技能监听器
  318. */
  319. private cleanupSkillListeners() {
  320. const skillManager = SkillManager.getInstance();
  321. if (skillManager) {
  322. skillManager.offSkillChanged('heal', this.onHealSkillChanged.bind(this));
  323. }
  324. }
  325. onDestroy() {
  326. // 清理事件监听
  327. const eventBus = EventBus.getInstance();
  328. eventBus.off(GameEvents.RESET_WALL_HEALTH, this.onResetWallHealthEvent, this);
  329. eventBus.off(GameEvents.WALL_HEALTH_CHANGED, this.onWallHealthChangedEvent, this);
  330. this.cleanupSkillListeners();
  331. }
  332. }