| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- import { _decorator, Component, Node } from 'cc';
- import { WeaponConfig } from '../../Core/ConfigManager';
- const { ccclass, property } = _decorator;
- /**
- * 武器信息组件
- * 用于存储和管理挂载到武器节点上的武器配置信息
- */
- @ccclass('WeaponInfo')
- export class WeaponInfo extends Component {
-
- // 编辑器面板可见属性
- @property({ displayName: "武器ID", readonly: true })
- public weaponId: string = "";
-
- @property({ displayName: "武器名称", readonly: true })
- public weaponName: string = "";
-
- @property({ displayName: "武器等级", readonly: true })
- public weaponLevel: number = 1;
-
- @property({ displayName: "武器伤害", readonly: true })
- public weaponDamage: number = 0;
-
- @property({ displayName: "射速", readonly: true })
- public fireRate: number = 0;
-
- @property({ displayName: "射程", readonly: true })
- public range: number = 0;
-
- @property({ displayName: "是否激活", readonly: true })
- public activeStatus: boolean = false;
-
- @property({ displayName: "总伤害", readonly: true })
- public totalDamage: number = 0;
-
- @property({ displayName: "击杀数", readonly: true })
- public killCount: number = 0;
-
- @property({ displayName: "剩余冷却时间(秒)", readonly: true })
- public remainingCooldown: number = 0;
-
- // 武器配置数据
- private _weaponConfig: WeaponConfig | null = null;
-
- // 武器等级(用于升级系统)
- private _weaponLevel: number = 1;
-
- // 武器是否已激活(用于战斗状态管理)
- private _isActive: boolean = false;
-
- // 武器最后开火时间(用于冷却计算)
- private _lastFireTime: number = 0;
-
- // 武器当前弹药数(如果适用)
- private _currentAmmo: number = -1; // -1表示无限弹药
-
- // 武器累计伤害统计
- private _totalDamageDealt: number = 0;
-
- // 武器击杀数统计
- private _killCount: number = 0;
-
- /**
- * 设置武器配置
- * @param config 武器配置数据
- */
- public setWeaponConfig(config: WeaponConfig): void {
- this._weaponConfig = config;
- this._weaponLevel = 1;
- this._isActive = true;
- this._lastFireTime = 0;
- this._totalDamageDealt = 0;
- this._killCount = 0;
-
- // 更新编辑器面板可见属性
- this.weaponId = config.id;
- this.weaponName = config.name;
- this.weaponLevel = this._weaponLevel;
- this.weaponDamage = this.getDamage();
- this.fireRate = config.stats.fireRate;
- this.range = config.stats.range;
- this.activeStatus = this._isActive;
- this.totalDamage = this._totalDamageDealt;
- this.killCount = this._killCount;
- this.remainingCooldown = 0;
-
- // 如果武器配置中有弹药限制,设置初始弹药数
- if (config.bulletConfig && config.bulletConfig.count) {
- // 这里可以根据具体需求设置弹药数,目前设为无限
- this._currentAmmo = -1;
- }
-
- // 武器配置已设置
- }
-
- /**
- * 获取武器配置
- * @returns 武器配置数据
- */
- public getWeaponConfig(): WeaponConfig | null {
- return this._weaponConfig;
- }
-
- /**
- * 获取武器ID
- * @returns 武器ID
- */
- public getWeaponId(): string | null {
- return this._weaponConfig ? this._weaponConfig.id : null;
- }
-
- /**
- * 获取武器名称
- * @returns 武器名称
- */
- public getWeaponName(): string | null {
- return this._weaponConfig ? this._weaponConfig.name : null;
- }
-
- /**
- * 获取武器类型
- * @returns 武器类型
- */
- public getWeaponType(): string | null {
- return this._weaponConfig ? this._weaponConfig.type : null;
- }
-
- /**
- * 获取武器稀有度
- * @returns 武器稀有度
- */
- public getWeaponRarity(): string | null {
- return this._weaponConfig ? (this._weaponConfig.rarity || 'common') : null;
- }
-
- /**
- * 设置武器等级
- * @param level 武器等级
- */
- public setWeaponLevel(level: number): void {
- if (level < 1) {
- console.warn(`[WeaponInfo] 武器等级不能小于1,当前设置: ${level}`);
- return;
- }
-
- const maxLevel = this._weaponConfig?.upgradeConfig?.maxLevel || 10;
- if (level > maxLevel) {
- console.warn(`[WeaponInfo] 武器等级不能超过最大等级 ${maxLevel},当前设置: ${level}`);
- return;
- }
-
- this._weaponLevel = level;
-
- // 更新编辑器面板可见属性
- this.weaponLevel = this._weaponLevel;
- this.weaponDamage = this.getDamage();
-
- // 武器等级已设置
- }
-
- /**
- * 获取武器等级
- * @returns 武器等级
- */
- public getWeaponLevel(): number {
- return this._weaponLevel;
- }
-
- /**
- * 获取武器最大等级
- * @returns 武器最大等级
- */
- public getMaxWeaponLevel(): number {
- return this._weaponConfig?.upgradeConfig?.maxLevel || 10;
- }
-
- /**
- * 设置武器激活状态
- * @param active 是否激活
- */
- public setActive(active: boolean): void {
- this._isActive = active;
-
- // 更新编辑器面板可见属性
- this.activeStatus = this._isActive;
-
- // 武器激活状态已设置
- }
-
- /**
- * 获取武器激活状态
- * @returns 是否激活
- */
- public isActive(): boolean {
- return this._isActive;
- }
-
- /**
- * 检查武器是否可以开火(基于冷却时间)
- * @returns 是否可以开火
- */
- public canFire(): boolean {
- if (!this._isActive || !this._weaponConfig) {
- this.remainingCooldown = 0;
- return false;
- }
-
- const currentTime = Date.now() / 1000; // 转换为秒
- const fireRate = this._weaponConfig.stats.fireRate || 1.0;
- const cooldown = 1.0 / fireRate; // 计算冷却时间
- const timeSinceLastFire = currentTime - this._lastFireTime;
-
- // 更新剩余冷却时间
- this.remainingCooldown = Math.max(0, cooldown - timeSinceLastFire);
-
- return timeSinceLastFire >= cooldown;
- }
-
- /**
- * 记录开火时间
- */
- public recordFireTime(): void {
- this._lastFireTime = Date.now() / 1000;
-
- // 开火后立即设置冷却时间
- if (this._weaponConfig) {
- const fireRate = this._weaponConfig.stats.fireRate || 1.0;
- this.remainingCooldown = 1.0 / fireRate;
- }
- }
-
- /**
- * 获取武器伤害值(考虑等级加成)
- * @returns 武器伤害值
- */
- public getDamage(): number {
- if (!this._weaponConfig) {
- return 0;
- }
-
- if (this._weaponLevel === 0) {
- return 0; // 未解锁武器伤害为0
- }
-
- // 优先从武器配置的upgradeConfig中获取伤害值
- if (this._weaponConfig.upgradeConfig && this._weaponConfig.upgradeConfig.levels) {
- const levelConfig = this._weaponConfig.upgradeConfig.levels[this._weaponLevel.toString()];
- if (levelConfig && typeof levelConfig.damage === 'number') {
- return levelConfig.damage;
- }
- }
-
- // 如果upgradeConfig中没有伤害值,使用基础伤害 + 等级加成作为后备
- const baseDamage = this._weaponConfig.stats.damage || 0;
- return baseDamage + (this._weaponLevel - 1);
- }
-
- /**
- * 获取武器射程
- * @returns 武器射程
- */
- public getRange(): number {
- return this._weaponConfig?.stats.range || 0;
- }
-
- /**
- * 获取武器射速
- * @returns 武器射速
- */
- public getFireRate(): number {
- return this._weaponConfig?.stats.fireRate || 1.0;
- }
-
- /**
- * 获取子弹速度
- * @returns 子弹速度
- */
- public getBulletSpeed(): number {
- return this._weaponConfig?.stats.bulletSpeed || 100;
- }
-
- /**
- * 获取武器精度
- * @returns 武器精度
- */
- public getAccuracy(): number {
- return this._weaponConfig?.stats.accuracy || 1.0;
- }
-
- /**
- * 获取当前剩余冷却时间
- * @returns 剩余冷却时间(秒)
- */
- public getRemainingCooldown(): number {
- if (!this._isActive || !this._weaponConfig) {
- return 0;
- }
-
- const currentTime = Date.now() / 1000;
- const fireRate = this._weaponConfig.stats.fireRate || 1.0;
- const cooldown = 1.0 / fireRate;
- const timeSinceLastFire = currentTime - this._lastFireTime;
-
- const remaining = Math.max(0, cooldown - timeSinceLastFire);
- this.remainingCooldown = remaining; // 同时更新编辑器属性
-
- return remaining;
- }
-
- /**
- * 添加伤害统计
- * @param damage 造成的伤害
- */
- public addDamageDealt(damage: number): void {
- this._totalDamageDealt += damage;
-
- // 更新编辑器面板可见属性
- this.totalDamage = this._totalDamageDealt;
-
- // 累计伤害已更新
- }
-
- /**
- * 获取总伤害统计
- * @returns 总伤害
- */
- public getTotalDamageDealt(): number {
- return this._totalDamageDealt;
- }
-
- /**
- * 增加击杀数
- */
- public addKill(): void {
- this._killCount++;
-
- // 更新编辑器面板可见属性
- this.killCount = this._killCount;
-
- // 击杀数已更新
- }
-
- /**
- * 获取击杀数
- * @returns 击杀数
- */
- public getKillCount(): number {
- return this._killCount;
- }
-
- /**
- * 重置统计数据
- */
- public resetStats(): void {
- this._totalDamageDealt = 0;
- this._killCount = 0;
- this._lastFireTime = 0;
-
- // 更新编辑器面板可见属性
- this.totalDamage = this._totalDamageDealt;
- this.killCount = this._killCount;
- this.remainingCooldown = 0;
-
- // 武器统计数据已重置
- }
-
- /**
- * 获取武器升级所需费用
- * @param targetLevel 目标等级
- * @returns 升级费用,如果无法升级返回-1
- */
- public getUpgradeCost(targetLevel?: number): number {
- if (!this._weaponConfig || !this._weaponConfig.upgradeConfig) {
- return -1;
- }
-
- const level = targetLevel || (this._weaponLevel + 1);
- const levelConfig = this._weaponConfig.upgradeConfig.levels?.[level.toString()];
-
- return levelConfig?.cost || -1;
- }
-
- /**
- * 检查是否可以升级
- * @returns 是否可以升级
- */
- public canUpgrade(): boolean {
- if (!this._weaponConfig || !this._weaponConfig.upgradeConfig) {
- return false;
- }
-
- const maxLevel = this._weaponConfig.upgradeConfig.maxLevel || 10;
- return this._weaponLevel < maxLevel;
- }
-
- /**
- * 升级武器
- * @returns 是否升级成功
- */
- public upgrade(): boolean {
- if (!this.canUpgrade()) {
- return false;
- }
-
- this._weaponLevel++;
-
- // 更新编辑器面板可见属性
- this.weaponLevel = this._weaponLevel;
- this.weaponDamage = this.getDamage();
-
- // 武器升级完成
- return true;
- }
-
- /**
- * 更新所有编辑器面板可见属性
- * 用于确保编辑器中显示的信息与内部状态同步
- */
- private updateEditorProperties(): void {
- if (this._weaponConfig) {
- this.weaponId = this._weaponConfig.id;
- this.weaponName = this._weaponConfig.name;
- this.fireRate = this._weaponConfig.stats.fireRate;
- this.range = this._weaponConfig.stats.range;
- }
-
- this.weaponLevel = this._weaponLevel;
- this.weaponDamage = this.getDamage();
- this.activeStatus = this._isActive;
- this.totalDamage = this._totalDamageDealt;
- this.killCount = this._killCount;
-
- // 更新剩余冷却时间
- if (this._weaponConfig && this._isActive) {
- const currentTime = Date.now() / 1000;
- const fireRate = this._weaponConfig.stats.fireRate || 1.0;
- const cooldown = 1.0 / fireRate;
- const timeSinceLastFire = currentTime - this._lastFireTime;
- this.remainingCooldown = Math.max(0, cooldown - timeSinceLastFire);
- } else {
- this.remainingCooldown = 0;
- }
- }
-
- /**
- * 获取武器信息摘要
- * @returns 武器信息字符串
- */
- public getWeaponSummary(): string {
- if (!this._weaponConfig) {
- return '无武器配置';
- }
-
- return `${this._weaponConfig.name} (Lv.${this._weaponLevel}) - 伤害:${this.getDamage()} 射速:${this.getFireRate()} 射程:${this.getRange()}`;
- }
-
- /**
- * 组件销毁时清理
- */
- onDestroy(): void {
- this._weaponConfig = null;
- }
- }
|