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 _gos; protected List _setList = new List();//已经设置好坐标的靶子 protected static int RamdomNum = 50; /// /// 是否和已生成的重叠 /// /// /// protected bool CheckOverLap(SpineAnimationLoader target, Vector2 newPos) { foreach (var item in _setList) { var distanceMin = item.Radius() + target.Radius(); if (distanceMin >= Vector2.Distance(item.transform.localPosition, newPos)) { //有重叠的 return true; } } return false; } protected Func IsUIBlock = (SpineAnimationLoader target, Vector2 pos, 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}"); //上方157 左方 392 if (paddingTop <= 157 && paddingLeft <= 392) return true; //上方170 右方133 if (paddingTop <= 170 && paddingRight <= 133) return true; //下148 左方 370 if (paddingDown <= 148f && paddingLeft <= 370) return true; //下216 右方 133 if (paddingDown <= 216 && paddingRight <= 330) return true; //右方 133 if (paddingRight <= 133) return true; return false; }; public void SetGo(List gos) { _setList.Clear(); _gos = gos; InitPos(gos); } public abstract void InitPos(List gos); public int ScreenWidth() { var ScreenWidth = Screen.width; return ScreenWidth; } public int ScreenHeight() { var ScreenHeight = Screen.height; return ScreenHeight; } } /// /// 随机位置静止 /// public class Stay : Move { public override void InitPos(List 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, false) || (CheckOverLap(gos[i], randomPos) && count > 0)) { randomPos = gos[i].GetRandomPos(); count--; } // 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]); } } } /// /// 左右水平移动 /// public class LeftToRight : Move, IUpdate { public override void InitPos(List gos) { //随机屏幕左侧位置 for (int i = 0; i < gos.Count; i++) { var randomPos = gos[i].GetRandomPos(PosType.Left); int count = RamdomNum; while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && 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); } } } /// /// 右左水平移动 /// public class RightToLeft : Move { public override void InitPos(List gos) { //随机屏幕右侧位置 for (int i = 0; i < gos.Count; i++) { var randomPos = gos[i].GetRandomPos(PosType.Right); int count = RamdomNum; while (CheckOverLap(gos[i], randomPos) && count > 0) { randomPos = gos[i].GetRandomPos(PosType.Right); count--; } var go = gos[i]; go.SetPos(randomPos); //目标点是横向对称的位置 go.transform.DOLocalMoveX(-go.transform.localPosition.x, go.GetDuration()).SetEase(Ease.InSine); _setList.Add(go); } } } /// /// 相对水平移动 /// public class RelativeHor : Move { public override void InitPos(List gos) { //随机屏幕左右侧位置 一左一右 for (int i = 0; i < gos.Count; i++) { var tempPos = i % 2 == 0 ? PosType.Left : PosType.Right; var randomPos = gos[i].GetRandomPos(tempPos); int count = RamdomNum; while (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()).SetEase(Ease.InSine); _setList.Add(go); } } } /// /// 相对垂直移动 /// public class RelativeVet : Move { public override void InitPos(List 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, 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()).SetEase(Ease.InSine); _setList.Add(go); } } } /// /// 同步斜向移动 /// public class Diagonal : Move { public override void InitPos(List gos) { //随机屏幕对角侧位置 一上一下 var random = Random.Range(0, 1); 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); int count = RamdomNum; while (IsUIBlock(gos[i], randomPos, false) && count > 0) { randomPos = gos[i].GetRandomPos(posType); 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); } } } /// /// 同步W型移动 /// public class W : Move { public override void InitPos(List 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); int count = RamdomNum; while (IsUIBlock(gos[i], randomPos, false) && count > 0) { randomPos = gos[i].GetRandomPos(tempPos); 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); } } } /// /// 相对W型移动 /// public class W2 : Move { public override void InitPos(List gos) { List temp = new List(); //四个角落随机创建 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); int count = RamdomNum; while (IsUIBlock(gos[i], randomPos, false) && count > 0) { randomPos = gos[i].GetRandomPos(tempPos); 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); } } } /// /// 横排同步水平移动 垂直运动 /// public class HOR : Move { public override void InitPos(List 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, 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); } } } /// /// 垂直同步水平移动 /// public class VET : Move { public override void InitPos(List 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, 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); } } } /// /// 旋转移动 /// public class ROT : Move, IUpdate { private float speed; public override void InitPos(List 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, 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); } } } }