|
|
@@ -0,0 +1,410 @@
|
|
|
+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;
|
|
|
+
|
|
|
+ // 武器配置数据
|
|
|
+ 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;
|
|
|
+
|
|
|
+ // 如果武器配置中有弹药限制,设置初始弹药数
|
|
|
+ if (config.bulletConfig && config.bulletConfig.count) {
|
|
|
+ // 这里可以根据具体需求设置弹药数,目前设为无限
|
|
|
+ this._currentAmmo = -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(`[WeaponInfo] 武器配置已设置: ${config.name} (${config.id})`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取武器配置
|
|
|
+ * @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();
|
|
|
+
|
|
|
+ console.log(`[WeaponInfo] 武器等级已设置为: ${level}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取武器等级
|
|
|
+ * @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;
|
|
|
+
|
|
|
+ console.log(`[WeaponInfo] 武器激活状态已设置为: ${active}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取武器激活状态
|
|
|
+ * @returns 是否激活
|
|
|
+ */
|
|
|
+ public isActive(): boolean {
|
|
|
+ return this._isActive;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查武器是否可以开火(基于冷却时间)
|
|
|
+ * @returns 是否可以开火
|
|
|
+ */
|
|
|
+ public canFire(): boolean {
|
|
|
+ if (!this._isActive || !this._weaponConfig) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ const currentTime = Date.now() / 1000; // 转换为秒
|
|
|
+ const fireRate = this._weaponConfig.stats.fireRate || 1.0;
|
|
|
+ const cooldown = 1.0 / fireRate; // 计算冷却时间
|
|
|
+
|
|
|
+ return (currentTime - this._lastFireTime) >= cooldown;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 记录开火时间
|
|
|
+ */
|
|
|
+ public recordFireTime(): void {
|
|
|
+ this._lastFireTime = Date.now() / 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取武器伤害值(考虑等级加成)
|
|
|
+ * @returns 武器伤害值
|
|
|
+ */
|
|
|
+ public getDamage(): number {
|
|
|
+ if (!this._weaponConfig) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ const baseDamage = this._weaponConfig.stats.damage || 0;
|
|
|
+
|
|
|
+ // 根据等级计算伤害加成(简单的线性增长)
|
|
|
+ // 每级增加10%的基础伤害
|
|
|
+ const levelMultiplier = 1 + (this._weaponLevel - 1) * 0.1;
|
|
|
+ const finalDamage = Math.floor(baseDamage * levelMultiplier);
|
|
|
+
|
|
|
+ return finalDamage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取武器射程
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加伤害统计
|
|
|
+ * @param damage 造成的伤害
|
|
|
+ */
|
|
|
+ public addDamageDealt(damage: number): void {
|
|
|
+ this._totalDamageDealt += damage;
|
|
|
+
|
|
|
+ // 更新编辑器面板可见属性
|
|
|
+ this.totalDamage = this._totalDamageDealt;
|
|
|
+
|
|
|
+ console.log(`[WeaponInfo] 累计伤害增加 ${damage},总伤害: ${this._totalDamageDealt}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取总伤害统计
|
|
|
+ * @returns 总伤害
|
|
|
+ */
|
|
|
+ public getTotalDamageDealt(): number {
|
|
|
+ return this._totalDamageDealt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加击杀数
|
|
|
+ */
|
|
|
+ public addKill(): void {
|
|
|
+ this._killCount++;
|
|
|
+
|
|
|
+ // 更新编辑器面板可见属性
|
|
|
+ this.killCount = this._killCount;
|
|
|
+
|
|
|
+ console.log(`[WeaponInfo] 击杀数增加,当前击杀数: ${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;
|
|
|
+
|
|
|
+ console.log(`[WeaponInfo] 武器统计数据已重置`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取武器升级所需费用
|
|
|
+ * @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();
|
|
|
+
|
|
|
+ console.log(`[WeaponInfo] 武器 ${this.getWeaponName()} 升级到等级 ${this._weaponLevel}`);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取武器信息摘要
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+}
|