UnityGameProjectsCode / RelianceGame / Stat Control / InformantStat.cs
InformantStat.cs
Raw
using System.Collections.Generic;
using UnityEngine;

public class InformantStat : MonoBehaviour
{
    public int radius;
    private int modifiedRadius;
    List<GameObject> bots = new List<GameObject>();
    private AIMachine ai;
    private PlayerInfo pInfo;

    public bool playerBot = false;

    private void Awake()
    {
        gameObject.SetActive(false);

        if (!playerBot)
            ai = transform.parent.gameObject.GetComponent<AIMachine>();
        else
            pInfo = transform.parent.gameObject.GetComponent<PlayerInfo>();
    }

    public void SetRadius(int radiusIncrease)
    {
        gameObject.SetActive(true);
        modifiedRadius = radius + radiusIncrease;
        GetComponent<SphereCollider>().radius = modifiedRadius;
    }

    private void Update()
    {
        if (bots.Count > 0)
        {
            for (int i = 0; i < bots.Count; i++)
            {
                if (!bots[i].activeSelf)
                {
                    bots.RemoveAt(i);
                }
            }
        }
    }

    void OnTriggerEnter(Collider other)
    {
        var colName = other.gameObject.name;

        if (colName == "EnemyBot(Clone)" || colName == "EnemyBot OP(Clone)" || colName == "Turret(Clone)" || colName == "Enemy Structure(Clone)")
        {
            bots.Add(other.gameObject);
            other.gameObject.GetComponent<Health>().enemyHealthCanvas.enabled = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (!playerBot)
            OnFriendlyExit(other);
        else
            OnPlayerExit(other);
    }

    public void OnPlayerExit(Collider other)
    {
        var colName = other.gameObject.name;

        bool inOthersRange = false;

        List<GameObject> pFriendlies = pInfo.GetFriendliesList();

        if (colName == "EnemyBot(Clone)" || colName == "EnemyBot OP(Clone)" || colName == "Turret(Clone)" || colName == "Enemy Structure(Clone)")
        {
            GameObject exitingBot = other.gameObject;

            for (int i = 0; i < bots.Count; i++)
            {
                if (exitingBot == bots[i])
                {
                    for (int y = 0; y < pFriendlies.Count; y++)
                    {
                        InformantStat targetStat = null;

                        foreach (Transform child in pFriendlies[y].GetComponentsInChildren<Transform>())
                        {
                            if (child.gameObject.GetComponent<InformantStat>())
                            {
                                targetStat = child.GetComponent<InformantStat>();
                            }
                        }

                        if (targetStat != null)
                        {
                            for (int x = 0; x < targetStat.bots.Count; x++)
                            {
                                if (targetStat.bots[x] == exitingBot)
                                {
                                    inOthersRange = true;
                                    break;
                                }
                            }
                        }

                        if (inOthersRange)
                            break;
                    }

                    if (!inOthersRange)
                    {
                        other.gameObject.GetComponent<Health>().enemyHealthCanvas.enabled = false;
                        bots.RemoveAt(i);
                    }
                }
            }
        }
    }

    public void OnFriendlyExit(Collider other)
    {
        var colName = other.gameObject.name;

        bool inOthersRange = false;

        if (colName == "EnemyBot(Clone)" || colName == "EnemyBot OP(Clone)" || colName == "Turret(Clone)" || colName == "Enemy Structure(Clone)")
        {
            GameObject exitingBot = other.gameObject;

            for (int i = 0; i < bots.Count; i++)
            {
                //does not work for player because there is no way to get the friendlies list off the player. (use PlayerInfo)

                if (exitingBot == bots[i])
                {
                    for (int y = 0; y < ai.friendlyBots.Count; y++)
                    {
                        InformantStat targetStat = null;

                        foreach (Transform child in ai.friendlyBots[y].GetComponentsInChildren<Transform>())
                        {
                            if (child.GetComponent<InformantStat>())
                            {
                                targetStat = child.GetComponent<InformantStat>();
                            }
                        }

                        if (targetStat != null)
                        {
                            for (int x = 0; x < targetStat.bots.Count; x++)
                            {
                                if (targetStat.bots[x] == exitingBot)
                                {
                                    inOthersRange = true;
                                    break;
                                }
                            }
                        }

                        if (inOthersRange)
                            break;
                    }

                    if (!inOthersRange)
                    {
                        other.gameObject.GetComponent<Health>().enemyHealthCanvas.enabled = false;
                        bots.RemoveAt(i);
                    }
                }
            }
        }
    }
}