//de reden dat ik dit dubbel doe is performance, nu hoef ik niet iedere keer deze variabelen te maken als ik update
    public void ChangeColorGridPart(PathFinding.Node node, Color c)
    {
        float   _x, _y, _z;
        Vector3 _leftBotBack, _rightBotBack, _leftBotFront, _rightBotFront,
                _leftTopBack, _rightTopBack, _leftTopFront, _rightTopFront;

        _x = node.x * pathfinding.widthSizeNode;
        _y = node.y * pathfinding.heightSizeNode;
        _z = node.z * pathfinding.widthSizeNode;

        //assign positions

        //top
        _leftBotBack = leftBotBack + new Vector3(_x, _y, _z);

        _rightBotBack    = _leftBotBack;
        _rightBotBack.x += pathfinding.widthSizeNode;

        _leftBotFront    = _leftBotBack;
        _leftBotFront.z += pathfinding.widthSizeNode;

        _rightBotFront    = _leftBotFront;
        _rightBotFront.x += pathfinding.widthSizeNode;

        //bot
        _leftTopBack = leftBotBack + new Vector3(_x, _y - pathfinding.heightSizeNode, _z);

        _rightTopBack    = _leftTopBack;
        _rightTopBack.x += pathfinding.widthSizeNode;

        _leftTopFront    = _leftTopBack;
        _leftTopFront.z += pathfinding.widthSizeNode;

        _rightTopFront    = _leftTopFront;
        _rightTopFront.x += pathfinding.widthSizeNode;

        //bottom
        Debug.DrawLine(_leftBotBack, _rightBotBack, c, durationPathVisualization);
        Debug.DrawLine(_leftBotBack, _leftBotFront, c, durationPathVisualization);
        Debug.DrawLine(_rightBotFront, _rightBotBack, c, durationPathVisualization);
        Debug.DrawLine(_rightBotFront, _leftBotFront, c, durationPathVisualization);

        //mid
        Debug.DrawLine(_leftBotBack, _leftTopBack, c, durationPathVisualization);
        Debug.DrawLine(_rightBotBack, _rightTopBack, c, durationPathVisualization);
        Debug.DrawLine(_leftBotFront, _leftTopFront, c, durationPathVisualization);
        Debug.DrawLine(_rightBotFront, _rightTopFront, c, durationPathVisualization);

        //top
        Debug.DrawLine(_leftTopBack, _rightTopBack, c, durationPathVisualization);
        Debug.DrawLine(_leftTopBack, _leftTopFront, c, durationPathVisualization);
        Debug.DrawLine(_rightTopFront, _rightTopBack, c, durationPathVisualization);
        Debug.DrawLine(_rightTopFront, _leftTopFront, c, durationPathVisualization);
    }
Ejemplo n.º 2
0
    private void PrepareNode(int x, int y, int z)
    {
        if (x < 0 || x >= lengthX)
        {
            return;
        }
        if (y < 0 || y + 1 >= lengthY)
        {
            return;
        }
        if (z < 0 || z >= lengthZ)
        {
            return;
        }

        //hier alle checks doen
        checkNode = p.grid[x, y, z];

        if (closed.Contains(checkNode) || !checkNode.filled)
        {
            return;
        }

        //check if already waiting to be checked
        foreach (Node node in open)
        {
            if (node.node == checkNode)
            {
                return;
            }
        }

        //switch to node above
        checkNode = p.grid[x, y + 1, z];
        if (checkNode.filled && !bake.myNodes.Contains(checkNode))
        {
            return;
        }

        _position      = new Vector3(x, y, z);
        distanceOrigin = Vector3.Distance(_position, origin);
        distanceGoal   = Vector3.Distance(_position, dest);
        adjecentNodes.Add(new Node(p.grid[x, y, z], nodeToCheck, (int)(distanceOrigin + distanceGoal)));
    }
Ejemplo n.º 3
0
    void Awake()
    {
        GenerateDungeon();

        // Create the paths for every room
        Dictionary <Room, List <PathFinding.Node> > roomRoutes = new Dictionary <Room, List <PathFinding.Node> >();

        for (int i = 0; i < rooms.Count; i++)
        {
            Room s = rooms[i];
            for (int j = i + 1; j < rooms.Count; j++)
            {
                Room             g     = rooms[j];
                PathFinding.Node start = new PathFinding.Node(s.X_Cell, s.Y_Cell);
                PathFinding.Node goal  = new PathFinding.Node(g.X_Cell, g.Y_Cell);
                pathFinder.findPath(cells, start, goal);
                //roomRoutes.Add (s, pathFinder.findPath(cells, start, goal));
            }
        }
    }
Ejemplo n.º 4
0
        //Insert a Pathfinding node in the tree
        public void Insert(PathFinding.Node pfNode, QuadTreeNode node = null, int DepthLevel = 0)
        {
            if (node == null)
            {
                node = Head;
            }

            //Make sure Box is smaller than current quadrant. If yes, go deeper, otherwise add elements here
            if (DepthLevel < this.Depth)
            {
                //Process North-West Part
                if (node.Children[0] != null)
                {
                    if (pfNode.position.X <= node.Position.X && pfNode.position.Z <= node.Position.Y)
                    {
                        Insert(pfNode, node.Children[0], DepthLevel + 1);
                    }
                }

                //Process North-East Part
                if (node.Children[1] != null)
                {
                    if (pfNode.position.X >= node.Position.X && pfNode.position.Z <= node.Position.Y)
                    {
                        Insert(pfNode, node.Children[1], DepthLevel + 1);
                    }
                }

                //Process South-West Part
                if (node.Children[2] != null)
                {
                    if (pfNode.position.X <= node.Position.X && pfNode.position.Z >= node.Position.Y)
                    {
                        Insert(pfNode, node.Children[2], DepthLevel + 1);
                    }
                }

                //Process South-East Part
                if (node.Children[3] != null)
                {
                    if (pfNode.position.X >= node.Position.X && pfNode.position.Z >= node.Position.Y)
                    {
                        Insert(pfNode, node.Children[3], DepthLevel + 1);
                    }
                }
            }
            else
            {
                //Add box in current layer
                if (node.PathFindingNodes == null)
                {
                    node.PathFindingNodes = new List <PathFinding.Node>();
                }

                if (!node.PathFindingNodes.Contains(pfNode))
                {
                    node.PathFindingNodes.Add(pfNode);
                    ItemCount++;
                }
            }
        }
    public void UpdateGrid()
    {
        if (!(pathfinding != null))
        {
            return;
        }
        if (!pathfinding.visualize)
        {
            return;
        }
        if (!showBake)
        {
            return;
        }
        //calculate corners
        CalcCorners();
        #region Play Time
        Color c = Color.white;
        //local squire
        Vector3 _leftBotBack, _rightBotBack, _leftBotFront, _rightBotFront,
                _leftTopBack, _rightTopBack, _leftTopFront, _rightTopFront;
        float _x, _y, _z;
        for (int x = 0; x < pathfinding.widthSize; x++)
        {
            for (int y = 0; y < pathfinding.heightSize; y++)
            {
                for (int z = 0; z < pathfinding.widthSize; z++)
                {
                    PathFinding.Node node = pathfinding.grid[x, y, z];
                    if (node.filled)
                    {
                        //get right color
                        switch (node.bakeType)
                        {
                        case PathFinding.BakeType.Enemy:
                            c = Color.red;
                            break;

                        case PathFinding.BakeType.Object:
                            c = Color.blue;
                            break;

                        case PathFinding.BakeType.Movable:
                            c = Color.green;
                            break;

                        default:
                            break;
                        }

                        _x = x * pathfinding.widthSizeNode;
                        _y = y * pathfinding.heightSizeNode;
                        _z = z * pathfinding.widthSizeNode;

                        //assign positions

                        //top
                        _leftBotBack = leftBotBack + new Vector3(_x, _y, _z);

                        _rightBotBack    = _leftBotBack;
                        _rightBotBack.x += pathfinding.widthSizeNode;

                        _leftBotFront    = _leftBotBack;
                        _leftBotFront.z += pathfinding.widthSizeNode;

                        _rightBotFront    = _leftBotFront;
                        _rightBotFront.x += pathfinding.widthSizeNode;

                        //bot
                        _leftTopBack = leftBotBack + new Vector3(_x, _y - pathfinding.heightSizeNode, _z);

                        _rightTopBack    = _leftTopBack;
                        _rightTopBack.x += pathfinding.widthSizeNode;

                        _leftTopFront    = _leftTopBack;
                        _leftTopFront.z += pathfinding.widthSizeNode;

                        _rightTopFront    = _leftTopFront;
                        _rightTopFront.x += pathfinding.widthSizeNode;

                        //bottom
                        Debug.DrawLine(_leftBotBack, _rightBotBack, c);
                        Debug.DrawLine(_leftBotBack, _leftBotFront, c);
                        Debug.DrawLine(_rightBotFront, _rightBotBack, c);
                        Debug.DrawLine(_rightBotFront, _leftBotFront, c);

                        //mid
                        Debug.DrawLine(_leftBotBack, _leftTopBack, c);
                        Debug.DrawLine(_rightBotBack, _rightTopBack, c);
                        Debug.DrawLine(_leftBotFront, _leftTopFront, c);
                        Debug.DrawLine(_rightBotFront, _rightTopFront, c);

                        //top
                        Debug.DrawLine(_leftTopBack, _rightTopBack, c);
                        Debug.DrawLine(_leftTopBack, _leftTopFront, c);
                        Debug.DrawLine(_rightTopFront, _rightTopBack, c);
                        Debug.DrawLine(_rightTopFront, _leftTopFront, c);
                    }
                }
            }
        }
        #endregion
    }
Ejemplo n.º 6
0
 public Node(PathFinding.Node _node, Node _parent, int _cost)
 {
     node   = _node;
     parent = _parent;
     cost   = _cost;
 }
Ejemplo n.º 7
0
 public Node(PathFinding.Node _node)
 {
     node = _node;
 }
Ejemplo n.º 8
0
    //bugs
    //when out of bounds, will travel to last known destination
    //when out of range, will repeat same checks and slow down game
    //bake is raar met specifieke objecten
    //z werkt niet goed met pathfinding

    private IEnumerator CalculatePath(Vector3 position, Callable callable) //omhoog / omlaag moet ook werken. hiervoor moet schuin gaan kunnen
    {
        PathFinding.Node destination = p.GetNodeFromVector(position);

        //pathfinding tools
        open   = new List <Node>();
        closed = new List <PathFinding.Node>();

        Pathfinding_Visualizer pV = PathFinding.visualizer;

        PathFinding.Node start     = p.GetNodeFromVector(transform.position);
        PathFinding.Node reference = start;

        int nodesBetween = 0;
        int y;

        if (start != null)
        {
            while (nodesBetween < maxNodesBetweenGroundAndTarget)
            {
                y = reference.y - nodesBetween;
                if (!(y > 0 && y < p.grid.GetLength(1)))
                {
                    nodesBetween++;
                    continue;
                }
                reference = p.grid[start.x, y, start.z];
                nodesBetween++;
                if (reference.filled && reference.bakeType == PathFinding.BakeType.Object)
                {
                    break;
                }
            }

            if (start.filled)
            {
                open.Add(new Node(reference));
            }
            else
            {
                Debug.Log("There is nothing walkable around the start point.");
            }
        }
        else
        {
            Debug.Log("Currently not in a walkable area, unable to create a path.");
        }

        if (start != null)
        {
            reference    = destination;
            nodesBetween = -maxNodesBetweenGroundAndTargetReverse;
            y            = 0;
            if (destination != null)
            {
                while (nodesBetween < maxNodesBetweenGroundAndTarget)
                {
                    y = reference.y - nodesBetween;
                    if (!(y > 0 && y < p.grid.GetLength(1)))
                    {
                        nodesBetween++;
                        continue;
                    }
                    reference = p.grid[start.x, y, start.z]; //BUG
                    nodesBetween++;
                    if (reference.filled && reference.bakeType == PathFinding.BakeType.Object)
                    {
                        break;
                    }
                }
            }
        }

        //cost calculation
        if (start != null)
        {
            origin = new Vector3(start.x, start.y, start.z);
        }
        if (destination != null)
        {
            dest = new Vector3(destination.x, destination.y, destination.z);
        }
        int checks = 0;

        //main loop
        if (destination != null)
        {
            while (open.Count > 0 && closed.Count < maxNodesCheckable) //hij gaat nu via het gevulde een pad zoeken ipv bovenop het pad
            {
                open.Sort();
                PathFinding.Node node;
                node = open[0].node;
                if (closed.Contains(node))
                {
                    open.RemoveAt(0);
                    continue;
                }

                if (p.visualize)
                {
                    //in dit geval weet je al dat +1 bestaat omdat dat een check gaat worden
                    pV.ChangeColorGridPart(p.grid[node.x, node.y + 1, node.z], Color.green);
                }

                checks++;
                if (checks >= checksPerFrame)
                {
                    checks = 0;
                    yield return(null);
                }

                adjecentNodes = new List <Node>();

                nodeToCheck = open[0];
                n           = nodeToCheck.node;

                //check if destination
                if (Vector3.Distance(dest, new Vector3(n.x, n.y, n.z)) <= stoppingNodeDistance)
                {
                    break;
                }

                open.RemoveAt(0);
                closed.Add(nodeToCheck.node);

                //front
                PrepareNode(n.x, n.y, n.z + 1);
                //back
                PrepareNode(n.x, n.y, n.z - 1);
                //right
                PrepareNode(n.x + 1, n.y, n.z);
                //left
                PrepareNode(n.x - 1, n.y, n.z);

                if (!_2d)
                {
                    //top checks
                    PrepareNode(n.x, n.y + 1, n.z + 1);
                    PrepareNode(n.x + 1, n.y + 1, n.z + 1);
                    PrepareNode(n.x + 1, n.y + 1, n.z);
                    PrepareNode(n.x + 1, n.y + 1, n.z - 1);
                    PrepareNode(n.x, n.y + 1, n.z - 1);
                    PrepareNode(n.x - 1, n.y + 1, n.z - 1);
                    PrepareNode(n.x - 1, n.y + 1, n.z);
                    PrepareNode(n.x - 1, n.y + 1, n.z + 1);

                    //bottom checks
                    PrepareNode(n.x, n.y - 1, n.z + 1);
                    PrepareNode(n.x + 1, n.y - 1, n.z + 1);
                    PrepareNode(n.x + 1, n.y - 1, n.z);
                    PrepareNode(n.x + 1, n.y - 1, n.z - 1);
                    PrepareNode(n.x, n.y - 1, n.z - 1);
                    PrepareNode(n.x - 1, n.y - 1, n.z - 1);
                    PrepareNode(n.x - 1, n.y - 1, n.z);
                    PrepareNode(n.x - 1, n.y - 1, n.z + 1);
                }

                foreach (Node _node in adjecentNodes)
                {
                    open.Add(_node);
                }
            }
        }

        //check if path has been found
        //if curnode != null
        //if curnode == destination

        //convert everything to a vector3 list
        List <Vector3> _path = new List <Vector3>();
        Node           curNode;
        Vector3        pos;

        if (open.Count > 0)
        {
            curNode = open[0];
            open.RemoveAt(0);
            while (curNode.parent != null)
            {
                //convert
                pos    = p.GetVectorFromNode(curNode.node);
                pos.y += p.heightSizeNode;
                if (p.visualize)
                {
                    pV.ChangeColorGridPart(curNode.node, Color.red);
                }
                _path.Add(pos);
                curNode = curNode.parent;
            }
            _path.Add(p.GetVectorFromNode(curNode.node));
        }
        calculate = null;
        if (_path.Count == 0)
        {
            Debug.Log("No path could be found.");
        }

        if (callable != null)
        {
            callable(_path);
        }
    }