private static void AddTileShapeCollectionForLayer(TMXGlueLib.MapLayer layer, Dictionary <string, TileCollisions.TileShapeCollection> collisionDictionary, TMXGlueLib.Tileset tileset, float tileDimension, float z, bool separateOnTileType) { Math.Geometry.AxisAlignedRectangle rectangle = null; Math.Geometry.Polygon polygon = null; Circle circle; var tiles = layer.data[0].tiles; bool sortOnY = layer.height > layer.width; if (tileset != null) { foreach (var tilesetTile in tileset.Tiles.Where(item => item.Objects?.@object?.Length > 0)) { var tilesetTileGid = tilesetTile.id + tileset.Firstgid; foreach (var tilesetObject in tilesetTile.Objects.@object) { var name = layer.Name; TiledMapToShapeCollectionConverter.ConvertTiledObjectToFrbShape(tilesetObject, out polygon, out rectangle, out circle); if (rectangle != null) { var collectionName = layer.Name; if (tilesetObject.Type != null && separateOnTileType) { collectionName += "_" + tilesetObject.Type; } var collection = GetOrAddTileShapeCollection(collectionName, collisionDictionary); collection.GridSize = tileDimension; rectangle.Z = z; if (sortOnY) { for (int y = 0; y < layer.height; y++) { for (int x = 0; x < layer.width; x++) { AddRectangleCloneAtXY(layer, tileDimension, rectangle, tiles, tilesetTileGid, x, y, collection); } } } else { for (int x = 0; x < layer.width; x++) { for (int y = 0; y < layer.height; y++) { AddRectangleCloneAtXY(layer, tileDimension, rectangle, tiles, tilesetTileGid, x, y, collection); } } } } else if (polygon != null) { var collectionName = layer.Name; if (tilesetObject.Type != null && separateOnTileType) { collectionName += "_" + tilesetObject.Type; } var collection = GetOrAddTileShapeCollection(collectionName, collisionDictionary); collection.GridSize = tileDimension; // For tile polygons we want them to be centered on the tile. // To do this, we shift all points by its position: for (int i = 0; i < polygon.Points.Count; i++) { var point = polygon.Points[i]; point.X += polygon.Position.X - tileDimension / 2.0f; point.Y += polygon.Position.Y + tileDimension / 2.0f; polygon.SetPoint(i, point); } polygon.Z = z; if (sortOnY) { for (int y = 0; y < layer.height; y++) { for (int x = 0; x < layer.width; x++) { var i = y * layer.width + x; if ((tiles[i] & 0x0fffffff) == tilesetTileGid) { var cloned = AddPolygonCloneAtXY(layer, tileDimension, polygon, tiles, tilesetTileGid, i, collection); ApplyFlip(tiles[i], cloned); } } } } else { for (int x = 0; x < layer.width; x++) { for (int y = 0; y < layer.height; y++) { var i = y * layer.width + x; if ((tiles[i] & 0x0fffffff) == tilesetTileGid) { var cloned = AddPolygonCloneAtXY(layer, tileDimension, polygon, tiles, tilesetTileGid, i, collection); ApplyFlip(tiles[i], cloned); } } } } } else if (circle != null) { throw new NotImplementedException("Need to handle circles..."); } } } } }
private static TileCollisions.TileShapeCollection GetTileShapeCollectionForLayer(TMXGlueLib.MapLayer layer, TMXGlueLib.Tileset tileset, float tileDimension, float z) { TileCollisions.TileShapeCollection toReturn = new TileCollisions.TileShapeCollection(); toReturn.Name = layer.Name; Math.Geometry.AxisAlignedRectangle rectangle = null; Math.Geometry.Polygon polygon = null; Circle circle; var tiles = layer.data[0].tiles; bool sortOnY = layer.height > layer.width; foreach (var tilesetTile in tileset.Tiles.Where(item => item.Objects?.@object?.Length > 0)) { var tilesetTileGid = tilesetTile.id + tileset.Firstgid; foreach (var tilesetObject in tilesetTile.Objects.@object) { TiledMapToShapeCollectionConverter.ConvertTiledObjectToFrbShape(tilesetObject, out polygon, out rectangle, out circle); if (rectangle != null) { rectangle.Z = z; if (sortOnY) { for (int y = 0; y < layer.height; y++) { for (int x = 0; x < layer.width; x++) { AddRectangleCloneAtXY(layer, tileDimension, toReturn, rectangle, tiles, tilesetTileGid, x, y); } } } else { for (int x = 0; x < layer.width; x++) { for (int y = 0; y < layer.height; y++) { AddRectangleCloneAtXY(layer, tileDimension, toReturn, rectangle, tiles, tilesetTileGid, x, y); } } } } else if (polygon != null) { // For tile polygons we want them to be centered on the tile. // To do this, we shift all points by its position: for (int i = 0; i < polygon.Points.Count; i++) { var point = polygon.Points[i]; point.X += polygon.Position.X - tileDimension / 2.0f; point.Y += polygon.Position.Y + tileDimension / 2.0f; polygon.SetPoint(i, point); } polygon.Z = z; if (sortOnY) { for (int y = 0; y < layer.height; y++) { for (int x = 0; x < layer.width; x++) { AddPolygonCloneAtXY(layer, tileDimension, toReturn, polygon, tiles, tilesetTileGid, x, y); } } } else { for (int x = 0; x < layer.width; x++) { for (int y = 0; y < layer.height; y++) { AddPolygonCloneAtXY(layer, tileDimension, toReturn, polygon, tiles, tilesetTileGid, x, y); } } } } else if (circle != null) { throw new NotImplementedException("Need to handle circles..."); } } } // The values are inserted sorted above for speed, now we set the XAxis - this will also sort but will be fast: if (sortOnY) { toReturn.SortAxis = Math.Axis.Y; } else { toReturn.SortAxis = Math.Axis.X; } toReturn.RefreshAllRepositionDirections(); return(toReturn); }