| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- using System;
- using DG.Tweening;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Random = UnityEngine.Random;
- public interface IUpdate
- {
- public void Update() { }
- }
- public abstract class Move
- {
- protected List<SpineAnimationLoader> _gos;
- protected List<SpineAnimationLoader> _setList = new List<SpineAnimationLoader>();//已经设置好坐标的靶子
- protected static int RamdomNum = 200;
- /// <summary>
- /// 是否和已生成的重叠
- /// </summary>
- /// <param name="target"></param>
- /// <returns></returns>
- protected bool CheckOverLap(SpineAnimationLoader target, Vector2 newPos, MoveType movetype = MoveType.Stay, int count = 0)
- {
- foreach (var item in _setList)
- {
- var distanceMin = item.Radius() + target.Radius();
- if (distanceMin >= Vector2.Distance(item.transform.localPosition, newPos))
- {
- //有重叠的
- return true;
- }
- if (movetype == MoveType.LeftToRight || movetype == MoveType.RightToLeft)
- {
- if (distanceMin >= Math.Abs(item.transform.localPosition.y - newPos.y))
- {
- return true;
- }
- }
- else if (movetype == MoveType.RelativeHor)//水平相对运动
- {
- if (count > 2)
- {
- //X处于同一方向的话 Y方向不能重叠
- if ((item.transform.localPosition.x < 0 && newPos.x < 0) || (item.transform.localPosition.x > 0 && newPos.x > 0))
- {
- if (distanceMin >= Math.Abs(item.transform.localPosition.y - newPos.y))
- {
- return true;
- }
- }
- }
- else
- {
- //只有两个的话 对向也不能重叠
- if (distanceMin >= Math.Abs(item.transform.localPosition.y - newPos.y))
- {
- return true;
- }
- }
- }
- else if (movetype == MoveType.RelativeVet)
- {
- if (count > 2)
- {
- //Y处于同一方向的话 X方向不能重叠
- if ((item.transform.localPosition.y < 0 && newPos.y < 0) || (item.transform.localPosition.y > 0 && newPos.y > 0))
- {
- if (distanceMin >= Math.Abs(item.transform.localPosition.x - newPos.x))
- {
- return true;
- }
- }
- }
- else
- {
- //只有两个的话 对向也不能重叠
- if (distanceMin >= Math.Abs(item.transform.localPosition.x - newPos.x))
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- protected Func<SpineAnimationLoader, Vector2, MoveType, bool, bool> IsUIBlock = (SpineAnimationLoader target, Vector2 pos, MoveType moveType, bool log) =>
- {
- var radius = target.Radius();
- var canvasSize = GeneratingTarget.gm.GetCanvasSize();
- float halfW = canvasSize.x / 2;
- float halfH = canvasSize.y / 2;
- var paddingLeft = Math.Abs(pos.x - radius - (-halfW));
- var paddingTop = Math.Abs(halfH - (pos.y + radius));
- var paddingRight = Math.Abs(halfW - (pos.x + radius));
- var paddingDown = Math.Abs(pos.y - radius - (-halfH));
- if (log)
- Debug.Log($"pos={pos} paddingLeft={paddingLeft} paddingTop={paddingTop} paddingRight={paddingRight} paddingDown={paddingDown}");
- //if(moveType == MoveType.Stay || moveType.)
- //需要对称屏蔽
- if (moveType == MoveType.RightToLeft)
- {
- //屏蔽整个上方
- //上方
- if (paddingTop <= 164)
- return true;
- //下 右方
- if (paddingDown <= 108 && paddingRight <= 134)
- return true;
- }
- else if (moveType == MoveType.LeftToRight)
- {
- //上方 左方
- if (paddingTop <= 164 && paddingLeft <= 372)
- return true;
- //下 右方
- if (paddingDown <= 108 && paddingRight <= 134)
- return true;
- }
- else if (moveType == MoveType.RelativeHor)
- {
- //屏蔽整个上方
- //上方
- if (paddingTop <= 164)
- return true;
- //下 右方
- if (paddingDown <= 108 && paddingRight <= 134)
- return true;
- }
- else if (moveType == MoveType.RelativeVet)
- {
- //屏蔽整个左方
- //左方
- if (paddingLeft <= 372)
- return true;
- }
- else if(moveType == MoveType.Diagonal)
- {
- }
- else
- {
- //上方 左方
- if (paddingTop <= 164 && paddingLeft <= 372)
- return true;
- //下 右方
- if (paddingDown <= 108 && paddingRight <= 134)
- return true;
- }
- //左 右 133
- if (paddingRight <= 133 || paddingLeft <= 133)
- return true;
- return false;
- };
- public void SetGo(List<SpineAnimationLoader> gos)
- {
- _setList.Clear();
- _gos = gos;
- InitPos(gos);
- }
- public abstract void InitPos(List<SpineAnimationLoader> gos);
- public int ScreenWidth()
- {
- var ScreenWidth = Screen.width;
- return ScreenWidth;
- }
- public int ScreenHeight()
- {
- var ScreenHeight = Screen.height;
- return ScreenHeight;
- }
- }
- /// <summary>
- /// 随机位置静止
- /// </summary>
- public class Stay : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- float ScaleX(float value)
- {
- var width = ScreenWidth();
- var scale = 1280f / (float)width;
- value *= scale;
- return value;
- }
- //随机不重叠的位置
- for (int i = 0; i < gos.Count; i++)
- {
- var randomPos = gos[i].GetRandomPos();
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.Stay, false) || (CheckOverLap(gos[i], randomPos) && count > 0))
- {
- randomPos = gos[i].GetRandomPos();
- count--;
- if (count <= 0)//找不到就穷举
- {
- var canvasSize = GeneratingTarget.gm.GetCanvasSize();
- var HWidth = canvasSize.x * 0.5f;
- var HHeight = canvasSize.y * 0.5f;
- var canUse = false;
- var newPos = Vector2.zero;
- for (int xPos = 0; xPos <= HWidth * 0.7f; xPos += 20)
- {
- for (int yPos = 0; yPos < HHeight * 0.7f; yPos += 15)
- {
- newPos.x = xPos; newPos.y = yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
- {
- canUse = true;
- break;
- }
- newPos.x = xPos; newPos.y = -yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
- {
- canUse = true;
- break;
- }
- newPos.x = -xPos; newPos.y = yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
- {
- canUse = true;
- break;
- }
- newPos.x = -xPos; newPos.y = -yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.Stay, false) && !CheckOverLap(gos[i], newPos))
- {
- canUse = true;
- break;
- }
- }
- if (canUse)
- {
- randomPos = newPos;
- break;
- }
- }
- }
- }
- // Debug.Log($"i ={i} IsUIBlock={IsUIBlock(gos[i], randomPos, true)} CheckOverLap={CheckOverLap(gos[i], randomPos)} count={count}");
- gos[i].SetPos(randomPos);
- _setList.Add(gos[i]);
- }
- }
- }
- /// <summary>
- /// 左右水平移动
- /// </summary>
- public class LeftToRight : Move, IUpdate
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //随机屏幕左侧位置
- for (int i = 0; i < gos.Count; i++)
- {
- var randomPos = gos[i].GetRandomPos(PosType.Left);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.LeftToRight, false) || CheckOverLap(gos[i], randomPos, MoveType.LeftToRight) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(PosType.Left);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- //目标点是横向对称的位置
- go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
- _setList.Add(go);
- }
- }
- }
- /// <summary>
- /// 右左水平移动
- /// </summary>
- public class RightToLeft : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //随机屏幕右侧位置
- for (int i = 0; i < gos.Count; i++)
- {
- var randomPos = gos[i].GetRandomPos(PosType.Right);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.RightToLeft, false) || CheckOverLap(gos[i], randomPos, MoveType.RightToLeft) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(PosType.RightDown);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- //目标点是横向对称的位置
- go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
- _setList.Add(go);
- }
- }
- }
- /// <summary>
- /// 相对水平移动
- /// </summary>
- public class RelativeHor : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //随机屏幕左右侧位置 一左一右
- for (int i = 0; i < gos.Count; i++)
- {
- var tempPos = i % 2 == 0 ? PosType.LeftDown : PosType.RightDown;
- var randomPos = gos[i].GetRandomPos(tempPos, 0.3f);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.RelativeHor, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeHor, gos.Count) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(tempPos, 0.3f);
- count--;
- if (count <= 0)//找不到就穷举
- {
- var canvasSize = GeneratingTarget.gm.GetCanvasSize();
- var HWidth = canvasSize.x * 0.5f;
- var HHeight = canvasSize.y * 0.5f;
- var canUse = false;
- var newPos = Vector2.zero;
- for (int xPos = 0; xPos <= HWidth * 0.7f; xPos += 20)
- {
- for (int yPos = 0; yPos < HHeight * 0.7f; yPos += 15)
- {
- newPos.x = xPos; newPos.y = yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
- {
- canUse = true;
- break;
- }
- newPos.x = xPos; newPos.y = -yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
- {
- canUse = true;
- break;
- }
- newPos.x = -xPos; newPos.y = yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
- {
- canUse = true;
- break;
- }
- newPos.x = -xPos; newPos.y = -yPos;
- if (!IsUIBlock(gos[i], newPos, MoveType.RelativeHor, false) && !CheckOverLap(gos[i], newPos, MoveType.RelativeHor, gos.Count))
- {
- canUse = true;
- break;
- }
- }
- if (canUse)
- {
- randomPos = newPos;
- break;
- }
- }
- }
- }
- var go = gos[i];
- go.SetPos(randomPos);
- //目标点是横向对称的位置
- go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine);
- _setList.Add(go);
- }
- }
- }
- /// <summary>
- /// 相对垂直移动
- /// </summary>
- public class RelativeVet : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //随机屏幕上下侧位置 一上一下
- for (int i = 0; i < gos.Count; i++)
- {
- var tempPos = i % 2 == 0 ? PosType.Top : PosType.Down;
- var randomPos = gos[i].GetRandomPos(tempPos);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.RelativeVet, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeVet, gos.Count) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(tempPos);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- //目标点是纵向对称的位置
- go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration()).SetEase(Ease.InSine);
- _setList.Add(go);
- }
- }
- }
- /// <summary>
- /// 同步斜向移动
- /// </summary>
- public class Diagonal : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //随机屏幕对角侧位置 一上一下
- var random = Random.Range(0, 1);
- var xRate = 0.25f;
- var yRate = 0.25f;
- for (int i = 0; i < gos.Count; i++)
- {
- var temp = i % 2 == 0;
- PosType posType;
- if (random == 0)
- posType = temp ? PosType.LeftTop : PosType.LeftDown;
- else
- posType = temp ? PosType.RightTop : PosType.RightDown;
- var randomPos = gos[i].GetRandomPos(posType, xRate, yRate);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.Diagonal, false) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(posType, xRate, yRate);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- //目标点是对角位置
- go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
- go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
- _setList.Add(go);
- }
- }
- }
- /// <summary>
- /// 同步W型移动
- /// </summary>
- public class W : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //从左侧开始 一上一下 往右W轨迹运动
- for (int i = 0; i < gos.Count; i++)
- {
- var tempPos = i % 2 == 0 ? PosType.LeftTop : PosType.LeftDown;
- var randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.W, false) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- _setList.Add(go);
- Vector3[] positions = new Vector3[5];
- positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
- positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
- positions[2] = new Vector3(0, positions[0].y, 0);
- positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
- positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
- go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
- }
- }
- }
- /// <summary>
- /// 相对W型移动
- /// </summary>
- public class W2 : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- List<PosType> temp = new List<PosType>();
- //四个角落随机创建 W运动
- for (int i = 0; i < gos.Count; i++)
- {
- if (temp.Count <= 0)
- {
- temp.Add(PosType.LeftTop);
- temp.Add(PosType.LeftDown);
- temp.Add(PosType.RightDown);
- temp.Add(PosType.RightTop);
- }
- var random = Random.Range(0, temp.Count - 1);
- var tempPos = temp[random];
- temp.RemoveAt(random);
- var randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.W2, false) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(tempPos, 0.25f, 0.25f);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- _setList.Add(go);
- Vector3[] positions = new Vector3[5];
- positions[0] = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0);
- positions[4] = new Vector3(-positions[0].x, positions[0].y, 0);
- positions[2] = new Vector3(0, positions[0].y, 0);
- positions[1] = new Vector3(positions[0].x * 0.5f, -positions[0].y, 0);
- positions[3] = new Vector3(positions[0].x * -0.5f, -positions[0].y, 0);
- go.transform.DOLocalPath(positions, go.GetDuration(), PathType.CatmullRom).SetEase(Ease.Linear);
- }
- }
- }
- /// <summary>
- /// 横排同步水平移动 垂直运动
- /// </summary>
- public class HOR : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //随机屏幕上下侧位置
- var random = Random.Range(0, 1);
- var tempPos = random == 0 ? PosType.Top : PosType.Down;
- for (int i = 0; i < gos.Count; i++)
- {
- var randomPos = gos[i].GetRandomPos(tempPos);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.HOR, false) || CheckOverLap(gos[i], randomPos) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(tempPos);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- //目标点是纵向
- go.transform.DOLocalMoveY(-go.transform.localPosition.y, go.GetDuration());
- _setList.Add(go);
- }
- }
- }
- /// <summary>
- /// 垂直同步水平移动
- /// </summary>
- public class VET : Move
- {
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- //随机屏幕左右侧位置
- var random = Random.Range(0, 1);
- var tempPos = random == 0 ? PosType.Left : PosType.Right;
- for (int i = 0; i < gos.Count; i++)
- {
- var randomPos = gos[i].GetRandomPos(tempPos);
- int count = RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.VET, false) || CheckOverLap(gos[i], randomPos) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(tempPos);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- //目标点是横向
- go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration());
- _setList.Add(go);
- }
- }
- }
- /// <summary>
- /// 旋转移动
- /// </summary>
- public class ROT : Move, IUpdate
- {
- private float speed;
- public override void InitPos(List<SpineAnimationLoader> gos)
- {
- for (int i = 0; i < gos.Count; i++)
- {
- var randomPos = gos[i].GetRandomPos(PosType.Rotate);
- int count = 2 * RamdomNum;
- while (IsUIBlock(gos[i], randomPos, MoveType.ROT, false) || CheckOverLap(gos[i], randomPos) && count > 0)
- {
- randomPos = gos[i].GetRandomPos(PosType.Rotate);
- count--;
- }
- var go = gos[i];
- go.SetPos(randomPos);
- speed = 360f / go.GetDuration();
- _setList.Add(go);
- }
- }
- public void Update()
- {
- var center = new Vector3(ScreenWidth() * 0.5f, ScreenHeight() * 0.5f);
- foreach (var item in _setList)
- {
- if (!item.Equals(null))
- {
- var angle = speed * Time.deltaTime;
- item.rectTransform.Rotate(Vector3.forward, -angle);
- //center = Camera.main.ScreenToWorldPoint(new Vector3(center.x, center.y, 0));
- item.rectTransform.RotateAround(center, Vector3.forward, angle);
- }
- }
- }
- }
|