(client) chore:UI

This commit is contained in:
m0_75251201
2025-08-27 14:45:12 +08:00
parent 0c99e2beee
commit 63efa89ac1
10 changed files with 126 additions and 60 deletions

View File

@ -17,29 +17,19 @@ namespace Entity
/// 当前选中的背包槽位索引。
/// 当此值被设置时,如果与旧值不同,将触发 OnCurrentSelectedChanged 事件。
/// </summary>
public int currentSelected
public int CurrentSelected
{
get => _currentSelected;
set
{
var maxIndex = Inventory != null && Inventory.Capacity > 0 ? Inventory.Capacity - 1 : 0;
var clampedValue = Mathf.Clamp(value, 0, maxIndex);
if (_currentSelected != clampedValue)
{
_currentSelected = clampedValue;
OnCurrentSelectedChanged?.Invoke(_currentSelected); // 触发事件
}
_currentSelected = clampedValue;
}
}
public Inventory Inventory { get; private set; }
/// <summary>
/// 当当前选中的槽位索引改变时触发的事件。
/// 参数:新的选中索引。
/// </summary>
public event Action<int> OnCurrentSelectedChanged;
public override void Init(EntityDef entityDef)
{
@ -51,7 +41,7 @@ namespace Entity
// 使用属性来设置,确保触发事件和范围检查。
// 如果Inventory.Capacity为0则currentSelected会被钳制到0。
// 如果Inventory.Capacity为3currentSelected=0是有效值。
currentSelected = 0;
CurrentSelected = 0;
}
/// <summary>
@ -103,7 +93,7 @@ namespace Entity
public override WeaponResource GetCurrentWeapon()
{
var currentSelectItem = Inventory.GetSlot(currentSelected);
var currentSelectItem = Inventory.GetSlot(CurrentSelected);
return (WeaponResource)currentSelectItem.Item;
}
}

View File

@ -158,7 +158,8 @@ namespace Entity
}
public InventorySlot GetSlot(int i)
{
i = Mathf.Clamp(i, 0, Capacity - 1);
if (i < 0 || i >= OccupiedSlotsCount)
return null;
return _slots[i];
}

View File

@ -61,8 +61,9 @@ namespace Entity
{
var entity = other.GetComponent<Character>();
if (entity == null) return;
if (entity.TryPickupItem(itemResource, 1) == 0)
if (entity.TryPickupItem(itemResource, 1) <= 0)
{
Debug.Log("Kill");
Kill();
}
}