Encounter / Assets / Scripts / GUIHandlers / SpellHelperConnector.cs
SpellHelperConnector.cs
Raw
using System;
using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// Connector to call and destroy the Spell Helper UI when needed
/// </summary>
public class SpellHelperConnector : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
    private GameObject spellHelperObject;
    private GameObject container;

    private Spell spellT;
    private bool mouseDetected;
    private float t;
    /// <summary>
    /// Transfers spell into a container that will be transferred to the helper UI upon pointer entering it 
    /// </summary>
    /// <param name="spell">Spell being injected to the necessary helper UI</param>
    public void SpellInjectionStart(Spell spell) 
        => this.spellT = spell;
    public void OnPointerEnter(PointerEventData eventData)
    {
        mouseDetected = true;
        t = 0.0f;
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        mouseDetected = false;
        Destroy(container);
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        mouseDetected = false;
        if (container != null)
            Destroy(container);
    }
    public void OnDestroy()
    {
        // When a spell is used and an enemy dies, if the player hovers over a spell before switching turns
        // the helper will appear and remain forever, this check OnDestroy prevents that.
        if (container != null)
            Destroy(container);
    }
    public void OnDisable()
    {
        // When a spell is used and an enemy dies, if the player hovers over a spell before switching turns
        // the helper will appear and remain forever, this check OnDisable prevents that.
        if (container != null) 
            Destroy(container);
    }
    private void OnEnable() 
        => spellHelperObject = Resources.Load<GameObject>("Prefabs/Helper Object");
    private void Update()
    {
        if (mouseDetected)
        {
            t += Time.deltaTime;
            if (t >= .75f && container == null)
                ShowHelper();
        }
    }
    private void ShowHelper()
    {
        // Creates the Helper UI and injects the transferred spell info to the UI for its info
        container = Instantiate(spellHelperObject, Vector3.zero, Quaternion.identity);
        container.GetComponent<HelperUI>().InjectSpellInfo(spellT);
    }
}