Ejemplo n.º 1
0
    public void RecalculateVisibilityMap()
    {
        Profiler.BeginSample("los");
        mesh = GetComponent <TacticsTerrainMesh>();

        if (seenMap == null)
        {
            seenMap = new bool[mesh.size.x, mesh.size.y];
            for (int y = 0; y < mesh.size.y; y += 1)
            {
                for (int x = 0; x < mesh.size.x; x += 1)
                {
                    seenMap[x, y] = false;
                }
            }
            if (Application.isPlaying)
            {
                pc = Global.Instance().Maps.pc;
            }
            if (pc == null)
            {
                pc = FindObjectOfType <PCEvent>();
            }
        }

        if (oldLosTexture != null)
        {
            Destroy(oldLosTexture);
        }
        oldLosTexture         = losTexture;
        oldHeroPos            = heroPos;
        losTexture            = new Texture2D(mesh.size.x, mesh.size.y, TextureFormat.ARGB32, false);
        losTexture.filterMode = FilterMode.Point;
        losTexture.wrapMode   = TextureWrapMode.Clamp;
        heroPos  = pc.GetComponent <MapEvent3D>().TileToWorldCoords(pc.GetComponent <MapEvent>().location);
        heroPos += new Vector3(0.5f, 1.5f, 0.5f);

        map = new Color[mesh.size.x * mesh.size.y];
        BattleEvent battler = pc.GetComponent <BattleEvent>();
        Vector2Int  v       = new Vector2Int();

        for (int y = 0; y < mesh.size.y; y += 1)
        {
            for (int x = 0; x < mesh.size.x; x += 1)
            {
                float r = 1.0f;
                float b = 1.0f;
                v.x = x;
                v.y = y;
                if (battler.CanSeeLocation(mesh, v))
                {
                    seenMap[x, y] = true;
                }
                else
                {
                    r = 0.0f;
                    b = seenMap[x, y] ? 1.0f : 0.0f;
                }
                map[y * mesh.size.x + x] = new Color(r, 0.0f, b);
            }
        }
        losTexture.SetPixels(0, 0, mesh.size.x, mesh.size.y, map);
        losTexture.Apply();

        foreach (PCVisibility vis in GetComponent <Map>().GetEvents <PCVisibility>())
        {
            Vector2Int location = vis.GetComponent <MapEvent>().location;
            vis.SetVisibleByPC(CachedIsVisible(location));
        }

        Profiler.EndSample();
    }