UnitComponent.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class UnitComponentSystem : ObjectSystem<UnitComponent>, IAwake
  7. {
  8. public void Awake()
  9. {
  10. this.Get().Awake();
  11. }
  12. }
  13. public class UnitComponent: Component
  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.Id == 0)
  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. }
  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. }