RPGClient.Area.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using DeepMMO.Client.Battle;
  2. using DeepMMO.Protocol.Client;
  3. using System;
  4. using DeepCore.Game3D.Slave.Layer;
  5. namespace DeepMMO.Client
  6. {
  7. public partial class RPGClient
  8. {
  9. protected RPGBattleClient current_battle;
  10. protected RPGBattleClient next_battle;
  11. public RPGBattleClient CurrentBattle
  12. {
  13. get { return current_battle; }
  14. }
  15. public RPGBattleClient NextBattle
  16. {
  17. get { return next_battle; }
  18. }
  19. public int CurrentBattlePing
  20. {
  21. get { return current_battle != null ? current_battle.CurrentPing : 0; }
  22. }
  23. public LayerZone CurrentZoneLayer
  24. {
  25. get { return current_battle != null ? current_battle.Layer : null; }
  26. }
  27. public LayerPlayer CurrentZoneActor
  28. {
  29. get { return current_battle != null ? current_battle.Actor : null; }
  30. }
  31. public bool IsDelayReleaseBattleClient { get; set; }
  32. protected virtual void Area_Init()
  33. {
  34. this.game_client.Listen<ClientEnterZoneNotify>(Area_OnClientEnterZoneNotify);
  35. this.game_client.Listen<ClientLeaveZoneNotify>(Area_OnClientLeaveZoneNotify);
  36. this.game_client.Listen<ClientBattleEvent>(Area_OnClientBattleEvent);
  37. }
  38. protected virtual void Area_OnClientBattleEvent(ClientBattleEvent notify)
  39. {
  40. if (next_battle != null)
  41. {
  42. next_battle.OnReceived(notify);
  43. }
  44. else if (current_battle != null)
  45. {
  46. current_battle.OnReceived(notify);
  47. }
  48. else
  49. {
  50. throw new Exception("Battle Not Init !!!");
  51. }
  52. }
  53. protected virtual void Area_OnClientEnterZoneNotify(ClientEnterZoneNotify notify)
  54. {
  55. if (!IsDelayReleaseBattleClient && current_battle != null)
  56. {
  57. current_battle.Dispose();
  58. current_battle = null;
  59. }
  60. log.Info("ClientEnterZoneNotify : " + notify);
  61. var battle = CreateBattle(notify);
  62. battle.Layer.ActorAdded += Layer_ActorAdded;
  63. if (current_battle == null || !IsDelayReleaseBattleClient)
  64. {
  65. current_battle = battle;
  66. }
  67. else
  68. {
  69. next_battle = battle;
  70. }
  71. if (event_OnZoneChanged != null) event_OnZoneChanged(battle);
  72. }
  73. protected virtual void Area_OnClientLeaveZoneNotify(ClientLeaveZoneNotify notify)
  74. {
  75. log.Info("ClientLeaveZoneNotify : " + notify);
  76. if (event_OnZoneLeaved != null) event_OnZoneLeaved(current_battle);
  77. if (current_battle != null) { current_battle.Dispose(); }
  78. }
  79. protected virtual void Layer_ActorAdded(LayerZone layer, LayerPlayer actor)
  80. {
  81. if (next_battle != null)
  82. {
  83. if (current_battle != null)
  84. {
  85. current_battle.Dispose();
  86. }
  87. current_battle = next_battle;
  88. next_battle = null;
  89. }
  90. if (event_OnZoneActorEntered != null)
  91. event_OnZoneActorEntered(actor);
  92. }
  93. protected virtual RPGBattleClient CreateBattle(ClientEnterZoneNotify sd)
  94. {
  95. return new RPGBattleClient(this, sd);
  96. }
  97. protected virtual void Area_Disposing()
  98. {
  99. event_OnZoneChanged = null;
  100. event_OnZoneLeaved = null;
  101. event_OnZoneActorEntered = null;
  102. }
  103. private Action<RPGBattleClient> event_OnZoneChanged;
  104. private Action<RPGBattleClient> event_OnZoneLeaved;
  105. private Action<LayerPlayer> event_OnZoneActorEntered;
  106. public event Action<RPGBattleClient> OnZoneChanged { add { event_OnZoneChanged += value; } remove { event_OnZoneChanged -= value; } }
  107. public event Action<RPGBattleClient> OnZoneLeaved { add { event_OnZoneLeaved += value; } remove { event_OnZoneLeaved -= value; } }
  108. public event Action<LayerPlayer> OnZoneActorEntered { add { event_OnZoneActorEntered += value; } remove { event_OnZoneActorEntered -= value; } }
  109. }
  110. }