UnitComponent.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. using MongoDB.Bson.Serialization.Options;
  5. namespace ETModel
  6. {
  7. public class UnitComponent: Entity
  8. {
  9. [BsonElement]
  10. [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
  11. private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
  12. public override void Dispose()
  13. {
  14. if (this.IsDisposed)
  15. {
  16. return;
  17. }
  18. base.Dispose();
  19. foreach (Unit unit in this.idUnits.Values)
  20. {
  21. unit.Dispose();
  22. }
  23. this.idUnits.Clear();
  24. }
  25. public void Add(Unit unit)
  26. {
  27. this.idUnits.Add(unit.Id, unit);
  28. unit.Parent = this;
  29. }
  30. public Unit Get(long id)
  31. {
  32. this.idUnits.TryGetValue(id, out Unit unit);
  33. return unit;
  34. }
  35. public void Remove(long id)
  36. {
  37. Unit unit;
  38. this.idUnits.TryGetValue(id, out unit);
  39. this.idUnits.Remove(id);
  40. unit?.Dispose();
  41. }
  42. public void RemoveNoDispose(long id)
  43. {
  44. this.idUnits.Remove(id);
  45. }
  46. public int Count
  47. {
  48. get
  49. {
  50. return this.idUnits.Count;
  51. }
  52. }
  53. public Unit[] GetAll()
  54. {
  55. return this.idUnits.Values.ToArray();
  56. }
  57. }
  58. }