Exemple #1
0
    public int Compare(int a, int b)
    {
        float tileA = (_tileAStar.Get(a).distance + _tileAStar.Get(a).heuristic *_heuristicWeight);
        float tileB = (_tileAStar.Get(b).distance + _tileAStar.Get(b).heuristic *_heuristicWeight);

        if (tileA < tileB)
        {
            return(-1);
        }
        else if (tileA > tileB)
        {
            return(1);
        }
        return(0);
    }
Exemple #2
0
    public void Render(TileAStar tileAStar, int endX, int endY, int nodeSizeX, int nodeSizeY, Material pathMaterial)
    {
        List <Vector3> path  = new List <Vector3>();
        int            index = endX + endY * tileAStar.Width;

        while (index != -1)
        {
            path.Add(GetIndexCoords(tileAStar, index, nodeSizeX, nodeSizeY));
            index = tileAStar.Get(index).parent;
        }

        if (path.Count < 2)
        {
            return;
        }

        const float cornerSharpness = 0.75f;

        _lineRenderer.SetVertexCount(path.Count * 2 - 2);
        _lineRenderer.SetPosition(0, path[0]);
        for (int i = 1; i < path.Count - 1; ++i)
        {
            _lineRenderer.SetPosition(2 * i - 1, Vector3.Lerp(path[i - 1], path[i], cornerSharpness));
            _lineRenderer.SetPosition(2 * i, Vector3.Lerp(path[i + 1], path[i], cornerSharpness));
        }
        _lineRenderer.SetPosition(path.Count * 2 - 3, path[path.Count - 1]);

        _lineRenderer.sharedMaterial = pathMaterial;
    }
Exemple #3
0
    public void Render(TileAStar tileAStar, int minX, int minY, int width, int height, int nodeSizeX, int nodeSizeY, float distance, Material atlasMaterial)
    {
        width  = Mathf.Min(width, tileAStar.Width - minX);
        height = Mathf.Min(height, tileAStar.Height - minY);

        const int maxVerts = 65000;

        if (width * height * 4 > maxVerts)
        {
            Debug.LogError("Render tile dimensions too large: " + width + " * " + height + " = " + (width * height * 4) + " (> " + maxVerts + ")");
            return;
        }

        Vector3[] verts  = new Vector3[width * height * 8];
        Vector2[] uvs    = new Vector2[width * height * 8];
        Color32[] colors = new Color32[width * height * 8];
        int[]     tris   = new int[width * height * 6];

        int index = 0;

        for (int y = minY; y < minY + height; ++y)
        {
            for (int x = minX; x < minX + width; ++x, ++index)
            {
                int nodeIndex = x + y * tileAStar.Width;

                Color32 c;
                if (tileAStar.Get(nodeIndex).distance < 0.0f)
                {
                    c = Color.clear;
                }
                else if (tileAStar.Get(nodeIndex).distance >= TileAStar.INFINITY)
                {
                    c = new Color(0f, 0.1f, .4f, 1.0f);
                }
                else
                {
                    float value = Mathf.Clamp01(tileAStar.Get(nodeIndex).distance / distance);
                    c = new Color(1.0f, value, value, 1.0f);
                }

                verts[index * 4 + 0]  = new Vector3(x, y, 0);
                uvs[index * 4 + 0]    = new Vector2(0, 0);
                colors[index * 4 + 0] = c;

                verts[index * 4 + 1]  = new Vector3(x, y + 1, 0);
                uvs[index * 4 + 1]    = new Vector2(0, 1);
                colors[index * 4 + 1] = c;

                verts[index * 4 + 2]  = new Vector3(x + 1, y + 1, 0);
                uvs[index * 4 + 2]    = new Vector2(1, 1);
                colors[index * 4 + 2] = c;

                verts[index * 4 + 3]  = new Vector3(x + 1, y, 0);
                uvs[index * 4 + 3]    = new Vector2(1, 0);
                colors[index * 4 + 3] = c;

                tris[index * 6 + 0] = index * 4 + 0;
                tris[index * 6 + 1] = index * 4 + 1;
                tris[index * 6 + 2] = index * 4 + 2;
                tris[index * 6 + 3] = index * 4 + 2;
                tris[index * 6 + 4] = index * 4 + 3;
                tris[index * 6 + 5] = index * 4 + 0;
            }
        }

        _mesh.Clear();
        _mesh.vertices  = verts;
        _mesh.uv        = uvs;
        _mesh.colors32  = colors;
        _mesh.triangles = tris;

        _meshFilter.sharedMesh       = _mesh;
        _meshRenderer.sharedMaterial = atlasMaterial;
    }