private void ParseTiles(xTile.Layers.Layer platformLayer, AggregateTileHandler func) { List <TileAggregate> aggregates = new List <TileAggregate>(); Dictionary <Point, TileAggregate> pointToAggregate = new Dictionary <Point, TileAggregate>(); var width = platformLayer.LayerWidth; var height = platformLayer.LayerHeight; var tiles = platformLayer.Tiles; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (tiles[x, y] != null) { string type = ""; string color = ""; Dictionary <string, string> properties = new Dictionary <string, string>(); foreach (var property in tiles[x, y].TileSheet.Properties) { switch (property.Key) { case "Type": type = property.Value; break; case "Color": color = property.Value; break; default: properties[property.Key] = property.Value; break; } } TileAggregate aggregate; if (x > 0 && pointToAggregate.TryGetValue(new Point(x - 1, y), out aggregate) && aggregate.type == type && (aggregate.dir == TileDirection.HORIZONTAL || aggregate.dir == TileDirection.UNDEFINED) && aggregate.color == color) { aggregate.dir = TileDirection.HORIZONTAL; aggregate.rect.Width++; pointToAggregate[new Point(x, y)] = aggregate; } else if (y > 0 && pointToAggregate.TryGetValue(new Point(x, y - 1), out aggregate) && aggregate.type == type && (aggregate.dir == TileDirection.VERTICAL || aggregate.dir == TileDirection.UNDEFINED) && aggregate.color == color) { aggregate.dir = TileDirection.VERTICAL; aggregate.rect.Height++; pointToAggregate[new Point(x, y)] = aggregate; } else { aggregate = new TileAggregate(); aggregate.type = type; aggregate.color = color; aggregate.dir = TileDirection.UNDEFINED; aggregate.rect = new Rectangle(x, y, 1, 1); pointToAggregate[new Point(x, y)] = aggregate; aggregates.Add(aggregate); } foreach (var keyPair in properties) { aggregate.properties[keyPair.Key] = keyPair.Value; } } } } //Combine List <TileAggregate> combinedStripsToRemove = new List <TileAggregate>(); foreach (var aggregate in aggregates) { TileAggregate aboveAggregate; if (aggregate.rect.Y > 0 && pointToAggregate.TryGetValue(new Point(aggregate.rect.X, aggregate.rect.Y - 1), out aboveAggregate) && aboveAggregate.type == aggregate.type && (aboveAggregate.dir == TileDirection.HORIZONTAL || aboveAggregate.dir == TileDirection.COMBINED) && aboveAggregate.color == aggregate.color && aboveAggregate.rect.X == aggregate.rect.X && aboveAggregate.rect.Width == aggregate.rect.Width) { combinedStripsToRemove.Add(aggregate); aboveAggregate.dir = TileDirection.COMBINED; aboveAggregate.rect.Height++; pointToAggregate[new Point(aggregate.rect.X, aggregate.rect.Y)] = aboveAggregate; foreach (var keyPair in aggregate.properties) { aboveAggregate.properties[keyPair.Key] = keyPair.Value; } } } foreach (var aggregate in combinedStripsToRemove) { aggregates.Remove(aggregate); } func(aggregates, pointToAggregate); }
private void ParseTiles(xTile.Layers.Layer platformLayer, AggregateTileHandler func) { List<TileAggregate> aggregates = new List<TileAggregate>(); Dictionary<Point, TileAggregate> pointToAggregate = new Dictionary<Point, TileAggregate>(); var width = platformLayer.LayerWidth; var height = platformLayer.LayerHeight; var tiles = platformLayer.Tiles; for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { if(tiles[x, y] != null) { string type = ""; string color = ""; Dictionary<string, string> properties = new Dictionary<string, string>(); foreach(var property in tiles[x, y].TileSheet.Properties) { switch(property.Key) { case "Type": type = property.Value; break; case "Color": color = property.Value; break; default: properties[property.Key] = property.Value; break; } } TileAggregate aggregate; if(x > 0 && pointToAggregate.TryGetValue(new Point(x-1, y), out aggregate) && aggregate.type == type && (aggregate.dir == TileDirection.HORIZONTAL || aggregate.dir == TileDirection.UNDEFINED) && aggregate.color == color) { aggregate.dir = TileDirection.HORIZONTAL; aggregate.rect.Width++; pointToAggregate[new Point(x, y)] = aggregate; } else if(y > 0 && pointToAggregate.TryGetValue(new Point(x, y-1), out aggregate) && aggregate.type == type && (aggregate.dir == TileDirection.VERTICAL || aggregate.dir == TileDirection.UNDEFINED) && aggregate.color == color) { aggregate.dir = TileDirection.VERTICAL; aggregate.rect.Height++; pointToAggregate[new Point(x, y)] = aggregate; } else { aggregate = new TileAggregate(); aggregate.type = type; aggregate.color = color; aggregate.dir = TileDirection.UNDEFINED; aggregate.rect = new Rectangle(x, y, 1, 1); pointToAggregate[new Point(x, y)] = aggregate; aggregates.Add(aggregate); } foreach(var keyPair in properties) { aggregate.properties[keyPair.Key] = keyPair.Value; } } } } //Combine List<TileAggregate> combinedStripsToRemove = new List<TileAggregate>(); foreach(var aggregate in aggregates) { TileAggregate aboveAggregate; if(aggregate.rect.Y > 0 && pointToAggregate.TryGetValue(new Point(aggregate.rect.X, aggregate.rect.Y-1), out aboveAggregate) && aboveAggregate.type == aggregate.type && (aboveAggregate.dir == TileDirection.HORIZONTAL || aboveAggregate.dir == TileDirection.COMBINED) && aboveAggregate.color == aggregate.color && aboveAggregate.rect.X == aggregate.rect.X && aboveAggregate.rect.Width == aggregate.rect.Width) { combinedStripsToRemove.Add(aggregate); aboveAggregate.dir = TileDirection.COMBINED; aboveAggregate.rect.Height++; pointToAggregate[new Point(aggregate.rect.X, aggregate.rect.Y)] = aboveAggregate; foreach(var keyPair in aggregate.properties) { aboveAggregate.properties[keyPair.Key] = keyPair.Value; } } } foreach(var aggregate in combinedStripsToRemove) { aggregates.Remove(aggregate); } func(aggregates, pointToAggregate); }