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

/// <summary>
/// Handles general Encounter Generation and encounters created
/// </summary>
public class EncounterGenerator
{
    private int encounterValue;                        // Represents how many Encounters the player has generated

    // 1 is Shop, 2 is Enemy, 3 is Event
    private int previousEncounter;

    public int ShopChance { get; private set; }
    public int EnemyChance { get; private set; }
    public int EventChance { get; private set; }

    private const int ShopChanceNormal = 60;
    private const int EventChanceNormal = 100;
    private const int EnemyChanceNormal = 0;

    private const int ShopChanceMax = 100;
    private const int EventChanceMax = 95;
    private const int EnemyChanceMax = 85;
    private const int EnemyChanceMin = 45;
    private const int EventChanceMin = 55;
    private const int ShopChanceMin = 65;

    /// <summary>
    /// Changes the chance rates of rolling each of the encounters by modifing and lowering certain values
    /// </summary>
    /// <param name="deltaChanceRate">The amount to change from the chance rates, pos or neg</param>
    /// <param name="indicators">THREE VALUES: [0] indicates Shop, [1] indicates Enemy, [2] indicates Event</param>
    /// <exception cref="DataMisalignedException">If the length of the bool is less than or greater than 3</exception>
    public void ChangeChanceRates(int deltaChanceRate, bool[] indicators)
    {
        if (indicators.Length < 3 || indicators.Length > 3)
            throw new DataMisalignedException("Incompatible bool array given to ChangeChanceRates, please check all references.");

        // Shop
        if (indicators[0])
        {
            this.EnemyChance = Mathf.Clamp(this.EnemyChance - deltaChanceRate, EnemyChanceMin, EnemyChanceMax);
            this.EventChance = Mathf.Clamp(this.EventChance - deltaChanceRate, EventChanceMin, EventChanceMax);
        }
        // Enemy
        else if (indicators[1])
        {
            this.ShopChance = Mathf.Clamp(this.ShopChance - deltaChanceRate, ShopChanceMin, ShopChanceMax);
            this.EventChance = Mathf.Clamp(this.EventChance - deltaChanceRate, EventChanceMin, EventChanceMax);
        }
        // Event
        else
        {
            this.EnemyChance = Mathf.Clamp(this.EnemyChance - deltaChanceRate, EnemyChanceMin, EnemyChanceMax);
            this.ShopChance = Mathf.Clamp(this.ShopChance - deltaChanceRate, ShopChanceMin, ShopChanceMax);
        }
    }

    /// <summary>
    /// Retrieves the number of Encounters generated
    /// </summary>
    /// <returns>Number of Encounters generated</returns>
    public int GetEncounterValue()
        => encounterValue;
    /// <summary>
    /// CTOR for EncounterGenerator, simply sets the number of encounters generated to 0
    /// </summary>
    public EncounterGenerator() 
    {
        encounterValue = 0;
        this.ShopChance     = ShopChanceNormal;
        this.EnemyChance    = EnemyChanceNormal;
        this.EventChance    = EventChanceNormal;
    }
    /// <summary>
    /// Generates an Encounter
    /// </summary>
    public void GenerateEncounter()
    {
        if (encounterValue == 0)
        {
            GenerateShopEncounter();
            previousEncounter = 1;
        }
        // TODO---> Enable Event Encounters!
        else
        {
            int chanceValue = UnityEngine.Random.Range(0, 100);

            if (chanceValue >= this.ShopChance)
            {
                if (previousEncounter is not 1)
                {
                    GenerateShopEncounter();
                    previousEncounter = 1;
                }
                else
                {
                    GenerateEnemyEncounter();
                    previousEncounter = 2;
                }
            }
            //else if 
            //    GenerateEventEncounter();
            else if (chanceValue >= this.EnemyChance)
            {
                GenerateEnemyEncounter();
                previousEncounter = 2;
            }
        }

        encounterValue++;
    }
    private void GenerateShopEncounter() 
        => new ShopEncounter(encounterValue);
    private void GenerateEventEncounter() 
        => new EventEncounter(encounterValue);
    private void GenerateEnemyEncounter() 
        => new EnemyEncounter(encounterValue);
}