Encounter / Assets / Scripts / BattleLogic / Target.cs
Target.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Target : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
    public enum Targetted
    {
        Single,
        AllOfType,
        All
    }

    [Header("Arrow Components")]
    [SerializeField] private GameObject arrow_Prefab;
    [SerializeField] private Transform arrow_Transform;

    private GameObject arrow;
    private bool spellIndicationMode;
    private bool conInidicationMode;

    private SpellManagement spellManager;
    private ConsumableManagement consumableManager;

    public void SwitchSpellIndicationMode(bool toggle) 
    {
        try
        {
            spellIndicationMode = toggle;
            GetComponent<BoxCollider2D>().enabled = spellIndicationMode;
        }
        catch (MissingReferenceException) { }
    }
    public void SwitchConsumableIndicationMode(bool toggle)
    {
        try
        {
            conInidicationMode = toggle;
            GetComponent<BoxCollider2D>().enabled = conInidicationMode;
        }
        catch (MissingReferenceException) { }
    }
    public void InjectManager(SpellManagement sm) 
        => spellManager = sm;
    public void InjectManager(ConsumableManagement cm) 
        => consumableManager = cm;

    private void Awake()
    {
        spellIndicationMode = false;
        conInidicationMode = false;
        GetComponent<BoxCollider2D>().enabled = true;
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if ((spellIndicationMode || conInidicationMode) && arrow == null)
            arrow = Instantiate(arrow_Prefab, arrow_Transform);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if ((spellIndicationMode || conInidicationMode) && arrow != null)
            Destroy(arrow);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (arrow != null)
        {
            if (spellIndicationMode)
                spellManager.ChooseTarget(gameObject.GetComponent<Entity>());
            else if (conInidicationMode)
                consumableManager.ChooseTarget(gameObject.GetComponent<Entity>());

            Destroy(arrow);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Terrain"))
        {
            GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
            GetComponent<BoxCollider2D>().enabled = false;
            transform.rotation = Quaternion.identity;

            // Reactivate the canvas once touching ground to prevent odd visual turning of the UI while the enemies are falling
            if (TryGetComponent(out Enemy enemyComp))
                enemyComp.ReactivateCanvas();
        }
    }

    public void ReactivateDrop()
    {
        GetComponent<BoxCollider2D>().enabled = true;
        GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
    }
}