TilemapTileAnimationTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using MonoGame.Extended.Tests.Fixtures;
  6. using MonoGame.Extended.Tilemaps;
  7. namespace MonoGame.Extended.Tests
  8. {
  9. [Collection("GraphicsTest")]
  10. public sealed class TilemapTileAnimationTests
  11. {
  12. private readonly GraphicsTestFixture _graphicsFixture;
  13. public TilemapTileAnimationTests(GraphicsTestFixture graphicsTestFixture)
  14. {
  15. _graphicsFixture = graphicsTestFixture;
  16. }
  17. [Fact]
  18. public void Constructor_SetsFrames()
  19. {
  20. // Arrange
  21. var frames = new TilemapTileAnimationFrame[]
  22. {
  23. new(0, 0.1f),
  24. new(1, 0.2f)
  25. };
  26. // Act
  27. var animation = new TilemapTileAnimation(frames);
  28. // Assert
  29. Assert.Equal(frames, animation.Frames);
  30. Assert.Equal(2, animation.Frames.Length);
  31. }
  32. [Fact]
  33. public void TotalDuration_SumsAllFrameDurations()
  34. {
  35. // Arrange
  36. var frames = new TilemapTileAnimationFrame[]
  37. {
  38. new(0, 0.1f),
  39. new(1, 0.2f),
  40. new(2, 0.3f)
  41. };
  42. var animation = new TilemapTileAnimation(frames);
  43. // Act
  44. var total = animation.TotalDuration;
  45. // Assert
  46. Assert.Equal(0.6f, total, 0.001f);
  47. }
  48. [Fact]
  49. public void TotalDuration_WithSingleFrame_ReturnsSingleDuration()
  50. {
  51. // Arrange
  52. var frames = new TilemapTileAnimationFrame[] { new(0, 0.5f) };
  53. var animation = new TilemapTileAnimation(frames);
  54. // Act
  55. var total = animation.TotalDuration;
  56. // Assert
  57. Assert.Equal(0.5f, total, 0.001f);
  58. }
  59. [Fact]
  60. public void TotalDuration_WithEmptyFrames_ReturnsZero()
  61. {
  62. // Arrange
  63. var animation = new TilemapTileAnimation(Array.Empty<TilemapTileAnimationFrame>());
  64. // Act
  65. var total = animation.TotalDuration;
  66. // Assert
  67. Assert.Equal(0f, total);
  68. }
  69. [Fact]
  70. public void CurrentFrameIndex_InitiallyZero()
  71. {
  72. // Arrange
  73. var frames = new TilemapTileAnimationFrame[] { new(0, 0.1f) };
  74. var animation = new TilemapTileAnimation(frames);
  75. // Act & Assert
  76. Assert.Equal(0, animation.CurrentFrameIndex);
  77. }
  78. [Fact]
  79. public void CurrentFrame_ReturnsFirstFrame()
  80. {
  81. // Arrange
  82. var frames = new TilemapTileAnimationFrame[]
  83. {
  84. new(10, 0.1f),
  85. new(11, 0.2f)
  86. };
  87. var animation = new TilemapTileAnimation(frames);
  88. // Act
  89. var current = animation.CurrentFrame;
  90. // Assert
  91. Assert.Equal(10, current.TileId);
  92. Assert.Equal(0.1f, current.Duration, 0.001f);
  93. }
  94. [Fact]
  95. public void Update_WithSmallDelta_StaysOnSameFrame()
  96. {
  97. // Arrange
  98. var frames = new TilemapTileAnimationFrame[] { new(0, 0.5f), new(1, 0.5f) };
  99. var animation = new TilemapTileAnimation(frames);
  100. // Act
  101. animation.Update(0.1f);
  102. // Assert
  103. Assert.Equal(0, animation.CurrentFrameIndex);
  104. Assert.Equal(0, animation.CurrentFrame.TileId);
  105. }
  106. [Fact]
  107. public void Update_WithExactFrameDuration_AdvancesToNextFrame()
  108. {
  109. // Arrange
  110. var frames = new TilemapTileAnimationFrame[] { new(0, 0.5f), new(1, 0.5f) };
  111. var animation = new TilemapTileAnimation(frames);
  112. // Act
  113. animation.Update(0.5f);
  114. // Assert
  115. Assert.Equal(1, animation.CurrentFrameIndex);
  116. Assert.Equal(1, animation.CurrentFrame.TileId);
  117. }
  118. [Fact]
  119. public void Update_WithLargeDelta_AdvancesMultipleFrames()
  120. {
  121. // Arrange
  122. var frames = new TilemapTileAnimationFrame[]
  123. {
  124. new(0, 0.1f),
  125. new(1, 0.1f),
  126. new(2, 0.1f)
  127. };
  128. var animation = new TilemapTileAnimation(frames);
  129. // Act
  130. animation.Update(0.25f); // Should advance to frame 2 (0.1 + 0.1 + 0.05)
  131. // Assert
  132. Assert.Equal(2, animation.CurrentFrameIndex);
  133. Assert.Equal(2, animation.CurrentFrame.TileId);
  134. }
  135. [Fact]
  136. public void Update_WhenReachingEnd_WrapsToBeginning()
  137. {
  138. // Arrange
  139. var frames = new TilemapTileAnimationFrame[]
  140. {
  141. new(0, 0.1f),
  142. new(1, 0.1f)
  143. };
  144. var animation = new TilemapTileAnimation(frames);
  145. // Act
  146. animation.Update(0.25f); // Total 0.2, should wrap with 0.05 into first frame
  147. // Assert
  148. Assert.Equal(0, animation.CurrentFrameIndex);
  149. Assert.Equal(0, animation.CurrentFrame.TileId);
  150. }
  151. [Fact]
  152. public void Update_MultipleUpdates_AdvancesCorrectly()
  153. {
  154. // Arrange
  155. var frames = new TilemapTileAnimationFrame[]
  156. {
  157. new(0, 0.1f),
  158. new(1, 0.1f),
  159. new(2, 0.1f)
  160. };
  161. var animation = new TilemapTileAnimation(frames);
  162. // Act & Assert - Step through animation
  163. animation.Update(0.05f);
  164. Assert.Equal(0, animation.CurrentFrameIndex);
  165. animation.Update(0.06f); // Total 0.11
  166. Assert.Equal(1, animation.CurrentFrameIndex);
  167. animation.Update(0.15f); // Total 0.26, should wrap
  168. Assert.Equal(2, animation.CurrentFrameIndex);
  169. animation.Update(0.15f); // Total 0.41, should wrap to frame 1
  170. Assert.Equal(1, animation.CurrentFrameIndex);
  171. }
  172. [Fact]
  173. public void Update_WithEmptyFrames_DoesNotThrow()
  174. {
  175. // Arrange
  176. var animation = new TilemapTileAnimation(Array.Empty<TilemapTileAnimationFrame>());
  177. // Act & Assert - Should not throw
  178. animation.Update(1.0f);
  179. }
  180. [Fact]
  181. public void Reset_ResetsToFirstFrame()
  182. {
  183. // Arrange
  184. var frames = new TilemapTileAnimationFrame[] { new(0, 0.1f), new(1, 0.1f) };
  185. var animation = new TilemapTileAnimation(frames);
  186. animation.Update(0.15f); // Advance to second frame
  187. // Act
  188. animation.Reset();
  189. // Assert
  190. Assert.Equal(0, animation.CurrentFrameIndex);
  191. }
  192. [Fact]
  193. public void GetFrameAtTime_WithZeroTime_ReturnsFirstFrame()
  194. {
  195. // Arrange
  196. var frames = new TilemapTileAnimationFrame[]
  197. {
  198. new(10, 0.1f),
  199. new(11, 0.2f),
  200. new(12, 0.3f)
  201. };
  202. var animation = new TilemapTileAnimation(frames);
  203. // Act
  204. var frame = animation.GetFrameAtTime(0f);
  205. // Assert
  206. Assert.Equal(10, frame.TileId);
  207. }
  208. [Fact]
  209. public void GetFrameAtTime_WithinFirstFrame_ReturnsFirstFrame()
  210. {
  211. // Arrange
  212. var frames = new TilemapTileAnimationFrame[]
  213. {
  214. new(10, 0.1f),
  215. new(11, 0.2f)
  216. };
  217. var animation = new TilemapTileAnimation(frames);
  218. // Act
  219. var frame = animation.GetFrameAtTime(0.05f);
  220. // Assert
  221. Assert.Equal(10, frame.TileId);
  222. }
  223. [Fact]
  224. public void GetFrameAtTime_InSecondFrame_ReturnsSecondFrame()
  225. {
  226. // Arrange
  227. var frames = new TilemapTileAnimationFrame[]
  228. {
  229. new(10, 0.1f),
  230. new(11, 0.2f),
  231. new(12, 0.3f)
  232. };
  233. var animation = new TilemapTileAnimation(frames);
  234. // Act
  235. var frame = animation.GetFrameAtTime(0.15f); // 0.1 + 0.05 into second frame
  236. // Assert
  237. Assert.Equal(11, frame.TileId);
  238. }
  239. [Fact]
  240. public void GetFrameAtTime_BeyondTotalDuration_Wraps()
  241. {
  242. // Arrange
  243. var frames = new TilemapTileAnimationFrame[]
  244. {
  245. new(10, 0.1f),
  246. new(11, 0.2f)
  247. };
  248. var animation = new TilemapTileAnimation(frames); // Total duration: 0.3
  249. // Act
  250. var frame = animation.GetFrameAtTime(0.35f); // 0.05 into second loop
  251. // Assert
  252. Assert.Equal(10, frame.TileId);
  253. }
  254. [Fact]
  255. public void GetFrameAtTime_WithNegativeTime_WrapsFromEnd()
  256. {
  257. // Arrange
  258. var frames = new TilemapTileAnimationFrame[]
  259. {
  260. new(10, 0.1f),
  261. new(11, 0.2f)
  262. };
  263. var animation = new TilemapTileAnimation(frames); // Total duration: 0.3
  264. // Act
  265. var frame = animation.GetFrameAtTime(-0.1f); // Should wrap to 0.2
  266. // Assert
  267. Assert.Equal(11, frame.TileId);
  268. }
  269. [Fact]
  270. public void GetFrameAtTime_WithEmptyFrames_ThrowsInvalidOperationException()
  271. {
  272. // Arrange
  273. var animation = new TilemapTileAnimation(Array.Empty<TilemapTileAnimationFrame>());
  274. // Act & Assert
  275. Assert.Throws<InvalidOperationException>(() => animation.GetFrameAtTime(0f));
  276. }
  277. [Fact]
  278. public void GetFrameAtTime_DoesNotAffectCurrentState()
  279. {
  280. // Arrange
  281. var frames = new TilemapTileAnimationFrame[]
  282. {
  283. new(10, 0.1f),
  284. new(11, 0.2f)
  285. };
  286. var animation = new TilemapTileAnimation(frames);
  287. // Act
  288. var frame1 = animation.GetFrameAtTime(0.15f);
  289. var frame2 = animation.GetFrameAtTime(0.25f);
  290. // Assert - CurrentFrameIndex should still be 0
  291. Assert.Equal(11, frame1.TileId);
  292. Assert.Equal(11, frame2.TileId);
  293. Assert.Equal(0, animation.CurrentFrameIndex);
  294. Assert.Equal(10, animation.CurrentFrame.TileId);
  295. }
  296. }
  297. }