Move.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. }
  311. var go = gos[i];
  312. go.SetPos(randomPos);
  313. //目标点是横向对称的位置
  314. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  315. _setList.Add(go);
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 相对垂直移动
  321. /// </summary>
  322. public class RelativeVet : Move
  323. {
  324. public override void InitPos(List<SpineAnimationLoader> gos)
  325. {
  326. //随机屏幕上下侧位置 一上一下
  327. for (int i = 0; i < gos.Count; i++)
  328. {
  329. var tempPos = i % 2 == 0 ? PosType.Top : PosType.Down;
  330. var randomPos = gos[i].GetRandomPos(tempPos);
  331. int count = RamdomNum;
  332. while (IsUIBlock(gos[i], randomPos, MoveType.RelativeVet, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeVet, gos.Count) && count > 0)
  333. {
  334. randomPos = gos[i].GetRandomPos(tempPos);
  335. count--;
  336. }
  337. var go = gos[i];
  338. go.SetPos(randomPos);
  339. //目标点是纵向对称的位置
  340. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration()).SetEase(Ease.InSine);
  341. _setList.Add(go);
  342. }
  343. }
  344. }
  345. /// <summary>
  346. /// 同步斜向移动
  347. /// </summary>
  348. public class Diagonal : Move
  349. {
  350. public override void InitPos(List<SpineAnimationLoader> gos)
  351. {
  352. //随机屏幕对角侧位置 一上一下
  353. var random = Random.Range(0, 1);
  354. var xRate = 0.25f;
  355. var yRate = 0.25f;
  356. for (int i = 0; i < gos.Count; i++)
  357. {
  358. var temp = i % 2 == 0;
  359. PosType posType;
  360. if (random == 0)
  361. posType = temp ? PosType.LeftTop : PosType.LeftDown;
  362. else
  363. posType = temp ? PosType.RightTop : PosType.RightDown;
  364. var randomPos = gos[i].GetRandomPos(posType, xRate, yRate);
  365. int count = RamdomNum;
  366. while (IsUIBlock(gos[i], randomPos, MoveType.Diagonal, false) && count > 0)
  367. {
  368. randomPos = gos[i].GetRandomPos(posType, xRate, yRate);
  369. count--;
  370. }
  371. var go = gos[i];
  372. go.SetPos(randomPos);
  373. //目标点是对角位置
  374. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  375. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  376. _setList.Add(go);
  377. }
  378. }
  379. }
  380. /// <summary>
  381. /// 同步W型移动
  382. /// </summary>
  383. public class W : Move
  384. {
  385. public override void InitPos(List<SpineAnimationLoader> gos)
  386. {
  387. //从左侧开始 一上一下 往右W轨迹运动
  388. for (int i = 0; i < gos.Count; i++)
  389. {
  390. var tempPos = i % 2 == 0 ? PosType.LeftTop : PosType.LeftDown;
  391. var randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  392. int count = RamdomNum;
  393. while (IsUIBlock(gos[i], randomPos, MoveType.W, false) && count > 0)
  394. {
  395. randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  396. count--;
  397. }
  398. var go = gos[i];
  399. go.SetPos(randomPos);
  400. _setList.Add(go);
  401. Vector3[] positions = new Vector3[5];
  402. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  403. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  404. positions[2] = new Vector3(0, positions[0].y, 0);
  405. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  406. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  407. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  408. }
  409. }
  410. }
  411. /// <summary>
  412. /// 相对W型移动
  413. /// </summary>
  414. public class W2 : Move
  415. {
  416. public override void InitPos(List<SpineAnimationLoader> gos)
  417. {
  418. List<PosType> temp = new List<PosType>();
  419. //四个角落随机创建 W运动
  420. for (int i = 0; i < gos.Count; i++)
  421. {
  422. if (temp.Count <= 0)
  423. {
  424. temp.Add(PosType.LeftTop);
  425. temp.Add(PosType.LeftDown);
  426. temp.Add(PosType.RightDown);
  427. temp.Add(PosType.RightTop);
  428. }
  429. var random = Random.Range(0, temp.Count - 1);
  430. var tempPos = temp[random];
  431. temp.RemoveAt(random);
  432. var randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  433. int count = RamdomNum;
  434. while (IsUIBlock(gos[i], randomPos, MoveType.W2, false) && count > 0)
  435. {
  436. randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
  437. count--;
  438. }
  439. var go = gos[i];
  440. go.SetPos(randomPos);
  441. _setList.Add(go);
  442. Vector3[] positions = new Vector3[5];
  443. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  444. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  445. positions[2] = new Vector3(0, positions[0].y, 0);
  446. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  447. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  448. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  449. }
  450. }
  451. }
  452. /// <summary>
  453. /// 横排同步水平移动 垂直运动
  454. /// </summary>
  455. public class HOR : Move
  456. {
  457. public override void InitPos(List<SpineAnimationLoader> gos)
  458. {
  459. //随机屏幕上下侧位置
  460. var random = Random.Range(0, 1);
  461. var tempPos = random == 0 ? PosType.Top : PosType.Down;
  462. for (int i = 0; i < gos.Count; i++)
  463. {
  464. var randomPos = gos[i].GetRandomPos(tempPos);
  465. int count = RamdomNum;
  466. while (IsUIBlock(gos[i], randomPos, MoveType.HOR, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  467. {
  468. randomPos = gos[i].GetRandomPos(tempPos);
  469. count--;
  470. }
  471. var go = gos[i];
  472. go.SetPos(randomPos);
  473. //目标点是纵向
  474. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  475. _setList.Add(go);
  476. }
  477. }
  478. }
  479. /// <summary>
  480. /// 垂直同步水平移动
  481. /// </summary>
  482. public class VET : Move
  483. {
  484. public override void InitPos(List<SpineAnimationLoader> gos)
  485. {
  486. //随机屏幕左右侧位置
  487. var random = Random.Range(0, 1);
  488. var tempPos = random == 0 ? PosType.Left : PosType.Right;
  489. for (int i = 0; i < gos.Count; i++)
  490. {
  491. var randomPos = gos[i].GetRandomPos(tempPos);
  492. int count = RamdomNum;
  493. while (IsUIBlock(gos[i], randomPos, MoveType.VET, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  494. {
  495. randomPos = gos[i].GetRandomPos(tempPos);
  496. count--;
  497. }
  498. var go = gos[i];
  499. go.SetPos(randomPos);
  500. //目标点是横向
  501. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  502. _setList.Add(go);
  503. }
  504. }
  505. }
  506. /// <summary>
  507. /// 旋转移动
  508. /// </summary>
  509. public class ROT : Move, IUpdate
  510. {
  511. private float speed;
  512. public override void InitPos(List<SpineAnimationLoader> gos)
  513. {
  514. for (int i = 0; i < gos.Count; i++)
  515. {
  516. var randomPos = gos[i].GetRandomPos(PosType.Rotate);
  517. int count = 2 * RamdomNum;
  518. while (IsUIBlock(gos[i], randomPos, MoveType.ROT, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  519. {
  520. randomPos = gos[i].GetRandomPos(PosType.Rotate);
  521. count--;
  522. }
  523. var go = gos[i];
  524. go.SetPos(randomPos);
  525. speed = 360f / go.GetDuration();
  526. _setList.Add(go);
  527. }
  528. }
  529. public void Update()
  530. {
  531. var center = new Vector3(ScreenWidth() * 0.5f, ScreenHeight() * 0.5f);
  532. foreach (var item in _setList)
  533. {
  534. if (!item.Equals(null))
  535. {
  536. var angle = speed * Time.deltaTime;
  537. item.rectTransform.Rotate(Vector3.forward, -angle);
  538. //center = Camera.main.ScreenToWorldPoint(new Vector3(center.x, center.y, 0));
  539. item.rectTransform.RotateAround(center, Vector3.forward, angle);
  540. }
  541. }
  542. }
  543. }