Move.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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
  136. {
  137. //上方 左方
  138. if (paddingTop <= 164 && paddingLeft <= 372)
  139. return true;
  140. //下 右方
  141. if (paddingDown <= 108 && paddingRight <= 134)
  142. return true;
  143. }
  144. //左 右 133
  145. if (paddingRight <= 133 || paddingLeft <= 133)
  146. return true;
  147. return false;
  148. };
  149. public void SetGo(List<SpineAnimationLoader> gos)
  150. {
  151. _setList.Clear();
  152. _gos = gos;
  153. InitPos(gos);
  154. }
  155. public abstract void InitPos(List<SpineAnimationLoader> gos);
  156. public int ScreenWidth()
  157. {
  158. var ScreenWidth = Screen.width;
  159. return ScreenWidth;
  160. }
  161. public int ScreenHeight()
  162. {
  163. var ScreenHeight = Screen.height;
  164. return ScreenHeight;
  165. }
  166. }
  167. /// <summary>
  168. /// 随机位置静止
  169. /// </summary>
  170. public class Stay : Move
  171. {
  172. public override void InitPos(List<SpineAnimationLoader> gos)
  173. {
  174. float ScaleX(float value)
  175. {
  176. var width = ScreenWidth();
  177. var scale = 1280f / (float)width;
  178. value *= scale;
  179. return value;
  180. }
  181. //随机不重叠的位置
  182. for (int i = 0; i < gos.Count; i++)
  183. {
  184. var randomPos = gos[i].GetRandomPos();
  185. int count = RamdomNum;
  186. while (IsUIBlock(gos[i], randomPos, MoveType.Stay, false) || (CheckOverLap(gos[i], randomPos) && count > 0))
  187. {
  188. randomPos = gos[i].GetRandomPos();
  189. count--;
  190. if (count <= 0)//找不到就穷举
  191. {
  192. var canvasSize = GeneratingTarget.gm.GetCanvasSize();
  193. var HWidth = canvasSize.x * 0.5f;
  194. var HHeight = canvasSize.y * 0.5f;
  195. var canUse = false;
  196. var newPos = Vector2.zero;
  197. for (int xPos = 0; xPos <= HWidth * 0.7f; xPos += 20)
  198. {
  199. for (int yPos = 0; yPos < HHeight * 0.7f; yPos += 15)
  200. {
  201. newPos.x = xPos; newPos.y = yPos;
  202. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  203. {
  204. canUse = true;
  205. break;
  206. }
  207. newPos.x = xPos; newPos.y = -yPos;
  208. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  209. {
  210. canUse = true;
  211. break;
  212. }
  213. newPos.x = -xPos; newPos.y = yPos;
  214. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  215. {
  216. canUse = true;
  217. break;
  218. }
  219. newPos.x = -xPos; newPos.y = -yPos;
  220. if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
  221. {
  222. canUse = true;
  223. break;
  224. }
  225. }
  226. if (canUse)
  227. {
  228. randomPos = newPos;
  229. break;
  230. }
  231. }
  232. }
  233. }
  234. // Debug.Log($"i ={i} IsUIBlock={IsUIBlock(gos[i], randomPos, true)} CheckOverLap={CheckOverLap(gos[i], randomPos)} count={count}");
  235. gos[i].SetPos(randomPos);
  236. _setList.Add(gos[i]);
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// 左右水平移动
  242. /// </summary>
  243. public class LeftToRight : Move, IUpdate
  244. {
  245. public override void InitPos(List<SpineAnimationLoader> gos)
  246. {
  247. //随机屏幕左侧位置
  248. for (int i = 0; i < gos.Count; i++)
  249. {
  250. var randomPos = gos[i].GetRandomPos(PosType.Left);
  251. int count = RamdomNum;
  252. while (IsUIBlock(gos[i], randomPos, MoveType.LeftToRight, false) || CheckOverLap(gos[i], randomPos, MoveType.LeftToRight) && count > 0)
  253. {
  254. randomPos = gos[i].GetRandomPos(PosType.Left);
  255. count--;
  256. }
  257. var go = gos[i];
  258. go.SetPos(randomPos);
  259. //目标点是横向对称的位置
  260. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  261. _setList.Add(go);
  262. }
  263. }
  264. }
  265. /// <summary>
  266. /// 右左水平移动
  267. /// </summary>
  268. public class RightToLeft : Move
  269. {
  270. public override void InitPos(List<SpineAnimationLoader> gos)
  271. {
  272. //随机屏幕右侧位置
  273. for (int i = 0; i < gos.Count; i++)
  274. {
  275. var randomPos = gos[i].GetRandomPos(PosType.Right);
  276. int count = RamdomNum;
  277. while (IsUIBlock(gos[i], randomPos, MoveType.RightToLeft, false) || CheckOverLap(gos[i], randomPos, MoveType.RightToLeft) && count > 0)
  278. {
  279. randomPos = gos[i].GetRandomPos(PosType.RightDown);
  280. count--;
  281. }
  282. var go = gos[i];
  283. go.SetPos(randomPos);
  284. //目标点是横向对称的位置
  285. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  286. _setList.Add(go);
  287. }
  288. }
  289. }
  290. /// <summary>
  291. /// 相对水平移动
  292. /// </summary>
  293. public class RelativeHor : Move
  294. {
  295. public override void InitPos(List<SpineAnimationLoader> gos)
  296. {
  297. //随机屏幕左右侧位置 一左一右
  298. for (int i = 0; i < gos.Count; i++)
  299. {
  300. var tempPos = i % 2 == 0 ? PosType.LeftDown : PosType.RightDown;
  301. var randomPos = gos[i].GetRandomPos(tempPos);
  302. int count = RamdomNum;
  303. while (IsUIBlock(gos[i], randomPos, MoveType.RelativeHor, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeHor, gos.Count) && count > 0)
  304. {
  305. randomPos = gos[i].GetRandomPos(tempPos);
  306. count--;
  307. }
  308. var go = gos[i];
  309. go.SetPos(randomPos);
  310. //目标点是横向对称的位置
  311. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
  312. _setList.Add(go);
  313. }
  314. }
  315. }
  316. /// <summary>
  317. /// 相对垂直移动
  318. /// </summary>
  319. public class RelativeVet : Move
  320. {
  321. public override void InitPos(List<SpineAnimationLoader> gos)
  322. {
  323. //随机屏幕上下侧位置 一上一下
  324. for (int i = 0; i < gos.Count; i++)
  325. {
  326. var tempPos = i % 2 == 0 ? PosType.Top : PosType.Down;
  327. var randomPos = gos[i].GetRandomPos(tempPos);
  328. int count = RamdomNum;
  329. while (IsUIBlock(gos[i], randomPos, MoveType.RelativeVet, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeVet, gos.Count) && count > 0)
  330. {
  331. randomPos = gos[i].GetRandomPos(tempPos);
  332. count--;
  333. }
  334. var go = gos[i];
  335. go.SetPos(randomPos);
  336. //目标点是纵向对称的位置
  337. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration()).SetEase(Ease.InSine);
  338. _setList.Add(go);
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// 同步斜向移动
  344. /// </summary>
  345. public class Diagonal : Move
  346. {
  347. public override void InitPos(List<SpineAnimationLoader> gos)
  348. {
  349. //随机屏幕对角侧位置 一上一下
  350. var random = Random.Range(0, 1);
  351. for (int i = 0; i < gos.Count; i++)
  352. {
  353. var temp = i % 2 == 0;
  354. PosType posType;
  355. if (random == 0)
  356. posType = temp ? PosType.LeftTop : PosType.LeftDown;
  357. else
  358. posType = temp ? PosType.RightTop : PosType.RightDown;
  359. var randomPos = gos[i].GetRandomPos(posType, 0.25f);
  360. int count = RamdomNum;
  361. while (IsUIBlock(gos[i], randomPos, MoveType.Diagonal, false) && count > 0)
  362. {
  363. randomPos = gos[i].GetRandomPos(posType, 0.25f);
  364. count--;
  365. }
  366. var go = gos[i];
  367. go.SetPos(randomPos);
  368. //目标点是对角位置
  369. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  370. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  371. _setList.Add(go);
  372. }
  373. }
  374. }
  375. /// <summary>
  376. /// 同步W型移动
  377. /// </summary>
  378. public class W : Move
  379. {
  380. public override void InitPos(List<SpineAnimationLoader> gos)
  381. {
  382. //从左侧开始 一上一下 往右W轨迹运动
  383. for (int i = 0; i < gos.Count; i++)
  384. {
  385. var tempPos = i % 2 == 0 ? PosType.LeftTop : PosType.LeftDown;
  386. var randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
  387. int count = RamdomNum;
  388. while (IsUIBlock(gos[i], randomPos, MoveType.W, false) && count > 0)
  389. {
  390. randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
  391. count--;
  392. }
  393. var go = gos[i];
  394. go.SetPos(randomPos);
  395. _setList.Add(go);
  396. Vector3[] positions = new Vector3[5];
  397. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  398. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  399. positions[2] = new Vector3(0, positions[0].y, 0);
  400. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  401. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  402. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  403. }
  404. }
  405. }
  406. /// <summary>
  407. /// 相对W型移动
  408. /// </summary>
  409. public class W2 : Move
  410. {
  411. public override void InitPos(List<SpineAnimationLoader> gos)
  412. {
  413. List<PosType> temp = new List<PosType>();
  414. //四个角落随机创建 W运动
  415. for (int i = 0; i < gos.Count; i++)
  416. {
  417. if (temp.Count <= 0)
  418. {
  419. temp.Add(PosType.LeftTop);
  420. temp.Add(PosType.LeftDown);
  421. temp.Add(PosType.RightDown);
  422. temp.Add(PosType.RightTop);
  423. }
  424. var random = Random.Range(0, temp.Count - 1);
  425. var tempPos = temp[random];
  426. temp.RemoveAt(random);
  427. var randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
  428. int count = RamdomNum;
  429. while (IsUIBlock(gos[i], randomPos, MoveType.W2, false) && count > 0)
  430. {
  431. randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
  432. count--;
  433. }
  434. var go = gos[i];
  435. go.SetPos(randomPos);
  436. _setList.Add(go);
  437. Vector3[] positions = new Vector3[5];
  438. positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
  439. positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
  440. positions[2] = new Vector3(0, positions[0].y, 0);
  441. positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
  442. positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
  443. go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
  444. }
  445. }
  446. }
  447. /// <summary>
  448. /// 横排同步水平移动 垂直运动
  449. /// </summary>
  450. public class HOR : Move
  451. {
  452. public override void InitPos(List<SpineAnimationLoader> gos)
  453. {
  454. //随机屏幕上下侧位置
  455. var random = Random.Range(0, 1);
  456. var tempPos = random == 0 ? PosType.Top : PosType.Down;
  457. for (int i = 0; i < gos.Count; i++)
  458. {
  459. var randomPos = gos[i].GetRandomPos(tempPos);
  460. int count = RamdomNum;
  461. while (IsUIBlock(gos[i], randomPos, MoveType.HOR, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  462. {
  463. randomPos = gos[i].GetRandomPos(tempPos);
  464. count--;
  465. }
  466. var go = gos[i];
  467. go.SetPos(randomPos);
  468. //目标点是纵向
  469. go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
  470. _setList.Add(go);
  471. }
  472. }
  473. }
  474. /// <summary>
  475. /// 垂直同步水平移动
  476. /// </summary>
  477. public class VET : Move
  478. {
  479. public override void InitPos(List<SpineAnimationLoader> gos)
  480. {
  481. //随机屏幕左右侧位置
  482. var random = Random.Range(0, 1);
  483. var tempPos = random == 0 ? PosType.Left : PosType.Right;
  484. for (int i = 0; i < gos.Count; i++)
  485. {
  486. var randomPos = gos[i].GetRandomPos(tempPos);
  487. int count = RamdomNum;
  488. while (IsUIBlock(gos[i], randomPos, MoveType.VET, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  489. {
  490. randomPos = gos[i].GetRandomPos(tempPos);
  491. count--;
  492. }
  493. var go = gos[i];
  494. go.SetPos(randomPos);
  495. //目标点是横向
  496. go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
  497. _setList.Add(go);
  498. }
  499. }
  500. }
  501. /// <summary>
  502. /// 旋转移动
  503. /// </summary>
  504. public class ROT : Move, IUpdate
  505. {
  506. private float speed;
  507. public override void InitPos(List<SpineAnimationLoader> gos)
  508. {
  509. for (int i = 0; i < gos.Count; i++)
  510. {
  511. var randomPos = gos[i].GetRandomPos(PosType.Rotate);
  512. int count = 2 * RamdomNum;
  513. while (IsUIBlock(gos[i], randomPos, MoveType.ROT, false) || CheckOverLap(gos[i], randomPos) && count > 0)
  514. {
  515. randomPos = gos[i].GetRandomPos(PosType.Rotate);
  516. count--;
  517. }
  518. var go = gos[i];
  519. go.SetPos(randomPos);
  520. speed = 360f / go.GetDuration();
  521. _setList.Add(go);
  522. }
  523. }
  524. public void Update()
  525. {
  526. var center = new Vector3(ScreenWidth() * 0.5f, ScreenHeight() * 0.5f);
  527. foreach (var item in _setList)
  528. {
  529. if (!item.Equals(null))
  530. {
  531. var angle = speed * Time.deltaTime;
  532. item.rectTransform.Rotate(Vector3.forward, -angle);
  533. //center = Camera.main.ScreenToWorldPoint(new Vector3(center.x, center.y, 0));
  534. item.rectTransform.RotateAround(center, Vector3.forward, angle);
  535. }
  536. }
  537. }
  538. }