MonsterEncounter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. public MonsterInstance(Monster template)
  17. {
  18. Template = template;
  19. CharacterSheet = template.CreateCharacterSheet();
  20. }
  21. }
  22. private List<MonsterInstance> _monsters = new List<MonsterInstance>();
  23. private int _visibleMonsterCount; // Partial info: agent sees this many, not the full count
  24. public IReadOnlyList<MonsterInstance> Monsters => _monsters.AsReadOnly();
  25. public int AliveMonsterCount
  26. {
  27. get
  28. {
  29. int count = 0;
  30. foreach (var m in _monsters)
  31. if (!m.IsDefeated) count++;
  32. return count;
  33. }
  34. }
  35. /// <summary>
  36. /// How many monsters the agent can actually see in the room.
  37. /// This is less than or equal to AliveMonsterCount (incomplete information).
  38. /// </summary>
  39. public int VisibleMonsterCount => _visibleMonsterCount;
  40. public bool HasLivingMonsters => AliveMonsterCount > 0;
  41. /// <summary>Spawn monsters for this encounter based on a template.</summary>
  42. public void SpawnMonsters(Monster[] monsterTemplates, int minCount, int maxCount)
  43. {
  44. _monsters.Clear();
  45. int count = Random.Range(minCount, maxCount + 1);
  46. for (int i = 0; i < count; i++)
  47. {
  48. Monster template = monsterTemplates[Random.Range(0, monsterTemplates.Length)];
  49. _monsters.Add(new MonsterInstance(template));
  50. }
  51. // Agent sees 60-100% of the actual monsters (incomplete info)
  52. _visibleMonsterCount = Mathf.RoundToInt(AliveMonsterCount * Random.Range(0.6f, 1.0f));
  53. _visibleMonsterCount = Mathf.Max(1, _visibleMonsterCount); // At least see 1 if any exist
  54. }
  55. /// <summary>Mark a monster as defeated (called after combat resolution).</summary>
  56. public void DefeateMonster(int index)
  57. {
  58. if (index >= 0 && index < _monsters.Count)
  59. {
  60. _monsters[index].CharacterSheet.Stats.CurrentHealth = 0;
  61. }
  62. }
  63. /// <summary>
  64. /// Get the perceived challenge level from the agent's perspective.
  65. /// This is based on visible monsters only, not actual count.
  66. /// </summary>
  67. public float GetPerceivedChallengeRating(CharacterSheet agentStats)
  68. {
  69. if (_visibleMonsterCount == 0)
  70. return 0;
  71. // Simple calculation: sum the "threat" of visible monsters
  72. float totalThreat = 0;
  73. int counted = 0;
  74. foreach (var monster in _monsters)
  75. {
  76. if (monster.IsDefeated) continue;
  77. if (counted >= _visibleMonsterCount) break;
  78. float monsterThreat = monster.CharacterSheet.GetEffectiveAttack() +
  79. monster.CharacterSheet.Stats.CurrentHealth * 0.1f;
  80. totalThreat += monsterThreat;
  81. counted++;
  82. }
  83. float agentPower = agentStats.GetEffectiveAttack() + agentStats.GetEffectiveDefense();
  84. return agentPower > 0 ? totalThreat / agentPower : totalThreat;
  85. }
  86. /// <summary>
  87. /// Get the actual challenge rating (true difficulty, not visible to agent).
  88. /// This is what combat system would use internally.
  89. /// </summary>
  90. public float GetActualChallengeRating(CharacterSheet agentStats)
  91. {
  92. if (_monsters.Count == 0)
  93. return 0;
  94. float totalThreat = 0;
  95. foreach (var monster in _monsters)
  96. {
  97. if (monster.IsDefeated) continue;
  98. float monsterThreat = monster.CharacterSheet.GetEffectiveAttack() +
  99. monster.CharacterSheet.Stats.CurrentHealth * 0.1f;
  100. totalThreat += monsterThreat;
  101. }
  102. float agentPower = agentStats.GetEffectiveAttack() + agentStats.GetEffectiveDefense();
  103. return agentPower > 0 ? totalThreat / agentPower : totalThreat;
  104. }
  105. /// <summary>Reset all monsters' health for a retry.</summary>
  106. public void ResetEncounter()
  107. {
  108. foreach (var monster in _monsters)
  109. {
  110. monster.CharacterSheet.ResetForNewFight();
  111. }
  112. }
  113. }
  114. }