ActorComponent.cs 1.0 KB

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