Util.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using CrossgateToolkit;
  5. public class Util
  6. {
  7. public static void Init()
  8. {
  9. Application.targetFrameRate = 60;
  10. CGTool.PATH = new CGTool.CGPath()
  11. {
  12. BIN = Application.persistentDataPath + "/bin",
  13. PAL = Application.persistentDataPath + "/pal",
  14. MAP = Application.persistentDataPath + "/map",
  15. BGM = Application.persistentDataPath + "/bgm",
  16. AUDIO = Application.persistentDataPath + "/se"
  17. };
  18. CGTool.Init();
  19. }
  20. // 获取距离目标单位直线方向一定距离的位置
  21. public static Vector2 GetTargetNearPosition(Vector2 fromPosition,Vector2 targetPosition,float distance)
  22. {
  23. Vector2 direction = targetPosition - fromPosition;
  24. Vector2 normalizedDirection = direction.normalized;
  25. // Vector2 point = targetPosition - normalizedDirection * distance;
  26. // 移动 C 点到圆上,使得 C 到 A 的距离等于半径 X
  27. // float distanceToMove = Vector2.Distance(point, fromPosition) - distance;
  28. Vector2 adjustedC = targetPosition - normalizedDirection * distance;
  29. return adjustedC;
  30. }
  31. //获取方向增量
  32. public static Vector2Int GetDirectionVector(Anime.DirectionType direction)
  33. {
  34. Vector2Int vector2 = Vector2Int.zero;
  35. switch (direction)
  36. {
  37. case Anime.DirectionType.North:
  38. vector2.x = -1;
  39. vector2.y = 1;
  40. break;
  41. case Anime.DirectionType.NorthEast:
  42. vector2.x = 0;
  43. vector2.y = 1;
  44. break;
  45. case Anime.DirectionType.East:
  46. vector2.x = 1;
  47. vector2.y = 1;
  48. break;
  49. case Anime.DirectionType.SouthEast:
  50. vector2.x = 1;
  51. vector2.y = 0;
  52. break;
  53. case Anime.DirectionType.South:
  54. vector2.x = 1;
  55. vector2.y = -1;
  56. break;
  57. case Anime.DirectionType.SouthWest:
  58. vector2.x = 0;
  59. vector2.y = -1;
  60. break;
  61. case Anime.DirectionType.West:
  62. vector2.x = -1;
  63. vector2.y = -1;
  64. break;
  65. case Anime.DirectionType.NorthWest:
  66. vector2.x = -1;
  67. vector2.y = 0;
  68. break;
  69. }
  70. return vector2;
  71. }
  72. // 获取方向上某距离的点
  73. public static Vector2 GetDirectionPoint(Vector2 fromPosition,Anime.DirectionType directionType,float distance)
  74. {
  75. Vector2 vector2 = GetDirectionVector(directionType);
  76. vector2 = vector2.normalized;
  77. Vector2 point = fromPosition + vector2 * distance;
  78. return point;
  79. }
  80. // 获取目标单位方向角度
  81. public static float GetTargetAngle(Vector2 fromPosition,Vector2 targetPosition)
  82. {
  83. Vector2 diff = targetPosition - fromPosition;
  84. float angle = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
  85. angle = angle - 90;
  86. return angle;
  87. }
  88. // 获取目标单位方向
  89. public static Anime.DirectionType GetTargetDirection(Vector2 fromPosition,Vector2 targetPosition)
  90. {
  91. Vector2 diff = targetPosition - fromPosition;
  92. float angle = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
  93. angle = angle - 90;
  94. if (angle <= 25 && angle > -25) return Anime.DirectionType.NorthEast;
  95. if (angle <= -25 && angle > -75) return Anime.DirectionType.East;
  96. if (angle <= -75 && angle > -105) return Anime.DirectionType.SouthEast;
  97. if (angle <= -105 && angle > -155) return Anime.DirectionType.South;
  98. if (angle <= -155 && angle > -205) return Anime.DirectionType.SouthWest;
  99. if (angle <= -205 && angle > -255) return Anime.DirectionType.West;
  100. if (angle <= -255 && angle > -285) return Anime.DirectionType.NorthWest;
  101. if (angle <= -285 && angle > -335) return Anime.DirectionType.North;
  102. if (angle <= -335 && angle > -385) return Anime.DirectionType.NorthEast;
  103. if (angle > 25 && angle < 75) return Anime.DirectionType.North;
  104. if (angle > 75 && angle < 105) return Anime.DirectionType.NorthWest;
  105. if (angle > 105 && angle < 155) return Anime.DirectionType.West;
  106. if (angle > 155 && angle < 205) return Anime.DirectionType.SouthWest;
  107. if (angle > 205 && angle < 255) return Anime.DirectionType.South;
  108. if (angle > 255 && angle < 285) return Anime.DirectionType.SouthEast;
  109. if (angle > 285 && angle < 335) return Anime.DirectionType.East;
  110. if (angle > 335 && angle < 385) return Anime.DirectionType.NorthEast;
  111. return Anime.DirectionType.East;
  112. }
  113. // 获取反方向
  114. public static Anime.DirectionType GetReverseDirection(Anime.DirectionType directionType)
  115. {
  116. switch (directionType)
  117. {
  118. case Anime.DirectionType.North:
  119. return Anime.DirectionType.South;
  120. case Anime.DirectionType.NorthEast:
  121. return Anime.DirectionType.SouthWest;
  122. case Anime.DirectionType.East:
  123. return Anime.DirectionType.West;
  124. case Anime.DirectionType.SouthEast:
  125. return Anime.DirectionType.NorthWest;
  126. case Anime.DirectionType.South:
  127. return Anime.DirectionType.North;
  128. case Anime.DirectionType.SouthWest:
  129. return Anime.DirectionType.NorthEast;
  130. case Anime.DirectionType.West:
  131. return Anime.DirectionType.East;
  132. case Anime.DirectionType.NorthWest:
  133. return Anime.DirectionType.SouthEast;
  134. default:
  135. return Anime.DirectionType.East;
  136. }
  137. }
  138. public static Anime.DirectionType GetRandomDirection()
  139. {
  140. int random = Random.Range(0, 8);
  141. switch (random)
  142. {
  143. case 0:
  144. return Anime.DirectionType.North;
  145. case 1:
  146. return Anime.DirectionType.NorthEast;
  147. case 2:
  148. return Anime.DirectionType.East;
  149. case 3:
  150. return Anime.DirectionType.SouthEast;
  151. case 4:
  152. return Anime.DirectionType.South;
  153. case 5:
  154. return Anime.DirectionType.SouthWest;
  155. case 6:
  156. return Anime.DirectionType.West;
  157. case 7:
  158. return Anime.DirectionType.NorthWest;
  159. default:
  160. return Anime.DirectionType.East;
  161. }
  162. }
  163. // 获取上一个方向 左旋
  164. public static Anime.DirectionType GetPrevDirection(Anime.DirectionType directionType)
  165. {
  166. switch (directionType)
  167. {
  168. case Anime.DirectionType.North:
  169. return Anime.DirectionType.NorthWest;
  170. case Anime.DirectionType.NorthEast:
  171. return Anime.DirectionType.North;
  172. case Anime.DirectionType.East:
  173. return Anime.DirectionType.NorthEast;
  174. case Anime.DirectionType.SouthEast:
  175. return Anime.DirectionType.East;
  176. case Anime.DirectionType.South:
  177. return Anime.DirectionType.SouthEast;
  178. case Anime.DirectionType.SouthWest:
  179. return Anime.DirectionType.South;
  180. case Anime.DirectionType.West:
  181. return Anime.DirectionType.SouthWest;
  182. case Anime.DirectionType.NorthWest:
  183. return Anime.DirectionType.West;
  184. default:
  185. return Anime.DirectionType.East;
  186. }
  187. }
  188. public static Anime.DirectionType GetDoublePrevDirection(Anime.DirectionType directionType)
  189. {
  190. switch (directionType)
  191. {
  192. case Anime.DirectionType.North:
  193. return Anime.DirectionType.West;
  194. case Anime.DirectionType.NorthEast:
  195. return Anime.DirectionType.NorthWest;
  196. case Anime.DirectionType.East:
  197. return Anime.DirectionType.North;
  198. case Anime.DirectionType.SouthEast:
  199. return Anime.DirectionType.NorthEast;
  200. case Anime.DirectionType.South:
  201. return Anime.DirectionType.East;
  202. case Anime.DirectionType.SouthWest:
  203. return Anime.DirectionType.SouthEast;
  204. case Anime.DirectionType.West:
  205. return Anime.DirectionType.South;
  206. case Anime.DirectionType.NorthWest:
  207. return Anime.DirectionType.SouthWest;
  208. default:
  209. return Anime.DirectionType.East;
  210. }
  211. }
  212. // 获取下一个方向 右旋
  213. public static Anime.DirectionType GetNextDirection(Anime.DirectionType directionType)
  214. {
  215. switch (directionType)
  216. {
  217. case Anime.DirectionType.North:
  218. return Anime.DirectionType.NorthEast;
  219. case Anime.DirectionType.NorthEast:
  220. return Anime.DirectionType.East;
  221. case Anime.DirectionType.East:
  222. return Anime.DirectionType.SouthEast;
  223. case Anime.DirectionType.SouthEast:
  224. return Anime.DirectionType.South;
  225. case Anime.DirectionType.South:
  226. return Anime.DirectionType.SouthWest;
  227. case Anime.DirectionType.SouthWest:
  228. return Anime.DirectionType.West;
  229. case Anime.DirectionType.West:
  230. return Anime.DirectionType.NorthWest;
  231. case Anime.DirectionType.NorthWest:
  232. return Anime.DirectionType.North;
  233. default:
  234. return Anime.DirectionType.East;
  235. }
  236. }
  237. // 获取下第二个方向
  238. public static Anime.DirectionType GetDoubleNextDirection(Anime.DirectionType directionType)
  239. {
  240. switch (directionType)
  241. {
  242. case Anime.DirectionType.North:
  243. return Anime.DirectionType.East;
  244. case Anime.DirectionType.NorthEast:
  245. return Anime.DirectionType.SouthEast;
  246. case Anime.DirectionType.East:
  247. return Anime.DirectionType.South;
  248. case Anime.DirectionType.SouthEast:
  249. return Anime.DirectionType.SouthWest;
  250. case Anime.DirectionType.South:
  251. return Anime.DirectionType.West;
  252. case Anime.DirectionType.SouthWest:
  253. return Anime.DirectionType.NorthWest;
  254. case Anime.DirectionType.West:
  255. return Anime.DirectionType.North;
  256. case Anime.DirectionType.NorthWest:
  257. return Anime.DirectionType.NorthEast;
  258. default:
  259. return Anime.DirectionType.East;
  260. }
  261. }
  262. public static List<uint> TestSerial = new List<uint>()
  263. {
  264. // 旧版单位
  265. // 100000, 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008, 100009, 100010, 100011, 100012,
  266. // 100013, 100014, 100015, 100016, 100017, 100018, 100019, 100020, 100021, 100022, 100023, 100025, 100026,
  267. // 100027, 100028, 100029, 100030, 100031, 100032, 100033, 100034, 100035, 100036, 100037, 100038, 100039,
  268. // 100040, 100041, 100042, 100043, 100044, 100045, 100046, 100047, 100048, 100050, 100051, 100052, 100053,
  269. // 100054, 100055, 100056, 100057, 100058, 100059, 100060, 100061, 100062, 100063, 100064, 100065, 100066,
  270. // 100067, 100068, 100069, 100070, 100071, 100072, 100073, 100075, 100076, 100077, 100078, 100079, 100080,
  271. // 100081, 100082, 100083, 100084, 100085, 100086, 100087, 100088, 100089, 100090, 100091, 100092, 100093,
  272. // 100094, 100095, 100096, 100097, 100098, 100100, 100101, 100102, 100103, 100104, 100105, 100106, 100107,
  273. // 100108, 100109, 100110, 100111, 100112, 100113, 100114, 100115, 100116, 100117, 100118, 100119, 100120,
  274. // 100121, 100122, 100123, 100125, 100126, 100127, 100128, 100129, 100130, 100131, 100132, 100133, 100134,
  275. // 100135, 100136, 100137, 100138, 100139, 100140, 100141, 100142, 100143, 100144, 100145, 100146, 100147,
  276. // 100148, 100150, 100151, 100152, 100153, 100154, 100155, 100156, 100157, 100158, 100159, 100160, 100161,
  277. // 100162, 100163, 100164, 100165, 100166, 100167, 100168, 100169, 100170, 100171, 100172, 100173, 100250,
  278. // 100251, 100252, 100253, 100254, 100255, 100256, 100257, 100258, 100259, 100260, 100261, 100262, 100263,
  279. // 100264, 100265, 100266, 100267, 100268, 100269, 100270, 100271, 100272, 100273, 100275, 100276, 100277,
  280. // 100278, 100279, 100280, 100281, 100282, 100283, 100284, 100285, 100286, 100287, 100288, 100289, 100290,
  281. // 100291, 100292, 100293, 100294, 100295, 100296, 100297, 100298, 100300, 100301, 100302, 100303, 100304,
  282. // 100305, 100306, 100307, 100308, 100309, 100310, 100311, 100312, 100313, 100314, 100315, 100316, 100317,
  283. // 100318, 100319, 100320, 100321, 100322, 100323, 100325, 100326, 100327, 100328, 100329, 100330, 100331,
  284. // 100332, 100333, 100334, 100335, 100336, 100337, 100338, 100339, 100340, 100341, 100342, 100343, 100344,
  285. // 100345, 100346, 100347, 100348, 100350, 100351, 100352, 100353, 100354, 100355, 100356, 100357, 100358,
  286. // 100359, 100360, 100361, 100362, 100363, 100364, 100365, 100366, 100367, 100368, 100369, 100370, 100371,
  287. // 100372, 100373, 100375, 100376, 100377, 100378, 100379, 100380, 100381, 100382, 100383, 100384, 100385,
  288. // 100386, 100387, 100388, 100389, 100390, 100391, 100392, 100393, 100394, 100395, 100396, 100397, 100398,
  289. // 100400, 100401, 100402, 100403, 100404, 100405, 100406, 100407, 100408, 100409, 100410, 100411, 100412,
  290. // 100413, 100414, 100415, 100416, 100417, 100418, 100419, 100420, 100421, 100422, 100423, 100425, 100426,
  291. // 100427, 100428, 100429, 100430, 100450, 100451, 100452, 100453, 100454, 100455, 100475, 100476, 100477,
  292. // 100478, 100479, 100480, 100500, 100501, 100502, 100503, 100504, 100505, 100525, 100526, 100527, 100528,
  293. // 100529, 100530, 100550, 100551, 100552, 100553, 100554, 100555, 100575, 100576, 100577, 100578, 100579,
  294. // 100580, 100600, 100601, 100602, 100603, 100604, 100605, 100625, 100626, 100627, 100628, 100629, 100630,
  295. // 100650, 100651, 100652, 100653, 100654, 100655, 100675, 100676, 100677, 100678, 100679, 100680, 100700,
  296. // 100701, 100702, 100703, 100704, 100705, 100725, 100726, 100727, 100728, 100729, 100730, 100750, 100751,
  297. // 100752, 100753, 100754, 100755, 100800, 100801, 100802, 100803, 100804, 100805, 100900, 100901,
  298. // 新版单位
  299. 105000, 105001, 105002, 105003, 105004, 105005, 105006, 105007, 105008, 105009, 105010, 105011, 105012,
  300. 105013, 105014, 105014, 105016, 105017, 105018, 105019, 105020, 105021, 105022, 105023, 105025, 105026,
  301. 105027, 105028, 105029, 105030, 105031, 105032, 105033, 105034, 105035, 105036, 105037, 105038, 105039,
  302. 105040, 105041, 105042, 105043, 105044, 105045, 105046, 105047, 105048, 105050, 105051, 105052, 105053,
  303. 105054, 105055, 105056, 105057, 105058, 105059, 105060, 105061, 105062, 105063, 105064, 105065, 105066,
  304. 105067, 105068, 105069, 105070, 105071, 105072, 105073, 105075, 105076, 105077, 105078, 105079, 105080,
  305. 105081, 105082, 105083, 105084, 105085, 105086, 105087, 105088, 105089, 105090, 105091, 105092, 105093,
  306. 105094, 105095, 105096, 105097, 105098, 105100, 105101, 105102, 105103, 105104, 105105, 105106, 105107,
  307. 105108, 105109, 105110, 105111, 105112, 105113, 105114, 105115, 105116, 105117, 105118, 105119, 105120,
  308. 105121, 105122, 105123, 105125, 105126, 105127, 105128, 105129, 105130, 105131, 105132, 105133, 105134,
  309. 105135, 105136, 105137, 105138, 105139, 105140, 105141, 105142, 105143, 105144, 105145, 105146, 105147,
  310. 105148, 105150, 105151, 105152, 105153, 105154, 105155, 105156, 105157, 105158, 105159, 105160, 105161,
  311. 105162, 105163, 105164, 105165, 105166, 105167, 105168, 105169, 105170, 105171, 105172, 105173, 105250,
  312. 105251, 105252, 105253, 105254, 105255, 105256, 105257, 105258, 105259, 105260, 105261, 105262, 105263,
  313. 105264, 105265, 105266, 105267, 105268, 105269, 105270, 105271, 105272, 105273, 105275, 105276, 105277,
  314. 105278, 105279, 105280, 105281, 105282, 105283, 105284, 105285, 105286, 105287, 105288, 105289, 105290,
  315. 105291, 105292, 105293, 105294, 105295, 105296, 105297, 105298, 105300, 105301, 105302, 105303, 105304,
  316. 105305, 105306, 105307, 105308, 105309, 105310, 105311, 105312, 105313, 105314, 105315, 105316, 105317,
  317. 105318, 105319, 105320, 105321, 105322, 105323, 105325, 105326, 105327, 105328, 105329, 105330, 105331, 105332,
  318. 105333, 105334, 105335, 105336, 105337, 105338, 105339, 105340, 105341, 105342, 105343, 105344, 105345,
  319. 105346, 105347, 105348, 105350, 105351, 105352, 105353, 105354, 105355, 105356, 105357, 105358, 105359,
  320. 105360, 105361, 105362, 105363, 105364, 105365, 105366, 105367, 105368, 105369, 105370, 105371, 105372,
  321. 105373, 105375, 105376, 105377, 105378, 105379, 105380, 105381, 105382, 105383, 105384, 105385, 105386,
  322. 105387, 105388, 105389, 105390, 105391, 105392, 105393, 105394, 105395, 105396, 105397, 105398, 105400,
  323. 105401, 105402, 105403, 105404, 105405, 105406, 105407, 105408, 105409, 105410, 105411, 105412, 105413,
  324. 105414, 105415, 105416, 105417, 105418, 105419, 105420, 105421, 105422, 105423, 106000, 106001, 106002,
  325. 106003, 106004, 106005, 106006, 106007, 106008, 106009, 106010, 106011, 106012, 106013, 106014, 106015,
  326. 106016, 106017, 106018, 106019, 106020, 106021, 106022, 106023, 106025, 106026, 106027, 106028, 106029,
  327. 106030, 106031, 106032, 106033, 106034, 106035, 106036, 106037, 106038, 106039, 106040, 106041, 106042,
  328. 106043, 106044, 106045, 106046, 106047, 106048, 106050, 106051, 106052, 106053, 106054, 106055, 106056,
  329. 106057, 106058, 106059, 106060, 106061, 106062, 106063, 106064, 106065, 106066, 106067, 106068, 106069,
  330. 106070, 106071, 106072, 106073, 106075, 106076, 106077, 106078, 106079, 106080, 106081, 106082, 106083,
  331. 106084, 106085, 106086, 106087, 106088, 106089, 106090, 106091, 106092, 106093, 106094, 106095, 106096,
  332. 106097, 106098, 106100, 106101, 106102, 106103, 106104, 106105, 106106, 106107, 106108, 106109, 106110,
  333. 106111, 106112, 106113, 106114, 106115, 106116, 106117, 106118, 106119, 106120, 106121, 106122, 106123,
  334. 106125, 106126, 106127, 106128, 106129, 106130, 106131, 106132, 106133, 106134, 106135, 106136, 106137,
  335. 106138, 106139, 106140, 106141, 106142, 106143, 106144, 106145, 106146, 106147, 106148, 106150, 106151,
  336. 106152, 106153, 106154, 106155, 106156, 106157, 106158, 106159, 106160, 106161, 106162, 106163, 106164,
  337. 106165, 106166, 106167, 106168, 106169, 106170, 106171, 106172, 106173, 106250, 106251, 106252, 106253,
  338. 106254, 106255, 106256, 106257, 106258, 106259, 106260, 106261, 106262, 106263, 106264, 106265, 106266,
  339. 106267, 106268, 106269, 106270, 106271, 106272, 106273, 106275, 106276, 106277, 106278, 106279, 106280,
  340. 106281, 106282, 106283, 106284, 106285, 106286, 106287, 106288, 106289, 106290, 106291, 106292, 106293,
  341. 106294, 106295, 106296, 106297, 106298, 106300, 106301, 106302, 106303, 106304, 106305, 106306, 106307,
  342. 106308, 106309, 106310, 106311, 106312, 106313, 106314, 106315, 106316, 106317, 106318, 106319, 106320,
  343. 106321, 106322, 106323, 106325, 106326, 106327, 106328, 106329, 106330, 106331, 106332, 106333, 106334,
  344. 106335, 106336, 106337, 106338, 106339, 106340, 106341, 106342, 106343, 106344, 106345, 106346, 106347,
  345. 106348, 106350, 106351, 106352, 106353, 106354, 106355, 106356, 106357, 106358, 106359, 106360, 106361,
  346. 106362, 106363, 106364, 106365, 106366, 106367, 106368, 106369, 106370, 106371, 106372, 106373, 106375,
  347. 106376, 106377, 106378, 106379, 106380, 106381, 106382, 106383, 106384, 106385, 106386, 106387, 106388,
  348. 106389, 106390, 106391, 106392, 106393, 106394, 106395, 106396, 106397, 106398, 106400, 106401, 106402,
  349. 106403, 106404, 106405, 106406, 106407, 106408, 106409, 106410, 106411, 106412, 106413, 106414, 106415,
  350. 106416, 106417, 106418, 106419, 106420, 106421, 106422, 106423, 106425, 106426, 106427, 106428, 106429,
  351. 106430, 106450, 106451, 106452, 106453, 106454, 106455, 106475, 106476, 106477, 106478, 106479, 106480,
  352. 106500, 106501, 106502, 106503, 106504, 106505, 106525, 106526, 106527, 106528, 106529, 106530, 106550,
  353. 106551, 106552, 106553, 106554, 106555, 106575, 106576, 106577, 106578, 106579, 106580, 106600, 106601,
  354. 106602, 106603, 106604, 106605, 106625, 106626, 106627, 106628, 106629, 106630, 106650, 106651, 106652,
  355. 106653, 106654, 106655, 106675, 106676, 106677, 106678, 106679, 106680, 106700, 106701, 106702, 106703,
  356. 106704, 106705, 106725, 106726, 106727, 106728, 106729, 106730, 106750, 106751, 106752, 106753, 106754,
  357. 106755
  358. };
  359. public static uint GetRandomSerial()
  360. {
  361. int random = Random.Range(0, TestSerial.Count);
  362. return TestSerial[random];
  363. }
  364. public static Anime.ActionType GetNextAction(Anime.ActionType actionType)
  365. {
  366. int action = (int)actionType;
  367. action++;
  368. if (action > 20) action = 0;
  369. return (Anime.ActionType)action;
  370. }
  371. }