2025-10-03 00:31:34 +08:00
|
|
|
|
using Configs;
|
2025-07-14 11:42:02 +08:00
|
|
|
|
using Data;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using Item;
|
2025-10-03 00:31:34 +08:00
|
|
|
|
using Managers;
|
2025-07-14 11:42:02 +08:00
|
|
|
|
using UnityEngine;
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
// 添加 System 命名空间以使用 Action
|
2025-07-14 11:42:02 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Entity
|
|
|
|
|
|
{
|
2025-09-03 19:59:22 +08:00
|
|
|
|
public class Character : LivingEntity
|
2025-07-14 11:42:02 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
private int _currentWeaponIndex; // 私有字段用于存储实际值
|
|
|
|
|
|
private Attributes _defAttributes;
|
|
|
|
|
|
|
|
|
|
|
|
public override Attributes DefAttributes
|
2025-09-19 08:26:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
var weaponAttributes = GetCurrentWeapon()?.Attributes;
|
|
|
|
|
|
_defAttributes ??= weaponAttributes != null
|
|
|
|
|
|
? new Attributes(weaponAttributes)
|
|
|
|
|
|
: new Attributes(base.DefAttributes);
|
|
|
|
|
|
_defAttributes.health = base.DefAttributes.health;
|
|
|
|
|
|
_defAttributes.defense = base.DefAttributes.defense;
|
|
|
|
|
|
return _defAttributes;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前选中的背包槽位索引。
|
|
|
|
|
|
/// 当此值被设置时,如果与旧值不同,将触发 OnCurrentSelectedChanged 事件。
|
|
|
|
|
|
/// </summary>
|
2025-10-10 14:08:23 +08:00
|
|
|
|
public int CurrentWeaponIndex
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
get => _currentWeaponIndex;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
var maxIndex = WeaponInventory != null && WeaponInventory.Capacity > 0
|
|
|
|
|
|
? WeaponInventory.Capacity - 1
|
|
|
|
|
|
: 0;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
var clampedValue = Mathf.Clamp(value, 0, maxIndex);
|
2025-10-10 14:08:23 +08:00
|
|
|
|
_currentWeaponIndex = clampedValue;
|
2025-09-03 19:59:22 +08:00
|
|
|
|
InitWeaponAnimator();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
public Inventory WeaponInventory { get; private set; }
|
|
|
|
|
|
public Inventory ItemInventory { get; private set; }
|
|
|
|
|
|
public InventorySlot Coin { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public CharacterDef characterDef => entityDef as CharacterDef;
|
2025-08-28 16:20:24 +08:00
|
|
|
|
|
2025-10-03 00:31:34 +08:00
|
|
|
|
public override void Init(EntityDef entityDefine)
|
2025-07-14 11:42:02 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
WeaponInventory = new Inventory(this, 3);
|
|
|
|
|
|
ItemInventory = new Inventory(this, 99999);
|
2025-10-03 00:31:34 +08:00
|
|
|
|
var coinItem = ItemResourceManager.Instance.GetItem(ConfigManager.Instance.GetValue<string>("CoinItem"));
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (!KeyValueArchiveManager.Instance.HasKey("coinCount"))
|
2025-10-03 00:31:34 +08:00
|
|
|
|
KeyValueArchiveManager.Instance.Set("coinCount", 0);
|
|
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
Coin = new InventorySlot(coinItem, 0);
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
|
WeaponInventory.OnInventoryChanged += InventoryChange;
|
|
|
|
|
|
CurrentWeaponIndex = 0;
|
2025-10-03 00:31:34 +08:00
|
|
|
|
base.Init(entityDefine);
|
2025-07-14 11:42:02 +08:00
|
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
2025-07-14 11:42:02 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 尝试将指定物品添加到角色的背包中。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itemResource">要尝试添加的物品资源。</param>
|
|
|
|
|
|
/// <param name="quantity">要尝试添加的数量。</param>
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
/// 未成功添加到背包的物品数量。
|
|
|
|
|
|
/// 如果返回0,表示所有物品都成功添加;
|
|
|
|
|
|
/// 如果返回quantity,表示未能添加任何物品;
|
|
|
|
|
|
/// 如果返回一个介于0和quantity之间的值,表示部分物品被添加。
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
public int TryPickupItem(ItemResource itemResource, int quantity)
|
2025-07-14 11:42:02 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
if (Coin.Item == itemResource)
|
|
|
|
|
|
return quantity - Coin.AddQuantity(quantity);
|
|
|
|
|
|
if (itemResource is not WeaponResource weaponResource)
|
|
|
|
|
|
return ItemInventory.AddItem(itemResource, quantity);
|
|
|
|
|
|
var remainingQuantity = WeaponInventory.AddItem(weaponResource, quantity);
|
|
|
|
|
|
return remainingQuantity > 0 ? ItemInventory.AddItem(itemResource, quantity) : remainingQuantity;
|
2025-07-14 11:42:02 +08:00
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
public override WeaponResource GetCurrentWeapon()
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
|
var currentSelectItem = WeaponInventory.GetSlot(CurrentWeaponIndex);
|
|
|
|
|
|
return currentSelectItem?.Item as WeaponResource ??
|
|
|
|
|
|
ItemResourceManager.Instance.GetItem(characterDef?.defaultWeapon) as WeaponResource;
|
2025-09-19 08:26:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InventoryChange()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitWeaponAnimator();
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-07-14 11:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|