UnitComponent.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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: Component
  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. }
  29. public Unit Get(long id)
  30. {
  31. this.idUnits.TryGetValue(id, out Unit unit);
  32. return unit;
  33. }
  34. public void Remove(long id)
  35. {
  36. Unit unit;
  37. this.idUnits.TryGetValue(id, out unit);
  38. this.idUnits.Remove(id);
  39. unit?.Dispose();
  40. }
  41. public void RemoveNoDispose(long id)
  42. {
  43. this.idUnits.Remove(id);
  44. }
  45. public int Count
  46. {
  47. get
  48. {
  49. return this.idUnits.Count;
  50. }
  51. }
  52. public Unit[] GetAll()
  53. {
  54. return this.idUnits.Values.ToArray();
  55. }
  56. }
  57. }