Move.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using System;
  2. using DG.Tweening;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Random = UnityEngine.Random;
  7. public interface IUpdate
  8. {
  9. public void Update() { }
  10. }
  11. public abstract class Move
  12. {
  13. protected List<SpineAnimationLoader> _gos;
  14. protected List<SpineAnimationLoader> _setList = new List<SpineAnimationLoader>();//已经设置好坐标的靶子
  15. protected static int RamdomNum = 50;
  16. /// <summary>
  17. /// 是否和已生成的重叠
  18. /// </summary>
  19. /// <param name="target"></param>
  20. /// <returns></returns>
  21. protected bool CheckOverLap(SpineAnimationLoader target, Vector2 newPos)
  22. {
  23. foreach (var item in _setList)
  24. {
  25. var distanceMin = item.Radius() + target.Radius();
  26. if (distanceMin >= Vector2.Distance(item.transform.localPosition, newPos))
  27. {
  28. //有重叠的
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. protected Func<SpineAnimationLoader, Vector2, bool, bool> IsUIBlock = (SpineAnimationLoader target, Vector2 pos, bool log) =>
  35. {
  36. var radius = target.Radius();
  37. var canvasSize = GeneratingTarget.gm.GetCanvasSize();
  38. float halfW = canvasSize.x / 2;
  39. float halfH = canvasSize.y / 2;
  40. var paddingLeft = Math.Abs(pos.x - radius - (-halfW));
  41. var paddingTop = Math.Abs(halfH - (pos.y + radius));
  42. var paddingRight = Math.Abs(halfW - (pos.x + radius));
  43. var paddingDown = Math.Abs(pos.y - radius - (-halfH));
  44. if (log)
  45. Debug.Log($"pos={pos} paddingLeft={paddingLeft} paddingTop={paddingTop} paddingRight={paddingRight} paddingDown={paddingDown}");
  46. ////上方170 右方133
  47. //if (paddingTop <= 170 && paddingRight <= 133)
  48. // return true;
  49. ////下148 左方 370
  50. //if (paddingDown <= 148f && paddingLeft <= 370)
  51. // return true;
  52. //需要对称屏蔽
  53. //上方 左方
  54. if ((paddingDown <= 164 || paddingTop <= 164) && (paddingLeft <= 372 || paddingRight <= 372))
  55. return true;
  56. //下 右方
  57. if ((paddingDown <= 108 || paddingTop <= 108) && (paddingRight <= 134 || paddingLeft <= 134))
  58. return true;
  59. //右方 133
  60. if (paddingRight <= 133 || paddingLeft <= 133)
  61. return true;
  62. return false;
  63. };
  64. public void SetGo(List<SpineAnimationLoader> gos)
  65. {
  66. _setList.Clear();
  67. _gos = gos;
  68. InitPos(gos);
  69. }
  70. public abstract void InitPos(List<SpineAnimationLoader> gos);
  71. public int ScreenWidth()
  72. {
  73. var ScreenWidth = Screen.width;
  74. return ScreenWidth;
  75. }
  76. public int ScreenHeight()
  77. {
  78. var ScreenHeight = Screen.height;
  79. return ScreenHeight;
  80. }
  81. }
  82. /// <summary>
  83. /// 随机位置静止
  84. /// </summary>
  85. public class Stay : Move
  86. {
  87. public override void InitPos(List<SpineAnimationLoader> gos)
  88. {
  89. float ScaleX(float value)
  90. {
  91. var width = ScreenWidth();
  92. var scale = 1280f / (float)width;
  93. value *= scale;
  94. return value;
  95. }
  96. //随机不重叠的位置
  97. for (int i = 0; i < gos.Count; i++)
  98. {
  99. var randomPos = gos[i].GetRandomPos();
  100. int count = RamdomNum;
  101. while (IsUIBlock(gos[i], randomPos, false) || (CheckOverLap(gos[i], randomPos) && count > 0))
  102. {
  103. randomPos = gos[i].GetRandomPos();
  104. count--;
  105. }
  106. // Debug.Log($"i ={i} IsUIBlock={IsUIBlock(gos[i], randomPos, true)} CheckOverLap={CheckOverLap(gos[i], randomPos)} count={count}");
  107. gos[i].SetPos(randomPos);
  108. _setList.Add(gos[i]);
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 左右水平移动
  114. /// </summary>
  115. public class LeftToRight : Move, IUpdate
  116. {
  117. public override void InitPos(List<SpineAnimationLoader> gos)
  118. {
  119. //随机屏幕左侧位置
  120. for (int i = 0; i < gos.Count; i++)
  121. {
  122. var randomPos = gos[i].GetRandomPos(PosType.Left);
  123. int count = RamdomNum;
  124. while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  125. {
  126. randomPos = gos[i].GetRandomPos(PosType.Left);
  127. count--;
  128. }
  129. var go = gos[i];
  130. go.SetPos(randomPos);
  131. //目标点是横向对称的位置
  132. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  133. _setList.Add(go);
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 右左水平移动
  139. /// </summary>
  140. public class RightToLeft : Move
  141. {
  142. public override void InitPos(List<SpineAnimationLoader> gos)
  143. {
  144. //随机屏幕右侧位置
  145. for (int i = 0; i < gos.Count; i++)
  146. {
  147. var randomPos = gos[i].GetRandomPos(PosType.Right);
  148. int count = RamdomNum;
  149. while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  150. {
  151. randomPos = gos[i].GetRandomPos(PosType.RightDown);
  152. count--;
  153. }
  154. var go = gos[i];
  155. go.SetPos(randomPos);
  156. //目标点是横向对称的位置
  157. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  158. _setList.Add(go);
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// 相对水平移动
  164. /// </summary>
  165. public class RelativeHor : Move
  166. {
  167. public override void InitPos(List<SpineAnimationLoader> gos)
  168. {
  169. //随机屏幕左右侧位置 一左一右
  170. for (int i = 0; i < gos.Count; i++)
  171. {
  172. var tempPos = i % 2 == 0 ? PosType.LeftDown : PosType.RightDown;
  173. var randomPos = gos[i].GetRandomPos(tempPos);
  174. int count = RamdomNum;
  175. while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  176. {
  177. randomPos = gos[i].GetRandomPos(tempPos);
  178. count--;
  179. }
  180. var go = gos[i];
  181. go.SetPos(randomPos);
  182. //目标点是横向对称的位置
  183. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  184. _setList.Add(go);
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// 相对垂直移动
  190. /// </summary>
  191. public class RelativeVet : Move
  192. {
  193. public override void InitPos(List<SpineAnimationLoader> gos)
  194. {
  195. //随机屏幕上下侧位置 一上一下
  196. for (int i = 0; i < gos.Count; i++)
  197. {
  198. var tempPos = i % 2 == 0 ? PosType.Top : PosType.Down;
  199. var randomPos = gos[i].GetRandomPos(tempPos);
  200. int count = RamdomNum;
  201. while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  202. {
  203. randomPos = gos[i].GetRandomPos(tempPos);
  204. count--;
  205. }
  206. var go = gos[i];
  207. go.SetPos(randomPos);
  208. //目标点是纵向对称的位置
  209. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration()).SetEase(Ease.InSine);
  210. _setList.Add(go);
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 同步斜向移动
  216. /// </summary>
  217. public class Diagonal : Move
  218. {
  219. public override void InitPos(List<SpineAnimationLoader> gos)
  220. {
  221. //随机屏幕对角侧位置 一上一下
  222. var random = Random.Range(0, 1);
  223. for (int i = 0; i < gos.Count; i++)
  224. {
  225. var temp = i % 2 == 0;
  226. PosType posType;
  227. if (random == 0)
  228. posType = temp ? PosType.LeftTop : PosType.LeftDown;
  229. else
  230. posType = temp ? PosType.RightTop : PosType.RightDown;
  231. var randomPos = gos[i].GetRandomPos(posType);
  232. int count = RamdomNum;
  233. while (IsUIBlock(gos[i], randomPos, false) && count > 0)
  234. {
  235. randomPos = gos[i].GetRandomPos(posType);
  236. count--;
  237. }
  238. var go = gos[i];
  239. go.SetPos(randomPos);
  240. //目标点是对角位置
  241. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  242. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  243. _setList.Add(go);
  244. }
  245. }
  246. }
  247. /// <summary>
  248. /// 同步W型移动
  249. /// </summary>
  250. public class W : Move
  251. {
  252. public override void InitPos(List<SpineAnimationLoader> gos)
  253. {
  254. //从左侧开始 一上一下 往右W轨迹运动
  255. for (int i = 0; i < gos.Count; i++)
  256. {
  257. var tempPos = i % 2 == 0 ? PosType.LeftTop : PosType.LeftDown;
  258. var randomPos = gos[i].GetRandomPos(tempPos);
  259. int count = RamdomNum;
  260. while (IsUIBlock(gos[i], randomPos, false) && count > 0)
  261. {
  262. randomPos = gos[i].GetRandomPos(tempPos);
  263. count--;
  264. }
  265. var go = gos[i];
  266. go.SetPos(randomPos);
  267. _setList.Add(go);
  268. Vector3[] positions = new Vector3[5];
  269. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  270. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  271. positions[2] = new Vector3(0, positions[0].y, 0);
  272. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  273. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  274. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  275. }
  276. }
  277. }
  278. /// <summary>
  279. /// 相对W型移动
  280. /// </summary>
  281. public class W2 : Move
  282. {
  283. public override void InitPos(List<SpineAnimationLoader> gos)
  284. {
  285. List<PosType> temp = new List<PosType>();
  286. //四个角落随机创建 W运动
  287. for (int i = 0; i < gos.Count; i++)
  288. {
  289. if (temp.Count <= 0)
  290. {
  291. temp.Add(PosType.LeftTop);
  292. temp.Add(PosType.LeftDown);
  293. temp.Add(PosType.RightDown);
  294. temp.Add(PosType.RightTop);
  295. }
  296. var random = Random.Range(0, temp.Count - 1);
  297. var tempPos = temp[random];
  298. temp.RemoveAt(random);
  299. var randomPos = gos[i].GetRandomPos(tempPos);
  300. int count = RamdomNum;
  301. while (IsUIBlock(gos[i], randomPos, false) && count > 0)
  302. {
  303. randomPos = gos[i].GetRandomPos(tempPos);
  304. count--;
  305. }
  306. var go = gos[i];
  307. go.SetPos(randomPos);
  308. _setList.Add(go);
  309. Vector3[] positions = new Vector3[5];
  310. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  311. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  312. positions[2] = new Vector3(0, positions[0].y, 0);
  313. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  314. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  315. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 横排同步水平移动 垂直运动
  321. /// </summary>
  322. public class HOR : Move
  323. {
  324. public override void InitPos(List<SpineAnimationLoader> gos)
  325. {
  326. //随机屏幕上下侧位置
  327. var random = Random.Range(0, 1);
  328. var tempPos = random == 0 ? PosType.Top : PosType.Down;
  329. for (int i = 0; i < gos.Count; i++)
  330. {
  331. var randomPos = gos[i].GetRandomPos(tempPos);
  332. int count = RamdomNum;
  333. while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  334. {
  335. randomPos = gos[i].GetRandomPos(tempPos);
  336. count--;
  337. }
  338. var go = gos[i];
  339. go.SetPos(randomPos);
  340. //目标点是纵向
  341. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  342. _setList.Add(go);
  343. }
  344. }
  345. }
  346. /// <summary>
  347. /// 垂直同步水平移动
  348. /// </summary>
  349. public class VET : Move
  350. {
  351. public override void InitPos(List<SpineAnimationLoader> gos)
  352. {
  353. //随机屏幕左右侧位置
  354. var random = Random.Range(0, 1);
  355. var tempPos = random == 0 ? PosType.Left : PosType.Right;
  356. for (int i = 0; i < gos.Count; i++)
  357. {
  358. var randomPos = gos[i].GetRandomPos(tempPos);
  359. int count = RamdomNum;
  360. while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  361. {
  362. randomPos = gos[i].GetRandomPos(tempPos);
  363. count--;
  364. }
  365. var go = gos[i];
  366. go.SetPos(randomPos);
  367. //目标点是横向
  368. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  369. _setList.Add(go);
  370. }
  371. }
  372. }
  373. /// <summary>
  374. /// 旋转移动
  375. /// </summary>
  376. public class ROT : Move, IUpdate
  377. {
  378. private float speed;
  379. public override void InitPos(List<SpineAnimationLoader> gos)
  380. {
  381. for (int i = 0; i < gos.Count; i++)
  382. {
  383. var randomPos = gos[i].GetRandomPos(PosType.Rotate);
  384. int count = 2 * RamdomNum;
  385. while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  386. {
  387. randomPos = gos[i].GetRandomPos(PosType.Rotate);
  388. count--;
  389. }
  390. var go = gos[i];
  391. go.SetPos(randomPos);
  392. speed = 360f / go.GetDuration();
  393. _setList.Add(go);
  394. }
  395. }
  396. public void Update()
  397. {
  398. var center = new Vector3(ScreenWidth() * 0.5f, ScreenHeight() * 0.5f);
  399. foreach (var item in _setList)
  400. {
  401. if (!item.Equals(null))
  402. {
  403. var angle = speed * Time.deltaTime;
  404. item.rectTransform.Rotate(Vector3.forward, -angle);
  405. //center = Camera.main.ScreenToWorldPoint(new Vector3(center.x, center.y, 0));
  406. item.rectTransform.RotateAround(center, Vector3.forward, angle);
  407. }
  408. }
  409. }
  410. }