2025-07-14 11:42:02 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Data;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using Item;
|
|
|
|
|
using Managers;
|
2025-07-14 11:42:02 +08:00
|
|
|
|
using UnityEngine;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using Utils;
|
|
|
|
|
// 添加 System 命名空间以使用 Action
|
2025-07-14 11:42:02 +08:00
|
|
|
|
|
|
|
|
|
namespace Entity
|
|
|
|
|
{
|
2025-07-21 13:58:58 +08:00
|
|
|
|
public class Character : Entity
|
2025-07-14 11:42:02 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
private int _currentSelected; // 私有字段用于存储实际值
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前选中的背包槽位索引。
|
|
|
|
|
/// 当此值被设置时,如果与旧值不同,将触发 OnCurrentSelectedChanged 事件。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int CurrentSelected
|
|
|
|
|
{
|
|
|
|
|
get => _currentSelected;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var maxIndex = Inventory != null && Inventory.Capacity > 0 ? Inventory.Capacity - 1 : 0;
|
|
|
|
|
var clampedValue = Mathf.Clamp(value, 0, maxIndex);
|
|
|
|
|
_currentSelected = clampedValue;
|
2025-08-27 22:39:34 +08:00
|
|
|
|
InitWeaponAnimator();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Inventory Inventory { get; private set; }
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
public override void Init(EntityDef entityDef)
|
2025-07-14 11:42:02 +08:00
|
|
|
|
{
|
2025-08-27 19:56:49 +08:00
|
|
|
|
base.Init(entityDef);
|
|
|
|
|
|
|
|
|
|
Inventory = new Inventory(this, 3);
|
|
|
|
|
|
|
|
|
|
// 初始化 currentSelected。
|
|
|
|
|
// 使用属性来设置,确保触发事件和范围检查。
|
|
|
|
|
// 如果Inventory.Capacity为0,则currentSelected会被钳制到0。
|
|
|
|
|
// 如果Inventory.Capacity为3,currentSelected=0是有效值。
|
|
|
|
|
CurrentSelected = 0;
|
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-08-27 19:56:49 +08:00
|
|
|
|
if (Inventory == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"Character '{name}' inventory is not initialized. Cannot pickup item.");
|
|
|
|
|
return quantity; // 如果背包未初始化,则视为未能添加任何物品
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var remainingQuantity = Inventory.AddItem(itemResource, quantity);
|
|
|
|
|
|
|
|
|
|
return 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-08-27 19:56:49 +08:00
|
|
|
|
var currentSelectItem = Inventory.GetSlot(CurrentSelected);
|
|
|
|
|
return (WeaponResource)currentSelectItem?.Item;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
2025-07-14 11:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|