/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Items.Actions { using Opsive.Shared.Events; using Opsive.Shared.Inventory; using Opsive.UltimateCharacterController.Character.Abilities.Items; using Opsive.UltimateCharacterController.Items.Actions.PerspectiveProperties; #if ULTIMATE_CHARACTER_CONTROLLER_MULTIPLAYER using Opsive.UltimateCharacterController.Networking.Game; #endif using UnityEngine; /// /// An item that can shine a light. /// public class Flashlight : UsableItem { [Tooltip("The battery attribute that should be modified when the flashlight is active.")] [HideInInspector] [SerializeField] protected Traits.AttributeModifier m_BatteryModifier = new Traits.AttributeModifier("Battery", 0, Traits.Attribute.AutoUpdateValue.Decrease); public Traits.AttributeModifier BatteryModifier { get { return m_BatteryModifier; } set { m_BatteryModifier = value; } } private IFlashlightPerspectiveProperties m_FlashlightPerpectiveProperties; /// /// Initialize the default values. /// protected override void Awake() { base.Awake(); m_FlashlightPerpectiveProperties = m_ActivePerspectiveProperties as IFlashlightPerspectiveProperties; if (m_BatteryModifier != null) { if (m_BatteryModifier.Initialize(m_GameObject)) { EventHandler.RegisterEvent(m_BatteryModifier.Attribute, "OnAttributeReachedDestinationValue", OnBatteryEmpty); } } } /// /// Initialize the visible object transform. /// protected override void Start() { base.Start(); if (m_FlashlightPerpectiveProperties == null) { m_FlashlightPerpectiveProperties = m_ActivePerspectiveProperties as IFlashlightPerspectiveProperties; if (m_FlashlightPerpectiveProperties == null) { Debug.LogError("Error: The First/Third Person Flashlight Item Properties component cannot be found for the Item " + name + "." + "Ensure the component exists and the component's Action ID matches the Action ID of the Item (" + m_ID + ")"); } } } /// /// Returns the ItemIdentifier which can be used by the item. /// /// The ItemIdentifier which can be used by the item. public override IItemIdentifier GetConsumableItemIdentifier() { return null; } /// /// Returns the amout of UsableItemIdentifier which has been consumed by the UsableItem. /// /// The amount consumed of the UsableItemIdentifier. public override int GetConsumableItemIdentifierAmount() { return -1; } /// /// Can the item be used? /// /// The itemAbility that is trying to use the item. /// The state of the Use ability when calling CanUseItem. /// True if the item can be used. public override bool CanUseItem(ItemAbility itemAbility, UseAbilityState abilityState) { if (!base.CanUseItem(itemAbility, abilityState)) { return false; } // The flashlight can't be used if there is no battery left. if (m_BatteryModifier != null && !m_BatteryModifier.IsValid()) { return false; } return true; } /// /// Starts the item use. /// /// The item ability that is using the item. public override void StartItemUse(ItemAbility itemAbility) { base.StartItemUse(itemAbility); ToggleFlashlight(!m_FlashlightPerpectiveProperties.Light.activeSelf); } /// /// Activates or deactives the flashlight. /// /// Should the flashlight be activated? public void ToggleFlashlight(bool active) { m_FlashlightPerpectiveProperties.Light.SetActive(active); #if ULTIMATE_CHARACTER_CONTROLLER_MULTIPLAYER if (m_NetworkInfo != null) { if (!m_NetworkInfo.IsLocalPlayer()) { return; } m_NetworkCharacter.ToggleFlashlight(this, active); } #endif if (m_BatteryModifier != null) { m_BatteryModifier.EnableModifier(active); } } /// /// The flashlight battery is empty. /// private void OnBatteryEmpty() { ToggleFlashlight(false); } /// /// The item has started to be unequipped by the character. /// public override void StartUnequip() { base.StartUnequip(); ToggleFlashlight(false); } /// /// The character perspective between first and third person has changed. /// /// Is the character in a first person perspective? protected override void OnChangePerspectives(bool firstPersonPerspective) { base.OnChangePerspectives(firstPersonPerspective); var active = m_FlashlightPerpectiveProperties.Light.activeSelf; m_FlashlightPerpectiveProperties.Light.SetActive(false); m_FlashlightPerpectiveProperties = m_ActivePerspectiveProperties as IFlashlightPerspectiveProperties; if (active) { m_FlashlightPerpectiveProperties.Light.SetActive(true); } } /// /// The object has been destroyed. /// protected override void OnDestroy() { base.OnDestroy(); if (m_BatteryModifier != null && m_BatteryModifier.Attribute != null) { EventHandler.UnregisterEvent(m_BatteryModifier.Attribute, "OnAttributeReachedDestinationValue", OnBatteryEmpty); } } } }