UnitComponent.cs 1.3 KB

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