Files

103 lines
3.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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