Encounter / Assets / Scripts / BattleLogic / OrderOfCombat.cs
OrderOfCombat.cs
Raw
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

/// <summary>
/// Logic handler, will serve as the data to be read to the UI for the Turn 
/// Visualization for the player to view
/// </summary>
public class OrderOfCombat
{
    private Dictionary<Entity, int> orderPairs = new Dictionary<Entity, int>();
    private List<Entity> entityOrder = new List<Entity>();

    private Dictionary<Entity, int> turnOrder = new();

    /// <summary>
    /// Retrieves the created turn order for reference for instances like revival for the entity to be placed back in
    /// </summary>
    /// <returns>The original turn order with all entities alive</returns>
    public Dictionary<Entity, int> RetrieveOriginalTurnOrder()
        => turnOrder;
    /// <summary>
    /// Adds the entity and its roll to the map for sorting later
    /// </summary>
    /// <param name="et">Entity to map</param>
    /// <param name="orderV">The rolled initiative value to map with the entity</param>
    public void CallOrderInitiative(Entity et, int orderV) 
        => orderPairs.Add(et, orderV);
    /// <summary>
    /// Sorts all of the entities based off their values that are mapped to them within the dictionary and creates the new ordered list
    /// 
    /// Descending Order, i.e. 19 --> 12 --> 3 would be the battle order by roll
    /// </summary>
    public List<Entity> SortOrder()
    {
        entityOrder = new List<Entity>();
        List<int> entsTaken = new List<int>(); // List of references to already used entities based off index

        // While there are entities to order 
        while (entityOrder.Count != orderPairs.Count)
        {
            Entity ent = null;
            int ret = 0;       // A base low value is given to always allow a first check for the first available entity roll
            int pos = -1;       // Position indicator for reference to add to the list for checking

            for (int i = 0; i < orderPairs.Count; i++)
            {
                // If the entity list already contains this index, skip it
                if (entsTaken.Contains(i)) continue;
                // Otherwise check to see if the roll is higher than the 
                else if (orderPairs.ElementAt(i).Value > ret)
                {
                    ret = orderPairs.ElementAt(i).Value;
                    ent = orderPairs.ElementAt(i).Key;
                    pos = i;
                }
            }

            // If the entity isn't null than the position is added to the entities that have been added list as well as moves the entity up the hierarchy
            if (ent != null)
            {
                entityOrder.Add(ent);
                entsTaken.Add(pos);
            }
        }

        for (int i = 0; i < entityOrder.Count; i++)
        {
            if (i is 0)
                turnOrder = new();

            turnOrder.Add(entityOrder[i], i);
        }

        return entityOrder;
    }
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        foreach (KeyValuePair<Entity, int> pair in turnOrder)
            sb.Append($"{pair.Key.GetName()} at turn {pair.Value}\n");

        return sb.ToString();
    }
}