Combatant.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using UnityEngine;
  2. namespace MazeGen
  3. {
  4. /// <summary>
  5. /// One participant in a battle - either an agent or a monster instance - behind a single
  6. /// interface so <see cref="CombatResolver"/> doesn't need to branch on which it is.
  7. ///
  8. /// Wraps the *live* <see cref="CharacterSheet"/>, not a copy: resolving a fight mutates real
  9. /// agent and monster health, which is exactly what "only the result of the fight has changed
  10. /// for that room" means. <see cref="StartHealth"/> is snapshotted before resolution purely so
  11. /// the arena playback can animate health draining down from where it actually started.
  12. /// </summary>
  13. public class Combatant
  14. {
  15. public const int TeamAgents = 0;
  16. public const int TeamMonsters = 1;
  17. /// <summary>Index of this combatant within its own battle. Combat events refer to these.</summary>
  18. public readonly int Id;
  19. public readonly int Team;
  20. public readonly string Name;
  21. public readonly Color Color;
  22. /// <summary>Set for agent combatants, null for monsters.</summary>
  23. public readonly MazeAgent Agent;
  24. /// <summary>Set for monster combatants, null for agents.</summary>
  25. public readonly MonsterEncounter.MonsterInstance Monster;
  26. public readonly CharacterSheet Sheet;
  27. /// <summary>Health at the moment the battle was resolved, for playback purposes.</summary>
  28. public readonly float StartHealth;
  29. public bool IsAgent => Agent != null;
  30. public bool IsAlive => Sheet.Stats.IsAlive;
  31. /// <summary>
  32. /// Base max health, not GetEffectiveMaxHealth(): CharacterStats.CurrentHealth is clamped
  33. /// to the base value, so using the effective one here would give health bars that can
  34. /// never fill up whenever equipment grants a health bonus.
  35. /// </summary>
  36. public float MaxHealth => Mathf.Max(1f, Sheet.Stats.MaxHealth);
  37. private Combatant(int id, int team, string name, Color color, MazeAgent agent, MonsterEncounter.MonsterInstance monster, CharacterSheet sheet)
  38. {
  39. Id = id;
  40. Team = team;
  41. Name = name;
  42. Color = color;
  43. Agent = agent;
  44. Monster = monster;
  45. Sheet = sheet;
  46. StartHealth = sheet.Stats.CurrentHealth;
  47. }
  48. public static Combatant ForAgent(int id, MazeAgent agent, Color color)
  49. => new Combatant(id, TeamAgents, agent.CharacterSheet.Name, color, agent, null, agent.CharacterSheet);
  50. public static Combatant ForMonster(int id, MonsterEncounter.MonsterInstance monster)
  51. => new Combatant(id, TeamMonsters, monster.Template.MonsterName, monster.Template.Color, null, monster, monster.CharacterSheet);
  52. }
  53. }