UnitComponent.cs 853 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Model
  4. {
  5. public class UnitComponent: Component
  6. {
  7. private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
  8. public void Add(Unit unit)
  9. {
  10. this.idUnits.Add(unit.Id, unit);
  11. }
  12. public Unit Get(long id)
  13. {
  14. this.idUnits.TryGetValue(id, out Unit unit);
  15. return unit;
  16. }
  17. public void Remove(long id)
  18. {
  19. Unit unit;
  20. this.idUnits.TryGetValue(id, out unit);
  21. this.idUnits.Remove(id);
  22. unit?.Dispose();
  23. }
  24. public int Count
  25. {
  26. get
  27. {
  28. return this.idUnits.Count;
  29. }
  30. }
  31. public Unit[] GetAll()
  32. {
  33. return this.idUnits.Values.ToArray();
  34. }
  35. public override void Dispose()
  36. {
  37. if (this.Id == 0)
  38. {
  39. return;
  40. }
  41. base.Dispose();
  42. foreach (Unit unit in this.idUnits.Values)
  43. {
  44. unit.Dispose();
  45. }
  46. }
  47. }
  48. }