Move.cs 18 KB

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