ActorComponent.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using Base;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class ActorComponentEvent : ObjectEvent<ActorComponent>, IAwake
  7. {
  8. public void Awake()
  9. {
  10. this.Get().Awake();
  11. }
  12. }
  13. /// <summary>
  14. /// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server
  15. /// </summary>
  16. public class ActorComponent: Component
  17. {
  18. private long actorId;
  19. public async void Awake()
  20. {
  21. try
  22. {
  23. this.actorId = this.Owner.Id;
  24. Game.Scene.GetComponent<ActorManagerComponent>().Add(this.Owner);
  25. await Game.Scene.GetComponent<LocationProxyComponent>().Add(this.actorId);
  26. }
  27. catch (Exception e)
  28. {
  29. Log.Error(e.ToString());
  30. }
  31. }
  32. public override async void Dispose()
  33. {
  34. try
  35. {
  36. if (this.Id == 0)
  37. {
  38. return;
  39. }
  40. base.Dispose();
  41. Game.Scene.GetComponent<ActorManagerComponent>().Remove(actorId);
  42. await Game.Scene.GetComponent<LocationProxyComponent>().Remove(this.actorId);
  43. }
  44. catch (Exception e)
  45. {
  46. Log.Error(e.ToString());
  47. }
  48. }
  49. }
  50. }