UnitComponent.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class UnitComponentEvent : ObjectEvent<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 void Add(Unit unit)
  23. {
  24. this.idUnits.Add(unit.Id, unit);
  25. }
  26. public Unit Get(long id)
  27. {
  28. Unit unit;
  29. this.idUnits.TryGetValue(id, out unit);
  30. return unit;
  31. }
  32. public void Remove(long id)
  33. {
  34. Unit unit;
  35. this.idUnits.TryGetValue(id, out unit);
  36. this.idUnits.Remove(id);
  37. unit?.Dispose();
  38. }
  39. public void RemoveNoDispose(long id)
  40. {
  41. this.idUnits.Remove(id);
  42. }
  43. public int Count
  44. {
  45. get
  46. {
  47. return this.idUnits.Count;
  48. }
  49. }
  50. public Unit[] GetAll()
  51. {
  52. return this.idUnits.Values.ToArray();
  53. }
  54. public override void Dispose()
  55. {
  56. if (this.Id == 0)
  57. {
  58. return;
  59. }
  60. base.Dispose();
  61. foreach (Unit unit in this.idUnits.Values)
  62. {
  63. unit.Dispose();
  64. }
  65. Instance = null;
  66. }
  67. }
  68. }