WeaponInfo.ts 13 KB

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