示例#1
0
    public static IEnumerator Init(float pStepSize, Vector2 pTopLeft, Vector2 pBotRight)
    {
        //Debug.Log($"Init");

        playerSize = Game.Instance.PlayerManager.PLAYER_SIZE;

        stepSize = pStepSize;
        grid     = new List <List <Node> >();

        Vector2 botLeft  = new Vector2(pTopLeft.x, pBotRight.y);
        Vector2 topRight = new Vector2(pBotRight.x, pTopLeft.y);

        positionShift = -botLeft;

        yield return(new WaitForEndOfFrame());        //without this some MapObjects are ignored

        const int max_steps_per_frame = 100;
        int       steps = 0;

        for (float x = botLeft.x; x < topRight.x; x += pStepSize)
        {
            grid.Add(new List <Node>());

            for (float y = botLeft.y; y < topRight.y; y += pStepSize)
            {
                Vector2 nodePos    = new Vector2(x, y);
                bool    isWalkable = !PathFinderController.OverlapsWithMapObject(nodePos);

                if (debug_draw_grid)
                {
                    Utils.DebugDrawCross(nodePos, isWalkable ? Color.green : Color.red, 1);
                }

                SVector2 nodePosScaled = GetScaledVector(nodePos);

                Node node = new Node(nodePosScaled, isWalkable);
                grid.Last().Add(node);

                //do only X steps per frame to avoid big lag
                steps++;
                if (steps % max_steps_per_frame == 0)
                {
                    //Debug.Log($"Steps: {steps}");
                    yield return(new WaitForEndOfFrame());
                }
            }
        }

        //astar = new Astar(grid);
        Debug_DrawGrid();
        isInited = true;
        OnInited?.Invoke();
        //Debug.Log($"Astar init");
    }
示例#2
0
 /// <summary>
 /// Adds the pPoint to the pCollection if it does not overlap with
 /// any map object and is not in pIgnorePoints.
 /// Returns true if added.
 /// </summary>
 private static bool TryAddPointToCollection(Vector2 pCenter, List <Vector2> pIgnorePoints, List <Vector2> pCollection, Vector2 pPoint)
 {
     //note: IsReachable method does not work well when player is touching
     //a map object (always unreachable)
     //if(!Utils.ContainsPoint(pIgnorePoints, pPoint) && IsReachable(pCenter, pPoint))
     if (!Utils.ContainsPoint(pIgnorePoints, pPoint) && !PathFinderController.OverlapsWithMapObject(pPoint))
     {
         pCollection.Add(pPoint);
         return(true);
     }
     return(false);
 }
示例#3
0
    /// <summary>
    /// Check if the last 4 nodes are in zig-zag shape.
    /// If so, try to replace one node with new one making the pPath more straight.
    /// X			X - X
    /// |				|
    /// X - X	=>		X
    ///		|			|
    ///		X			X
    /// </summary>
    private static void TryStraightenThePath(List <Vector2> pPath, float pStepSize, bool pDebug)
    {
        if (pPath.Count < 4)
        {
            return;
        }
        if (!IsZigZag(pPath.GetRange(pPath.Count - 4, 4), pStepSize))
        {
            return;
        }

        Vector2 p1 = pPath[pPath.Count - 4];
        Vector2 p2 = pPath[pPath.Count - 3];
        Vector2 p3 = pPath[pPath.Count - 2];
        Vector2 p4 = pPath[pPath.Count - 1];

        Utils.DebugDrawPath(new List <Vector2>()
        {
            p1, p2, p3, p4
        }, Color.red);

        Vector2 newPoint = p1 + (p2 - p1) * 2;

        if (!PathFinderController.OverlapsWithMapObject(newPoint))
        {
            pPath.RemoveAt(pPath.Count - 2);
            pPath.Insert(pPath.Count - 1, newPoint);
            return;
        }
        newPoint = p4 + (p3 - p4) * 2;
        if (!PathFinderController.OverlapsWithMapObject(newPoint))
        {
            pPath.RemoveAt(pPath.Count - 3);
            pPath.Insert(pPath.Count - 2, newPoint);
            return;
        }
    }