Encounter / Assets / Scripts / GUIHandlers / AspectHelperManager.cs
AspectHelperManager.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// The system attached to the items, spells, and abilities themselves that instantiates the AspectHelperUI linked object
/// </summary>
public class AspectHelperManager : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    [Header("General")]
    [SerializeField] private GameObject aspectHelperPrefab;         // The reference to the prefab that will help the user with information about the aspect they are looking at
    [SerializeField] private Transform instanceLocation;            // The location that the instance should be put at

    [Header("Smart Instancing")]
    [SerializeField] private bool smartInstanceLocation;            // Trigger for if the instance should be logically spawned in a location next to the manager
    [SerializeField] private Vector2 offset;                        // The offset when instancing next to one of this game objects anchors with smart instancing enabled

    private GameObject aspectHelperInstance;                        // Instance of the prefab that is already available
    private object aspectHeld;                                      // The information to be displayed in the helper instance

    /// <summary>
    /// Injects an offset vector2 value when certain instances require variances in a scene
    /// </summary>
    /// <param name="offsetValue"></param>
    public void InjectOffset(Vector2 offsetValue)
        => offset = offsetValue;
    /// <summary>
    /// For the looting aspects, switches the sign so that the aspect instance doesn't transpose off screen when moving to different inventories
    /// </summary>
    /// <param name="negative">Indicator if the switch is to the negative transform of the manager</param>
    public void SwitchOffsetSign(bool negative)
    {
        if (negative)
            offset.x *= -1f;
        else
            return;
    }
    /// <summary>
    /// Injects the transform that the helper instance will be attached to
    /// </summary>
    /// <param name="t">The transform parent</param>
    public void InjectTransform(Transform t)
        => instanceLocation = t;
    /// <summary>
    /// When the aspect retainers change than the aspectHeld changes to match
    /// </summary>
    /// <param name="aspect">The aspect that is being held</param>
    public void InjectAspect(object aspect) 
        => aspectHeld = aspect;
    /// <summary>
    /// Determines when the pointer enters if the aspect helper simply pops up in a designated transform or calculates smart instancing
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (aspectHelperInstance == null)
        {
            if (smartInstanceLocation)
                SmartInstancing();
            else 
                aspectHelperInstance = Instantiate(aspectHelperPrefab, instanceLocation);

            AspectHelperUI aspectHelperRef = aspectHelperInstance.GetComponent<AspectHelperUI>();
            aspectHelperRef.InjectAspectInformation(aspectHeld);
        }
    }
    /// <summary>
    /// Calculates the new position of the helper instance when an aspect manager is marked as a smart instancer
    /// </summary>
    private void SmartInstancing()
    {
        Vector2 pos = transform.position;
        pos.x += offset.x;

        // Calculates where in the screen the aspect is so the helper instance doesn't transpose off the screen
        if (instanceLocation.GetComponent<RectTransform>().rect.height < pos.y + offset.y)
            pos.y -= offset.y;
        else
            pos.y += offset.y;

        aspectHelperInstance = Instantiate(aspectHelperPrefab, pos, Quaternion.identity, instanceLocation);
    }
    /// <summary>
    /// Destroys the instance of the aspect helper if it has been instantiated
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData)
    {
        if (aspectHelperInstance != null)
            DestroyImmediate(aspectHelperInstance);
    }
    /// <summary>
    /// Destroys any instances that are active after the aspect has been moved or taken
    /// </summary>
    private void OnDisable()
    {
        if (aspectHelperInstance != null)
            DestroyImmediate(aspectHelperInstance);
    }
}