public static LayeredTileMap FromReducedTileMapInfo(TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName, string tilbToLoad) { var toReturn = new LayeredTileMap(); string oldRelativeDirectory = FileManager.RelativeDirectory; FileManager.RelativeDirectory = FileManager.GetDirectory(tilbToLoad); MapDrawableBatch mdb; for (int i = 0; i < rtmi.Layers.Count; i++) { var reducedLayer = rtmi.Layers[i]; mdb = MapDrawableBatch.FromReducedLayer(reducedLayer, contentManagerName, rtmi.CellWidthInPixels, rtmi.CellHeightInPixels, rtmi.QuadWidth, rtmi.QuadHeight); mdb.AttachTo(toReturn, false); mdb.RelativeZ = reducedLayer.Z; toReturn.mMapLists.Add(mdb); } FileManager.RelativeDirectory = oldRelativeDirectory; return(toReturn); }
public static void RemoveTiles(this LayeredTileMap map, Func <List <NamedValue>, bool> predicate, Dictionary <string, List <NamedValue> > Properties) { // Force execution now for performance reasons var filteredInfos = Properties.Values.Where(predicate).ToList(); foreach (var layer in map.MapLayers) { List <int> indexes = new List <int>(); foreach (var itemThatPasses in filteredInfos) { string tileName = itemThatPasses .FirstOrDefault(item => item.Name.ToLowerInvariant() == "name") .Value as string; if (layer.NamedTileOrderedIndexes.ContainsKey(tileName)) { var intsOnThisLayer = layer.NamedTileOrderedIndexes[tileName]; indexes.AddRange(intsOnThisLayer); } } layer.RemoveQuads(indexes); } }
public static void RemoveTiles(this LayeredTileMap map, Func <TileMapInfo, bool> predicate, IEnumerable <TileMapInfo> info) { // Force execution now for performance reasons var filteredInfos = info.Where(predicate).ToList(); foreach (var layer in map.MapLayers) { List <int> indexes = new List <int>(); foreach (var itemThatPasses in filteredInfos) { if (layer.NamedTileOrderedIndexes.ContainsKey(itemThatPasses.Name)) { var intsOnThisLayer = layer.NamedTileOrderedIndexes[itemThatPasses.Name]; indexes.AddRange(intsOnThisLayer); } } layer.RemoveQuads(indexes); } }
public static LayeredTileMap FromScene(string fileName, string contentManagerName, bool verifySameTexturePerLayer) { Section.GetAndStartContextAndTime("Initial FromScene"); LayeredTileMap toReturn = new LayeredTileMap(); string absoluteFileName = FileManager.MakeAbsolute(fileName); Section.EndContextAndTime(); Section.GetAndStartContextAndTime("FromFile"); SceneSave ses = SceneSave.FromFile(absoluteFileName); Section.EndContextAndTime(); Section.GetAndStartContextAndTime("BreaksNStuff"); string oldRelativeDirectory = FileManager.RelativeDirectory; FileManager.RelativeDirectory = FileManager.GetDirectory(absoluteFileName); var breaks = GetZBreaks(ses.SpriteList); int valueBefore = 0; MapDrawableBatch mdb; int valueAfter; float zValue = 0; Section.EndContextAndTime(); Section.GetAndStartContextAndTime("Create MDBs"); for (int i = 0; i < breaks.Count; i++) { valueAfter = breaks[i]; int count = valueAfter - valueBefore; mdb = MapDrawableBatch.FromSpriteSaves(ses.SpriteList, valueBefore, count, contentManagerName, verifySameTexturePerLayer); mdb.AttachTo(toReturn, false); mdb.RelativeZ = zValue; toReturn.mMapLists.Add(mdb); zValue += toReturn.mZSplit; valueBefore = valueAfter; } valueAfter = ses.SpriteList.Count; if (valueBefore != valueAfter) { int count = valueAfter - valueBefore; mdb = MapDrawableBatch.FromSpriteSaves(ses.SpriteList, valueBefore, count, contentManagerName, verifySameTexturePerLayer); mdb.AttachTo(toReturn, false); mdb.RelativeZ = zValue; toReturn.mMapLists.Add(mdb); } Section.EndContextAndTime(); FileManager.RelativeDirectory = oldRelativeDirectory; return(toReturn); }
private static void AddTileShapeCollections(LayeredTileMap layeredTileMap, TiledMapSave tms) { var allTilesets = tms.Tilesets; Dictionary <TMXGlueLib.Tileset, bool> hasShapesDictionary = new Dictionary <TMXGlueLib.Tileset, bool>(); Dictionary <string, TileCollisions.TileShapeCollection> nameCollisionPairs = new Dictionary <string, TileCollisions.TileShapeCollection>(); for (int i = 0; i < tms.Layers.Count; i++) { var layer = tms.Layers[i]; // Currently we only support 1 tileset per layer, so we'll find the tileset for this layer var firstNonZero = layer.data[0].tiles.FirstOrDefault(item => item != 0); if (firstNonZero != 0) { var tileset = tms.GetTilesetForGid(firstNonZero); if (tileset != null) { bool hasShapes; if (hasShapesDictionary.ContainsKey(tileset)) { hasShapes = hasShapesDictionary[tileset]; } else { // We don't know if this tileset has shapes yet, so let's figure it out: hasShapes = tileset.Tiles.Any(item => item.Objects?.@object?.Length > 0); hasShapesDictionary[tileset] = hasShapes; } if (hasShapes) { AddTileShapeCollectionForLayer(layer, nameCollisionPairs, tileset, tms.tilewidth, i); } } } } foreach (var item in nameCollisionPairs.Values) { var sortOnY = layeredTileMap.Height > layeredTileMap.Width; if (sortOnY) { item.SortAxis = Math.Axis.Y; } else { item.SortAxis = Math.Axis.X; } item.RefreshAllRepositionDirections(); layeredTileMap.Collisions.Add(item); } }
public static void RemoveTiles(this LayeredTileMap map, Func <List <NamedValue>, bool> predicate, Dictionary <string, List <NamedValue> > Properties) { // Force execution now for performance reasons var filteredInfos = Properties.Values.Where(predicate).ToList(); foreach (var layer in map.MapLayers) { RemoveTilesFromLayer(filteredInfos, layer); } }
/// <summary> /// Creates entities from a single layer for any tile with the EntityToCreate property. /// </summary> /// <param name="mapLayer">The layer to create entities from.</param> /// <param name="layeredTileMap">The map which contains the mapLayer instance.</param> public static void CreateEntitiesFrom(MapDrawableBatch mapLayer, LayeredTileMap layeredTileMap) { var entitiesToRemove = new List<string>(); CreateEntitiesFrom(entitiesToRemove, mapLayer, layeredTileMap.Properties); foreach (var entityToRemove in entitiesToRemove) { string remove = entityToRemove; mapLayer.RemoveTiles(t => t.Any(item => item.Name == "EntityToCreate" && item.Value as string == remove), layeredTileMap.Properties); } }
public static void CreateEntitiesFrom(LayeredTileMap layeredTileMap) { var entitiesToRemove = new List<string>(); foreach (var layer in layeredTileMap.MapLayers) { CreateEntitiesFrom(entitiesToRemove, layer, layeredTileMap.Properties); } foreach (var entityToRemove in entitiesToRemove) { string remove = entityToRemove; layeredTileMap.RemoveTiles(t => t.Any(item => item.Name == "EntityToCreate" && item.Value as string == remove), layeredTileMap.Properties); } }
public void Activity(LayeredTileMap layeredTileMap) { foreach (var kvp in mAnimationChainContainers) { AnimationChainContainer container = kvp.Value; int indexBefore = container.CurrentFrameIndex; container.Activity(TimeManager.SecondDifference); if (container.CurrentFrameIndex != indexBefore) { ReactToChangedAnimationFrame(kvp.Key, kvp.Value, layeredTileMap); } } }
public void Activity(LayeredTileMap layeredTileMap) { foreach (var kvp in mAnimationChainContainers) { AnimationChainContainer container = kvp.Value; int indexBefore = container.CurrentFrameIndex; container.Activity(TimeManager.SecondDifference); if (container.CurrentFrameIndex != indexBefore) { ReactToChangedAnimationFrame(kvp.Key, kvp.Value, layeredTileMap); } } }
private static void AddShapeCollections(LayeredTileMap layeredTileMap, TiledMapSave tms) { foreach (var mapObjectgroup in tms.objectgroup) { int indexInAllLayers = tms.MapLayers.IndexOf(mapObjectgroup); var shapeCollection = tms.ToShapeCollection(mapObjectgroup.Name); if (shapeCollection != null && shapeCollection.IsEmpty == false) { // This makes all shapes have the same Z as the index layer, which is useful if instantiating objects, so they're layered properly shapeCollection.Shift(new Microsoft.Xna.Framework.Vector3(0, 0, indexInAllLayers)); shapeCollection.Name = mapObjectgroup.Name; layeredTileMap.ShapeCollections.Add(shapeCollection); } } }
public void Activity(LayeredTileMap layeredTileMap) { foreach (var kvp in mAnimationChainContainers) { AnimationChainContainer container = kvp.Value; int index = container.CurrentFrameIndex; container.Activity(TimeManager.SecondDifference); if (container.CurrentFrameIndex != index) { if (container.AnimationChain.Name.StartsWith("Rain")) { int m = 3; } ReactToChangedAnimationFrame(kvp.Key, kvp.Value, layeredTileMap); } } }
private void ReactToChangedAnimationFrame(string spriteName, AnimationChainContainer animationChainContainer, LayeredTileMap layeredTileMap) { AnimationFrame animationFrame = animationChainContainer.CurrentFrame; foreach (var mapLayer in layeredTileMap.MapLayers) { var nameDictionary = mapLayer.NamedTileOrderedIndexes; if (nameDictionary.ContainsKey(spriteName)) { var indexes = nameDictionary[spriteName]; foreach (int value in indexes) { mapLayer.PaintTileTextureCoordinates(value, animationFrame.LeftCoordinate, animationFrame.TopCoordinate); } } } }
public void Activity(LayeredTileMap layeredTileMap) { foreach (var kvp in mAnimationChainContainers) { AnimationChainContainer container = kvp.Value; int index = container.CurrentFrameIndex; container.Activity(TimeManager.SecondDifference); if (container.CurrentFrameIndex != index) { if (container.AnimationChain.Name.StartsWith("Rain")) { int m = 3; } ReactToChangedAnimationFrame(kvp.Key, kvp.Value, layeredTileMap); } } }
private static void AddTileShapeCollections(LayeredTileMap layeredTileMap, TiledMapSave tms) { var allTilesets = tms.Tilesets; Dictionary <TMXGlueLib.Tileset, bool> hasShapesDictionary = new Dictionary <TMXGlueLib.Tileset, bool>(); for (int i = 0; i < tms.Layers.Count; i++) { var layer = tms.Layers[i]; // Currently we only support 1 tileset per layer, so we'll find the tileset for this layer var firstNonZero = layer.data[0].tiles.FirstOrDefault(item => item != 0); if (firstNonZero != 0) { var tileset = tms.GetTilesetForGid(firstNonZero); if (tileset != null) { bool hasShapes; if (hasShapesDictionary.ContainsKey(tileset)) { hasShapes = hasShapesDictionary[tileset]; } else { // We don't know if this tileset has shapes yet, so let's figure it out: hasShapes = tileset.Tiles.Any(item => item.Objects?.@object?.Length > 0); hasShapesDictionary[tileset] = hasShapes; } if (hasShapes) { TileCollisions.TileShapeCollection tileShapeCollection = GetTileShapeCollectionForLayer(layer, tileset, tms.tilewidth, i); layeredTileMap.Collisions.Add(tileShapeCollection); } } } } }
private static void AddTileShapeCollections(LayeredTileMap layeredTileMap, TiledMapSave tms, bool separateOnTileType) { var allTilesets = tms.Tilesets; Dictionary <string, TileCollisions.TileShapeCollection> nameCollisionPairs = new Dictionary <string, TileCollisions.TileShapeCollection>(); for (int i = 0; i < tms.Layers.Count; i++) { var layer = tms.Layers[i]; // Currently we only support 1 tileset per layer, so we'll find the tileset for this layer var firstNonZero = layer.data[0].tiles.FirstOrDefault(item => item != 0); TMXGlueLib.Tileset tileset = null; if (firstNonZero != 0) { tileset = tms.GetTilesetForGid(firstNonZero); } AddTileShapeCollectionForLayer(layer, nameCollisionPairs, tileset, tms.tilewidth, i, separateOnTileType); } foreach (var item in nameCollisionPairs.Values) { if (item.Rectangles.Count > 0 || item.Polygons.Count > 0) { var sortOnY = layeredTileMap.Height > layeredTileMap.Width; if (sortOnY) { item.SortAxis = Math.Axis.Y; } else { item.SortAxis = Math.Axis.X; } item.RefreshAllRepositionDirections(); layeredTileMap.Collisions.Add(item); } } }
public override void Initialize(bool addToManagers) { // Generated Initialize LoadStaticContent(ContentManagerName); TiledMap = new FlatRedBall.TileGraphics.LayeredTileMap(); TiledMap.Name = "TiledMap"; TileCollisionShapes = new FlatRedBall.Math.Geometry.ShapeCollection(); TileCollisionShapes.Name = "TileCollisionShapes"; BulletList = new PositionedObjectList <LudumDare29.Entities.Bullet>(); BulletList.Name = "BulletList"; NextLevelEntityList = new PositionedObjectList <LudumDare29.Entities.NextLevelEntity>(); NextLevelEntityList.Name = "NextLevelEntityList"; PlayerInstance = new LudumDare29.Entities.Player(ContentManagerName, false); PlayerInstance.Name = "PlayerInstance"; SignEntityList = new PositionedObjectList <LudumDare29.Entities.SignEntity>(); SignEntityList.Name = "SignEntityList"; ActionEntityList = new PositionedObjectList <LudumDare29.Entities.ActionEntity>(); ActionEntityList.Name = "ActionEntityList"; EnemyBulletList = new PositionedObjectList <LudumDare29.Entities.EnemyBullet>(); EnemyBulletList.Name = "EnemyBulletList"; EnemyCornerList = new PositionedObjectList <LudumDare29.Entities.EnemyCorner>(); EnemyCornerList.Name = "EnemyCornerList"; GroundEnemyList = new PositionedObjectList <LudumDare29.Entities.GroundEnemy>(); GroundEnemyList.Name = "GroundEnemyList"; EntityCollisionShapes = new FlatRedBall.Math.Geometry.ShapeCollection(); EntityCollisionShapes.Name = "EntityCollisionShapes"; EnemyCollisionGround = new FlatRedBall.Math.Geometry.ShapeCollection(); EnemyCollisionGround.Name = "EnemyCollisionGround"; TheEndText = new FlatRedBall.Graphics.Text(); TheEndText.Name = "TheEndText"; PostInitialize(); base.Initialize(addToManagers); if (addToManagers) { AddToManagers(); } }
public static LayeredTileMap FromReducedTileMapInfo(TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName, string tilbToLoad) { var toReturn = new LayeredTileMap(); string oldRelativeDirectory = FileManager.RelativeDirectory; FileManager.RelativeDirectory = FileManager.GetDirectory(tilbToLoad); MapDrawableBatch mdb; if (rtmi.NumberCellsWide != 0) { toReturn.mNumberTilesWide = rtmi.NumberCellsWide; } if (rtmi.NumberCellsTall != 0) { toReturn.mNumberTilesTall = rtmi.NumberCellsTall; } toReturn.mWidthPerTile = rtmi.QuadWidth; toReturn.mHeightPerTile = rtmi.QuadHeight; for (int i = 0; i < rtmi.Layers.Count; i++) { var reducedLayer = rtmi.Layers[i]; mdb = MapDrawableBatch.FromReducedLayer(reducedLayer, rtmi, contentManagerName); mdb.AttachTo(toReturn, false); mdb.RelativeZ = reducedLayer.Z; toReturn.mMapLists.Add(mdb); } FileManager.RelativeDirectory = oldRelativeDirectory; return(toReturn); }
public override void Destroy() { // Generated Destroy BulletFactory.Destroy(); EnemyBulletFactory.Destroy(); if (mStartLevel != null) { mStartLevel.Destroy(); mStartLevel = null; } if (mLevel2 != null) { mLevel2.Destroy(); mLevel2 = null; } if (mLevel3 != null) { mLevel3.Destroy(); mLevel3 = null; } if (mLevel4 != null) { mLevel4.Destroy(); mLevel4 = null; } if (mEndLevel != null) { mEndLevel.Destroy(); mEndLevel = null; } BulletList.MakeOneWay(); NextLevelEntityList.MakeOneWay(); SignEntityList.MakeOneWay(); ActionEntityList.MakeOneWay(); EnemyBulletList.MakeOneWay(); EnemyCornerList.MakeOneWay(); GroundEnemyList.MakeOneWay(); if (TiledMap != null) { TiledMap.Destroy(); } if (TileCollisionShapes != null) { TileCollisionShapes.RemoveFromManagers(ContentManagerName != "Global"); } for (int i = BulletList.Count - 1; i > -1; i--) { BulletList[i].Destroy(); } for (int i = NextLevelEntityList.Count - 1; i > -1; i--) { NextLevelEntityList[i].Destroy(); } if (PlayerInstance != null) { PlayerInstance.Destroy(); PlayerInstance.Detach(); } for (int i = SignEntityList.Count - 1; i > -1; i--) { SignEntityList[i].Destroy(); } for (int i = ActionEntityList.Count - 1; i > -1; i--) { ActionEntityList[i].Destroy(); } for (int i = EnemyBulletList.Count - 1; i > -1; i--) { EnemyBulletList[i].Destroy(); } for (int i = EnemyCornerList.Count - 1; i > -1; i--) { EnemyCornerList[i].Destroy(); } for (int i = GroundEnemyList.Count - 1; i > -1; i--) { GroundEnemyList[i].Destroy(); } if (EntityCollisionShapes != null) { EntityCollisionShapes.RemoveFromManagers(ContentManagerName != "Global"); } if (EnemyCollisionGround != null) { EnemyCollisionGround.RemoveFromManagers(ContentManagerName != "Global"); } if (TheEndText != null) { TextManager.RemoveText(TheEndText); } BulletList.MakeTwoWay(); NextLevelEntityList.MakeTwoWay(); SignEntityList.MakeTwoWay(); ActionEntityList.MakeTwoWay(); EnemyBulletList.MakeTwoWay(); EnemyCornerList.MakeTwoWay(); GroundEnemyList.MakeTwoWay(); base.Destroy(); CustomDestroy(); }
public static LayeredTileMap FromReducedTileMapInfo(TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName, string tilbToLoad) { var toReturn = new LayeredTileMap(); string oldRelativeDirectory = FileManager.RelativeDirectory; FileManager.RelativeDirectory = FileManager.GetDirectory(tilbToLoad); MapDrawableBatch mdb; if (rtmi.NumberCellsWide != 0) { toReturn.mNumberTilesWide = rtmi.NumberCellsWide; } if (rtmi.NumberCellsTall != 0) { toReturn.mNumberTilesTall = rtmi.NumberCellsTall; } toReturn.mWidthPerTile = rtmi.QuadWidth; toReturn.mHeightPerTile = rtmi.QuadHeight; for (int i = 0; i < rtmi.Layers.Count; i++) { var reducedLayer = rtmi.Layers[i]; mdb = MapDrawableBatch.FromReducedLayer(reducedLayer, rtmi, contentManagerName); mdb.AttachTo(toReturn, false); mdb.RelativeZ = reducedLayer.Z; toReturn.mMapLists.Add(mdb); } FileManager.RelativeDirectory = oldRelativeDirectory; return toReturn; }
private void ReactToChangedAnimationFrame(string spriteName, AnimationChainContainer animationChainContainer, LayeredTileMap layeredTileMap) { AnimationFrame animationFrame = animationChainContainer.CurrentFrame; foreach (var mapLayer in layeredTileMap.MapLayers) { var nameDictionary = mapLayer.NamedTileOrderedIndexes; if (nameDictionary.ContainsKey(spriteName)) { var indexes = nameDictionary[spriteName]; foreach (int value in indexes) { mapLayer.PaintTileTextureCoordinates(value, animationFrame.LeftCoordinate, animationFrame.TopCoordinate, animationFrame.RightCoordinate, animationFrame.BottomCoordinate); } } } }
private void ReactToChangedAnimationFrame(string spriteName, AnimationChainContainer animationChainContainer, LayeredTileMap layeredTileMap) { AnimationFrame animationFrame = animationChainContainer.CurrentFrame; Microsoft.Xna.Framework.Vector4 textureValues = new Microsoft.Xna.Framework.Vector4(); foreach (var mapLayer in layeredTileMap.MapLayers) { var nameDictionary = mapLayer.NamedTileOrderedIndexes; if (nameDictionary.ContainsKey(spriteName)) { var indexes = nameDictionary[spriteName]; foreach (int value in indexes) { textureValues.X = animationFrame.LeftCoordinate; textureValues.Y = animationFrame.RightCoordinate; textureValues.Z = animationFrame.TopCoordinate; textureValues.W = animationFrame.BottomCoordinate; var flipFlags = mapLayer.FlipFlagArray[value]; if ((flipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) { var temp = textureValues.Y; textureValues.Y = textureValues.X; textureValues.X = temp; } if ((flipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) { var temp = textureValues.Z; textureValues.Z = textureValues.W; textureValues.W = temp; } mapLayer.PaintTileTextureCoordinates(value, textureValues.X, textureValues.Z, textureValues.Y, textureValues.W); // not sure why it's done this way, copied from MapDrawableBatch... if ((flipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) { mapLayer.ApplyDiagonalFlip(value); } } } } }
internal static MapDrawableBatch FromReducedLayer(TMXGlueLib.DataTypes.ReducedLayerInfo reducedLayerInfo, LayeredTileMap owner, TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName) { int tileDimensionWidth = reducedLayerInfo.TileWidth; int tileDimensionHeight = reducedLayerInfo.TileHeight; float quadWidth = reducedLayerInfo.TileWidth; float quadHeight = reducedLayerInfo.TileHeight; string textureName = reducedLayerInfo.Texture; #if IOS || ANDROID textureName = textureName.ToLowerInvariant(); #endif Texture2D texture = FlatRedBallServices.Load <Texture2D>(textureName, contentManagerName); MapDrawableBatch toReturn = new MapDrawableBatch(reducedLayerInfo.Quads.Count, tileDimensionWidth, tileDimensionHeight, texture); toReturn.Name = reducedLayerInfo.Name; Vector3 position = new Vector3(); Vector2 tileDimensions = new Vector2(quadWidth, quadHeight); IEnumerable <TMXGlueLib.DataTypes.ReducedQuadInfo> quads = null; if (rtmi.NumberCellsWide > rtmi.NumberCellsTall) { quads = reducedLayerInfo.Quads.OrderBy(item => item.LeftQuadCoordinate).ToList(); toReturn.mSortAxis = SortAxis.X; } else { quads = reducedLayerInfo.Quads.OrderBy(item => item.BottomQuadCoordinate).ToList(); toReturn.mSortAxis = SortAxis.Y; } foreach (var quad in quads) { position.X = quad.LeftQuadCoordinate; position.Y = quad.BottomQuadCoordinate; // The Z of the quad should be relative to this layer, not absolute Z values. // A multi-layer map will offset the individual layer Z values, the quads should have a Z of 0. // position.Z = reducedLayerInfo.Z; var textureValues = new Vector4(); // The purpose of CoordinateAdjustment is to bring the texture values "in", to reduce the chance of adjacent // tiles drawing on a given tile quad. If we don't do this, we can get slivers of adjacent colors appearing, causing // lines or grid patterns. // To bring the values "in" we have to consider rotated quads. textureValues.X = CoordinateAdjustment + (float)quad.LeftTexturePixel / (float)texture.Width; // Left textureValues.Y = -CoordinateAdjustment + (float)(quad.LeftTexturePixel + tileDimensionWidth) / (float)texture.Width; // Right textureValues.Z = CoordinateAdjustment + (float)quad.TopTexturePixel / (float)texture.Height; // Top textureValues.W = -CoordinateAdjustment + (float)(quad.TopTexturePixel + tileDimensionHeight) / (float)texture.Height; // Bottom // pad before doing any rotations/flipping const bool pad = true; if (pad) { const float amountToAdd = .0000001f; textureValues.X += amountToAdd; // Left textureValues.Y -= amountToAdd; // Right textureValues.Z += amountToAdd; // Top textureValues.W -= amountToAdd; // Bottom } if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) { var temp = textureValues.Y; textureValues.Y = textureValues.X; textureValues.X = temp; } if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) { var temp = textureValues.Z; textureValues.Z = textureValues.W; textureValues.W = temp; } int tileIndex = toReturn.AddTile(position, tileDimensions, //quad.LeftTexturePixel, quad.TopTexturePixel, quad.LeftTexturePixel + tileDimensionWidth, quad.TopTexturePixel + tileDimensionHeight); textureValues); if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) { toReturn.ApplyDiagonalFlip(tileIndex); } // This was moved to outside of this conversion, to support shaps //if (quad.QuadSpecificProperties != null) //{ // var listToAdd = quad.QuadSpecificProperties.ToList(); // listToAdd.Add(new NamedValue { Name = "Name", Value = quad.Name }); // owner.Properties.Add(quad.Name, listToAdd); //} if (quad.RotationDegrees != 0) { // Tiled rotates clockwise :( var rotationRadians = -MathHelper.ToRadians(quad.RotationDegrees); Vector3 bottomLeftPos = toReturn.Vertices[tileIndex * 4].Position; Vector3 vertPos = toReturn.Vertices[tileIndex * 4 + 1].Position; MathFunctions.RotatePointAroundPoint(bottomLeftPos, ref vertPos, rotationRadians); toReturn.Vertices[tileIndex * 4 + 1].Position = vertPos; vertPos = toReturn.Vertices[tileIndex * 4 + 2].Position; MathFunctions.RotatePointAroundPoint(bottomLeftPos, ref vertPos, rotationRadians); toReturn.Vertices[tileIndex * 4 + 2].Position = vertPos; vertPos = toReturn.Vertices[tileIndex * 4 + 3].Position; MathFunctions.RotatePointAroundPoint(bottomLeftPos, ref vertPos, rotationRadians); toReturn.Vertices[tileIndex * 4 + 3].Position = vertPos; } toReturn.RegisterName(quad.Name, tileIndex); } return(toReturn); }
public static void AddCollisionFrom(this TileShapeCollection tileShapeCollection, LayeredTileMap layeredTileMap) { var tilesWithCollision = layeredTileMap.Properties .Where(item => item.Value.Any(property => property.Name == "HasCollision" && (string)property.Value == "True")) .Select(item => item.Key).ToList(); tileShapeCollection.AddCollisionFrom(layeredTileMap, tilesWithCollision); }
// 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); } } } } }
public static LayeredTileMap FromReducedTileMapInfo(TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName, string tilbToLoad) { var toReturn = new LayeredTileMap(); string oldRelativeDirectory = FileManager.RelativeDirectory; FileManager.RelativeDirectory = FileManager.GetDirectory(tilbToLoad); MapDrawableBatch mdb; for (int i = 0; i < rtmi.Layers.Count; i++) { mdb = MapDrawableBatch.FromReducedLayer(rtmi.Layers[i], contentManagerName, rtmi.CellWidthInPixels, rtmi.CellHeightInPixels, rtmi.QuadWidth, rtmi.QuadHeight); mdb.AttachTo(toReturn, false); mdb.RelativeZ = i; toReturn.mMapLists.Add(mdb); } FileManager.RelativeDirectory = oldRelativeDirectory; return toReturn; }
public static void AddCollisionFrom(this TileShapeCollection tileShapeCollection, LayeredTileMap layeredTileMap, Dictionary<string, TileMapInfo> tileMapInfos) { var stringEnum = tileMapInfos.Values.Where(item => item.HasCollision).Select(item=>item.Name); tileShapeCollection.AddCollisionFrom(layeredTileMap, stringEnum); }
public static LayeredTileMap FromScene(string fileName, string contentManagerName, bool verifySameTexturePerLayer) { Section.GetAndStartContextAndTime("Initial FromScene"); LayeredTileMap toReturn = new LayeredTileMap(); string absoluteFileName = FileManager.MakeAbsolute(fileName); Section.EndContextAndTime(); Section.GetAndStartContextAndTime("FromFile"); SpriteEditorScene ses = SpriteEditorScene.FromFile(absoluteFileName); Section.EndContextAndTime(); Section.GetAndStartContextAndTime("BreaksNStuff"); string oldRelativeDirectory = FileManager.RelativeDirectory; FileManager.RelativeDirectory = FileManager.GetDirectory(absoluteFileName); var breaks = GetZBreaks(ses.SpriteList); int valueBefore = 0; MapDrawableBatch mdb; int valueAfter; float zValue = 0; Section.EndContextAndTime(); Section.GetAndStartContextAndTime("Create MDBs"); for(int i = 0; i < breaks.Count; i++) { valueAfter = breaks[i]; int count = valueAfter - valueBefore; mdb = MapDrawableBatch.FromSpriteSaves(ses.SpriteList, valueBefore, count, contentManagerName, verifySameTexturePerLayer); mdb.AttachTo(toReturn, false); mdb.RelativeZ = zValue; toReturn.mMapLists.Add(mdb); zValue += toReturn.mZSplit; valueBefore = valueAfter; } valueAfter = ses.SpriteList.Count; if (valueBefore != valueAfter) { int count = valueAfter - valueBefore; mdb = MapDrawableBatch.FromSpriteSaves(ses.SpriteList, valueBefore, count, contentManagerName, verifySameTexturePerLayer); mdb.AttachTo(toReturn, false); mdb.RelativeZ = zValue; toReturn.mMapLists.Add(mdb); } Section.EndContextAndTime(); FileManager.RelativeDirectory = oldRelativeDirectory; return toReturn; }
public static void AddCollisionFrom(this TileShapeCollection tileShapeCollection, LayeredTileMap layeredTileMap, string nameToUse) { AddCollisionFrom(tileShapeCollection, layeredTileMap, new List<string> { nameToUse }); }
internal static MapDrawableBatch FromReducedLayer(TMXGlueLib.DataTypes.ReducedLayerInfo reducedLayerInfo, LayeredTileMap owner, TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName) { int tileDimensionWidth = reducedLayerInfo.TileWidth; int tileDimensionHeight = reducedLayerInfo.TileHeight; float quadWidth = reducedLayerInfo.TileWidth; float quadHeight = reducedLayerInfo.TileHeight; string textureName = reducedLayerInfo.Texture; #if IOS || ANDROID textureName = textureName.ToLowerInvariant(); #endif Texture2D texture = FlatRedBallServices.Load<Texture2D>(textureName, contentManagerName); MapDrawableBatch toReturn = new MapDrawableBatch(reducedLayerInfo.Quads.Count, tileDimensionWidth, tileDimensionHeight, texture); toReturn.Name = reducedLayerInfo.Name; Vector3 position = new Vector3(); Vector2 tileDimensions = new Vector2(quadWidth, quadHeight); IEnumerable<TMXGlueLib.DataTypes.ReducedQuadInfo> quads = null; if (rtmi.NumberCellsWide > rtmi.NumberCellsTall) { quads = reducedLayerInfo.Quads.OrderBy(item => item.LeftQuadCoordinate); toReturn.mSortAxis = SortAxis.X; } else { quads = reducedLayerInfo.Quads.OrderBy(item => item.BottomQuadCoordinate); toReturn.mSortAxis = SortAxis.Y; } foreach (var quad in quads) { position.X = quad.LeftQuadCoordinate; position.Y = quad.BottomQuadCoordinate; // The Z of the quad should be relative to this layer, not absolute Z values. // A multi-layer map will offset the individual layer Z values, the quads should have a Z of 0. // position.Z = reducedLayerInfo.Z; var textureValues = new Vector4(); textureValues.X = CoordinateAdjustment + (float)quad.LeftTexturePixel / (float)texture.Width; // Left textureValues.Y = -CoordinateAdjustment + (float)(quad.LeftTexturePixel + tileDimensionWidth) / (float)texture.Width; // Right textureValues.Z = CoordinateAdjustment + (float)quad.TopTexturePixel / (float)texture.Height; // Top textureValues.W = -CoordinateAdjustment + (float)(quad.TopTexturePixel + tileDimensionHeight) / (float)texture.Height; // Bottom // pad before doing any rotations/flipping const bool pad = true; if (pad) { const float amountToAdd = .0000001f; textureValues.X += amountToAdd; // Left textureValues.Y -= amountToAdd; // Right textureValues.Z += amountToAdd; // Top textureValues.W -= amountToAdd; // Bottom } if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) { var temp = textureValues.Y; textureValues.Y = textureValues.X; textureValues.X = temp; } if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) { var temp = textureValues.Z; textureValues.Z = textureValues.W; textureValues.W = temp; } int tileIndex = toReturn.AddTile(position, tileDimensions, //quad.LeftTexturePixel, quad.TopTexturePixel, quad.LeftTexturePixel + tileDimensionWidth, quad.TopTexturePixel + tileDimensionHeight); textureValues); if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) { toReturn.ApplyDiagonalFlip(tileIndex); } if (quad.QuadSpecificProperties != null) { var listToAdd = quad.QuadSpecificProperties.ToList(); listToAdd.Add(new NamedValue { Name = "Name", Value = quad.Name }); owner.Properties.Add(quad.Name, listToAdd); } toReturn.RegisterName(quad.Name, tileIndex); } return toReturn; }
public static void AddCollisionFrom(this TileShapeCollection tileShapeCollection, LayeredTileMap layeredTileMap, IEnumerable<string> namesToUse) { Func<List<TMXGlueLib.DataTypes.NamedValue>, bool> predicate = (list) => { var nameProperty = list.FirstOrDefault(item => item.Name.ToLower() == "name"); return namesToUse.Contains(nameProperty.Value); }; AddCollisionFrom(tileShapeCollection, layeredTileMap, predicate); }
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); } } } } } }
public static void CreateEntitiesFrom(LayeredTileMap layeredTileMap, Dictionary<string, TileMapInfo> tileMapInfos) { // prob need to clear out the tileShapeCollection var entitiesToRemove = new List<string>(); float dimension = float.NaN; float dimensionHalf = 0; foreach (var layer in layeredTileMap.MapLayers) { var dictionary = layer.NamedTileOrderedIndexes; foreach (var info in tileMapInfos) { if (!string.IsNullOrEmpty(info.Value.EntityToCreate) && dictionary.ContainsKey(info.Key)) { var factories = Assembly.GetExecutingAssembly() .GetTypes() .Where(t => t.GetInterfaces().Contains(typeof (IEntityFactory)) && t.GetConstructor(Type.EmptyTypes) != null) .Select( t => { var propertyInfo = t.GetProperty("Self"); var value = propertyInfo.GetValue(null, null); return value as IEntityFactory; }).ToList(); foreach (var factory in factories) { var type = factory.GetType(); var methodInfo = type.GetMethod("CreateNew", new[] {typeof (Layer)}); var returntypeString = methodInfo.ReturnType.Name; if (info.Value.EntityToCreate.EndsWith(returntypeString)) { entitiesToRemove.Add(info.Value.EntityToCreate); var indexList = dictionary[info.Key]; foreach (var index in indexList) { float left; float bottom; layer.GetBottomLeftWorldCoordinateForOrderedTile(index, out left, out bottom); if (float.IsNaN(dimension)) { dimension = layer.Vertices[index + 1].Position.X - left; dimensionHalf = dimension/2.0f; } var entity = factory.CreateNew() as PositionedObject; if (entity != null) { entity.X = left + dimensionHalf; entity.Y = bottom + dimensionHalf; } } } } } } } foreach (var entityToRemove in entitiesToRemove) { string remove = entityToRemove; layeredTileMap.RemoveTiles(t => t.EntityToCreate == remove, tileMapInfos.Values); } }