GamePanelContainer.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using DeepMMO.Client.Win32.Forms;
  5. using DeepMMO.Protocol;
  6. using DeepMMO.Protocol.Client;
  7. using DeepEditor.Common.Net;
  8. using DeepEditor.Common.Drawing;
  9. namespace DeepMMO.Client.Win32.Battle
  10. {
  11. public partial class GamePanelContainer : UserControl, FormSessionTracer.ISession
  12. {
  13. public RPGClient Client { get; private set; }
  14. public GamePanel BattlePanel { get; private set; }
  15. public FormSessionTracer SessionView { get; private set; }
  16. public bool AutoUpdateBattleClient { get; set; } = true;
  17. public GamePanelContainer()
  18. {
  19. InitializeComponent();
  20. this.Disposed += GamePanelContainer_Disposed;
  21. }
  22. public void Init(RPGClient client)
  23. {
  24. this.Client = client;
  25. this.Client.OnZoneChanged += Client_OnZoneChanged;
  26. this.Client.OnZoneLeaved += Client_OnZoneLeaved;
  27. this.Client.OnGameDisconnected += Client_OnGameDisconnected1;
  28. this.Client.OnError += Client_OnError;
  29. this.SessionView = new FormSessionTracer(this);
  30. this.SessionView.ShowInTaskbar = false;
  31. this.SessionView.FormClosing += (object sender, FormClosingEventArgs e) =>
  32. {
  33. if (this.Visible)
  34. {
  35. e.Cancel = true;
  36. SessionView.Hide();
  37. }
  38. };
  39. }
  40. long FormSessionTracer.ISession.TotalRecvBytes
  41. {
  42. get { return this.Client.GameClient.TotalRecvBytes; }
  43. }
  44. long FormSessionTracer.ISession.TotalSentBytes
  45. {
  46. get { return this.Client.GameClient.TotalSentBytes; }
  47. }
  48. string FormSessionTracer.ISession.Title
  49. {
  50. get { return string.Format("GameClient-{0} : Time={1}", Client.GameClient.Name, DateTime.Now - Client.GameClient.ConnectTime); }
  51. }
  52. //------------------------------------------------------------------------------------------------------
  53. public void UpdateBattle(int intervalMS)
  54. {
  55. if (BattlePanel != null)
  56. {
  57. //battle_view.updateBattle(intervalMS);
  58. this.lbl_ZoneUUID.Text = BattlePanel.ToString();
  59. }
  60. }
  61. //------------------------------------------------------------------------------------------------------
  62. public void do_login()
  63. {
  64. Client.GameClient.Disconnect();
  65. if (new FormLogin(Client).ShowDialog(this) == DialogResult.OK)
  66. {
  67. if (new FormRoles(Client).ShowDialog(this) == DialogResult.OK)
  68. {
  69. btn_Login.Enabled = false;
  70. }
  71. else
  72. {
  73. Client.GameClient.Disconnect();
  74. btn_Login.Enabled = true;
  75. }
  76. }
  77. else
  78. {
  79. btn_Login.Enabled = true;
  80. }
  81. }
  82. public void do_logout()
  83. {
  84. this.Client.GameClient.Request<ClientExitGameResponse>(new ClientExitGameRequest(), (err2, rsp2) =>
  85. {
  86. if (Response.CheckSuccess(rsp2))
  87. {
  88. Client.GameClient.Disconnect();
  89. }
  90. else
  91. {
  92. Client_OnErrorResponse(rsp2, err2);
  93. }
  94. });
  95. }
  96. public void do_reconnect()
  97. {
  98. Client.GameClient.Disconnect();
  99. Client.Connect_Connect((rsp1) =>
  100. {
  101. if (Response.CheckSuccess(rsp1))
  102. {
  103. this.Client.GameClient.Request<ClientEnterGameResponse>(new ClientEnterGameRequest()
  104. {
  105. c2s_roleUUID = Client.LastRoleData.uuid
  106. },
  107. (err2, rsp2) =>
  108. {
  109. if (Response.CheckSuccess(rsp2))
  110. {
  111. }
  112. else
  113. {
  114. Client_OnErrorResponse(rsp2, err2);
  115. }
  116. });
  117. }
  118. else
  119. {
  120. Client_OnErrorResponse(rsp1);
  121. }
  122. });
  123. }
  124. //------------------------------------------------------------------------------------------------------
  125. #region 游戏客户端事件
  126. protected virtual void Client_OnErrorResponse(Response rsp, Exception err = null)
  127. {
  128. var prefix = (rsp != null) ? (rsp.GetType().Name + " : " + rsp + Environment.NewLine) : "";
  129. var suffix = (err != null) ? (err.Message + Environment.NewLine + err.StackTrace) : "";
  130. MessageBox.Show(prefix + suffix, "Client_Response");
  131. }
  132. protected virtual void Client_OnError(Exception err)
  133. {
  134. err.ShowMessageBox();
  135. }
  136. protected virtual void Client_OnGameDisconnected1(DeepCore.FuckPomeloClient.PomeloClient arg1, DeepCore.FuckPomeloClient.CloseReason arg2)
  137. {
  138. btn_Login.Enabled = true;
  139. }
  140. protected virtual void Client_OnZoneChanged(DeepMMO.Client.Battle.RPGBattleClient obj)
  141. {
  142. this.SuspendLayout();
  143. if (BattlePanel != null)
  144. {
  145. this.panel1.Controls.Remove(this.BattlePanel);
  146. BattlePanel.Dispose();
  147. BattlePanel = null;
  148. }
  149. this.BattlePanel = new GamePanel(this, obj);
  150. this.BattlePanel.Dock = DockStyle.Fill;
  151. this.BattlePanel.BattleView.OnDrawHUD += BattleView_OnDrawHUD;
  152. this.BattlePanel.BattleView.AutoUpdateBattleClient = this.AutoUpdateBattleClient;
  153. this.ResumeLayout(false);
  154. this.panel1.Controls.Add(BattlePanel);
  155. }
  156. private void BattleView_OnDrawHUD(DeepEditor.Common.G3D.GLView v, Graphics g)
  157. {
  158. g.DrawString("NetPing=" + Client.GameClient.CurrentPing, DefaultFont, Brushes.White, new Point(10, 200));
  159. }
  160. protected virtual void Client_OnZoneLeaved(DeepMMO.Client.Battle.RPGBattleClient obj)
  161. {
  162. this.SuspendLayout();
  163. if (BattlePanel != null)
  164. {
  165. this.panel1.Controls.Remove(this.BattlePanel);
  166. BattlePanel.Dispose();
  167. BattlePanel = null;
  168. }
  169. this.ResumeLayout(false);
  170. }
  171. #endregion
  172. //------------------------------------------------------------------------------------------------------
  173. #region 窗体事件
  174. private void GamePanelContainer_Disposed(object sender, EventArgs e)
  175. {
  176. this.SessionView.Dispose();
  177. this.Client.Dispose();
  178. }
  179. #endregion
  180. //------------------------------------------------------------------------------------------------------
  181. #region 功能按钮
  182. //protected FormMail formMail = null;
  183. //private FormWriteMail formWriteMail = null;
  184. //protected Form formBag = null;
  185. //private Form formShop = null;
  186. //private FormChat formChat = null;
  187. private void menu_Main_Click(object sender, EventArgs e)
  188. {
  189. }
  190. protected virtual void btn_Login_Click(object sender, EventArgs e)
  191. {
  192. do_login();
  193. }
  194. private void btn_Logout_Click(object sender, EventArgs e)
  195. {
  196. do_logout();
  197. }
  198. private void btn_Reconnect_Click(object sender, EventArgs e)
  199. {
  200. do_reconnect();
  201. }
  202. // private void btn_Chat_Click(object sender, EventArgs e)
  203. // {
  204. // if (formChat == null)
  205. // {
  206. // formChat = new FormChat(client);
  207. // }
  208. // formChat.Show();
  209. // }
  210. // private void btn_TC_Click(object sender, EventArgs e)
  211. // {
  212. // }
  213. // private void btn_Mail_Click(object sender, EventArgs e)
  214. // {
  215. // if (formMail == null)
  216. // {
  217. // formMail = new FormMail(client);
  218. // }
  219. // formMail.Show();
  220. // }
  221. // private void btn_Bag_Click(object sender, EventArgs e)
  222. // {
  223. // if (formBag == null || formBag.IsDisposed)
  224. // {
  225. // //formBag = new FormBag(client);
  226. // }
  227. // formBag.Show();
  228. // }
  229. // private void btn_Shop_Click(object sender, EventArgs e)
  230. // {
  231. // if (formShop == null || formBag.IsDisposed)
  232. // {
  233. // formShop = new FormStore(client);
  234. // }
  235. // formShop.Show();
  236. // }
  237. #endregion
  238. //------------------------------------------------------------------------------------------------------
  239. }
  240. }