/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Character
{
using Opsive.Shared.Game;
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Input;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Utility;
using UnityEngine;
///
/// The ItemHandler manages the movement for each equipped item.
///
public class ItemHandler : MonoBehaviour
{
private InventoryBase m_Inventory;
private PlayerInput m_PlayerInput;
///
/// Initialize the default values.
///
private void Start()
{
m_PlayerInput = gameObject.GetCachedComponent();
m_Inventory = gameObject.GetCachedComponent();
EventHandler.RegisterEvent(gameObject, "OnInventoryEquipItem", OnEquipItem);
}
///
/// Moves the item in each slot.
///
private void FixedUpdate()
{
var lookVector = m_PlayerInput.GetLookVector(false);
for (int i = 0; i < m_Inventory.SlotCount; ++i) {
var item = m_Inventory.GetActiveItem(i);
if (item != null && item.IsActive() && item.DominantItem) {
item.Move(lookVector.x, lookVector.y);
}
}
// Each object should only be updated once. Clear the frame after execution to allow the objects to be updated again.
UnityEngineUtility.ClearUpdatedObjects();
}
///
/// An item has been equipped.
///
/// The equipped item.
/// The slot that the item now occupies.
private void OnEquipItem(Items.Item item, int slotID)
{
if (item.IsActive() && item.DominantItem) {
UnityEngineUtility.ClearUpdatedObjects();
item.Move(0, 0);
}
}
///
/// The character has been destroyed.
///
private void OnDestroy()
{
EventHandler.UnregisterEvent(gameObject, "OnInventoryEquipItem", OnEquipItem);
}
}
}