Ejemplo n.º 1
0
        public void ImportMerge(TextureResource image, TileImportOptions options)
        {
            int tilesWide = (image.Width - options.MarginX) / (TileWidth + options.SpaceX);
            int tilesHigh = (image.Height - options.MarginY) / (TileHeight + options.SpaceY);

            Dictionary <string, Tile> existingHashes = new Dictionary <string, Tile>();
            Dictionary <string, Tile> newHashes      = new Dictionary <string, Tile>();

            using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) {
                foreach (Tile tile in _tiles)
                {
                    TextureResource tileTex = Tiles.GetTileTexture(tile.Uid);
                    string          hash    = Convert.ToBase64String(sha1.ComputeHash(tileTex.RawData));
                    existingHashes[hash] = tile;
                }

                for (int y = 0; y < tilesHigh; y++)
                {
                    for (int x = 0; x < tilesWide; x++)
                    {
                        Rectangle srcLoc = new Rectangle(
                            options.MarginX + x * (TileWidth + options.SpaceX),
                            options.MarginY + y * (TileHeight + options.SpaceY),
                            TileWidth, TileHeight);

                        TextureResource tileTex = image.Crop(srcLoc);
                        string          hash    = Convert.ToBase64String(sha1.ComputeHash(tileTex.RawData));

                        if (options.ImportPolicty == TileImportPolicy.SourceUnique && newHashes.ContainsKey(hash))
                        {
                            continue;
                        }
                        else if (options.ImportPolicty == TileImportPolicy.SetUnique && existingHashes.ContainsKey(hash))
                        {
                            continue;
                        }

                        Tile newTile = _tiles.Add(tileTex);

                        existingHashes[hash] = newTile;
                        newHashes[hash]      = newTile;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static Tile FromXmlProxy(LibraryX.TileDefX proxy, TileResourceCollection tileCollection)
        {
            if (proxy == null)
            {
                return(null);
            }

            string[] loc = proxy.Location.Split(new char[] { ',' });
            if (loc.Length != 2)
            {
                throw new Exception("Malformed location: " + proxy.Location);
            }

            int x = Convert.ToInt32(loc[0]);
            int y = Convert.ToInt32(loc[1]);

            if (x < 0 || y < 0)
            {
                throw new Exception("Invalid location: " + proxy.Location);
            }

            TileCoord coord = new TileCoord(x, y);
            Tile      tile  = new PhysicalTile(proxy.Uid)
            {
                Pool = tileCollection._pool,
            };

            if (proxy.Properties != null)
            {
                foreach (var propertyProxy in proxy.Properties)
                {
                    tile.PropertyManager.CustomProperties.Add(Property.FromXmlProxy(propertyProxy));
                }
            }

            tileCollection._locations[tile.Uid] = coord;
            tileCollection.Add(tile);

            return(tile);
        }