/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Items { using Opsive.Shared.Game; using Opsive.Shared.Utility; using Opsive.UltimateCharacterController.StateSystem; using UnityEngine; /// /// Component which represents the item object actually rendererd. The main responsibility is to determine the location that the object should be rendered at. /// public abstract class PerspectiveItem : StateBehavior { [Tooltip("The GameObject of the object rendered. Can be a prefab or a GameObject that is a child of the character.")] [SerializeField] protected GameObject m_Object; [Tooltip("If Object is a prefab, specifies the local position of the spawned object.")] [SerializeField] protected Vector3 m_LocalSpawnPosition; [Tooltip("If Object is a prefab, specifies the local rotation of the spawned object.")] [SerializeField] protected Vector3 m_LocalSpawnRotation; [NonSerialized] public GameObject Object { get { return m_Object; } set { m_Object = value; } } public abstract bool FirstPersonItem { get; } protected GameObject m_Character; protected Item m_Item; /// /// Initialize the perspective item. /// /// The character GameObject that the item is parented to. /// True if the item was initialized successfully. public virtual bool Initialize(GameObject character) { m_Character = character; if (m_Object != null) { var item = m_Object.GetComponentInParent(); // If the item is not null then it is being spawned at runtime. if (item != null) { var localScale = m_Object.transform.localScale; var parent = GetSpawnParent(character, item.SlotID, false); if (parent == null) { return false; } m_Object.transform.parent = parent; m_Object.transform.localScale = localScale; m_Object.transform.localPosition = m_LocalSpawnPosition; m_Object.transform.localRotation = Quaternion.Euler(m_LocalSpawnRotation); } // Layer sanity check. if (m_Object.layer == m_Character.layer) { Debug.LogWarning($"Warning: The item {name} has the same layer as the character. This will likely cause collision problems and should be changed."); } } m_Item = gameObject.GetCachedComponent(); return true; } /// /// Returns the parent that the VisibleItem object should spawn at. /// /// The character that the item should spawn under. /// The character slot that the VisibleItem object should spawn under. /// Should the object be parented to the item slot ID? /// The parent that the VisibleItem object should spawn at. protected abstract Transform GetSpawnParent(GameObject character, int slotID, bool parentToItemSlotID); /// /// Starts the perspective item. Will be called after the item has been started. /// public virtual void ItemStarted() { } /// /// Is the VisibleItem active? /// /// True if the VisibleItem is active. public virtual bool IsActive() { return m_Object.activeSelf; } /// /// Activates or deactivates the VisibleItem. /// /// Should the VisibleItem be activated? public virtual void SetActive(bool active) { m_Object.SetActive(active); } /// /// Returns the current VisibleItem object. /// /// The current VisibleItem object. public virtual GameObject GetVisibleObject() { return m_Object; } /// /// The VisibleItem has been picked up by the character. /// public virtual void Pickup() { } /// /// The item has started to be equipped. /// /// Is the item being equipped immediately? Immediate equips will occur from the default loadout or quickly switching to the item. public virtual void StartEquip(bool immediateEquip) { } /// /// Moves the item according to the horizontal and vertical movement, as well as the character velocity. /// /// -1 to 1 value specifying the amount of horizontal movement. /// -1 to 1 value specifying the amount of vertical movement. public virtual void Move(float horizontalMovement, float verticalMovement) { } /// /// The item has started to be unequipped. /// public virtual void StartUnequip() { } /// /// The item has been unequipped. /// public virtual void Unequip() { } /// /// The item has been removed. /// public virtual void Remove() { if (m_Object != null) { m_Object.SetActive(false); } } } }