コード例 #1
0
    public void ResetAffectedTiles()
    {
        List <NodeComponent> affectedTiles = new List <NodeComponent>();

        affectedTiles.AddRange(Open.Values);
        affectedTiles.AddRange(Closed);
        affectedTiles.Add(Start);
        affectedTiles.Add(End);

        for (int i = 0; i < affectedTiles.Count; i++)
        {
            NodeComponent affected = affectedTiles[i];

            affected.ResetNode();
            affected.GetComponent <SpriteRenderer>().color = Color.white;
        }
    }
コード例 #2
0
    // 1 => path found!, 0 => next step required, -1 => no path possible
    public int NextStep()
    {
        if (Open.Count == 0)
        {
            return(-1);
        }

        NodeComponent currentTile = Open.Pop();

        if (currentTile == null)
        {
            return(-1);
        }

        if (currentTile == End)
        {
            return(1);
        }

        List <NodeComponent> neighbours = currentTile.GetComponent <TileComponent>().GetCrossNeighbours();

        if (DoDiagonal)
        {
            neighbours.AddRange(currentTile.GetComponent <TileComponent>().GetDiagonalNeighbours());
        }

        for (int i = 0; i < neighbours.Count; i++)
        {
            NodeComponent currentNeighbour = neighbours[i];

            if (Open.Contains(currentNeighbour))
            {
                float currentF = currentTile.GetG() + currentNeighbour.GetH() + Vector3.Magnitude(currentNeighbour.transform.position - currentTile.transform.position);

                if (currentF < currentNeighbour.GetF())
                {
                    Open.Remove(currentNeighbour);
                    Open.Add(currentNeighbour);
                }
            }
            else if (Closed.Contains(currentNeighbour))
            {
                float currentF = currentTile.GetG() + currentNeighbour.GetH() + Vector3.Magnitude(currentNeighbour.transform.position - currentTile.transform.position);

                if (currentF < currentNeighbour.GetF())
                {
                    Closed.Remove(currentNeighbour);
                    Open.Add(currentNeighbour);
                }
            }
            else
            {
                FormatNode(currentNeighbour, currentTile);
                Open.Add(currentNeighbour);
            }
        }

        ChangeTileToColor(currentTile, Color.blue);
        Closed.Add(currentTile);

        return(0);
    }
コード例 #3
0
    void ChangeTileToColor(NodeComponent _node, Color _color)
    {
        SpriteRenderer sprite = _node.GetComponent <SpriteRenderer>();

        sprite.color = _color;
    }