UnityGameProjectsCode / RelianceGame / Mission Scripts / SpawnEnemies.cs
SpawnEnemies.cs
Raw
using System.Collections.Generic;
using UnityEngine;

public class SpawnEnemies : MonoBehaviour
{
    private string difficulty;
    private int reinforcementCap;
    public int easyBotMin, easyBotMax, easyTurretMin, easyTurretMax;
    public int normalBotMin, normalBotMax, normalOPBotMin, normalOPBotMax, normalTurretMin, normalTurretMax;
    public int hardBotMin, hardBotMax, hardOPBotMin, hardOPBotMax, hardTurretMin, hardTurretMax;
    public int insaneBotMin, insaneBotMax, insaneOPBotMin, insaneOPBotMax, insaneTurretMin, insaneTurretMax;
    public int spawnDistance;
    private GameManager gameManager;
    private SpawnObjectives spawnObjs;
    private DisplayLog logD;
    public GameObject movingBot;
    public GameObject movingBotOP;
    public GameObject turretBot;
    private List<GameObject> movingBotSpawns = new List<GameObject>();
    private List<GameObject> turretBotSpawns = new List<GameObject>();
    private List<GameObject> reinforceSpawns = new List<GameObject>();
    private List<GameObject> playerBots = new List<GameObject>();


    private int reinforcementsSpawned = 0; //used to track how many enemies have been spawned as reinforcements to limit how many will show up

    public bool easy;
    public bool normal;
    public bool hard;
    public bool insane;

    private bool capReached = false;
    private bool chaseUnits = false;

    private void Awake()
    {
        gameManager = GameObject.Find("Persistent Object").GetComponent<GameManager>();//get reference to game manager to set the difficulty
        spawnObjs = GameObject.Find("EventSystem").GetComponent<SpawnObjectives>();
        logD = GameObject.Find("RunningUI/Text Log/Log Panel/Content").GetComponent<DisplayLog>();

        difficulty = gameManager.difficulty;

        if (difficulty == "Easy") //set the bool for what difficulty the mission is
        {
            easy = true;
            normal = false;
            hard = false;
            insane = false;
            reinforcementCap = 3;
        }

        if (difficulty == "Normal")
        {
            easy = false;
            normal = true;
            hard = false;
            insane = false;
            reinforcementCap = 5;
        }

        if (difficulty == "hard")
        {
            easy = false;
            normal = false;
            hard = true;
            insane = false;
            reinforcementCap = 7;
        }

        if (difficulty == "Insane")
        {
            easy = false;
            normal = false;
            hard = false;
            insane = true;
            reinforcementCap = 9;
        }


        foreach (GameObject movingBot in GameObject.FindGameObjectsWithTag("moveSpawn"))//add all moving bot spawns in the level to the list
        {
            movingBotSpawns.Add(movingBot);
        }
        foreach (GameObject turretBot in GameObject.FindGameObjectsWithTag("turretSpawn"))//add all turret spawns in the level to the list
        {
            turretBotSpawns.Add(turretBot);
        }
        foreach (GameObject reinforceSpawn in GameObject.FindGameObjectsWithTag("ReinforceSpawn"))//add all turret spawns in the level to the list
        {
            reinforceSpawns.Add(reinforceSpawn);
        }
        foreach (GameObject playerBot in GameObject.FindGameObjectsWithTag("Target"))//add the player and friendly bots to a list
        {
            if (playerBot.GetComponent<AIMachine>() || playerBot.GetComponent<PlayerShoot>() || playerBot.GetComponent<AIAllyMachine>())
                playerBots.Add(playerBot);
        }

        SpawnBots();
        gameManager.BotSetup();

        Debug.Log("Enemy Spawning Complete");
    }

    private void Update()
    {
        if (gameManager.missionName == "Assault")
        {
            if (gameManager.remainingEnemies <= 3 && reinforcementsSpawned < reinforcementCap && spawnObjs.capAreaInstance.GetComponent<CaptureArea>().roundedRate > 0)
            {
                Debug.Log("<color=yellow>Spawning reinforcements</color>");
                SpawnReinforcements();
            }

            if (reinforcementsSpawned >= reinforcementCap && capReached == false)
            {
                Debug.Log("<color=red>Reinforcement cap reached</color>");
                capReached = true;
            }

            if (gameManager.exitOpen && chaseUnits == false)
            {
                chaseUnits = true;
                Debug.Log("<color=yellow>Exit open, spawning chase units</color>");
                reinforcementsSpawned = 0;
                reinforcementCap = 10;
                capReached = false;
            }
        }
        else if (gameManager.missionName == "Hack")
        {
            if (gameManager.remainingEnemies <= 3 && reinforcementsSpawned < reinforcementCap && spawnObjs.terminalInstance.GetComponent<CaptureArea>().roundedRate > 0)
            {
                Debug.Log("<color=yellow>Spawning reinforcements</color>");
                SpawnReinforcements();
            }

            if (reinforcementsSpawned >= reinforcementCap && capReached == false)
            {
                Debug.Log("<color=red>Reinforcement cap reached</color>");
                capReached = true;
            }

            if (gameManager.exitOpen && chaseUnits == false)
            {
                chaseUnits = true;
                Debug.Log("<color=yellow>Exit open, spawning chase units</color>");
                reinforcementsSpawned = 0;
                reinforcementCap = 10;
                capReached = false;
            }
        }
        else if (gameManager.missionName == "Defend")
        {
            Health structureHP = spawnObjs.structureInstance.GetComponent<Health>();
            if (gameManager.remainingEnemies <= 3 && reinforcementsSpawned < reinforcementCap && structureHP.health < structureHP.maxHealth && spawnObjs.structureInstance != null)
            {
                Debug.Log("<color=yellow>Spawning reinforcements</color>");
                SpawnReinforcements();
            }

            if (reinforcementsSpawned >= reinforcementCap && capReached == false)
            {
                Debug.Log("<color=red>Reinforcement cap reached</color>");
                capReached = true;
            }
        }
        else if (gameManager.missionName == "Destroy")
        {
            Health structureHP = spawnObjs.structureInstance.GetComponent<Health>();
            if (gameManager.remainingEnemies <= 3 && reinforcementsSpawned < reinforcementCap && structureHP.health < structureHP.maxHealth && spawnObjs.structureInstance != null)
            {
                Debug.Log("<color=yellow>Spawning reinforcements</color>");
                SpawnReinforcements();
            }

            if (reinforcementsSpawned >= reinforcementCap && capReached == false)
            {
                Debug.Log("<color=red>Reinforcement cap reached</color>");
                capReached = true;
            }

            if (gameManager.exitOpen && chaseUnits == false)
            {
                chaseUnits = true;
                Debug.Log("<color=yellow>Exit open, spawning chase units</color>");
                reinforcementsSpawned = 0;
                reinforcementCap = 10;
                capReached = false;
            }
        }
        else if (gameManager.missionName == "Protect")
        {
            if (gameManager.allyBots.Count >= 1 && gameManager.remainingEnemies <= 3 && reinforcementsSpawned <= reinforcementCap)
            {
                Debug.Log("<color=yellow>Spawning reinforcements</color>");
                SpawnReinforcements();
            }

            if (reinforcementsSpawned >= reinforcementCap && capReached == false)
            {
                Debug.Log("<color=red>Reinforcement cap reached</color>");
                capReached = true;
            }
        }
        else if (gameManager.missionName == "Kill")
        {
            if (gameManager.remainingEnemies <= 3 && reinforcementsSpawned < reinforcementCap)
            {
                Debug.Log("<color=yellow>Spawning reinforcements</color>");
                SpawnReinforcements();
            }

            if (reinforcementsSpawned >= reinforcementCap && capReached == false)
            {
                Debug.Log("<color=red>Reinforcement cap reached</color>");
                capReached = true;
            }

            if (gameManager.exitOpen && chaseUnits == false)
            {
                chaseUnits = true;
                Debug.Log("<color=yellow>Exit open, spawning chase units</color>");
                reinforcementsSpawned = 0;
                reinforcementCap = 10;
                capReached = false;
            }
        }
    }

    public void SpawnBots()
    {
        //check the difficulty and then spawn the bots based on the public variables
        int botRandom = 0;
        int botOPRandom = 0;
        int turretRandom = 0;

        List<GameObject> usedSpawns = new List<GameObject>();

        switch (difficulty)
        {
            case "Easy":
                botRandom = Random.Range(easyBotMin, easyBotMax);
                turretRandom = Random.Range(easyTurretMin, easyTurretMax);
                break;
            case "Normal":
                botRandom = Random.Range(normalBotMin, normalBotMax);
                botOPRandom = Random.Range(normalOPBotMin, normalOPBotMax);
                turretRandom = Random.Range(normalTurretMin, normalTurretMax);
                break;
            case "Hard":
                botRandom = Random.Range(hardBotMin, hardBotMax);
                botOPRandom = Random.Range(hardOPBotMin, hardOPBotMax);
                turretRandom = Random.Range(hardTurretMin, hardTurretMax);
                break;
            case "Insane":
                botRandom = Random.Range(insaneBotMin, insaneBotMax);
                botOPRandom = Random.Range(insaneOPBotMin, insaneOPBotMax);
                turretRandom = Random.Range(insaneTurretMin, insaneTurretMax);
                break;
        }

        int randIterator = 0;
        List<GameObject> currentSpawns = new List<GameObject>();
        GameObject objectToSpawn = null;

        for (int x = 0; x < 3; x++)
        {
            switch (x)
            {
                case 0:
                    randIterator = botRandom;
                    currentSpawns = movingBotSpawns;
                    objectToSpawn = movingBot;
                    break;
                case 1:
                    randIterator = botOPRandom;
                    currentSpawns = movingBotSpawns;
                    objectToSpawn = movingBotOP;
                    break;
                case 2:
                    randIterator = turretRandom;
                    currentSpawns = turretBotSpawns;
                    objectToSpawn = turretBot;
                    break;
            }

            for (int i = 0; i < randIterator; i++)
            {
                bool sameSpawn = false;
                int randomSpawn = Random.Range(0, currentSpawns.Count);

                for (int y = 0; y < usedSpawns.Count; y++)
                {
                    if (currentSpawns[randomSpawn] == usedSpawns[y])
                    {
                        sameSpawn = true;
                        break;
                    }
                }

                if (sameSpawn == false)
                {
                    Instantiate(objectToSpawn, currentSpawns[randomSpawn].transform);
                    usedSpawns.Add(currentSpawns[randomSpawn]);
                }
                else
                    i--;
            }

            usedSpawns.Clear();
        }
    }

    public void SpawnReinforcements() //called when the mission type needs more enemies to spawn in
    {
        logD.RecieveLog("Enemy reinforcements have appeared!!");

        for (int i = 0; i < reinforceSpawns.Count; i++) //for each spawn
        {
            float minDistance = float.MaxValue;

            for (int y = 0; y < playerBots.Count; y++) //check EACH bots distance to the current spawn and then decide to spawn or not
            {
                var botDist = Vector3.Distance(reinforceSpawns[i].transform.position, playerBots[y].transform.position);

                if (botDist < minDistance) //get the shortest distance to this spawn
                {
                    minDistance = botDist;
                }
            }

            //if the current spawn point is at a good range and the reinforcement cap has not been reached yet, spawn an enemy
            if (minDistance >= spawnDistance && reinforcementsSpawned < reinforcementCap)
            {
                var newEnemy = Instantiate(movingBot, reinforceSpawns[i].transform);
                gameManager.movingEnemies.Add(newEnemy);
                newEnemy.GetComponent<EnemyAIMachine>().defender = true;
                //gameManager.remainingEnemies++;
                reinforcementsSpawned++;
                //Debug.Log("Extra enemies spawned: " + reinforcementsSpawned);
            }

        }
    }
}