| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using UnityEngine;
- /// <summary>
- /// Holds agent stats and provides utility methods
- /// Each stat is 1-100, total pool is (number of stats * 100) / 10 = 40 for 4 stats
- /// </summary>
- public class AgentStats
- {
- public int Strength { get; private set; }
- public int Speed { get; private set; }
- public int Magic { get; private set; }
- public int Dexterity { get; private set; }
- private const int STAT_TOTAL_POOL = 40; // (4 stats * 100) / 10
- private const int NUM_STATS = 4;
- private const int MAX_STAT_VALUE = 100;
- private const int MIN_STAT_VALUE = 1;
- public AgentStats()
- {
- GenerateRandomStats();
- }
- /// <summary>
- /// Generates random stats by creating 4 random values and shuffling them
- /// This ensures all stats get a fair distribution
- /// </summary>
- private void GenerateRandomStats()
- {
- int[] stats = new int[NUM_STATS];
- // Generate 4 random values between 1 and 100
- for (int i = 0; i < NUM_STATS; i++)
- {
- stats[i] = Random.Range(MIN_STAT_VALUE, MAX_STAT_VALUE + 1);
- }
- // Shuffle the stats array to randomly assign values
- for (int i = NUM_STATS - 1; i > 0; i--)
- {
- int randomIndex = Random.Range(0, i + 1);
- // Swap
- int temp = stats[i];
- stats[i] = stats[randomIndex];
- stats[randomIndex] = temp;
- }
- Strength = stats[0];
- Speed = stats[1];
- Magic = stats[2];
- Dexterity = stats[3];
- // Normalize to fit within pool
- NormalizeStats();
- }
- /// <summary>
- /// Normalizes stats to roughly fit within the STAT_TOTAL_POOL
- /// </summary>
- private void NormalizeStats()
- {
- int total = Strength + Speed + Magic + Dexterity;
- if (total > STAT_TOTAL_POOL)
- {
- float scale = (float)STAT_TOTAL_POOL / total;
- Strength = Mathf.Max(MIN_STAT_VALUE, (int)(Strength * scale));
- Speed = Mathf.Max(MIN_STAT_VALUE, (int)(Speed * scale));
- Magic = Mathf.Max(MIN_STAT_VALUE, (int)(Magic * scale));
- Dexterity = Mathf.Max(MIN_STAT_VALUE, (int)(Dexterity * scale));
- }
- }
- public int GetTotalStats()
- {
- return Strength + Speed + Magic + Dexterity;
- }
- public override string ToString()
- {
- return $"STR: {Strength} | SPD: {Speed} | MAG: {Magic} | DEX: {Dexterity} | Total: {GetTotalStats()}";
- }
- }
|