Beispiel #1
0
    void Click()
    {
        Vector3 scenePos = scene.transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        Vector2 nodePos  = IsoUtil.ScreenToIsoGrid(scene.size, scenePos.x, scenePos.y);

        int nodeX = (int)nodePos.x;
        int nodeZ = (int)nodePos.y;

        if (grid.CheckInGrid(nodeX, nodeZ) && grid.GetNode(nodeX, nodeZ).walkable)
        {
            IsoObject obj = (IsoObject)Instantiate(isoObjectPrefab);
            obj.SetNodePosition(nodeX, nodeZ);
            if (obj.GetWalkable(grid))
            {
                scene.AddIsoObject(obj);
                obj.SetNodePosition(nodeX, nodeZ);
                obj.transform.localScale = Vector3.one;
                obj.SetWalkable(false, grid);
            }
            else
            {
                Destroy(obj.gameObject);
            }
        }
    }
Beispiel #2
0
    public bool GetWalkable(PathGrid grid)
    {
        bool flag = false;

        foreach (Vector3 v in m_spanPosArray)
        {
            int nodeX = Mathf.FloorToInt(v.x / size);
            int nodeY = Mathf.FloorToInt(v.z / size);
            if (nodeX < 0 || nodeX > grid.gridX - 1)
            {
                return(false);
            }
            if (nodeY < 0 || nodeY > grid.gridZ - 1)
            {
                return(false);
            }
            flag = grid.GetNode(nodeX, nodeY).walkable;
            if (!flag)
            {
                return(false);
            }
        }
        return(true);
    }
Beispiel #3
0
    public void FindPath(PathGrid g, Vector3 startPos, Vector3 endPos)
    {
        //Translate start and end position into nodes on this
        //agents map
        PathNode start = g.GetNode(startPos);
        PathNode end   = g.GetNode(endPos);

        //Two lists initialised to represent possible and impossible nodes for the path
        List <PathNode> openNodes   = new List <PathNode>();
        List <PathNode> closedNodes = new List <PathNode>();

        //Initialise by adding the first node
        openNodes.Add(start);

        //While the open list has not exhausted all of the nodes (and a solution has not been found)
        while (openNodes.Count > 0)
        {
            //Initialise the current node to the first in the open list
            PathNode current = openNodes[0];

            //Cycle through the open list, if the f cost is less than the current,
            //or it is equal but the heuristic cost is lower, make this node the current
            for (int i = 1; i < openNodes.Count; i++)
            {
                if (openNodes[i].getFCost() < current.getFCost() ||
                    openNodes[i].getFCost() == current.getFCost() && openNodes[i].hCost < current.hCost)
                {
                    current = openNodes[i];
                }
            }

            //Remove the node with the lowest predicted cost from the open list and add
            //it to the closed list
            openNodes.Remove(current);
            closedNodes.Add(current);

            //Break the while loop if the target node has been reached
            if (current == end)
            {
                InvertPath(start, end, g);
                return;
            }

            //Cycle through the current nodes neighbours, ignoring the ones
            //that are blocked
            foreach (PathNode n in g.GetNeighbours(current))
            {
                if (!n.canWalk || closedNodes.Contains(n))
                {
                    continue;
                }

                //Establish the neighbouring node with the lowest predicted cost
                //Add it to the path list
                int movementCost = current.gCost + GetNodeDistance(current, n);
                if (movementCost < n.gCost || !openNodes.Contains(n))
                {
                    n.gCost  = movementCost;
                    n.hCost  = GetNodeDistance(n, end);
                    n.parent = current;

                    if (!openNodes.Contains(n))
                    {
                        openNodes.Add(n);
                    }
                }
            }
        }
    }