Browse Source

优化重叠

972455782 1 năm trước cách đây
mục cha
commit
81ffdbf541

+ 90 - 17
Assets/Game_Hyperspace/Script/Move.cs

@@ -21,7 +21,7 @@ public abstract class Move
     /// </summary>
     /// <param name="target"></param>
     /// <returns></returns>
-    protected bool CheckOverLap(SpineAnimationLoader target, Vector2 newPos, MoveType movetype = MoveType.Stay)
+    protected bool CheckOverLap(SpineAnimationLoader target, Vector2 newPos, MoveType movetype = MoveType.Stay, int count = 0)
     {
         foreach (var item in _setList)
         {
@@ -31,21 +31,51 @@ public abstract class Move
                 //有重叠的
                 return true;
             }
-            if(movetype == MoveType.RelativeHor)//水平相对运动
+            if (movetype == MoveType.LeftToRight || movetype == MoveType.RightToLeft)
             {
-                //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 (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)
+            }
+            else if (movetype == MoveType.RelativeVet)
             {
-                //Y处于同一方向的话 X方向不能重叠
-                if ((item.transform.localPosition.y < 0 && newPos.y < 0) || (item.transform.localPosition.y > 0 && newPos.y > 0))
+                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;
@@ -148,6 +178,49 @@ public class Stay : Move
             {
                 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 - 200f; xPos += 20)
+                    {
+                        for (int yPos = 0; yPos < HHeight - 150f; yPos += 15)
+                        {
+                            newPos.x = xPos; newPos.y = yPos;
+                            if (!IsUIBlock(gos[i], newPos, false) && !CheckOverLap(gos[i], newPos))
+                            {
+                                canUse = true;
+                                break;
+                            }
+                            newPos.x = xPos; newPos.y = -yPos;
+                            if (!IsUIBlock(gos[i], newPos, false) && !CheckOverLap(gos[i], newPos))
+                            {
+                                canUse = true;
+                                break;
+                            }
+                            newPos.x = -xPos; newPos.y = yPos;
+                            if (!IsUIBlock(gos[i], newPos, false) && !CheckOverLap(gos[i], newPos))
+                            {
+                                canUse = true;
+                                break;
+                            }
+                            newPos.x = -xPos; newPos.y = -yPos;
+                            if (!IsUIBlock(gos[i], newPos, 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);
@@ -168,7 +241,7 @@ public class LeftToRight : Move, IUpdate
         {
             var randomPos = gos[i].GetRandomPos(PosType.Left);
             int count = RamdomNum;
-            while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
+            while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos, MoveType.LeftToRight) && count > 0)
             {
                 randomPos = gos[i].GetRandomPos(PosType.Left);
                 count--;
@@ -194,7 +267,7 @@ public class RightToLeft : Move
         {
             var randomPos = gos[i].GetRandomPos(PosType.Right);
             int count = RamdomNum;
-            while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos) && count > 0)
+            while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos, MoveType.RightToLeft) && count > 0)
             {
                 randomPos = gos[i].GetRandomPos(PosType.RightDown);
                 count--;
@@ -221,7 +294,7 @@ public class RelativeHor : Move
             var tempPos = i % 2 == 0 ? PosType.LeftDown : PosType.RightDown;
             var randomPos = gos[i].GetRandomPos(tempPos);
             int count = RamdomNum;
-            while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeHor) && count > 0)
+            while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeHor, gos.Count) && count > 0)
             {
                 randomPos = gos[i].GetRandomPos(tempPos);
                 count--;
@@ -248,7 +321,7 @@ public class RelativeVet : Move
             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, MoveType.RelativeVet) && count > 0)
+            while (IsUIBlock(gos[i], randomPos, false) || CheckOverLap(gos[i], randomPos, MoveType.RelativeVet, gos.Count) && count > 0)
             {
                 randomPos = gos[i].GetRandomPos(tempPos);
                 count--;
@@ -280,12 +353,12 @@ public class Diagonal : Move
             else
                 posType = temp ? PosType.RightTop : PosType.RightDown;
 
-            var randomPos = gos[i].GetRandomPos(posType);
+            var randomPos = gos[i].GetRandomPos(posType, 0.25f);
 
             int count = RamdomNum;
             while (IsUIBlock(gos[i], randomPos, false) && count > 0)
             {
-                randomPos = gos[i].GetRandomPos(posType);
+                randomPos = gos[i].GetRandomPos(posType, 0.25f);
                 count--;
             }
             var go = gos[i];
@@ -309,11 +382,11 @@ public class W : Move
         for (int i = 0; i < gos.Count; i++)
         {
             var tempPos = i % 2 == 0 ? PosType.LeftTop : PosType.LeftDown;
-            var randomPos = gos[i].GetRandomPos(tempPos);
+            var randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
             int count = RamdomNum;
             while (IsUIBlock(gos[i], randomPos, false) && count > 0)
             {
-                randomPos = gos[i].GetRandomPos(tempPos);
+                randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
                 count--;
             }
             var go = gos[i];
@@ -352,11 +425,11 @@ public class W2 : Move
             var tempPos = temp[random];
             temp.RemoveAt(random);
 
-            var randomPos = gos[i].GetRandomPos(tempPos);
+            var randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
             int count = RamdomNum;
             while (IsUIBlock(gos[i], randomPos, false) && count > 0)
             {
-                randomPos = gos[i].GetRandomPos(tempPos);
+                randomPos = gos[i].GetRandomPos(tempPos, 0.25f);
                 count--;
             }
             var go = gos[i];

+ 3 - 4
Assets/Game_Hyperspace/Script/SpineAnimationLoader.cs

@@ -120,14 +120,13 @@ public class SpineAnimationLoader : MonoBehaviour
         return radius;
     }
 
-    float paddingWRate = 0.45f;
-    float paddingHRate = 0.45f;
-
     /// <summary>
     /// 随机一个坐标
     /// </summary>
-    public Vector2 GetRandomPos(PosType posType = PosType.Random)
+    public Vector2 GetRandomPos(PosType posType = PosType.Random, float rate = 0)
     {
+        float paddingWRate = rate == 0 ? 0.45f : rate;
+        float paddingHRate = rate == 0 ? 0.45f : rate;
         var canvasSize = GeneratingTarget.gm.GetCanvasSize();
         ScreenWidth = canvasSize.x;
         ScreenHeight = canvasSize.y;