WeaponInfo.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. import { _decorator, Component, Node } from 'cc';
  2. import { WeaponConfig } from '../../Core/ConfigManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 武器信息组件
  6. * 用于存储和管理挂载到武器节点上的武器配置信息
  7. */
  8. @ccclass('WeaponInfo')
  9. export class WeaponInfo extends Component {
  10. // 编辑器面板可见属性
  11. @property({ displayName: "武器ID", readonly: true })
  12. public weaponId: string = "";
  13. @property({ displayName: "武器名称", readonly: true })
  14. public weaponName: string = "";
  15. @property({ displayName: "武器等级", readonly: true })
  16. public weaponLevel: number = 1;
  17. @property({ displayName: "武器伤害", readonly: true })
  18. public weaponDamage: number = 0;
  19. @property({ displayName: "射速", readonly: true })
  20. public fireRate: number = 0;
  21. @property({ displayName: "射程", readonly: true })
  22. public range: number = 0;
  23. @property({ displayName: "是否激活", readonly: true })
  24. public activeStatus: boolean = false;
  25. @property({ displayName: "总伤害", readonly: true })
  26. public totalDamage: number = 0;
  27. @property({ displayName: "击杀数", readonly: true })
  28. public killCount: number = 0;
  29. // 武器配置数据
  30. private _weaponConfig: WeaponConfig | null = null;
  31. // 武器等级(用于升级系统)
  32. private _weaponLevel: number = 1;
  33. // 武器是否已激活(用于战斗状态管理)
  34. private _isActive: boolean = false;
  35. // 武器最后开火时间(用于冷却计算)
  36. private _lastFireTime: number = 0;
  37. // 武器当前弹药数(如果适用)
  38. private _currentAmmo: number = -1; // -1表示无限弹药
  39. // 武器累计伤害统计
  40. private _totalDamageDealt: number = 0;
  41. // 武器击杀数统计
  42. private _killCount: number = 0;
  43. /**
  44. * 设置武器配置
  45. * @param config 武器配置数据
  46. */
  47. public setWeaponConfig(config: WeaponConfig): void {
  48. this._weaponConfig = config;
  49. this._weaponLevel = 1;
  50. this._isActive = true;
  51. this._lastFireTime = 0;
  52. this._totalDamageDealt = 0;
  53. this._killCount = 0;
  54. // 更新编辑器面板可见属性
  55. this.weaponId = config.id;
  56. this.weaponName = config.name;
  57. this.weaponLevel = this._weaponLevel;
  58. this.weaponDamage = this.getDamage();
  59. this.fireRate = config.stats.fireRate;
  60. this.range = config.stats.range;
  61. this.activeStatus = this._isActive;
  62. this.totalDamage = this._totalDamageDealt;
  63. this.killCount = this._killCount;
  64. // 如果武器配置中有弹药限制,设置初始弹药数
  65. if (config.bulletConfig && config.bulletConfig.count) {
  66. // 这里可以根据具体需求设置弹药数,目前设为无限
  67. this._currentAmmo = -1;
  68. }
  69. console.log(`[WeaponInfo] 武器配置已设置: ${config.name} (${config.id})`);
  70. }
  71. /**
  72. * 获取武器配置
  73. * @returns 武器配置数据
  74. */
  75. public getWeaponConfig(): WeaponConfig | null {
  76. return this._weaponConfig;
  77. }
  78. /**
  79. * 获取武器ID
  80. * @returns 武器ID
  81. */
  82. public getWeaponId(): string | null {
  83. return this._weaponConfig ? this._weaponConfig.id : null;
  84. }
  85. /**
  86. * 获取武器名称
  87. * @returns 武器名称
  88. */
  89. public getWeaponName(): string | null {
  90. return this._weaponConfig ? this._weaponConfig.name : null;
  91. }
  92. /**
  93. * 获取武器类型
  94. * @returns 武器类型
  95. */
  96. public getWeaponType(): string | null {
  97. return this._weaponConfig ? this._weaponConfig.type : null;
  98. }
  99. /**
  100. * 获取武器稀有度
  101. * @returns 武器稀有度
  102. */
  103. public getWeaponRarity(): string | null {
  104. return this._weaponConfig ? (this._weaponConfig.rarity || 'common') : null;
  105. }
  106. /**
  107. * 设置武器等级
  108. * @param level 武器等级
  109. */
  110. public setWeaponLevel(level: number): void {
  111. if (level < 1) {
  112. console.warn(`[WeaponInfo] 武器等级不能小于1,当前设置: ${level}`);
  113. return;
  114. }
  115. const maxLevel = this._weaponConfig?.upgradeConfig?.maxLevel || 10;
  116. if (level > maxLevel) {
  117. console.warn(`[WeaponInfo] 武器等级不能超过最大等级 ${maxLevel},当前设置: ${level}`);
  118. return;
  119. }
  120. this._weaponLevel = level;
  121. // 更新编辑器面板可见属性
  122. this.weaponLevel = this._weaponLevel;
  123. this.weaponDamage = this.getDamage();
  124. console.log(`[WeaponInfo] 武器等级已设置为: ${level}`);
  125. }
  126. /**
  127. * 获取武器等级
  128. * @returns 武器等级
  129. */
  130. public getWeaponLevel(): number {
  131. return this._weaponLevel;
  132. }
  133. /**
  134. * 获取武器最大等级
  135. * @returns 武器最大等级
  136. */
  137. public getMaxWeaponLevel(): number {
  138. return this._weaponConfig?.upgradeConfig?.maxLevel || 10;
  139. }
  140. /**
  141. * 设置武器激活状态
  142. * @param active 是否激活
  143. */
  144. public setActive(active: boolean): void {
  145. this._isActive = active;
  146. // 更新编辑器面板可见属性
  147. this.activeStatus = this._isActive;
  148. console.log(`[WeaponInfo] 武器激活状态已设置为: ${active}`);
  149. }
  150. /**
  151. * 获取武器激活状态
  152. * @returns 是否激活
  153. */
  154. public isActive(): boolean {
  155. return this._isActive;
  156. }
  157. /**
  158. * 检查武器是否可以开火(基于冷却时间)
  159. * @returns 是否可以开火
  160. */
  161. public canFire(): boolean {
  162. if (!this._isActive || !this._weaponConfig) {
  163. return false;
  164. }
  165. const currentTime = Date.now() / 1000; // 转换为秒
  166. const fireRate = this._weaponConfig.stats.fireRate || 1.0;
  167. const cooldown = 1.0 / fireRate; // 计算冷却时间
  168. return (currentTime - this._lastFireTime) >= cooldown;
  169. }
  170. /**
  171. * 记录开火时间
  172. */
  173. public recordFireTime(): void {
  174. this._lastFireTime = Date.now() / 1000;
  175. }
  176. /**
  177. * 获取武器伤害值(考虑等级加成)
  178. * @returns 武器伤害值
  179. */
  180. public getDamage(): number {
  181. if (!this._weaponConfig) {
  182. return 0;
  183. }
  184. const baseDamage = this._weaponConfig.stats.damage || 0;
  185. // 根据等级计算伤害加成(简单的线性增长)
  186. // 每级增加10%的基础伤害
  187. const levelMultiplier = 1 + (this._weaponLevel - 1) * 0.1;
  188. const finalDamage = Math.floor(baseDamage * levelMultiplier);
  189. return finalDamage;
  190. }
  191. /**
  192. * 获取武器射程
  193. * @returns 武器射程
  194. */
  195. public getRange(): number {
  196. return this._weaponConfig?.stats.range || 0;
  197. }
  198. /**
  199. * 获取武器射速
  200. * @returns 武器射速
  201. */
  202. public getFireRate(): number {
  203. return this._weaponConfig?.stats.fireRate || 1.0;
  204. }
  205. /**
  206. * 获取子弹速度
  207. * @returns 子弹速度
  208. */
  209. public getBulletSpeed(): number {
  210. return this._weaponConfig?.stats.bulletSpeed || 100;
  211. }
  212. /**
  213. * 获取武器精度
  214. * @returns 武器精度
  215. */
  216. public getAccuracy(): number {
  217. return this._weaponConfig?.stats.accuracy || 1.0;
  218. }
  219. /**
  220. * 添加伤害统计
  221. * @param damage 造成的伤害
  222. */
  223. public addDamageDealt(damage: number): void {
  224. this._totalDamageDealt += damage;
  225. // 更新编辑器面板可见属性
  226. this.totalDamage = this._totalDamageDealt;
  227. console.log(`[WeaponInfo] 累计伤害增加 ${damage},总伤害: ${this._totalDamageDealt}`);
  228. }
  229. /**
  230. * 获取总伤害统计
  231. * @returns 总伤害
  232. */
  233. public getTotalDamageDealt(): number {
  234. return this._totalDamageDealt;
  235. }
  236. /**
  237. * 增加击杀数
  238. */
  239. public addKill(): void {
  240. this._killCount++;
  241. // 更新编辑器面板可见属性
  242. this.killCount = this._killCount;
  243. console.log(`[WeaponInfo] 击杀数增加,当前击杀数: ${this._killCount}`);
  244. }
  245. /**
  246. * 获取击杀数
  247. * @returns 击杀数
  248. */
  249. public getKillCount(): number {
  250. return this._killCount;
  251. }
  252. /**
  253. * 重置统计数据
  254. */
  255. public resetStats(): void {
  256. this._totalDamageDealt = 0;
  257. this._killCount = 0;
  258. this._lastFireTime = 0;
  259. // 更新编辑器面板可见属性
  260. this.totalDamage = this._totalDamageDealt;
  261. this.killCount = this._killCount;
  262. console.log(`[WeaponInfo] 武器统计数据已重置`);
  263. }
  264. /**
  265. * 获取武器升级所需费用
  266. * @param targetLevel 目标等级
  267. * @returns 升级费用,如果无法升级返回-1
  268. */
  269. public getUpgradeCost(targetLevel?: number): number {
  270. if (!this._weaponConfig || !this._weaponConfig.upgradeConfig) {
  271. return -1;
  272. }
  273. const level = targetLevel || (this._weaponLevel + 1);
  274. const levelConfig = this._weaponConfig.upgradeConfig.levels?.[level.toString()];
  275. return levelConfig?.cost || -1;
  276. }
  277. /**
  278. * 检查是否可以升级
  279. * @returns 是否可以升级
  280. */
  281. public canUpgrade(): boolean {
  282. if (!this._weaponConfig || !this._weaponConfig.upgradeConfig) {
  283. return false;
  284. }
  285. const maxLevel = this._weaponConfig.upgradeConfig.maxLevel || 10;
  286. return this._weaponLevel < maxLevel;
  287. }
  288. /**
  289. * 升级武器
  290. * @returns 是否升级成功
  291. */
  292. public upgrade(): boolean {
  293. if (!this.canUpgrade()) {
  294. return false;
  295. }
  296. this._weaponLevel++;
  297. // 更新编辑器面板可见属性
  298. this.weaponLevel = this._weaponLevel;
  299. this.weaponDamage = this.getDamage();
  300. console.log(`[WeaponInfo] 武器 ${this.getWeaponName()} 升级到等级 ${this._weaponLevel}`);
  301. return true;
  302. }
  303. /**
  304. * 更新所有编辑器面板可见属性
  305. * 用于确保编辑器中显示的信息与内部状态同步
  306. */
  307. private updateEditorProperties(): void {
  308. if (this._weaponConfig) {
  309. this.weaponId = this._weaponConfig.id;
  310. this.weaponName = this._weaponConfig.name;
  311. this.fireRate = this._weaponConfig.stats.fireRate;
  312. this.range = this._weaponConfig.stats.range;
  313. }
  314. this.weaponLevel = this._weaponLevel;
  315. this.weaponDamage = this.getDamage();
  316. this.activeStatus = this._isActive;
  317. this.totalDamage = this._totalDamageDealt;
  318. this.killCount = this._killCount;
  319. }
  320. /**
  321. * 获取武器信息摘要
  322. * @returns 武器信息字符串
  323. */
  324. public getWeaponSummary(): string {
  325. if (!this._weaponConfig) {
  326. return '无武器配置';
  327. }
  328. return `${this._weaponConfig.name} (Lv.${this._weaponLevel}) - 伤害:${this.getDamage()} 射速:${this.getFireRate()} 射程:${this.getRange()}`;
  329. }
  330. /**
  331. * 组件销毁时清理
  332. */
  333. onDestroy(): void {
  334. this._weaponConfig = null;
  335. }
  336. }