/JTacticalSim.Component/Battle/Round.cs

https://github.com/Queztionmark/JTacticalSim · C# · 87 lines · 57 code · 15 blank · 15 comment · 8 complexity · a3ab94aba84351b62b7525690fd52b5c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Configuration;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using JTacticalSim.API;
  8. using JTacticalSim.API.Component;
  9. namespace JTacticalSim.Component.Battle
  10. {
  11. /// <summary>
  12. /// Represents a full round of battle
  13. /// </summary>
  14. public class Round : GameComponentBase, IRound
  15. {
  16. public event BattleRoundStart RoundStart;
  17. public event BattleRoundEnd RoundEnd;
  18. public event BattleSkirmishStart SkirmishStart;
  19. public event BattleSkirmishEnd SkirmishEnd;
  20. public ISkirmish CurrentSkirmish { get; private set; }
  21. private IBattle _battle { get; set; }
  22. public List<ISkirmish> Skirmishes { get; private set; }
  23. public Round(IBattle battle)
  24. {
  25. _battle = battle;
  26. Skirmishes = new List<ISkirmish>();
  27. }
  28. public void AddSkirmish(ISkirmish skirmish)
  29. {
  30. Skirmishes.Add(skirmish);
  31. skirmish.SkirmishEnd += SkirmishEnded;
  32. skirmish.SkirmishStart += SkirmishStarted;
  33. }
  34. public IEnumerable<IUnit> GetDefeatedUnits() { return Skirmishes.SelectMany(s => s.Destroyed); }
  35. public void DoBattle()
  36. {
  37. On_RoundStart(new EventArgs());
  38. Skirmishes.ForEach(s => s.DoBattle());
  39. On_RoundEnd(new EventArgs());
  40. }
  41. // Event Handlers
  42. public void On_RoundStart(EventArgs e)
  43. {
  44. if (RoundStart != null) RoundStart(this, e);
  45. }
  46. public void On_RoundEnd(EventArgs e)
  47. {
  48. if (RoundEnd != null) RoundEnd(this, e);
  49. }
  50. /// <summary>
  51. /// Pass up the skirmish so we can act at the UI level
  52. /// </summary>
  53. /// <param name="sender"></param>
  54. /// <param name="e"></param>
  55. public void SkirmishEnded(object sender, EventArgs e)
  56. {
  57. CurrentSkirmish = null;
  58. if (SkirmishEnd != null) SkirmishEnd(this, e);
  59. }
  60. /// <summary>
  61. /// Pass up the skirmish so we can act at the UI level
  62. /// </summary>
  63. /// <param name="sender"></param>
  64. /// <param name="e"></param>
  65. public void SkirmishStarted(object sender, EventArgs e)
  66. {
  67. // Set the current skirmish
  68. CurrentSkirmish = ((ISkirmish)sender);
  69. if (SkirmishStart != null) SkirmishStart(this, e);
  70. }
  71. }
  72. }