Exemplo n.º 1
0
    public bool CanSeeLocation(TacticsTerrainMesh mesh, Vector2Int to)
    {
        LineOfSightEffect los = battle.GetComponent <LineOfSightEffect>();

        if (los.sitemap == null)
        {
            los.RegenSitemap(mesh);
        }
        if (sight == 0)
        {
            sight = unit.Get(StatTag.SIGHT);
        }
        //return MathHelper3D.InLos(mesh, this.location, location, sight);
        Vector2Int at    = location;
        Vector3    at3   = new Vector3(at.x + 0.5f, mesh.HeightAt(at.x, at.y) + 1.5f, at.y + 0.5f);
        Vector3    to3   = new Vector3(to.x + 0.5f, mesh.HeightAt(to.x, to.y) + 1.5f, to.y + 0.5f);
        Vector3    delta = (to3 - at3);

        if (delta.sqrMagnitude > sight * sight)
        {
            return(false);
        }
        else
        {
            return(los.sitemap[
                       to.y * (mesh.size.x * mesh.size.y * mesh.size.x) +
                       to.x * (mesh.size.x * mesh.size.y) +
                       at.y * (mesh.size.x) +
                       at.x]);
        }
    }
Exemplo n.º 2
0
    public void ConfigureNewGrid(Vector2Int at, Vector2Int size, TacticsTerrainMesh terrain,
                                 Func <Vector2Int, bool> rangeRule, Func <Vector2Int, bool> selectRule)
    {
        transform.position = new Vector3(0.0f, 0.0f, 0.0f);
        LineOfSightEffect los = terrain.GetComponent <LineOfSightEffect>();

        MeshFilter filter = this.mesh;

        if (filter.mesh != null)
        {
            Destroy(filter.mesh);
        }
        Mesh mesh = new Mesh();

        filter.mesh = mesh;

        List <Vector3> vertices = new List <Vector3>();
        List <Vector2> uvs      = new List <Vector2>();
        List <int>     tris     = new List <int>();

        for (int y = at.y; y < at.y + size.y; y += 1)
        {
            for (int x = at.x; x < at.x + size.x; x += 1)
            {
                if (terrain.HeightAt(x, y) == 0.0f || !los.CachedIsVisible(new Vector2Int(x, y)))
                {
                    continue;
                }
                bool selectable = selectRule(new Vector2Int(x, y));
                bool rangeable  = rangeRule(new Vector2Int(x, y));
                if (selectable || rangeable)
                {
                    int   vertIndex = vertices.Count;
                    float height    = terrain.HeightAt(x, y);
                    for (int i = 0; i < 4; i += 1)
                    {
                        if (selectable)
                        {
                            uvs.Add(new Vector2(
                                        i % 2 == 0 ? .5f : 0,
                                        i < 2 ? .5f : 0));
                        }
                        else if (rangeable)
                        {
                            uvs.Add(new Vector2(
                                        i % 2 == 0 ? 1 : .5f,
                                        i < 2 ? .5f : 0));
                        }
                        vertices.Add(new Vector3(
                                         x + (i % 2 == 0 ? 1 : 0),
                                         height,
                                         y + (i < 2 ? 1 : 0)));
                    }
                    tris.Add(vertIndex);
                    tris.Add(vertIndex + 2);
                    tris.Add(vertIndex + 1);
                    tris.Add(vertIndex + 1);
                    tris.Add(vertIndex + 2);
                    tris.Add(vertIndex + 3);
                }
            }
        }

        mesh.vertices  = vertices.ToArray();
        mesh.uv        = uvs.ToArray();
        mesh.triangles = tris.ToArray();
        mesh.RecalculateNormals();
    }