TiledMapTilesetTileAnimationFrame.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. namespace MonoGame.Extended.Tiled
  5. {
  6. public struct TiledMapTilesetTileAnimationFrame
  7. {
  8. public readonly int LocalTileIdentifier;
  9. public readonly TimeSpan Duration;
  10. public readonly Vector2[] TextureCoordinates;
  11. private readonly Dictionary<TiledMapTileFlipFlags, Vector2[]> _flipDictionary = new Dictionary<TiledMapTileFlipFlags, Vector2[]>();
  12. internal TiledMapTilesetTileAnimationFrame(TiledMapTileset tileset, int localTileIdentifier, int durationInMilliseconds)
  13. {
  14. LocalTileIdentifier = localTileIdentifier;
  15. Duration = new TimeSpan(0, 0, 0, 0, durationInMilliseconds);
  16. TextureCoordinates = new Vector2[4];
  17. CreateTextureCoordinates(tileset);
  18. }
  19. public Vector2[] GetTextureCoordinates(TiledMapTileFlipFlags flipFlags)
  20. {
  21. if (!_flipDictionary.TryGetValue(flipFlags, out Vector2[] flippedTextureCoordiantes))
  22. {
  23. return TextureCoordinates;
  24. }
  25. else
  26. {
  27. return flippedTextureCoordiantes;
  28. }
  29. }
  30. public void CreateTextureRotations(TiledMapTileset tileset, TiledMapTileFlipFlags flipFlags)
  31. {
  32. if (!_flipDictionary.ContainsKey(flipFlags))
  33. {
  34. if (flipFlags == TiledMapTileFlipFlags.None)
  35. {
  36. _flipDictionary.Add(flipFlags, TextureCoordinates);
  37. }
  38. else
  39. {
  40. _flipDictionary.Add(flipFlags, TransformTextureCoordinates(tileset, flipFlags));
  41. }
  42. }
  43. }
  44. public Vector2[] TransformTextureCoordinates(TiledMapTileset tileset, TiledMapTileFlipFlags flipFlags)
  45. {
  46. var sourceRectangle = tileset.GetTileRegion(LocalTileIdentifier);
  47. var texture = tileset.Texture;
  48. var texelLeft = (float)sourceRectangle.X / texture.Width;
  49. var texelTop = (float)sourceRectangle.Y / texture.Height;
  50. var texelRight = (sourceRectangle.X + sourceRectangle.Width) / (float)texture.Width;
  51. var texelBottom = (sourceRectangle.Y + sourceRectangle.Height) / (float)texture.Height;
  52. var flipDiagonally = (flipFlags & TiledMapTileFlipFlags.FlipDiagonally) != 0;
  53. var flipHorizontally = (flipFlags & TiledMapTileFlipFlags.FlipHorizontally) != 0;
  54. var flipVertically = (flipFlags & TiledMapTileFlipFlags.FlipVertically) != 0;
  55. var transform = new Vector2[4];
  56. transform[0].X = texelLeft;
  57. transform[0].Y = texelTop;
  58. transform[1].X = texelRight;
  59. transform[1].Y = texelTop;
  60. transform[2].X = texelLeft;
  61. transform[2].Y = texelBottom;
  62. transform[3].X = texelRight;
  63. transform[3].Y = texelBottom;
  64. if (flipDiagonally)
  65. {
  66. FloatHelper.Swap(ref transform[1].X, ref transform[2].X);
  67. FloatHelper.Swap(ref transform[1].Y, ref transform[2].Y);
  68. }
  69. if (flipHorizontally)
  70. {
  71. if (flipDiagonally)
  72. {
  73. FloatHelper.Swap(ref transform[0].Y, ref transform[1].Y);
  74. FloatHelper.Swap(ref transform[2].Y, ref transform[3].Y);
  75. }
  76. else
  77. {
  78. FloatHelper.Swap(ref transform[0].X, ref transform[1].X);
  79. FloatHelper.Swap(ref transform[2].X, ref transform[3].X);
  80. }
  81. }
  82. if (flipVertically)
  83. {
  84. if (flipDiagonally)
  85. {
  86. FloatHelper.Swap(ref transform[0].X, ref transform[2].X);
  87. FloatHelper.Swap(ref transform[1].X, ref transform[3].X);
  88. }
  89. else
  90. {
  91. FloatHelper.Swap(ref transform[0].Y, ref transform[2].Y);
  92. FloatHelper.Swap(ref transform[1].Y, ref transform[3].Y);
  93. }
  94. }
  95. transform[0] = transform[0];
  96. transform[1] = transform[1];
  97. transform[2] = transform[2];
  98. transform[3] = transform[3];
  99. return transform;
  100. }
  101. private void CreateTextureCoordinates(TiledMapTileset tileset)
  102. {
  103. var sourceRectangle = tileset.GetTileRegion(LocalTileIdentifier);
  104. var texture = tileset.Texture;
  105. var texelLeft = (float)sourceRectangle.X / texture.Width;
  106. var texelTop = (float)sourceRectangle.Y / texture.Height;
  107. var texelRight = (sourceRectangle.X + sourceRectangle.Width) / (float)texture.Width;
  108. var texelBottom = (sourceRectangle.Y + sourceRectangle.Height) / (float)texture.Height;
  109. TextureCoordinates[0].X = texelLeft;
  110. TextureCoordinates[0].Y = texelTop;
  111. TextureCoordinates[1].X = texelRight;
  112. TextureCoordinates[1].Y = texelTop;
  113. TextureCoordinates[2].X = texelLeft;
  114. TextureCoordinates[2].Y = texelBottom;
  115. TextureCoordinates[3].X = texelRight;
  116. TextureCoordinates[3].Y = texelBottom;
  117. }
  118. public override string ToString()
  119. {
  120. return $"{LocalTileIdentifier}:{Duration}";
  121. }
  122. }
  123. }