UnitComponent.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace ET
  4. {
  5. public class UnitComponentSystem : AwakeSystem<UnitComponent>
  6. {
  7. public override void Awake(UnitComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. public class UnitComponent: Entity
  13. {
  14. public static UnitComponent Instance { get; private set; }
  15. public Unit MyUnit;
  16. private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
  17. public void Awake()
  18. {
  19. Instance = this;
  20. }
  21. public override void Dispose()
  22. {
  23. if (this.IsDisposed)
  24. {
  25. return;
  26. }
  27. base.Dispose();
  28. foreach (Unit unit in this.idUnits.Values)
  29. {
  30. unit.Dispose();
  31. }
  32. this.idUnits.Clear();
  33. Instance = null;
  34. }
  35. public void Add(Unit unit)
  36. {
  37. this.idUnits.Add(unit.Id, unit);
  38. unit.Parent = this;
  39. }
  40. public Unit Get(long id)
  41. {
  42. Unit unit;
  43. this.idUnits.TryGetValue(id, out unit);
  44. return unit;
  45. }
  46. public void Remove(long id)
  47. {
  48. Unit unit;
  49. this.idUnits.TryGetValue(id, out unit);
  50. this.idUnits.Remove(id);
  51. unit?.Dispose();
  52. }
  53. public void RemoveNoDispose(long id)
  54. {
  55. this.idUnits.Remove(id);
  56. }
  57. public int Count
  58. {
  59. get
  60. {
  61. return this.idUnits.Count;
  62. }
  63. }
  64. public Unit[] GetAll()
  65. {
  66. return this.idUnits.Values.ToArray();
  67. }
  68. }
  69. }