Encounter / Assets / Scripts / EncounterManagement / EnemyEncounter.cs
EnemyEncounter.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

/// <summary>
/// Handles Enemy Encounter Generation
/// </summary>
public class EnemyEncounter : Encounter<GameObject[]>
{
    public GameObject[] Enemies { get; private set; }

    private const int EnemyType = 1;
    private List<GameObject> enemyPrefabFiles;

    private const int EnemyMedEncounter             = 3;
    private const int EnemyHardEncounter            = 4;
    private const int EnemyImpossibleEncounter      = 5;

    private const int SwamplandsEdgeValue           = 10;
    private const int FrozenWastelandEdgeValue      = 15;
    private const int DarkVillageEdgeValue          = 20;

    private bool IsTestEncounter = false;

    public EnemyEncounter(int encounterValue)
    {
        if (IsTestEncounter)
        {
            ClimateManager.Instance.DecipherLocation(encounterValue, this);
            LoadEnemyPrefabs(string.Empty);
            GenerateEnemies(1, encounterValue);

            this.SetEncounter(Enemies);
            GameManager.Instance.IndicateEncounterType(EnemyType, this);
        }
        else
        {
            string locationInfo;
            int enemyAmount;

            if (encounterValue < SwamplandsEdgeValue)
            {
                locationInfo = ClimateManager.Instance.DecipherLocation(encounterValue, this);
                enemyAmount = UnityEngine.Random.Range(1, EnemyMedEncounter);
            }
            else if (encounterValue < FrozenWastelandEdgeValue)
            {
                locationInfo = ClimateManager.Instance.DecipherLocation(encounterValue, this);
                enemyAmount = UnityEngine.Random.Range(1, EnemyHardEncounter);
            }
            else if (encounterValue < DarkVillageEdgeValue)
            {
                locationInfo = ClimateManager.Instance.DecipherLocation(encounterValue, this);
                enemyAmount = UnityEngine.Random.Range(1, EnemyHardEncounter);
            }
            else
            {
                locationInfo = ClimateManager.Instance.DecipherLocation(encounterValue, this);
                enemyAmount = UnityEngine.Random.Range(1, EnemyImpossibleEncounter);
            }

            LoadEnemyPrefabs(locationInfo);
            GenerateEnemies(enemyAmount, encounterValue);
            GameManager.Instance.IndicateEncounterType(EnemyType, this);
            this.SetEncounter(Enemies);
        }
    }
    /// <summary>
    /// Loads the location enemy prefabs as well as general enemy prefabs that can appear in any location
    /// </summary>
    /// <param name="locationInfo">Name of the Location that is loading enemies from</param>
    private void LoadEnemyPrefabs(string locationInfo)
    {
        if (IsTestEncounter)
            enemyPrefabFiles = new List<GameObject>(Resources.LoadAll<GameObject>($"Entities/Enemies/Test").ToList());
        else
        {
            enemyPrefabFiles = new List<GameObject>(Resources.LoadAll<GameObject>($"Entities/Enemies/{locationInfo}").ToList());
            List<GameObject> generalEnemies = Resources.LoadAll<GameObject>("Entities/Enemies/General").ToList();
            generalEnemies.ForEach(e =>
            {
                enemyPrefabFiles.Add(e);
            });
        }
    }
    /// <summary>
    /// Generates a selection of enemies from the loaded prefab files
    /// </summary>
    /// <param name="enemyAmount"></param>
    private void GenerateEnemies(int enemyAmount, int encounterValue)
    {
        if (IsTestEncounter)
        {
            this.Enemies = new GameObject[3];

            this.Enemies[0] = enemyPrefabFiles[0];
            this.Enemies[1] = enemyPrefabFiles[0];
            this.Enemies[2] = enemyPrefabFiles[0];

            return;
        }
        else
        {
            Enemies = new GameObject[enemyAmount];

            int enemyEncounterValue;

            if (encounterValue < 5)
                enemyEncounterValue = 6;
            else if (encounterValue < 12)
                enemyEncounterValue = 13;
            else if (encounterValue < 20)
                enemyEncounterValue = 25;
            else if (encounterValue < 100)
                enemyEncounterValue = 100;
            else
            {
                int chance = UnityEngine.Random.Range(0, 5);
            
                if (chance <= 3)
                {
                    CameraManager.Instance.DarkenScreen().GetAwaiter();

                    // MAY DAY MAY DAY FUCKOS YOU GOT A BOSS
                    GameObject boss = Resources.Load<GameObject>("Entities/Enemies/Bosses/Delora The Maiden of Everlasting Torture");
            
                    this.Enemies = new GameObject[1];
                    this.Enemies[0] = boss;
                    Globals.IsBossEncounter = true;
                    return;
                }
                else
                    enemyEncounterValue = 100;
            }

            int retainerValue = enemyEncounterValue;
            int delta = 0;
            bool legendaryEncounter = false;
            for (int i = 0; i < enemyAmount; i++)
            {
                GameObject enemyConsidered = enemyPrefabFiles[UnityEngine.Random.Range(0, enemyPrefabFiles.Count)];
                Enemy enemyComp = enemyConsidered.GetComponent<Enemy>();

                // Very Easy    = 1
                // Easy         = 3
                // Average      = 5
                // Hard         = 10
                // VeryHard     = 15
                // Legendary    = 25

                switch (enemyComp.GetEnemyDifficulty())
                {
                    case Enemy.DifficultyRating.VeryEasy:
                        retainerValue -= 1;
                        delta = 1;
                        break;
                    case Enemy.DifficultyRating.Easy:
                        retainerValue -= 3;
                        delta = 3;
                        break;
                    case Enemy.DifficultyRating.Average:
                        retainerValue -= 5;
                        delta = 5;
                        break;
                    case Enemy.DifficultyRating.Hard:
                        retainerValue -= 10;
                        delta = 10;
                        break;
                    case Enemy.DifficultyRating.VeryHard:
                        retainerValue -= 15;
                        delta = 15;
                        break;
                    case Enemy.DifficultyRating.Legendary:
                        if (legendaryEncounter is false)
                        {
                            retainerValue -= 25;
                            delta = 25;
                            legendaryEncounter = true;
                            break;
                        }
                        else
                        {
                            --i;
                            continue;
                        }
                }

                if (retainerValue < 0)
                {
                    retainerValue += delta;
                    --i;
                    continue;
                }

                this.Enemies[i] = enemyConsidered;
            }
        }
    }
}