Move.cs 18 KB

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