/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Demo
{
using Opsive.Shared.Events;
using Opsive.Shared.Inventory;
using Opsive.UltimateCharacterController.Items;
using UnityEngine;
///
/// Enables or disables the cave light based on the flashlight equipped status.
///
public class FlashlightCave : MonoBehaviour
{
[Tooltip("The ItemDefinition used by the flashlight.")]
[SerializeField] protected ItemDefinitionBase m_FlashlightItemDefinition;
[Tooltip("The light within the cave.")]
[SerializeField] protected Light m_Light;
private GameObject m_Character;
///
/// Initialize the default values.
///
private void Awake()
{
m_Character = FindObjectOfType().Character;
EventHandler.RegisterEvent- (m_Character, "OnInventoryEquipItem", OnEquipItem);
EventHandler.RegisterEvent
- (m_Character, "OnInventoryUnequipItem", OnUnequipItem);
}
///
/// The character has equipped or unequipped the flashlight.
///
/// True if the flashlight was equipped.
private void FlashlightEquippedUnequipped(bool equipped)
{
m_Light.enabled = !equipped;
}
///
/// An item has been equipped.
///
/// The equipped item.
/// The slot that the item now occupies.
private void OnEquipItem(Item item, int slotID)
{
if (item.ItemDefinition != m_FlashlightItemDefinition) {
return;
}
FlashlightEquippedUnequipped(true);
}
///
/// An item has been unequipped.
///
/// The item that was unequipped.
/// The slot that the item was unequipped from.
private void OnUnequipItem(Item item, int slotID)
{
if (item.ItemDefinition != m_FlashlightItemDefinition) {
return;
}
FlashlightEquippedUnequipped(false);
}
///
/// The object has been destroyed.
///
private void OnDestroy()
{
EventHandler.UnregisterEvent
- (m_Character, "OnInventoryEquipItem", OnEquipItem);
EventHandler.UnregisterEvent
- (m_Character, "OnInventoryUnequipItem", OnUnequipItem);
}
}
}