AgentStats.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine;
  2. /// <summary>
  3. /// Holds agent stats and provides utility methods
  4. /// Each stat is 1-100, total pool is (number of stats * 100) / 10 = 40 for 4 stats
  5. /// </summary>
  6. public class AgentStats
  7. {
  8. public int Strength { get; private set; }
  9. public int Speed { get; private set; }
  10. public int Magic { get; private set; }
  11. public int Dexterity { get; private set; }
  12. private const int STAT_TOTAL_POOL = 40; // (4 stats * 100) / 10
  13. private const int NUM_STATS = 4;
  14. private const int MAX_STAT_VALUE = 100;
  15. private const int MIN_STAT_VALUE = 1;
  16. public AgentStats()
  17. {
  18. GenerateRandomStats();
  19. }
  20. /// <summary>
  21. /// Generates random stats by creating 4 random values and shuffling them
  22. /// This ensures all stats get a fair distribution
  23. /// </summary>
  24. private void GenerateRandomStats()
  25. {
  26. int[] stats = new int[NUM_STATS];
  27. // Generate 4 random values between 1 and 100
  28. for (int i = 0; i < NUM_STATS; i++)
  29. {
  30. stats[i] = Random.Range(MIN_STAT_VALUE, MAX_STAT_VALUE + 1);
  31. }
  32. // Shuffle the stats array to randomly assign values
  33. for (int i = NUM_STATS - 1; i > 0; i--)
  34. {
  35. int randomIndex = Random.Range(0, i + 1);
  36. // Swap
  37. int temp = stats[i];
  38. stats[i] = stats[randomIndex];
  39. stats[randomIndex] = temp;
  40. }
  41. Strength = stats[0];
  42. Speed = stats[1];
  43. Magic = stats[2];
  44. Dexterity = stats[3];
  45. // Normalize to fit within pool
  46. NormalizeStats();
  47. }
  48. /// <summary>
  49. /// Normalizes stats to roughly fit within the STAT_TOTAL_POOL
  50. /// </summary>
  51. private void NormalizeStats()
  52. {
  53. int total = Strength + Speed + Magic + Dexterity;
  54. if (total > STAT_TOTAL_POOL)
  55. {
  56. float scale = (float)STAT_TOTAL_POOL / total;
  57. Strength = Mathf.Max(MIN_STAT_VALUE, (int)(Strength * scale));
  58. Speed = Mathf.Max(MIN_STAT_VALUE, (int)(Speed * scale));
  59. Magic = Mathf.Max(MIN_STAT_VALUE, (int)(Magic * scale));
  60. Dexterity = Mathf.Max(MIN_STAT_VALUE, (int)(Dexterity * scale));
  61. }
  62. }
  63. public int GetTotalStats()
  64. {
  65. return Strength + Speed + Magic + Dexterity;
  66. }
  67. public override string ToString()
  68. {
  69. return $"STR: {Strength} | SPD: {Speed} | MAG: {Magic} | DEX: {Dexterity} | Total: {GetTotalStats()}";
  70. }
  71. }