Example #1
0
        private void GenerateMapMeshes()
        {
            _MapTileMeshes.Clear();
            Dictionary <string, List <TileInfo> > tiles = new Dictionary <string, List <TileInfo> >();

            for (int layer = 0; layer < MapData.NUM_LAYERS; layer++)
            {
                for (int x = 0; x < _mapInstance.GetMapData().GetWidth(); x++)
                {
                    for (int y = 0; y < _mapInstance.GetMapData().GetHeight(); y++)
                    {
                        Tuple <int, int> tileInfo = _mapInstance.GetMapData().GetTile(layer, x, y);
                        if (tileInfo == null)
                        {
                            continue;
                        }
                        int tileID = tileInfo.Item1;
                        if (tileID == 0)
                        {
                            continue;
                        }
                        int tilesetID = tileInfo.Item2;
                        if (tilesetID == -1)
                        {
                            continue;
                        }

                        TilesetData.Tileset tileset = TilesetData.GetTileset(tilesetID);

                        TileInfo tileInfo2;
                        tileInfo2.Layer     = layer;
                        tileInfo2.X         = x;
                        tileInfo2.Y         = y;
                        tileInfo2.TileID    = tileID;
                        tileInfo2.TilesetID = tilesetID;

                        string key;

                        if (tileID < 8)
                        {
                            key = "AutoTiles/" + tileset.GetAutoTile(tileID - 1) + "," + tilesetID + "," + tileID;
                        }
                        else
                        {
                            key = "Tilesets/" + tileset.ImagePath + "," + tilesetID;
                        }

                        if (!tiles.ContainsKey(key))
                        {
                            tiles.Add(key, new List <TileInfo>());
                        }

                        tiles[key].Add(tileInfo2);
                    }
                }
            }

            int[] hashValues = new int[] { 1, 2, 4, 8, 16, 32, 64, 128 };
            int[,] offsets = new int[, ] {
                { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 }
            };
            Tuple <int, int> targetInfo;

            for (int i = 0; i < tiles.Count; i++)
            {
                string key                  = tiles.ElementAt(i).Key;
                string textureName          = key.Split(',')[0];
                int    tilesetID            = tiles.ElementAt(i).Value[0].TilesetID;
                TilesetData.Tileset tileset = TilesetData.GetTileset(tilesetID);
                Texture             texture = Assets.GetTexture(textureName);
                bool isAutoTiles            = textureName.StartsWith("AutoTiles/");

                MapTileMeshData regularMeshData    = new MapTileMeshData(false, isAutoTiles, tileset, texture);
                MapTileMeshData reflectionMeshData = new MapTileMeshData(true, isAutoTiles, tileset, texture);

                for (int j = 0; j < tiles[key].Count; j++)
                {
                    int layer    = tiles[key][j].Layer;
                    int x        = tiles[key][j].X;
                    int y        = tiles[key][j].Y;
                    int tileID   = tiles[key][j].TileID;
                    int hashCode = -1;

                    if (isAutoTiles)
                    {
                        hashCode = 0;
                        for (int k = 0; k < 8; k++)
                        {
                            targetInfo = _mapInstance.GetMapData().GetTile(layer, x + offsets[k, 0], y + offsets[k, 1]);
                            if (targetInfo == null)
                            {
                                hashCode += hashValues[k];
                            }
                            else if (targetInfo.Item1 == tileID && targetInfo.Item2 == tilesetID)
                            {
                                hashCode += hashValues[k];
                            }
                        }
                    }

                    bool isReflection = tileset.GetReflectionFlag(tileID);
                    if (isReflection)
                    {
                        if (isAutoTiles)
                        {
                            reflectionMeshData.AddAutoMapTile(tileID, x, y, layer, hashCode);
                            reflectionMeshData.NumFrames  = texture.GetWidth() / 96;
                            reflectionMeshData.AutoTileID = tileID;
                        }
                        else
                        {
                            reflectionMeshData.AddRegularMapTile(tileID, x, y, layer);
                        }
                    }
                    else
                    {
                        if (isAutoTiles)
                        {
                            regularMeshData.AddAutoMapTile(tileID, x, y, layer, hashCode);
                            regularMeshData.NumFrames  = texture.GetWidth() / 96;
                            regularMeshData.AutoTileID = tileID;
                        }
                        else
                        {
                            regularMeshData.AddRegularMapTile(tileID, x, y, layer);
                        }
                    }
                }

                regularMeshData.Ready    = true;
                reflectionMeshData.Ready = true;

                if (regularMeshData.Valid())
                {
                    _MapTileMeshes.Add(regularMeshData);
                }
                if (reflectionMeshData.Valid())
                {
                    _MapTileMeshes.Add(reflectionMeshData);
                }
            }
        }