internal static ReducedQuadInfo FromSpriteSave(SpriteSave spriteSave, int textureWidth, int textureHeight) { ReducedQuadInfo toReturn = new ReducedQuadInfo(); toReturn.LeftQuadCoordinate = spriteSave.X - spriteSave.ScaleX; toReturn.BottomQuadCoordinate = spriteSave.Y - spriteSave.ScaleY; bool isRotated = spriteSave.RotationZ != 0; if (isRotated) { toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedDiagonallyFlag); } var leftTextureCoordinate = System.Math.Min( spriteSave.LeftTextureCoordinate, spriteSave.RightTextureCoordinate); var topTextureCoordinate = System.Math.Min(spriteSave.TopTextureCoordinate, spriteSave.BottomTextureCoordinate); if (spriteSave.LeftTextureCoordinate > spriteSave.RightTextureCoordinate) { toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedHorizontallyFlag); } if(spriteSave.TopTextureCoordinate > spriteSave.BottomTextureCoordinate) { toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedVerticallyFlag); } toReturn.LeftTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(leftTextureCoordinate * textureWidth); toReturn.TopTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(topTextureCoordinate * textureHeight); toReturn.Name = spriteSave.Name; return toReturn; }
internal static ReducedQuadInfo FromSpriteSave(SpriteSave spriteSave, int textureWidth, int textureHeight) { ReducedQuadInfo toReturn = new ReducedQuadInfo(); toReturn.LeftQuadCoordinate = spriteSave.X - spriteSave.ScaleX; toReturn.BottomQuadCoordinate = spriteSave.Y - spriteSave.ScaleY; bool isRotated = spriteSave.RotationZ != 0; if (isRotated) { toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedDiagonallyFlag); } var leftTextureCoordinate = System.Math.Min(spriteSave.LeftTextureCoordinate, spriteSave.RightTextureCoordinate); var topTextureCoordinate = System.Math.Min(spriteSave.TopTextureCoordinate, spriteSave.BottomTextureCoordinate); if (spriteSave.LeftTextureCoordinate > spriteSave.RightTextureCoordinate) { toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedHorizontallyFlag); } if (spriteSave.TopTextureCoordinate > spriteSave.BottomTextureCoordinate) { toReturn.FlipFlags = (byte)(toReturn.FlipFlags | ReducedQuadInfo.FlippedVerticallyFlag); } toReturn.LeftTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(leftTextureCoordinate * textureWidth); toReturn.TopTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(topTextureCoordinate * textureHeight); toReturn.Name = spriteSave.Name; return(toReturn); }
public static ReducedLayerInfo ReadFrom(BinaryReader reader, int version) { ReducedLayerInfo toReturn = new ReducedLayerInfo(); toReturn.Z = reader.ReadSingle(); toReturn.Texture = reader.ReadString(); toReturn.Name = reader.ReadString(); toReturn.NumberOfQuads = reader.ReadUInt32(); for (int i = 0; i < toReturn.NumberOfQuads; i++) { toReturn.Quads.Add(ReducedQuadInfo.ReadFrom(reader)); } if (version >= 2) { toReturn.TextureId = reader.ReadInt32(); } if (version >= 3) { toReturn.ParallaxMultiplierX = reader.ReadSingle(); toReturn.ParallaxMultiplierY = reader.ReadSingle(); toReturn.CameraOffsetX = reader.ReadSingle(); toReturn.CameraOffsetY = reader.ReadSingle(); } return(toReturn); }
internal static ReducedQuadInfo FromSpriteSave(SpriteSave spriteSave, int textureWidth, int textureHeight) { ReducedQuadInfo toReturn = new ReducedQuadInfo(); toReturn.LeftQuadCoordinate = spriteSave.X - spriteSave.ScaleX; toReturn.BottomQuadCorodinate = spriteSave.Y - spriteSave.ScaleY; toReturn.LeftTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.LeftTextureCoordinate * textureWidth); toReturn.TopTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.TopTextureCoordinate * textureHeight); toReturn.Name = spriteSave.Name; return toReturn; }
public static ReducedQuadInfo ReadFrom(BinaryReader reader) { ReducedQuadInfo toReturn = new ReducedQuadInfo(); toReturn.LeftQuadCoordinate = reader.ReadSingle(); toReturn.BottomQuadCorodinate = reader.ReadSingle(); toReturn.LeftTexturePixel = reader.ReadUInt16(); toReturn.TopTexturePixel = reader.ReadUInt16(); toReturn.Name = reader.ReadString(); return toReturn; }
public static ReducedQuadInfo ReadFrom(BinaryReader reader) { ReducedQuadInfo toReturn = new ReducedQuadInfo(); toReturn.LeftQuadCoordinate = reader.ReadSingle(); toReturn.BottomQuadCorodinate = reader.ReadSingle(); toReturn.LeftTexturePixel = reader.ReadUInt16(); toReturn.TopTexturePixel = reader.ReadUInt16(); toReturn.Name = reader.ReadString(); return(toReturn); }
public static ReducedLayerInfo ReadFrom(BinaryReader reader) { ReducedLayerInfo toReturn = new ReducedLayerInfo(); toReturn.Texture = reader.ReadString(); toReturn.NumberOfQuads = reader.ReadUInt32(); for (int i = 0; i < toReturn.NumberOfQuads; i++) { toReturn.Quads.Add(ReducedQuadInfo.ReadFrom(reader)); } return(toReturn); }
private static void CreateFromSpriteEditorScene(TiledMapSave tiledMapSave, float scale, float zOffset, FileReferenceType referenceType, ReducedTileMapInfo toReturn) { var ses = tiledMapSave.ToSceneSave(scale, referenceType); // This is not a stable sort! //ses.SpriteList.Sort((first, second) => first.Z.CompareTo(second.Z)); ses.SpriteList = ses.SpriteList.OrderBy(item => item.Z).ToList(); ReducedLayerInfo reducedLayerInfo = null; float z = float.NaN; int textureWidth = 0; int textureHeight = 0; AbstractMapLayer currentLayer = null; int indexInLayer = 0; foreach (var spriteSave in ses.SpriteList) { if (spriteSave.Z != z) { indexInLayer = 0; z = spriteSave.Z; int layerIndex = FlatRedBall.Math.MathFunctions.RoundToInt(z - zOffset); var abstractMapLayer = tiledMapSave.MapLayers[layerIndex]; currentLayer = abstractMapLayer; reducedLayerInfo = new ReducedLayerInfo { Z = spriteSave.Z, Texture = spriteSave.Texture, Name = abstractMapLayer.Name, TileWidth = FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.ScaleX * 2), TileHeight = FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.ScaleY * 2) }; var mapLayer = abstractMapLayer as MapLayer; // This should have data: if (mapLayer != null) { var idOfTexture = mapLayer.data[0].tiles.FirstOrDefault(item => item != 0); Tileset tileSet = tiledMapSave.GetTilesetForGid(idOfTexture); var tilesetIndex = tiledMapSave.Tilesets.IndexOf(tileSet); textureWidth = tileSet.Images[0].width; textureHeight = tileSet.Images[0].height; reducedLayerInfo.TextureId = tilesetIndex; toReturn.Layers.Add(reducedLayerInfo); } var objectGroup = tiledMapSave.MapLayers[layerIndex] as mapObjectgroup; // This code only works based on the assumption that only one tileset will be used in any given object layer's image objects var mapObjectgroupObject = [email protected](o => o.gid != null); if (mapObjectgroupObject?.gid != null) { var idOfTexture = mapObjectgroupObject.gid.Value; Tileset tileSet = tiledMapSave.GetTilesetForGid(idOfTexture); var tilesetIndex = tiledMapSave.Tilesets.IndexOf(tileSet); textureWidth = tileSet.Images[0].width; textureHeight = tileSet.Images[0].height; reducedLayerInfo.TextureId = tilesetIndex; toReturn.Layers.Add(reducedLayerInfo); } } ReducedQuadInfo quad = ReducedQuadInfo.FromSpriteSave(spriteSave, textureWidth, textureHeight); if (currentLayer is mapObjectgroup) { var asMapObjectGroup = currentLayer as mapObjectgroup; var objectInstance = asMapObjectGroup.@object[indexInLayer]; // skip over any non-sprite objects: while (objectInstance.gid == null) { indexInLayer++; if (indexInLayer >= [email protected]) { objectInstance = null; break; } else { objectInstance = asMapObjectGroup.@object[indexInLayer]; } } if (objectInstance != null && objectInstance.properties.Count != 0) { var nameProperty = objectInstance.properties.FirstOrDefault(item => item.StrippedNameLower == "name"); if (nameProperty != null) { quad.Name = nameProperty.value; } else { quad.Name = spriteSave.Name; bool needsName = string.IsNullOrEmpty(spriteSave.Name); if (needsName) { quad.Name = $"_{currentLayer.Name}runtime{indexInLayer}"; } } List <NamedValue> list = new List <NamedValue>(); foreach (var property in objectInstance.properties) { list.Add( new NamedValue { Name = property.StrippedName, Value = property.value, Type = property.Type } ); } quad.QuadSpecificProperties = list; } } reducedLayerInfo?.Quads.Add(quad); indexInLayer++; } }
/// <summary> /// Converts a TiledMapSave to a ReducedTileMapInfo object /// </summary> /// <param name="tiledMapSave">The TiledMapSave to convert</param> /// <param name="scale">The amount to scale by - default of 1</param> /// <param name="zOffset">The zOffset</param> /// <param name="directory">The directory of the file associated with the tiledMapSave, used to find file references.</param> /// <param name="referenceType">How the files in the .tmx are referenced.</param> /// <returns></returns> public static ReducedTileMapInfo FromTiledMapSave(TiledMapSave tiledMapSave, float scale, float zOffset, string directory, FileReferenceType referenceType) { var toReturn = new ReducedTileMapInfo(); toReturn.NumberCellsTall = tiledMapSave.Height; toReturn.NumberCellsWide = tiledMapSave.Width; var ses = tiledMapSave.ToSceneSave(scale, referenceType); ses.SpriteList.Sort((first, second) => first.Z.CompareTo(second.Z)); ReducedLayerInfo reducedLayerInfo = null; // If we rely on the image, it's both slow (have to open the images), and // doesn't work at runtime in games: //Dictionary<string, Point> loadedTextures = new Dictionary<string, Point>(); //SetCellWidthAndHeight(tiledMapSave, directory, toReturn, ses, loadedTextures); toReturn.CellHeightInPixels = (ushort)tiledMapSave.tileheight; toReturn.CellWidthInPixels = (ushort)tiledMapSave.tilewidth; SetQuadWidthAndHeight(toReturn, ses); float z = float.NaN; int textureWidth = 0; int textureHeight = 0; for (int i = 0; i < ses.SpriteList.Count; i++) { SpriteSave spriteSave = ses.SpriteList[i]; if (spriteSave.Z != z) { z = spriteSave.Z; reducedLayerInfo = new ReducedLayerInfo(); reducedLayerInfo.Z = spriteSave.Z; reducedLayerInfo.Texture = spriteSave.Texture; int layerIndex = FlatRedBall.Math.MathFunctions.RoundToInt(z - zOffset); var mapLayer = tiledMapSave.Layers[layerIndex]; // This should have data: var idOfTexture = mapLayer.data[0].tiles.FirstOrDefault(item => item != 0); Tileset tileSet = tiledMapSave.GetTilesetForGid(idOfTexture); var tilesetIndex = tiledMapSave.Tilesets.IndexOf(tileSet); textureWidth = tileSet.Images[0].width; textureHeight = tileSet.Images[0].height; reducedLayerInfo.Name = mapLayer.Name; reducedLayerInfo.TextureId = tilesetIndex; toReturn.Layers.Add(reducedLayerInfo); } ReducedQuadInfo quad = ReducedQuadInfo.FromSpriteSave(spriteSave, textureWidth, textureHeight); reducedLayerInfo.Quads.Add(quad); } return(toReturn); }