BattleMgr_Cmd.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using CommonAI.Zone;
  2. using CommonLang.Geometry;
  3. using ET.Client;
  4. using ET.EventType;
  5. using ET.Server;
  6. using System;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using Random = System.Random;
  10. using Vector2 = CommonLang.Geometry.Vector2;
  11. namespace ET
  12. {
  13. [Event]
  14. public class BattleFuncHandler : BEvent<BattleFunc>
  15. {
  16. protected override async ETTask OnEvent(BattleFunc a)
  17. {
  18. if(PlayerComponent.Instance == null || PlayerComponent.Instance.ClientScene() == null || PlayerComponent.Instance.ClientScene().GetComponent<SessionComponent>() == null)
  19. {
  20. return;
  21. }
  22. Log.Debug($"Battle Func:{a.FuncIndex}");
  23. var session = PlayerComponent.Instance.ClientScene().GetComponent<SessionComponent>().Session;
  24. switch (a.FuncIndex)
  25. {
  26. case (int)BattleFunc.FUNC.ClientIsReady:
  27. session.Call(new C2G_BattleNotify()
  28. {
  29. Message = BattleNotify.ClientIsReady.ToString()
  30. }).Coroutine();
  31. break;
  32. case (int)BattleFunc.FUNC.Start:
  33. session.Call(new C2G_BattleNotify()
  34. {
  35. Message = BattleNotify.StartRefreshMonster.ToString()
  36. }).Coroutine();
  37. break;
  38. case (int)BattleFunc.FUNC.Start2:
  39. session.Call(new C2G_BattleNotify()
  40. {
  41. Message = BattleNotify.StartRefreshMonster2.ToString()
  42. }).Coroutine();
  43. break;
  44. case (int)BattleFunc.FUNC.Start3:
  45. session.Call(new C2G_BattleNotify()
  46. {
  47. Message = BattleNotify.StartRefreshMonster3.ToString()
  48. }).Coroutine();
  49. break;
  50. case (int)KeyCode.F1:
  51. var units = new int[] { 101, 111, 121, 131 };
  52. var rand = new Random();
  53. foreach (var id in units)
  54. {
  55. var pos = GetRandomPos();
  56. session.Call(new C2G_AddUnitsToMap()
  57. {
  58. UnitId = id,
  59. Force = 1,
  60. X = (int)pos.X,
  61. Y = (int)pos.Y
  62. }).Coroutine();
  63. }
  64. break;
  65. /*case (int)KeyCode.F2:
  66. var units2 = new int[] { 102, 112, 122, 132 };
  67. rand = new Random();
  68. for (int i = 0; i < 2; i++)
  69. {
  70. var pos = GetRandomPos();
  71. session.Call(new C2G_AddUnitsToMap()
  72. {
  73. UnitId = units2[rand.Next(units2.Length)],
  74. Force = 1,
  75. X = (int)pos.X,
  76. Y = (int)pos.Y
  77. }).Coroutine();
  78. }
  79. break;
  80. case (int)KeyCode.F3:
  81. var units3 = new int[] { 103, 113, 123, 133 };
  82. rand = new Random();
  83. for (int i = 0; i < 1; i++)
  84. {
  85. var pos = GetRandomPos();
  86. session.Call(new C2G_AddUnitsToMap()
  87. {
  88. UnitId = units3[rand.Next(units3.Length)],
  89. Force = 1,
  90. X = (int)pos.X,
  91. Y = (int)pos.Y
  92. }).Coroutine();
  93. }
  94. break;*/
  95. case (int)KeyCode.F2:
  96. session.Call(new C2G_BattleNotify() { Message = "test:1" }).Coroutine();
  97. break;
  98. case (int)KeyCode.F3:
  99. session.Call(new C2G_BattleNotify() { Message = "test:2" }).Coroutine();
  100. break;
  101. case (int)KeyCode.F4:
  102. session.Call(new C2G_BattleNotify() { Message = "test:3" }).Coroutine();
  103. break;
  104. case (int)KeyCode.F5:
  105. session.Call(new C2G_BattleNotify() { Message = "test:4" }).Coroutine();
  106. break;
  107. case (int)KeyCode.F6:
  108. session.Call(new C2G_BattleNotify() { Message = "test:5" }).Coroutine();
  109. break;
  110. case (int)KeyCode.F7:
  111. session.Call(new C2G_BattleNotify() { Message = "test:6" }).Coroutine();
  112. break;
  113. case (int)KeyCode.F8:
  114. session.Call(new C2G_BattleNotify() { Message = "test:7" }).Coroutine();
  115. break;
  116. }
  117. await ETTask.CompletedTask;
  118. //EventSystem.Instance.Publish<ShowOrHideHeadBar>();
  119. //EventSystem.Instance.Publish<SoundMuteEvent>();
  120. }
  121. //获得当前战场(当前塔位置),随机位置
  122. public static int BattleCenterIndex = 0;
  123. private Vector2 vecTemp = new Vector2();
  124. private Vector2 GetRandomPos()
  125. {
  126. Vector2[] TowerPos = { new Vector2(){ X = 103, Y = 197 }, new Vector2() { X = 190, Y = 133 }, new Vector2() { X = 104, Y = 69 } };
  127. var tower = TowerPos[BattleCenterIndex];
  128. var rand = new Random();
  129. float r;
  130. double ang;
  131. if(BattleCenterIndex == 0)
  132. {
  133. r = (float)Math.Sqrt(rand.Next(3025)) + 10;
  134. ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 77f / 180f + Math.PI;
  135. }
  136. else if(BattleCenterIndex == 1)
  137. {
  138. r = (float)Math.Sqrt(rand.Next(3136)) + 10;
  139. ang = rand.Next(20) / 180.0f * Math.PI + Math.PI * 76f / 180f;
  140. }
  141. else
  142. {
  143. r = (float)Math.Sqrt(rand.Next(2809)) + 10;
  144. ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 148f / 180f;
  145. }
  146. vecTemp.X = tower.X + (float)(r * Math.Cos(ang));
  147. vecTemp.Y = tower.Y - (float)(r * Math.Sin(ang));
  148. return vecTemp;
  149. }
  150. }
  151. //测试代码,检测当前战场中心
  152. [Event]
  153. public class Test1EventHandler : BEvent<CameraPlaybleEvent>
  154. {
  155. protected override async ETTask OnEvent(CameraPlaybleEvent a)
  156. {
  157. if (a.Playable == "Camera1_2")
  158. {
  159. BattleFuncHandler.BattleCenterIndex = 1;
  160. }
  161. else if (a.Playable == "Camera2_3")
  162. {
  163. BattleFuncHandler.BattleCenterIndex = 2;
  164. }
  165. await ETTask.CompletedTask;
  166. }
  167. }
  168. [Event]
  169. public class Test2EventHandler : BEvent<EventType.GameoverEvent>
  170. {
  171. protected override async ETTask OnEvent(EventType.GameoverEvent args)
  172. {
  173. BattleFuncHandler.BattleCenterIndex = 0;
  174. await ETTask.CompletedTask;
  175. }
  176. }
  177. //发送战斗服指令相关
  178. public partial class BattleMgr
  179. {
  180. private bool autoFight;
  181. public bool AutoFight
  182. {
  183. get
  184. {
  185. return autoFight;
  186. }
  187. set
  188. {
  189. if(value == autoFight) return;
  190. else
  191. {
  192. autoFight = value;
  193. }
  194. }
  195. }
  196. public CommonAI.Zone.Action SetAutoFight(bool flag)
  197. {
  198. return new UnitGuardAction(UnitMgr.Instance.ActorId, flag, CommonAI.XmdsConstConfig.AUTO_GUARD_MODE_POINT);
  199. }
  200. }
  201. }