FormTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using CommonAI.RTS;
  2. using CommonAI.Zone;
  3. using CommonAI.Zone.Instance;
  4. using CommonAI.Zone.ZoneEditor;
  5. using CommonAIClient.Client;
  6. using CommonFroms;
  7. using CommonFroms.G2D;
  8. using CommonFroms.Utils;
  9. using CommonLang;
  10. using CommonLang.Concurrent;
  11. using GameEditorPlugin;
  12. using GameEditorPlugin.Win32.BattleClient;
  13. using GameEditorPlugin.Win32.Runtime;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.ComponentModel;
  17. using System.Data;
  18. using System.Drawing;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading;
  23. using System.Windows.Forms;
  24. using ZeusServerNode.Node;
  25. namespace ZeusServerTest
  26. {
  27. public partial class FormTest : Form
  28. {
  29. private NodeTestServer server;
  30. private NodeTestClientGroup bots = new NodeTestClientGroup();
  31. private Random random = new Random();
  32. public FormTest()
  33. {
  34. InitializeComponent();
  35. this.pictureBoxRoomLayer.MouseWheel += new MouseEventHandler(pictureBoxRoomLayer_MouseWheel);
  36. this.listView_Nodes.ContextMenuStrip = menu_Node;
  37. this.server = new NodeTestServer(new DirectoryInfo(Application.StartupPath));
  38. }
  39. private void FormTest_Load(object sender, EventArgs e)
  40. {
  41. new Thread(server.Start).Start();
  42. }
  43. private void timer1_Tick(object sender, EventArgs e)
  44. {
  45. if (server.Running)
  46. {
  47. RefreshZoneNodes();
  48. }
  49. }
  50. private void timer2_Tick(object sender, EventArgs e)
  51. {
  52. if (server.Running)
  53. {
  54. UpdateZoneNodes();
  55. bots.Update();
  56. pictureBoxRoomLayer.Refresh();
  57. }
  58. }
  59. //----------------------------------------------------------------------------------------------------------------------
  60. #region _ZoneNodes列表刷新以及显示_
  61. private class ListViewTagComparer : System.Collections.IComparer
  62. {
  63. public int Compare(object x, object y)
  64. {
  65. ZoneNode stag = x as ZoneNode;
  66. BattleView dtag = y as BattleView;
  67. return (dtag.Node == stag) ? 0 : -1;
  68. }
  69. }
  70. private void RefreshZoneNodes()
  71. {
  72. this.lbl_Status.Text = server.Status;
  73. if (server.Running)
  74. {
  75. IList<ZoneNode> nodes = server.ListZoneNodes();
  76. if (!FormUtils.ListViewItemTagEquals(listView_Nodes, (System.Collections.IList)nodes, new ListViewTagComparer()))
  77. {
  78. listView_Nodes.SuspendLayout();
  79. listView_Nodes.Items.Clear();
  80. foreach (ZoneNode node in nodes)
  81. {
  82. ListViewItem nodeItem = new ListViewItem(node.SceneID.ToString());
  83. nodeItem.SubItems.Add(node.ToString());
  84. nodeItem.SubItems.Add(node.PlayerCount.ToString());
  85. nodeItem.Tag = new BattleView(node);
  86. listView_Nodes.Items.Add(nodeItem);
  87. }
  88. listView_Nodes.ResumeLayout();
  89. }
  90. else
  91. {
  92. foreach (ListViewItem nodeItem in listView_Nodes.Items)
  93. {
  94. ZoneNode node = (nodeItem.Tag as BattleView).Node;
  95. nodeItem.SubItems[1].Text = (node.ToString());
  96. nodeItem.SubItems[2].Text = (node.PlayerCount.ToString());
  97. }
  98. }
  99. }
  100. else
  101. {
  102. listView_Nodes.Items.Clear();
  103. }
  104. }
  105. private void UpdateZoneNodes()
  106. {
  107. this.lbl_Status.Text = server.Status;
  108. if (server.Running)
  109. {
  110. foreach (ListViewItem nodeItem in listView_Nodes.Items)
  111. {
  112. BattleView view = (nodeItem.Tag as BattleView);
  113. view.update();
  114. }
  115. }
  116. }
  117. public ZoneNode SelectedZoneNode()
  118. {
  119. if (listView_Nodes.SelectedItems.Count > 0)
  120. {
  121. return (listView_Nodes.SelectedItems[0].Tag as BattleView).Node;
  122. }
  123. return null;
  124. }
  125. public BattleView SelectedBattleView()
  126. {
  127. if (listView_Nodes.SelectedItems.Count > 0)
  128. {
  129. return listView_Nodes.SelectedItems[0].Tag as BattleView;
  130. }
  131. return null;
  132. }
  133. //----------------------------------------------------------------------------------------------------------------------
  134. private void listView_Nodes_SelectedIndexChanged(object sender, EventArgs e)
  135. {
  136. BattleView bv = SelectedBattleView();
  137. if (bv != null)
  138. {
  139. float scale = Math.Max(
  140. ((float)pictureBoxRoomLayer.Width) / bv.Width,
  141. ((float)pictureBoxRoomLayer.Height) / bv.Height);
  142. bv.setCamera(bv.Width / 2, bv.Height / 2);
  143. bv.setCameraScale(scale, scale);
  144. }
  145. }
  146. #endregion
  147. //----------------------------------------------------------------------------------------------------------------------
  148. #region __BattleView__
  149. public class BattleLocalView : BattleLocal
  150. {
  151. private EditorScene mZone;
  152. public BattleLocalView(EditorScene zone)
  153. : base(ZoneNodeManager.Templates)
  154. {
  155. this.mZone = zone;
  156. this.Layer.ProcessMessage(new CommonAI.ZoneClient.ClientEnterScene(mZone.Data.ID, mZone.SpaceDIV, ZoneNodeManager.Templates.Templates.ResourceVersion));
  157. }
  158. public override EditorScene Zone
  159. {
  160. get { return mZone; }
  161. }
  162. }
  163. public class BattleView : DisplayBattleLocal<BattleLocalView>
  164. {
  165. public ZoneNode Node { get { return room; } }
  166. private ZoneNode room;
  167. private long mLastUpdateTime;
  168. public BattleView(ZoneNode room)
  169. {
  170. base.ShowGuardRange = false;
  171. base.ShowAttackRange = false;
  172. base.ShowDamageRange = false;
  173. base.ShowName = true;
  174. base.ShowHP = true;
  175. base.ShowGrid = true;
  176. this.room = room;
  177. base.InitClient(new BattleLocalView(room.Zone));
  178. this.room.Zone.OnPostEvent += zone_OnPostEvent;
  179. this.room.Zone.OnUpdate += zone_OnUpdate;
  180. }
  181. private void zone_OnPostEvent(InstanceZone zone, Event e)
  182. {
  183. Battle.onEventHandler(e);
  184. }
  185. private void zone_OnUpdate(CommonAI.Zone.Instance.InstanceZone zone)
  186. {
  187. SyncPosEvent.UnitPos pos = new SyncPosEvent.UnitPos();
  188. foreach (CommonAI.Zone.Instance.InstanceZoneObject so in room.Zone.allObjs())
  189. {
  190. CommonAI.ZoneClient.ZoneObject co = Client.Layer.GetObject(so.ID);
  191. if (co != null)
  192. {
  193. pos.ID = so.ID;
  194. pos.X = so.X;
  195. pos.Y = so.Y;
  196. pos.Z = so.Z;
  197. pos.Direction = so.Direction;
  198. if (so is CommonAI.Zone.Instance.InstanceUnit)
  199. {
  200. CommonAI.Zone.Instance.InstanceUnit su = so as CommonAI.Zone.Instance.InstanceUnit;
  201. pos.ST = (byte)su.CurrentActionStatus;
  202. }
  203. else
  204. {
  205. pos.ST = 0;
  206. }
  207. co.SyncPos(pos);
  208. }
  209. }
  210. }
  211. protected override void clientUpdate(int intervalMS)
  212. {
  213. Battle.BeginUpdate(intervalMS);
  214. Battle.Update(intervalMS);
  215. }
  216. public void update()
  217. {
  218. long curTime = CommonLang.CUtils.CurrentTimeMS;
  219. if (mLastUpdateTime == 0)
  220. {
  221. mLastUpdateTime = curTime;
  222. }
  223. int intervalMS = (int)(curTime - mLastUpdateTime);
  224. this.mLastUpdateTime = curTime;
  225. base.update(intervalMS);
  226. }
  227. }
  228. private Vector2 pic_lastMouesDown;
  229. private Vector2 pic_lastCameraPos;
  230. private void pictureBoxRoomLayer_MouseWheel(object sender, MouseEventArgs e)
  231. {
  232. try
  233. {
  234. BattleView bv = SelectedBattleView();
  235. if (bv != null)
  236. {
  237. int d = CMath.getDirect(e.Delta);
  238. if (d > 0)
  239. {
  240. float newD = bv.getCameraScale() * 1.1f;
  241. bv.setCameraScale(newD, newD);
  242. }
  243. else if (d < 0)
  244. {
  245. float newD = bv.getCameraScale() / 1.1f;
  246. bv.setCameraScale(newD, newD);
  247. }
  248. }
  249. }
  250. catch (Exception err)
  251. {
  252. Console.WriteLine(err.Message);
  253. }
  254. }
  255. private void pictureBoxRoomLayer_MouseDown(object sender, MouseEventArgs e)
  256. {
  257. pictureBoxRoomLayer.Focus();
  258. BattleView bv = SelectedBattleView();
  259. if (bv != null)
  260. {
  261. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  262. {
  263. pic_lastMouesDown = new Vector2(e.Location.X, e.Location.Y);
  264. pic_lastCameraPos = new Vector2(bv.CameraX, bv.CameraY);
  265. }
  266. }
  267. }
  268. private void pictureBoxRoomLayer_MouseUp(object sender, MouseEventArgs e)
  269. {
  270. pic_lastMouesDown = null;
  271. pic_lastCameraPos = null;
  272. }
  273. private void pictureBoxRoomLayer_MouseMove(object sender, MouseEventArgs e)
  274. {
  275. BattleView bv = SelectedBattleView();
  276. if (bv != null)
  277. {
  278. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  279. {
  280. if (pic_lastMouesDown != null)
  281. {
  282. float x = pic_lastCameraPos.x + bv.screenToWorldSizeX(pic_lastMouesDown.X - e.X);
  283. float y = pic_lastCameraPos.y + bv.screenToWorldSizeY(pic_lastMouesDown.Y - e.Y);
  284. bv.setCamera(x, y);
  285. }
  286. }
  287. }
  288. }
  289. private void pictureBoxRoomLayer_Paint(object sender, PaintEventArgs e)
  290. {
  291. try
  292. {
  293. BattleView bv = SelectedBattleView();
  294. if (bv != null)
  295. {
  296. bv.setWindow(new RectangleF(0, 0, pictureBoxRoomLayer.Width, pictureBoxRoomLayer.Height));
  297. bv.render(e.Graphics);
  298. }
  299. }
  300. catch (Exception err)
  301. {
  302. Console.WriteLine(err.Message);
  303. }
  304. }
  305. #endregion
  306. //----------------------------------------------------------------------------------------------------------------------
  307. #region __测试客户端__
  308. private void menuBtn_AddPlayer_Click(object sender, EventArgs e)
  309. {
  310. ZoneNode node = SelectedZoneNode();
  311. if (node != null)
  312. {
  313. AddTestClient(node);
  314. }
  315. }
  316. private void menuBtn_AddBots_Click(object sender, EventArgs e)
  317. {
  318. ZoneNode node = SelectedZoneNode();
  319. if (node != null)
  320. {
  321. AddTestClientGroup(node);
  322. }
  323. }
  324. private void AddTestClient(ZoneNode node)
  325. {
  326. string tempID = G2DTextDialog.Show(21.ToString(), "输入单位模板ID");
  327. int unit_template_id;
  328. if (tempID != null && int.TryParse(tempID, out unit_template_id))
  329. {
  330. try
  331. {
  332. NodeTestBattleClient client = server.GenBattleClient(node, unit_template_id);
  333. new FormTestClient(client).Show();
  334. }
  335. catch (Exception err)
  336. {
  337. MessageBox.Show(err.Message);
  338. }
  339. }
  340. }
  341. private void AddTestClientGroup(ZoneNode node)
  342. {
  343. string count = G2DTextDialog.Show(10.ToString(), "添加单位数量");
  344. int bot_count;
  345. if (count != null && int.TryParse(count, out bot_count))
  346. {
  347. try
  348. {
  349. for (int i = 0; i < bot_count; i++)
  350. {
  351. UnitInfo temp = CUtils.GetRandomInArray<UnitInfo>(server.PlayerTemplates, random);
  352. NodeTestBattleClient client = server.GenBattleClient(node, temp.ID);
  353. bots.AddClient(client);
  354. }
  355. }
  356. catch (Exception err)
  357. {
  358. MessageBox.Show(err.Message);
  359. }
  360. }
  361. }
  362. #endregion
  363. }
  364. }