Esempio n. 1
0
    public Godot.Collections.Array GetPath(Vector2 source, Vector2 target)
    {
        Vector2 cellSource = _tileMap.WorldToMap(source);
        Vector2 cellTarget = _tileMap.WorldToMap(target);

        int sourceId = getPointID((int)cellSource.x, (int)cellSource.y);
        int targetId = getPointID((int)cellTarget.x, (int)cellTarget.y);

        Godot.Collections.Array worldPath = new Godot.Collections.Array();

        if (!_aStar.HasPoint(sourceId) || !_aStar.HasPoint(targetId))
        {
            return(worldPath);
        }

        Vector2[] cellPath = _aStar.GetPointPath(sourceId, targetId);

        if (cellPath == null || cellPath.Length == 0)
        {
            return(worldPath);
        }


        // Reverse adding the points
        for (int index = 0; index < cellPath.Length; index++)
        {
            int id = getPointID((int)cellPath[index].x, (int)cellPath[index].y);
            worldPath.Add(_tilestoWorld[id]);
        }

        return(worldPath);
    }