MonsterEncounter.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace MazeGen
  4. {
  5. /// <summary>
  6. /// Represents a single monster encounter in a room: which monsters are present,
  7. /// their health state, and visibility to the agent (for the peek mechanic).
  8. /// </summary>
  9. public class MonsterEncounter
  10. {
  11. public class MonsterInstance
  12. {
  13. public Monster Template;
  14. public CharacterSheet CharacterSheet;
  15. public bool IsDefeated => !CharacterSheet.Stats.IsAlive;
  16. private List<Item> _remainingLoot; // Track which loot items haven't been claimed yet
  17. public MonsterInstance(Monster template)
  18. {
  19. Template = template;
  20. CharacterSheet = template.CreateCharacterSheet();
  21. // Generate the loot pool based on the template's loot table and min/max counts
  22. _remainingLoot = template.GenerateLootPool();
  23. }
  24. /// <summary>
  25. /// Get a random loot item from this monster instance's remaining loot pool.
  26. /// The selected item is removed so it won't be looted again.
  27. /// Returns null if no loot remains.
  28. /// </summary>
  29. public Item GetRandomLoot()
  30. {
  31. if (_remainingLoot == null || _remainingLoot.Count == 0)
  32. return null;
  33. int lootIndex = Random.Range(0, _remainingLoot.Count);
  34. Item loot = _remainingLoot[lootIndex];
  35. _remainingLoot.RemoveAt(lootIndex); // Remove so it can't be looted again
  36. return loot;
  37. }
  38. }
  39. private List<MonsterInstance> _monsters = new List<MonsterInstance>();
  40. private int _visibleMonsterCount; // Partial info: agent sees this many, not the full count
  41. public IReadOnlyList<MonsterInstance> Monsters => _monsters.AsReadOnly();
  42. public int AliveMonsterCount
  43. {
  44. get
  45. {
  46. int count = 0;
  47. foreach (var m in _monsters)
  48. if (!m.IsDefeated) count++;
  49. return count;
  50. }
  51. }
  52. /// <summary>
  53. /// How many monsters the agent can actually see in the room.
  54. /// This is less than or equal to AliveMonsterCount (incomplete information).
  55. /// </summary>
  56. public int VisibleMonsterCount => _visibleMonsterCount;
  57. public bool HasLivingMonsters => AliveMonsterCount > 0;
  58. /// <summary>Spawn monsters for this encounter based on a template.</summary>
  59. public void SpawnMonsters(Monster[] monsterTemplates, int minCount, int maxCount)
  60. {
  61. _monsters.Clear();
  62. int count = Random.Range(minCount, maxCount + 1);
  63. for (int i = 0; i < count; i++)
  64. {
  65. Monster template = monsterTemplates[Random.Range(0, monsterTemplates.Length)];
  66. _monsters.Add(new MonsterInstance(template));
  67. }
  68. // Agent sees 60-100% of the actual monsters (incomplete info)
  69. _visibleMonsterCount = Mathf.RoundToInt(AliveMonsterCount * Random.Range(0.6f, 1.0f));
  70. _visibleMonsterCount = Mathf.Max(1, _visibleMonsterCount); // At least see 1 if any exist
  71. }
  72. /// <summary>Mark a monster as defeated (called after combat resolution).</summary>
  73. public void DefeateMonster(int index)
  74. {
  75. if (index >= 0 && index < _monsters.Count)
  76. {
  77. _monsters[index].CharacterSheet.Stats.CurrentHealth = 0;
  78. }
  79. }
  80. /// <summary>
  81. /// Get the perceived challenge level from the agent's perspective.
  82. /// This is based on visible monsters only, not actual count.
  83. /// </summary>
  84. public float GetPerceivedChallengeRating(CharacterSheet agentStats)
  85. {
  86. if (_visibleMonsterCount == 0)
  87. return 0;
  88. // Simple calculation: sum the "threat" of visible monsters
  89. float totalThreat = 0;
  90. int counted = 0;
  91. foreach (var monster in _monsters)
  92. {
  93. if (monster.IsDefeated) continue;
  94. if (counted >= _visibleMonsterCount) break;
  95. float monsterThreat = monster.CharacterSheet.GetEffectiveAttack() +
  96. monster.CharacterSheet.Stats.CurrentHealth * 0.1f;
  97. totalThreat += monsterThreat;
  98. counted++;
  99. }
  100. float agentPower = agentStats.GetEffectiveAttack() + agentStats.GetEffectiveDefense();
  101. return agentPower > 0 ? totalThreat / agentPower : totalThreat;
  102. }
  103. /// <summary>
  104. /// Get the actual challenge rating (true difficulty, not visible to agent).
  105. /// This is what combat system would use internally.
  106. /// </summary>
  107. public float GetActualChallengeRating(CharacterSheet agentStats)
  108. {
  109. if (_monsters.Count == 0)
  110. return 0;
  111. float totalThreat = 0;
  112. foreach (var monster in _monsters)
  113. {
  114. if (monster.IsDefeated) continue;
  115. float monsterThreat = monster.CharacterSheet.GetEffectiveAttack() +
  116. monster.CharacterSheet.Stats.CurrentHealth * 0.1f;
  117. totalThreat += monsterThreat;
  118. }
  119. float agentPower = agentStats.GetEffectiveAttack() + agentStats.GetEffectiveDefense();
  120. return agentPower > 0 ? totalThreat / agentPower : totalThreat;
  121. }
  122. /// <summary>Reset all monsters' health for a retry.</summary>
  123. public void ResetEncounter()
  124. {
  125. foreach (var monster in _monsters)
  126. {
  127. monster.CharacterSheet.ResetForNewFight();
  128. }
  129. }
  130. }
  131. }