Move.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 = 200;
  16. /// <summary>
  17. /// 是否和已生成的重叠
  18. /// </summary>
  19. /// <param name="target"></param>
  20. /// <returns></returns>
  21. protected bool CheckOverLap(SpineAnimationLoader target, Vector2 newPos, MoveType movetype = MoveType.Stay, int count = 0)
  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. if (movetype == MoveType.LeftToRight || movetype == MoveType.RightToLeft)
  32. {
  33. if (distanceMin >= Math.Abs(item.transform.localPosition.y - newPos.y))
  34. {
  35. return true;
  36. }
  37. }
  38. else if (movetype == MoveType.RelativeHor)//水平相对运动
  39. {
  40. if (count > 2)
  41. {
  42. //X处于同一方向的话 Y方向不能重叠
  43. if ((item.transform.localPosition.x < 0 && newPos.x < 0) || (item.transform.localPosition.x > 0 && newPos.x > 0))
  44. {
  45. if (distanceMin >= Math.Abs(item.transform.localPosition.y - newPos.y))
  46. {
  47. return true;
  48. }
  49. }
  50. }
  51. else
  52. {
  53. //只有两个的话 对向也不能重叠
  54. if (distanceMin >= Math.Abs(item.transform.localPosition.y - newPos.y))
  55. {
  56. return true;
  57. }
  58. }
  59. }
  60. else if (movetype == MoveType.RelativeVet)
  61. {
  62. if (count > 2)
  63. {
  64. //Y处于同一方向的话 X方向不能重叠
  65. if ((item.transform.localPosition.y < 0 && newPos.y < 0) || (item.transform.localPosition.y > 0 && newPos.y > 0))
  66. {
  67. if (distanceMin >= Math.Abs(item.transform.localPosition.x - newPos.x))
  68. {
  69. return true;
  70. }
  71. }
  72. }
  73. else
  74. {
  75. //只有两个的话 对向也不能重叠
  76. if (distanceMin >= Math.Abs(item.transform.localPosition.x - newPos.x))
  77. {
  78. return true;
  79. }
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85. protected Func<SpineAnimationLoader, Vector2, MoveType, bool, bool> IsUIBlock = (SpineAnimationLoader target, Vector2 pos, MoveType moveType, bool log) =>
  86. {
  87. var radius = target.Radius();
  88. var canvasSize = GeneratingTarget.gm.GetCanvasSize();
  89. float halfW = canvasSize.x / 2;
  90. float halfH = canvasSize.y / 2;
  91. var paddingLeft = Math.Abs(pos.x - radius - (-halfW));
  92. var paddingTop = Math.Abs(halfH - (pos.y + radius));
  93. var paddingRight = Math.Abs(halfW - (pos.x + radius));
  94. var paddingDown = Math.Abs(pos.y - radius - (-halfH));
  95. if (log)
  96. Debug.Log($"pos={pos} paddingLeft={paddingLeft} paddingTop={paddingTop} paddingRight={paddingRight} paddingDown={paddingDown}");
  97. //if(moveType == MoveType.Stay || moveType.)
  98. //需要对称屏蔽
  99. if (moveType == MoveType.RightToLeft)
  100. {
  101. //屏蔽整个上方
  102. //上方
  103. if (paddingTop <= 164)
  104. return true;
  105. //下 右方
  106. if (paddingDown <= 108 && paddingRight <= 134)
  107. return true;
  108. }
  109. else if (moveType == MoveType.LeftToRight)
  110. {
  111. //上方 左方
  112. if (paddingTop <= 164 && paddingLeft <= 372)
  113. return true;
  114. //下 右方
  115. if (paddingDown <= 108 && paddingRight <= 134)
  116. return true;
  117. }
  118. else if (moveType == MoveType.RelativeHor)
  119. {
  120. //屏蔽整个上方
  121. //上方
  122. if (paddingTop <= 164)
  123. return true;
  124. //下 右方
  125. if (paddingDown <= 108 && paddingRight <= 134)
  126. return true;
  127. }
  128. else if (moveType == MoveType.RelativeVet)
  129. {
  130. //屏蔽整个左方
  131. //左方
  132. if (paddingLeft <= 372)
  133. return true;
  134. }
  135. else if(moveType == MoveType.Diagonal)
  136. {
  137. }
  138. else
  139. {
  140. //上方 左方
  141. if (paddingTop <= 164 && paddingLeft <= 372)
  142. return true;
  143. //下 右方
  144. if (paddingDown <= 108 && paddingRight <= 134)
  145. return true;
  146. }
  147. //左 右 133
  148. if (paddingRight <= 133 || paddingLeft <= 133)
  149. return true;
  150. return false;
  151. };
  152. public void SetGo(List<SpineAnimationLoader> gos)
  153. {
  154. _setList.Clear();
  155. _gos = gos;
  156. InitPos(gos);
  157. }
  158. public abstract void InitPos(List<SpineAnimationLoader> gos);
  159. public int ScreenWidth()
  160. {
  161. var ScreenWidth = Screen.width;
  162. return ScreenWidth;
  163. }
  164. public int ScreenHeight()
  165. {
  166. var ScreenHeight = Screen.height;
  167. return ScreenHeight;
  168. }
  169. }
  170. /// <summary>
  171. /// 随机位置静止
  172. /// </summary>
  173. public class Stay : Move
  174. {
  175. public override void InitPos(List<SpineAnimationLoader> gos)
  176. {
  177. float ScaleX(float value)
  178. {
  179. var width = ScreenWidth();
  180. var scale = 1280f / (float)width;
  181. value *= scale;
  182. return value;
  183. }
  184. //随机不重叠的位置
  185. for (int i = 0; i < gos.Count; i++)
  186. {
  187. var randomPos = gos[i].GetRandomPos();
  188. int count = RamdomNum;
  189. while (IsUIBlock(gos[i], randomPos, MoveType.Stay, false) || (CheckOverLap(gos[i], randomPos) && count > 0))
  190. {
  191. randomPos = gos[i].GetRandomPos();
  192. count--;
  193. if (count <= 0)//找不到就穷举
  194. {
  195. var canvasSize = GeneratingTarget.gm.GetCanvasSize();
  196. var HWidth = canvasSize.x * 0.5f;
  197. var HHeight = canvasSize.y * 0.5f;
  198. var canUse = false;
  199. var newPos = Vector2.zero;
  200. for (int xPos = 0; xPos <= HWidth * 0.7f; xPos += 20)
  201. {
  202. for (int yPos = 0; yPos < HHeight * 0.7f; yPos += 15)
  203. {
  204. newPos.x = xPos; newPos.y = yPos;
  205. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  206. {
  207. canUse = true;
  208. break;
  209. }
  210. newPos.x = xPos; newPos.y = -yPos;
  211. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  212. {
  213. canUse = true;
  214. break;
  215. }
  216. newPos.x = -xPos; newPos.y = yPos;
  217. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  218. {
  219. canUse = true;
  220. break;
  221. }
  222. newPos.x = -xPos; newPos.y = -yPos;
  223. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  224. {
  225. canUse = true;
  226. break;
  227. }
  228. }
  229. if (canUse)
  230. {
  231. randomPos = newPos;
  232. break;
  233. }
  234. }
  235. }
  236. }
  237. // Debug.Log($"i ={i} IsUIBlock={IsUIBlock(gos[i], randomPos, true)} CheckOverLap={CheckOverLap(gos[i], randomPos)} count={count}");
  238. gos[i].SetPos(randomPos);
  239. _setList.Add(gos[i]);
  240. }
  241. }
  242. }
  243. /// <summary>
  244. /// 左右水平移动
  245. /// </summary>
  246. public class LeftToRight : Move, IUpdate
  247. {
  248. public override void InitPos(List<SpineAnimationLoader> gos)
  249. {
  250. //随机屏幕左侧位置
  251. for (int i = 0; i < gos.Count; i++)
  252. {
  253. var randomPos = gos[i].GetRandomPos(PosType.Left);
  254. int count = RamdomNum;
  255. while (IsUIBlock(gos[i], randomPos, MoveType.LeftToRight, false) || CheckOverLap(gos[i], randomPos, MoveType.LeftToRight) && count > 0)
  256. {
  257. randomPos = gos[i].GetRandomPos(PosType.Left);
  258. count--;
  259. }
  260. var go = gos[i];
  261. go.SetPos(randomPos);
  262. //目标点是横向对称的位置
  263. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  264. _setList.Add(go);
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// 右左水平移动
  270. /// </summary>
  271. public class RightToLeft : Move
  272. {
  273. public override void InitPos(List<SpineAnimationLoader> gos)
  274. {
  275. //随机屏幕右侧位置
  276. for (int i = 0; i < gos.Count; i++)
  277. {
  278. var randomPos = gos[i].GetRandomPos(PosType.Right);
  279. int count = RamdomNum;
  280. while (IsUIBlock(gos[i], randomPos, MoveType.RightToLeft, false) || CheckOverLap(gos[i], randomPos, MoveType.RightToLeft) && count > 0)
  281. {
  282. randomPos = gos[i].GetRandomPos(PosType.RightDown);
  283. count--;
  284. }
  285. var go = gos[i];
  286. go.SetPos(randomPos);
  287. //目标点是横向对称的位置
  288. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  289. _setList.Add(go);
  290. }
  291. }
  292. }
  293. /// <summary>
  294. /// 相对水平移动
  295. /// </summary>
  296. public class RelativeHor : Move
  297. {
  298. public override void InitPos(List<SpineAnimationLoader> gos)
  299. {
  300. //随机屏幕左右侧位置 一左一右
  301. for (int i = 0; i < gos.Count; i++)
  302. {
  303. var tempPos = i % 2 == 0 ? PosType.LeftDown : PosType.RightDown;
  304. var randomPos = gos[i].GetRandomPos(tempPos, 0.3f);
  305. int count = RamdomNum;
  306. while (IsUIBlock(gos[i], randomPos, MoveType.RelativeHor, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeHor, gos.Count) && count > 0)
  307. {
  308. randomPos = gos[i].GetRandomPos(tempPos, 0.3f);
  309. count--;
  310. if (count <= 0)//找不到就穷举
  311. {
  312. var canvasSize = GeneratingTarget.gm.GetCanvasSize();
  313. var HWidth = canvasSize.x * 0.5f;
  314. var HHeight = canvasSize.y * 0.5f;
  315. var canUse = false;
  316. var newPos = Vector2.zero;
  317. for (int xPos = 0; xPos <= HWidth * 0.7f; xPos += 20)
  318. {
  319. for (int yPos = 0; yPos < HHeight * 0.7f; yPos += 15)
  320. {
  321. newPos.x = xPos; newPos.y = yPos;
  322. if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
  323. {
  324. canUse = true;
  325. break;
  326. }
  327. newPos.x = xPos; newPos.y = -yPos;
  328. if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
  329. {
  330. canUse = true;
  331. break;
  332. }
  333. newPos.x = -xPos; newPos.y = yPos;
  334. if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
  335. {
  336. canUse = true;
  337. break;
  338. }
  339. newPos.x = -xPos; newPos.y = -yPos;
  340. if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
  341. {
  342. canUse = true;
  343. break;
  344. }
  345. }
  346. if (canUse)
  347. {
  348. randomPos = newPos;
  349. break;
  350. }
  351. }
  352. }
  353. }
  354. var go = gos[i];
  355. go.SetPos(randomPos);
  356. //目标点是横向对称的位置
  357. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  358. _setList.Add(go);
  359. }
  360. }
  361. }
  362. /// <summary>
  363. /// 相对垂直移动
  364. /// </summary>
  365. public class RelativeVet : Move
  366. {
  367. public override void InitPos(List<SpineAnimationLoader> gos)
  368. {
  369. //随机屏幕上下侧位置 一上一下
  370. for (int i = 0; i < gos.Count; i++)
  371. {
  372. var tempPos = i % 2 == 0 ? PosType.Top : PosType.Down;
  373. var randomPos = gos[i].GetRandomPos(tempPos);
  374. int count = RamdomNum;
  375. while (IsUIBlock(gos[i], randomPos, MoveType.RelativeVet, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeVet, gos.Count) && count > 0)
  376. {
  377. randomPos = gos[i].GetRandomPos(tempPos);
  378. count--;
  379. }
  380. var go = gos[i];
  381. go.SetPos(randomPos);
  382. //目标点是纵向对称的位置
  383. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration()).SetEase(Ease.InSine);
  384. _setList.Add(go);
  385. }
  386. }
  387. }
  388. /// <summary>
  389. /// 同步斜向移动
  390. /// </summary>
  391. public class Diagonal : Move
  392. {
  393. public override void InitPos(List<SpineAnimationLoader> gos)
  394. {
  395. //随机屏幕对角侧位置 一上一下
  396. var random = Random.Range(0, 1);
  397. var xRate = 0.25f;
  398. var yRate = 0.25f;
  399. for (int i = 0; i < gos.Count; i++)
  400. {
  401. var temp = i % 2 == 0;
  402. PosType posType;
  403. if (random == 0)
  404. posType = temp ? PosType.LeftTop : PosType.LeftDown;
  405. else
  406. posType = temp ? PosType.RightTop : PosType.RightDown;
  407. var randomPos = gos[i].GetRandomPos(posType, xRate, yRate);
  408. int count = RamdomNum;
  409. while (IsUIBlock(gos[i], randomPos, MoveType.Diagonal, false) && count > 0)
  410. {
  411. randomPos = gos[i].GetRandomPos(posType, xRate, yRate);
  412. count--;
  413. }
  414. var go = gos[i];
  415. go.SetPos(randomPos);
  416. //目标点是对角位置
  417. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  418. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  419. _setList.Add(go);
  420. }
  421. }
  422. }
  423. /// <summary>
  424. /// 同步W型移动
  425. /// </summary>
  426. public class W : Move
  427. {
  428. public override void InitPos(List<SpineAnimationLoader> gos)
  429. {
  430. //从左侧开始 一上一下 往右W轨迹运动
  431. for (int i = 0; i < gos.Count; i++)
  432. {
  433. var tempPos = i % 2 == 0 ? PosType.LeftTop : PosType.LeftDown;
  434. var randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  435. int count = RamdomNum;
  436. while (IsUIBlock(gos[i], randomPos, MoveType.W, false) && count > 0)
  437. {
  438. randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  439. count--;
  440. }
  441. var go = gos[i];
  442. go.SetPos(randomPos);
  443. _setList.Add(go);
  444. Vector3[] positions = new Vector3[5];
  445. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  446. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  447. positions[2] = new Vector3(0, positions[0].y, 0);
  448. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  449. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  450. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  451. }
  452. }
  453. }
  454. /// <summary>
  455. /// 相对W型移动
  456. /// </summary>
  457. public class W2 : Move
  458. {
  459. public override void InitPos(List<SpineAnimationLoader> gos)
  460. {
  461. List<PosType> temp = new List<PosType>();
  462. //四个角落随机创建 W运动
  463. for (int i = 0; i < gos.Count; i++)
  464. {
  465. if (temp.Count <= 0)
  466. {
  467. temp.Add(PosType.LeftTop);
  468. temp.Add(PosType.LeftDown);
  469. temp.Add(PosType.RightDown);
  470. temp.Add(PosType.RightTop);
  471. }
  472. var random = Random.Range(0, temp.Count - 1);
  473. var tempPos = temp[random];
  474. temp.RemoveAt(random);
  475. var randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  476. int count = RamdomNum;
  477. while (IsUIBlock(gos[i], randomPos, MoveType.W2, false) && count > 0)
  478. {
  479. randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  480. count--;
  481. }
  482. var go = gos[i];
  483. go.SetPos(randomPos);
  484. _setList.Add(go);
  485. Vector3[] positions = new Vector3[5];
  486. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  487. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  488. positions[2] = new Vector3(0, positions[0].y, 0);
  489. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  490. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  491. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  492. }
  493. }
  494. }
  495. /// <summary>
  496. /// 横排同步水平移动 垂直运动
  497. /// </summary>
  498. public class HOR : Move
  499. {
  500. public override void InitPos(List<SpineAnimationLoader> gos)
  501. {
  502. //随机屏幕上下侧位置
  503. var random = Random.Range(0, 1);
  504. var tempPos = random == 0 ? PosType.Top : PosType.Down;
  505. for (int i = 0; i < gos.Count; i++)
  506. {
  507. var randomPos = gos[i].GetRandomPos(tempPos);
  508. int count = RamdomNum;
  509. while (IsUIBlock(gos[i], randomPos, MoveType.HOR, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  510. {
  511. randomPos = gos[i].GetRandomPos(tempPos);
  512. count--;
  513. }
  514. var go = gos[i];
  515. go.SetPos(randomPos);
  516. //目标点是纵向
  517. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  518. _setList.Add(go);
  519. }
  520. }
  521. }
  522. /// <summary>
  523. /// 垂直同步水平移动
  524. /// </summary>
  525. public class VET : Move
  526. {
  527. public override void InitPos(List<SpineAnimationLoader> gos)
  528. {
  529. //随机屏幕左右侧位置
  530. var random = Random.Range(0, 1);
  531. var tempPos = random == 0 ? PosType.Left : PosType.Right;
  532. for (int i = 0; i < gos.Count; i++)
  533. {
  534. var randomPos = gos[i].GetRandomPos(tempPos);
  535. int count = RamdomNum;
  536. while (IsUIBlock(gos[i], randomPos, MoveType.VET, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  537. {
  538. randomPos = gos[i].GetRandomPos(tempPos);
  539. count--;
  540. }
  541. var go = gos[i];
  542. go.SetPos(randomPos);
  543. //目标点是横向
  544. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  545. _setList.Add(go);
  546. }
  547. }
  548. }
  549. /// <summary>
  550. /// 旋转移动
  551. /// </summary>
  552. public class ROT : Move, IUpdate
  553. {
  554. private float speed;
  555. public override void InitPos(List<SpineAnimationLoader> gos)
  556. {
  557. for (int i = 0; i < gos.Count; i++)
  558. {
  559. var randomPos = gos[i].GetRandomPos(PosType.Rotate);
  560. int count = 2 * RamdomNum;
  561. while (IsUIBlock(gos[i], randomPos, MoveType.ROT, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  562. {
  563. randomPos = gos[i].GetRandomPos(PosType.Rotate);
  564. count--;
  565. }
  566. var go = gos[i];
  567. go.SetPos(randomPos);
  568. speed = 360f / go.GetDuration();
  569. _setList.Add(go);
  570. }
  571. }
  572. public void Update()
  573. {
  574. var center = new Vector3(ScreenWidth() * 0.5f, ScreenHeight() * 0.5f);
  575. foreach (var item in _setList)
  576. {
  577. if (!item.Equals(null))
  578. {
  579. var angle = speed * Time.deltaTime;
  580. item.rectTransform.Rotate(Vector3.forward, -angle);
  581. //center = Camera.main.ScreenToWorldPoint(new Vector3(center.x, center.y, 0));
  582. item.rectTransform.RotateAround(center, Vector3.forward, angle);
  583. }
  584. }
  585. }
  586. }