public static void AddCollisionFrom(this TileShapeCollection tileShapeCollection, LayeredTileMap layeredTileMap, Func<List<TMXGlueLib.DataTypes.NamedValue>, bool> predicate) { var properties = layeredTileMap.Properties; foreach (var kvp in properties) { string name = kvp.Key; var namedValues = kvp.Value; if (predicate(namedValues)) { float dimension = float.NaN; float dimensionHalf = 0; foreach (var layer in layeredTileMap.MapLayers) { var dictionary = layer.NamedTileOrderedIndexes; if (dictionary.ContainsKey(name)) { var indexList = dictionary[name]; foreach (var index in indexList) { float left; float bottom; layer.GetBottomLeftWorldCoordinateForOrderedTile(index, out left, out bottom); if (float.IsNaN(dimension)) { dimension = layer.Vertices[(index * 4) + 1].Position.X - left; dimensionHalf = dimension / 2.0f; tileShapeCollection.GridSize = dimension; } tileShapeCollection.AddCollisionAtWorld(left + dimensionHalf, bottom + dimensionHalf); } } } } } }
static void AddCollisionFrom(this TileShapeCollection tileShapeCollection, Scene scene, IEnumerable<string> namesToUse) { // prob need to clear out the tileShapeCollection float dimension = float.NaN; float dimensionHalf = 0; for (int i = 0; i < scene.Sprites.Count; i++) { if (namesToUse.Contains(scene.Sprites[i].Name)) { if (float.IsNaN(dimension)) { dimension = scene.Sprites[i].Width; dimensionHalf = dimension / 2.0f; tileShapeCollection.GridSize = dimension; } tileShapeCollection.AddCollisionAtWorld(scene.Sprites[i].X, scene.Sprites[i].Y); } } }
// This was not originally public but made public for situations where users want to // manually specify which tiles to use rather than relying on the HasCollision methods. public static void AddCollisionFrom(this TileShapeCollection tileShapeCollection, LayeredTileMap layeredTileMap, IEnumerable<string> namesToUse) { // prob need to clear out the tileShapeCollection float dimension = float.NaN; float dimensionHalf = 0; foreach (var layer in layeredTileMap.MapLayers) { var dictionary = layer.NamedTileOrderedIndexes; foreach (var name in namesToUse) { if (dictionary.ContainsKey(name)) { var indexList = dictionary[name]; foreach (var index in indexList) { float left; float bottom; layer.GetBottomLeftWorldCoordinateForOrderedTile(index, out left, out bottom); if (float.IsNaN(dimension)) { dimension = layer.Vertices[(index*4) + 1].Position.X - left; dimensionHalf = dimension / 2.0f; tileShapeCollection.GridSize = dimension; } tileShapeCollection.AddCollisionAtWorld(left + dimensionHalf, bottom + dimensionHalf); } } } } }