コード例 #1
0
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        foreach (Square point in squares)
        {
            Gizmos.DrawSphere(point.pos, 0.1f);
        }

        if (Pathing?.MasterPath == null)
        {
            return;
        }

        Gizmos.color = Color.red;

        int enteranceKey = GridUtilities.TwoToOne(enterances[enterance]);

        if (!Pathing.MasterPath.ContainsKey(enteranceKey))
        {
            return;
        }

        var enterancePaths = Pathing.MasterPath[enteranceKey];

        int key = GridUtilities.TwoToOne(exits[exit]);

        if (!enterancePaths.ContainsKey(key))
        {
            return;
        }

        var exitPaths = enterancePaths[key];

        DrawPath(exitPaths);
    }
コード例 #2
0
ファイル: AiPathing.cs プロジェクト: 40206111/CaTDefence
    void CheckNextPath(ref Path currentPath, Square next, int pathInt, Vector2Int dir)
    {
        if (next != null && !next.hasBox &&
            !PathIds.Contains(GridUtilities.TwoToOne(next.gridCoord)))
        {
            Path nextPath = new Path(next, currentPath);
            nextPath = DownPath(nextPath, pathInt, dir);

            if (nextPath != null)
            {
                currentPath.FutureSquares.Add(nextPath);
            }
        }
    }
コード例 #3
0
ファイル: AiPathing.cs プロジェクト: 40206111/CaTDefence
    public void SetEnterancesAndExits(List <Vector2Int> enterances, List <Vector2Int> exits)
    {
        Entrances = new List <int>();
        Exits     = new List <int>();

        foreach (Vector2Int coord in enterances)
        {
            Entrances.Add(GridUtilities.TwoToOne(coord));
        }

        foreach (Vector2Int coord in exits)
        {
            Exits.Add(GridUtilities.TwoToOne(coord));
        }
    }
コード例 #4
0
ファイル: AiPathing.cs プロジェクト: 40206111/CaTDefence
    bool NextToAnExit(Square end, Square left, Square right, Square forward, Square back)
    {
        bool output = false;

        output |= left?.gridCoord == end.gridCoord;
        output |= right?.gridCoord == end.gridCoord;
        output |= forward?.gridCoord == end.gridCoord;
        output |= back?.gridCoord == end.gridCoord;

        if (output)
        {
            return(false);
        }

        output |= left != null && left.edge && Exits.Contains(GridUtilities.TwoToOne(left.gridCoord));
        output |= right != null && right.edge && Exits.Contains(GridUtilities.TwoToOne(right.gridCoord));
        output |= forward != null && forward.edge && Exits.Contains(GridUtilities.TwoToOne(forward.gridCoord));
        output |= back != null && back.edge && Exits.Contains(GridUtilities.TwoToOne(back.gridCoord));

        return(output);
    }
コード例 #5
0
    private void OnMouseOver()
    {
        //if left or right click
        if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
        {
            RemovePathIndexes();
            highlightColour = Input.GetMouseButtonDown(1) ? Color.red : Color.yellow; //red for delete, yellow for create
            state          |= eSquareState.highlighted;
            startSquare     = gridCoord;
            endSquare       = startSquare;
            drawHorizontal  = true;
            drawPathIndexes.Add(GridUtilities.TwoToOne(startSquare));
        }
        else if (Input.GetMouseButton(0) || Input.GetMouseButton(1)) //left or right click down
        {
            //skip if same tile as last time
            if (gridCoord == endSquare)
            {
                return;
            }
            bool prevDrawHor = drawHorizontal;
            drawHorizontal = IsHorizontal(startSquare, gridCoord, drawHorizontal);

            //If we've changed direction our whole list is wrong
            if (prevDrawHor != drawHorizontal)
            {
                RemovePathIndexes();

                int startIndex = GridUtilities.TwoToOne(startSquare);
                int diff       = drawHorizontal ? startSquare.x - gridCoord.x : startSquare.y - gridCoord.y;
                int direction  = diff < 0 ? 1 : -1;
                int boxCount   = Mathf.Abs(diff) + 1; //plus one to include starting box
                int modifier   = drawHorizontal ? 1 : Grid.width;

                for (int i = 0; i < boxCount; ++i)
                {
                    int index = startIndex + (modifier * i * direction);
                    drawPathIndexes.Add(index);
                    Grid.squares[index].state |= eSquareState.highlighted;
                }
            }
            else
            {
                int diff      = drawHorizontal ? startSquare.x - endSquare.x : startSquare.y - endSquare.y;
                int direction = diff < 0 ? 1 : -1;
                int newDiff   = drawHorizontal ? endSquare.x - gridCoord.x : endSquare.y - gridCoord.y;
                int newDir    = newDiff < 0 ? 1 : -1;
                int boxCount  = Mathf.Abs(newDiff);
                int modifier  = drawHorizontal ? 1 : Grid.width;

                for (int i = 0; i < boxCount; ++i)
                {
                    if (newDir != direction && diff != 0)
                    {
                        if (drawPathIndexes.Count == 1)
                        {
                            direction *= -1;
                            continue;
                        }
                        int index = drawPathIndexes[drawPathIndexes.Count - 1];
                        Grid.squares[index].state &= ~eSquareState.highlighted;
                        drawPathIndexes.Remove(index);
                    }
                    else
                    {
                        int index = drawPathIndexes[drawPathIndexes.Count - 1] + (modifier * newDir);
                        Grid.squares[index].state |= eSquareState.highlighted;
                        drawPathIndexes.Add(index);
                    }
                }
            }

            endSquare = gridCoord;
        }
    }
コード例 #6
0
ファイル: AiPathing.cs プロジェクト: 40206111/CaTDefence
    Path DownPath(Path currentPath, int pathInt, Vector2Int lastTime)
    {
        pathInt++;

        if (pathInt > MaxPath)
        {
            return(null);
        }
        if (Branches >= MaxBranch)
        {
            return(null);
        }

        Vector2Int coord   = currentPath.Current.gridCoord;
        Square     current = currentPath.Current;

        if (coord == endSquare.gridCoord)
        {
            if (pathInt < LowestTotalPath)
            {
                MaxPath         = (int)(pathInt * 1.2f);
                LowestTotalPath = pathInt;
            }
            Branches++;
            return(currentPath);
        }

        Vector2Int difCoord = coord - endSquare.gridCoord;
        int        diff     = Mathf.Abs(difCoord.x) + Mathf.Abs(difCoord.y);

        if (pathInt + diff > MaxPath)
        {
            return(null);
        }

        PathIds.Add(GridUtilities.TwoToOne(coord));

        //squares on the edge of the map should either be boxes or
        //enterances/exits so there's no point in us checking them
        //if the straight distance to the exit will make out path too
        //long we shouldn't bother checking it either
        if (GridUtilities.IsOnEdge(current))
        {
            return(null);
        }

        if (RightIsBetter(current.gridCoord, endSquare.gridCoord, right))
        {
            Vector2Int temp = right;
            right = left;
            left  = temp;

            temp      = diagRight;
            diagRight = diagLeft;
            diagLeft  = temp;
        }

        Square forwardSquare       = GridUtilities.GetNextSquare(current, forward);
        Square leftSquare          = GridUtilities.GetNextSquare(current, left);
        Square diagLeftSquare      = GridUtilities.GetNextSquare(current, diagLeft);
        Square diagBackLeftSquare  = GridUtilities.GetNextSquare(current, -diagRight);
        Square rightSquare         = GridUtilities.GetNextSquare(current, right);
        Square diagRightSquare     = GridUtilities.GetNextSquare(current, diagRight);
        Square diagBackRightSquare = GridUtilities.GetNextSquare(current, -diagLeft);
        Square backSquare          = GridUtilities.GetNextSquare(current, back);

        currentPath.FutureSquares = new List <Path>();

        //FORWARD
        if (back != lastTime)
        {
            CheckNextPath(ref currentPath, forwardSquare, pathInt, forward);
        }
        //LEFT
        if (CheckDirection(left, forwardSquare, diagLeftSquare, lastTime, coord))
        {
            CheckNextPath(ref currentPath, leftSquare, pathInt, left);
        }
        //RIGHT
        if (CheckDirection(right, forwardSquare, diagRightSquare, lastTime, coord))
        {
            CheckNextPath(ref currentPath, rightSquare, pathInt, right);
        }
        //BACK
        if (forward != lastTime &&
            ((leftSquare != null && leftSquare.hasBox) ||
             (rightSquare != null && rightSquare.hasBox) ||
             (diagBackLeftSquare != null && diagBackLeftSquare.hasBox) ||
             (diagBackRightSquare != null && diagBackRightSquare.hasBox)))
        {
            CheckNextPath(ref currentPath, backSquare, pathInt, back);
        }

        PathIds.Remove(GridUtilities.TwoToOne(coord));

        if (currentPath.FutureSquares.Count > 0)
        {
            return(currentPath);
        }

        return(null);
    }