Example #1
0
        private void RefreshTile(CellTypeMap cellTypeMap, Vector2Int tileXY)
        {
            int x = tileXY.x;
            int y = tileXY.y;

            int tileMappingIndex = GetTileMappingIndex(cellTypeMap, tileXY);

            OP2UtilityDotNet.OP2Map.TileMapping mapping = UserData.current.map.tileMappings[tileMappingIndex];

            int tileSetIndex   = mapping.tilesetIndex;
            int tileImageIndex = mapping.tileGraphicIndex;

            string tileSetPath     = UserData.current.map.tilesetSources[tileSetIndex].tilesetFilename;
            int    tileSetNumTiles = (int)UserData.current.map.tilesetSources[tileSetIndex].numTiles;

            // Get tile texture
            Texture2D texture = TextureManager.LoadTileset(tileSetPath);

            if (texture == null)
            {
                throw new System.Exception("Could not find resource: " + tileSetPath);
            }

            // Get image offset
            int tileSize         = texture.height / tileSetNumTiles;
            int inverseTileIndex = tileSetNumTiles - tileImageIndex - 1;
            int texOffset        = inverseTileIndex * tileSize;

            // Load tile into tile map
            TileBase   tile         = LoadTile(tileSetPath, texOffset);
            Vector3Int cellPosition = new Vector3Int((int)x, m_Tilemap.size.y - (int)y - 1, 0);

            // Set minimap pixel
            Texture2D mTexture = TextureManager.LoadMinimapTileset(tileSetPath, tileSetNumTiles);
            Color     color    = mTexture.GetPixel(0, inverseTileIndex);

            minimapTexture.SetPixel(cellPosition.x, cellPosition.y, color);

            // Set tiles
            m_Tilemap.SetTile(cellPosition, tile);
            m_CellTypeMap.SetTile(cellPosition, m_CellTypeCache[(int)UserData.current.map.GetCellType(x, y)]);
            minimapTexture.Apply();
        }
Example #2
0
        private IEnumerator _Refresh(System.Action onCompleteCB)
        {
            // Create grid overlay tile
            Tile gridOverlayTile = ScriptableObject.CreateInstance <Tile>();

            gridOverlayTile.sprite = m_GridOverlayTile;
            gridOverlayTile.color  = Color.white;

            m_Tilemap.ClearAllTiles();
            m_GridOverlay.ClearAllTiles();
            m_CellTypeMap.ClearAllTiles();

            m_TileCache.Clear();

            // Cache calculated CellTypes
            onMapRefreshProgressCB?.Invoke(this, "Calculating Cell Types", 0.0f);
            yield return(null);

            CellTypeMap cellTypeMap = new CellTypeMap(UserData.current.map, UserData.current.GetCombinedMission());

            int mapWidth  = (int)UserData.current.map.WidthInTiles();
            int mapHeight = (int)UserData.current.map.HeightInTiles();

            minimapTexture = new Texture2D((int)mapWidth * TextureManager.minimapScale, (int)mapHeight * TextureManager.minimapScale, TextureFormat.ARGB32, false);

            Vector3Int[] cellPositions = new Vector3Int[(int)(mapWidth * mapHeight)];
            TileBase[]   cellTiles     = new TileBase[(int)(mapWidth * mapHeight)];
            TileBase[]   cellTypes     = new TileBase[(int)(mapWidth * mapHeight)];
            int          index         = 0;

            int updateFrequency = 5;

            for (int x = 0; x < mapWidth; ++x)
            {
                if (index % updateFrequency == 0)
                {
                    onMapRefreshProgressCB?.Invoke(this, "Reading tiles", (float)(x * mapHeight) / (mapWidth * mapHeight));
                    yield return(null);
                }

                for (int y = 0; y < mapHeight; ++y)
                {
                    int tileMappingIndex = GetTileMappingIndex(cellTypeMap, new Vector2Int(x, y));
                    OP2UtilityDotNet.OP2Map.TileMapping mapping = UserData.current.map.tileMappings[tileMappingIndex];

                    int tileSetIndex   = mapping.tilesetIndex;
                    int tileImageIndex = mapping.tileGraphicIndex;

                    string tileSetPath     = UserData.current.map.tilesetSources[tileSetIndex].tilesetFilename;
                    int    tileSetNumTiles = (int)UserData.current.map.tilesetSources[tileSetIndex].numTiles;

                    // Get tile texture
                    Texture2D texture = TextureManager.LoadTileset(tileSetPath);
                    if (texture == null)
                    {
                        onCompleteCB?.Invoke();
                        onMapRefreshedCB?.Invoke(this);
                        throw new System.Exception("Could not find resource: " + tileSetPath);
                    }

                    // Get image offset
                    int tileSize         = texture.height / tileSetNumTiles;
                    int inverseTileIndex = tileSetNumTiles - tileImageIndex - 1;
                    int texOffset        = inverseTileIndex * tileSize;

                    // Load tile into tile map
                    TileBase   tile         = LoadTile(tileSetPath, texOffset);
                    Vector3Int cellPosition = new Vector3Int((int)x, (int)(mapHeight - y - 1), 0);

                    cellPositions[index] = cellPosition;
                    cellTiles[index]     = tile;
                    cellTypes[index]     = m_CellTypeCache[(int)UserData.current.map.GetCellType(x, y)];

                    ++cellPosition.y;

                    // Set minimap pixel
                    Texture2D mTexture = TextureManager.LoadMinimapTileset(tileSetPath, tileSetNumTiles);
                    for (int my = 0; my < TextureManager.minimapScale; ++my)
                    {
                        for (int mx = 0; mx < TextureManager.minimapScale; ++mx)
                        {
                            Color color = mTexture.GetPixel(mx, inverseTileIndex * TextureManager.minimapScale + my);
                            minimapTexture.SetPixel(cellPosition.x * TextureManager.minimapScale + mx, cellPosition.y * TextureManager.minimapScale + my - 1, color);
                        }
                    }

                    ++index;
                }
            }

            m_GridOverlay.color = UserPrefs.gridOverlayColor;
            m_GridOverlay.gameObject.SetActive(UserPrefs.isGridOverlayVisible);

            onMapRefreshProgressCB?.Invoke(this, "Setting tiles", 1);
            yield return(null);

            // Set tiles
            m_Tilemap.SetTiles(cellPositions, cellTiles);

            // Create cell types
            onMapRefreshProgressCB?.Invoke(this, "Setting cell types", 1);
            yield return(null);

            m_CellTypeMap.SetTiles(cellPositions, cellTypes);

            // Create grid
            onMapRefreshProgressCB?.Invoke(this, "Creating grid", 1);
            yield return(null);

            TileBase[] overlayTiles = new TileBase[cellPositions.Length];
            for (int i = 0; i < overlayTiles.Length; ++i)
            {
                overlayTiles[i] = gridOverlayTile;
            }

            m_GridOverlay.SetTiles(cellPositions, overlayTiles);

            // Apply minimap texture
            minimapTexture.Apply();

            onMapRefreshProgressCB?.Invoke(this, "Creating units", 1);
            yield return(null);

            // Create units
            m_UnitRenderer.Refresh(() =>
            {
                // Inform listeners that we are done
                onCompleteCB?.Invoke();
                onMapRefreshedCB?.Invoke(this);
            });
        }