AgentStats.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public int Intelligence { get; private set; }
  13. public int Constitution { get; private set; }
  14. /// <summary>
  15. /// Max health derived from Constitution: 10 + Constitution (range: 11–110)
  16. /// </summary>
  17. public int MaxHealth => 10 + Constitution;
  18. /// <summary>
  19. /// Current health – starts at MaxHealth and decreases when taking damage.
  20. /// </summary>
  21. public int CurrentHealth { get; private set; }
  22. /// <summary>True once CurrentHealth reaches 0.</summary>
  23. public bool IsDead => CurrentHealth <= 0;
  24. private const int STAT_TOTAL_POOL = 60; // (6 stats * 100) / 10
  25. private const int NUM_STATS = 6;
  26. private const int MAX_STAT_VALUE = 100;
  27. private const int MIN_STAT_VALUE = 1;
  28. public AgentStats()
  29. {
  30. GenerateRandomStats();
  31. CurrentHealth = MaxHealth; // initialise after stats are generated
  32. }
  33. /// <summary>Apply damage; returns actual damage dealt.</summary>
  34. public int ApplyDamage(int amount)
  35. {
  36. int actual = Mathf.Min(amount, CurrentHealth);
  37. CurrentHealth -= actual;
  38. return actual;
  39. }
  40. /// <summary>
  41. /// Generates random stats by creating 4 random values and shuffling them
  42. /// This ensures all stats get a fair distribution
  43. /// </summary>
  44. private void GenerateRandomStats()
  45. {
  46. int[] stats = new int[NUM_STATS];
  47. // Generate 4 random values between 1 and 100
  48. for (int i = 0; i < NUM_STATS; i++)
  49. {
  50. stats[i] = Random.Range(MIN_STAT_VALUE, MAX_STAT_VALUE + 1);
  51. }
  52. // Shuffle the stats array to randomly assign values
  53. for (int i = NUM_STATS - 1; i > 0; i--)
  54. {
  55. int randomIndex = Random.Range(0, i + 1);
  56. // Swap
  57. int temp = stats[i];
  58. stats[i] = stats[randomIndex];
  59. stats[randomIndex] = temp;
  60. }
  61. Strength = stats[0];
  62. Speed = stats[1];
  63. Magic = stats[2];
  64. Dexterity = stats[3];
  65. Intelligence = stats[4];
  66. Constitution = stats[5];
  67. // Normalize to fit within pool
  68. NormalizeStats();
  69. }
  70. /// <summary>
  71. /// Normalizes stats to roughly fit within the STAT_TOTAL_POOL
  72. /// </summary>
  73. private void NormalizeStats()
  74. {
  75. int total = Strength + Speed + Magic + Dexterity + Intelligence;
  76. if (total > STAT_TOTAL_POOL)
  77. {
  78. float scale = (float)STAT_TOTAL_POOL / total;
  79. Strength = Mathf.Max(MIN_STAT_VALUE, (int)(Strength * scale));
  80. Speed = Mathf.Max(MIN_STAT_VALUE, (int)(Speed * scale));
  81. Magic = Mathf.Max(MIN_STAT_VALUE, (int)(Magic * scale));
  82. Dexterity = Mathf.Max(MIN_STAT_VALUE, (int)(Dexterity * scale));
  83. Intelligence = Mathf.Max(MIN_STAT_VALUE, (int)(Intelligence * scale));
  84. Constitution = Mathf.Max(MIN_STAT_VALUE, (int)(Constitution * scale));
  85. }
  86. }
  87. public int GetTotalStats()
  88. {
  89. return Strength + Speed + Magic + Dexterity + Intelligence + Constitution;
  90. }
  91. /// <summary>
  92. /// 0-1 chance to consider joining a group instead of going solo.
  93. /// Higher intelligence = more willing to team up strategically.
  94. /// </summary>
  95. public float GroupUpAffinity => Intelligence / 100f;
  96. /// <summary>
  97. /// 0-1 risk tolerance when evaluating fights or dangerous rooms.
  98. /// High intelligence = lower tolerance (more cautious).
  99. /// </summary>
  100. public float RiskTolerance => 1f - (Intelligence / 100f);
  101. public override string ToString()
  102. {
  103. return $"STR: {Strength} | SPD: {Speed} | MAG: {Magic} | DEX: {Dexterity} | INT: {Intelligence} | CON: {Constitution} | HP: {CurrentHealth}/{MaxHealth} | Total: {GetTotalStats()}";
  104. }
  105. }