UnitComponent.cs 1.1 KB

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