(client) chore:UI
This commit is contained in:
@ -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为3,currentSelected=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;
|
||||
}
|
||||
}
|
||||
|
@ -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];
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user