114 lines
3.7 KiB
C#
114 lines
3.7 KiB
C#
using UnityEngine;
|
|
using Ashwild.Player;
|
|
|
|
namespace Ashwild.Inventory
|
|
{
|
|
public class HotbarController : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private Transform toolHolder;
|
|
[SerializeField] private PlayerToolHolder toolHolderAnim;
|
|
[SerializeField] private Transform raycastOrigin;
|
|
|
|
private PlayerInventory inventory;
|
|
private GameObject currentHeldObject;
|
|
private ItemData currentHeldItem;
|
|
private bool isSwitching;
|
|
private HeldItemContext heldContext;
|
|
|
|
private void Start()
|
|
{
|
|
inventory = PlayerInventory.Instance;
|
|
inventory.onSelectedSlotChanged.AddListener(OnSelectionChanged);
|
|
inventory.onSlotChanged.AddListener(OnSlotChanged);
|
|
heldContext = new HeldItemContext { RaycastOrigin = raycastOrigin };
|
|
UpdateHeldItem(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (inventory != null)
|
|
{
|
|
inventory.onSelectedSlotChanged.RemoveListener(OnSelectionChanged);
|
|
inventory.onSlotChanged.RemoveListener(OnSlotChanged);
|
|
}
|
|
}
|
|
|
|
private void OnSelectionChanged(int index)
|
|
{
|
|
UpdateHeldItem(true);
|
|
}
|
|
|
|
private void OnSlotChanged(int index)
|
|
{
|
|
if (index == inventory.SelectedHotbarIndex)
|
|
UpdateHeldItem(true);
|
|
}
|
|
|
|
private void UpdateHeldItem(bool animate)
|
|
{
|
|
InventorySlot selected = inventory.GetSelectedSlot();
|
|
ItemData newItem = selected.IsEmpty ? null : selected.ItemData;
|
|
|
|
if (newItem == currentHeldItem)
|
|
return;
|
|
|
|
if (isSwitching)
|
|
{
|
|
if (currentHeldObject != null)
|
|
{
|
|
Destroy(currentHeldObject);
|
|
currentHeldObject = null;
|
|
}
|
|
isSwitching = false;
|
|
}
|
|
|
|
if (animate && currentHeldObject != null && toolHolderAnim != null)
|
|
{
|
|
isSwitching = true;
|
|
toolHolderAnim.PlayUnequipAnimation(() =>
|
|
{
|
|
if (currentHeldObject != null)
|
|
{
|
|
Destroy(currentHeldObject);
|
|
currentHeldObject = null;
|
|
}
|
|
currentHeldItem = newItem;
|
|
SpawnItem(newItem);
|
|
isSwitching = false;
|
|
|
|
if (currentHeldObject != null)
|
|
toolHolderAnim.PlayEquipAnimation();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
if (currentHeldObject != null)
|
|
{
|
|
Destroy(currentHeldObject);
|
|
currentHeldObject = null;
|
|
}
|
|
currentHeldItem = newItem;
|
|
SpawnItem(newItem);
|
|
|
|
if (animate && toolHolderAnim != null && currentHeldObject != null)
|
|
toolHolderAnim.PlayEquipAnimation();
|
|
}
|
|
}
|
|
|
|
private void SpawnItem(ItemData item)
|
|
{
|
|
if (item == null || item.HandPrefab == null || toolHolder == null)
|
|
return;
|
|
|
|
currentHeldObject = Instantiate(item.HandPrefab, toolHolder);
|
|
|
|
// Link the freshly spawned held item to the player so its behaviour
|
|
// (tool, consumable, ...) can interact — only the equipped item is alive.
|
|
IHeldItemBehaviour behaviour = currentHeldObject.GetComponentInChildren<IHeldItemBehaviour>(true);
|
|
if (behaviour != null)
|
|
behaviour.Setup(heldContext, item);
|
|
}
|
|
}
|
|
}
|