Ejemplo n.º 1
0
    public Script_Node(Vector3Int p_position, Vector3Int p_destination, Script_Node p_parent)
    {
        _position    = p_position;
        _destination = p_destination;
        _parent      = p_parent;


        if (p_parent != null)
        {
            _gScore = p_parent.GetGScore();
        }

        _gScore = 0;
        _hScore = 0;
        CalculateHScore();
        _fScore = 0;
        CalculateFScore();
    }
Ejemplo n.º 2
0
    private void CalculateNeighbours(Script_Node p_node)
    {
        int xCurrent = p_node.GetNodePosition().x;
        int zCurrent = p_node.GetNodePosition().z;

        int xMin = xCurrent;
        int xMax = xCurrent;
        int zMin = zCurrent;
        int zMax = zCurrent;

        for (int x = xCurrent - 1; x <= xCurrent; x++)
        {
            if (x < 0)
            {
                continue;
            }

            xMin = x;
            break;
        }
        for (int x = xCurrent + 1; x >= xCurrent; x--)
        {
            if (x >= _grid.GetWidth())
            {
                continue;
            }

            xMax = x;
            break;
        }
        for (int z = zCurrent - 1; z <= zCurrent; z++)
        {
            if (z < 0)
            {
                continue;
            }

            zMin = z;
            break;
        }
        for (int z = zCurrent + 1; z >= zCurrent; z--)
        {
            if (z >= _grid.GetHeight())
            {
                continue;
            }

            zMax = z;
            break;
        }

        for (int z = zMin; z <= zMax; z++)
        {
            for (int x = xMin; x <= xMax; x++)
            {
                if (_grid.AccessGridTile(x, z).GetWalkable() == true && !_closedDictionary.ContainsKey(z * _grid.GetWidth() + x))
                {
                    if ((x != xCurrent && z != zCurrent))
                    {
                        if (_grid.AccessGridTile(x, z + (zCurrent - z)) != null && _grid.AccessGridTile(x, z + (zCurrent - z)).GetWalkable() == false)
                        {
                            continue;
                        }

                        if (_grid.AccessGridTile(x + (xCurrent - x), z) != null && _grid.AccessGridTile(x + (xCurrent - x), z).GetWalkable() == false)
                        {
                            continue;
                        }
                    }

                    int tempGScore = 0;
                    if (x == xCurrent || z == zCurrent)
                    {
                        tempGScore = Constants.linearMovementCost;
                    }
                    else
                    {
                        tempGScore = Constants.diagonalMovementCost;
                    }

                    if (_openDictionary.ContainsKey(z * _grid.GetWidth() + x))
                    {
                        Script_Node node = GetOpenListElement(x, z);

                        if (node.GetParent() != null)
                        {
                            tempGScore += p_node.GetGScore();
                        }

                        if (tempGScore < node.GetGScore())
                        {
                            node.SetGScore(tempGScore);
                            node.CalculateFScore();
                            node.SetParent(p_node);
                        }
                    }


                    if (!_openDictionary.ContainsKey(z * _grid.GetWidth() + x))
                    {
                        Vector3Int location = new Vector3Int(x, 0, z);

                        Script_Node newNode = new Script_Node(location, p_node.GetNodeDestination(), p_node);
                        tempGScore += newNode.GetParent().GetGScore();

                        newNode.SetGScore(tempGScore);
                        newNode.CalculateHScore();
                        newNode.CalculateFScore();
                        AddOpenListElementAtPosition(x, z, newNode);
                    }
                }
            }
        }
    }