ActorLocationSenderComponent.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ETModel
  4. {
  5. public class ActorLocationSenderComponent: Component
  6. {
  7. public readonly Dictionary<long, ActorLocationSender> ActorLocationSenders = new Dictionary<long, ActorLocationSender>();
  8. public override void Dispose()
  9. {
  10. if (this.IsDisposed)
  11. {
  12. return;
  13. }
  14. base.Dispose();
  15. foreach (ActorLocationSender actorLocationSender in this.ActorLocationSenders.Values)
  16. {
  17. actorLocationSender.Dispose();
  18. }
  19. this.ActorLocationSenders.Clear();
  20. }
  21. public ActorLocationSender Get(long id)
  22. {
  23. if (id == 0)
  24. {
  25. throw new Exception($"actor id is 0");
  26. }
  27. if (this.ActorLocationSenders.TryGetValue(id, out ActorLocationSender actorLocationSender))
  28. {
  29. return actorLocationSender;
  30. }
  31. actorLocationSender = ComponentFactory.CreateWithId<ActorLocationSender>(id);
  32. actorLocationSender.Parent = this;
  33. this.ActorLocationSenders[id] = actorLocationSender;
  34. return actorLocationSender;
  35. }
  36. public void Remove(long id)
  37. {
  38. if (!this.ActorLocationSenders.TryGetValue(id, out ActorLocationSender actorMessageSender))
  39. {
  40. return;
  41. }
  42. this.ActorLocationSenders.Remove(id);
  43. actorMessageSender.Dispose();
  44. }
  45. }
  46. }