UnitComponent.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace ET
  4. {
  5. public class UnitComponentAwakeSystem : AwakeSystem<UnitComponent>
  6. {
  7. public override void Awake(UnitComponent self)
  8. {
  9. }
  10. }
  11. public class UnitComponentDestroySystem : DestroySystem<UnitComponent>
  12. {
  13. public override void Destroy(UnitComponent self)
  14. {
  15. foreach (Unit unit in self.idUnits.Values)
  16. {
  17. unit.Dispose();
  18. }
  19. self.idUnits.Clear();
  20. }
  21. }
  22. public static class UnitComponentSystem
  23. {
  24. public static void Add(this UnitComponent self, Unit unit)
  25. {
  26. self.idUnits.Add(unit.Id, unit);
  27. unit.Parent = self;
  28. }
  29. public static Unit Get(this UnitComponent self, long id)
  30. {
  31. Unit unit;
  32. self.idUnits.TryGetValue(id, out unit);
  33. return unit;
  34. }
  35. public static void Remove(this UnitComponent self, long id)
  36. {
  37. Unit unit;
  38. self.idUnits.TryGetValue(id, out unit);
  39. self.idUnits.Remove(id);
  40. unit?.Dispose();
  41. }
  42. public static void RemoveNoDispose(this UnitComponent self, long id)
  43. {
  44. self.idUnits.Remove(id);
  45. }
  46. public static Unit[] GetAll(this UnitComponent self)
  47. {
  48. return self.idUnits.Values.ToArray();
  49. }
  50. }
  51. }