UnitComponent.cs 929 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 void RemoveNoDispose(long id)
  25. {
  26. this.idUnits.Remove(id);
  27. }
  28. public int Count
  29. {
  30. get
  31. {
  32. return this.idUnits.Count;
  33. }
  34. }
  35. public Unit[] GetAll()
  36. {
  37. return this.idUnits.Values.ToArray();
  38. }
  39. public override void Dispose()
  40. {
  41. if (this.Id == 0)
  42. {
  43. return;
  44. }
  45. base.Dispose();
  46. foreach (Unit unit in this.idUnits.Values)
  47. {
  48. unit.Dispose();
  49. }
  50. }
  51. }
  52. }