| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using MonoGame.Extended.Tests.Fixtures;
- using MonoGame.Extended.Tilemaps;
- namespace MonoGame.Extended.Tests
- {
- [Collection("GraphicsTest")]
- public sealed class TilemapTileAnimationTests
- {
- private readonly GraphicsTestFixture _graphicsFixture;
- public TilemapTileAnimationTests(GraphicsTestFixture graphicsTestFixture)
- {
- _graphicsFixture = graphicsTestFixture;
- }
- [Fact]
- public void Constructor_SetsFrames()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(0, 0.1f),
- new(1, 0.2f)
- };
- // Act
- var animation = new TilemapTileAnimation(frames);
- // Assert
- Assert.Equal(frames, animation.Frames);
- Assert.Equal(2, animation.Frames.Length);
- }
- [Fact]
- public void TotalDuration_SumsAllFrameDurations()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(0, 0.1f),
- new(1, 0.2f),
- new(2, 0.3f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- var total = animation.TotalDuration;
- // Assert
- Assert.Equal(0.6f, total, 0.001f);
- }
- [Fact]
- public void TotalDuration_WithSingleFrame_ReturnsSingleDuration()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[] { new(0, 0.5f) };
- var animation = new TilemapTileAnimation(frames);
- // Act
- var total = animation.TotalDuration;
- // Assert
- Assert.Equal(0.5f, total, 0.001f);
- }
- [Fact]
- public void TotalDuration_WithEmptyFrames_ReturnsZero()
- {
- // Arrange
- var animation = new TilemapTileAnimation(Array.Empty<TilemapTileAnimationFrame>());
- // Act
- var total = animation.TotalDuration;
- // Assert
- Assert.Equal(0f, total);
- }
- [Fact]
- public void CurrentFrameIndex_InitiallyZero()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[] { new(0, 0.1f) };
- var animation = new TilemapTileAnimation(frames);
- // Act & Assert
- Assert.Equal(0, animation.CurrentFrameIndex);
- }
- [Fact]
- public void CurrentFrame_ReturnsFirstFrame()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(10, 0.1f),
- new(11, 0.2f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- var current = animation.CurrentFrame;
- // Assert
- Assert.Equal(10, current.TileId);
- Assert.Equal(0.1f, current.Duration, 0.001f);
- }
- [Fact]
- public void Update_WithSmallDelta_StaysOnSameFrame()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[] { new(0, 0.5f), new(1, 0.5f) };
- var animation = new TilemapTileAnimation(frames);
- // Act
- animation.Update(0.1f);
- // Assert
- Assert.Equal(0, animation.CurrentFrameIndex);
- Assert.Equal(0, animation.CurrentFrame.TileId);
- }
- [Fact]
- public void Update_WithExactFrameDuration_AdvancesToNextFrame()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[] { new(0, 0.5f), new(1, 0.5f) };
- var animation = new TilemapTileAnimation(frames);
- // Act
- animation.Update(0.5f);
- // Assert
- Assert.Equal(1, animation.CurrentFrameIndex);
- Assert.Equal(1, animation.CurrentFrame.TileId);
- }
- [Fact]
- public void Update_WithLargeDelta_AdvancesMultipleFrames()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(0, 0.1f),
- new(1, 0.1f),
- new(2, 0.1f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- animation.Update(0.25f); // Should advance to frame 2 (0.1 + 0.1 + 0.05)
- // Assert
- Assert.Equal(2, animation.CurrentFrameIndex);
- Assert.Equal(2, animation.CurrentFrame.TileId);
- }
- [Fact]
- public void Update_WhenReachingEnd_WrapsToBeginning()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(0, 0.1f),
- new(1, 0.1f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- animation.Update(0.25f); // Total 0.2, should wrap with 0.05 into first frame
- // Assert
- Assert.Equal(0, animation.CurrentFrameIndex);
- Assert.Equal(0, animation.CurrentFrame.TileId);
- }
- [Fact]
- public void Update_MultipleUpdates_AdvancesCorrectly()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(0, 0.1f),
- new(1, 0.1f),
- new(2, 0.1f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act & Assert - Step through animation
- animation.Update(0.05f);
- Assert.Equal(0, animation.CurrentFrameIndex);
- animation.Update(0.06f); // Total 0.11
- Assert.Equal(1, animation.CurrentFrameIndex);
- animation.Update(0.15f); // Total 0.26, should wrap
- Assert.Equal(2, animation.CurrentFrameIndex);
- animation.Update(0.15f); // Total 0.41, should wrap to frame 1
- Assert.Equal(1, animation.CurrentFrameIndex);
- }
- [Fact]
- public void Update_WithEmptyFrames_DoesNotThrow()
- {
- // Arrange
- var animation = new TilemapTileAnimation(Array.Empty<TilemapTileAnimationFrame>());
- // Act & Assert - Should not throw
- animation.Update(1.0f);
- }
- [Fact]
- public void Reset_ResetsToFirstFrame()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[] { new(0, 0.1f), new(1, 0.1f) };
- var animation = new TilemapTileAnimation(frames);
- animation.Update(0.15f); // Advance to second frame
- // Act
- animation.Reset();
- // Assert
- Assert.Equal(0, animation.CurrentFrameIndex);
- }
- [Fact]
- public void GetFrameAtTime_WithZeroTime_ReturnsFirstFrame()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(10, 0.1f),
- new(11, 0.2f),
- new(12, 0.3f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- var frame = animation.GetFrameAtTime(0f);
- // Assert
- Assert.Equal(10, frame.TileId);
- }
- [Fact]
- public void GetFrameAtTime_WithinFirstFrame_ReturnsFirstFrame()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(10, 0.1f),
- new(11, 0.2f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- var frame = animation.GetFrameAtTime(0.05f);
- // Assert
- Assert.Equal(10, frame.TileId);
- }
- [Fact]
- public void GetFrameAtTime_InSecondFrame_ReturnsSecondFrame()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(10, 0.1f),
- new(11, 0.2f),
- new(12, 0.3f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- var frame = animation.GetFrameAtTime(0.15f); // 0.1 + 0.05 into second frame
- // Assert
- Assert.Equal(11, frame.TileId);
- }
- [Fact]
- public void GetFrameAtTime_BeyondTotalDuration_Wraps()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(10, 0.1f),
- new(11, 0.2f)
- };
- var animation = new TilemapTileAnimation(frames); // Total duration: 0.3
- // Act
- var frame = animation.GetFrameAtTime(0.35f); // 0.05 into second loop
- // Assert
- Assert.Equal(10, frame.TileId);
- }
- [Fact]
- public void GetFrameAtTime_WithNegativeTime_WrapsFromEnd()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(10, 0.1f),
- new(11, 0.2f)
- };
- var animation = new TilemapTileAnimation(frames); // Total duration: 0.3
- // Act
- var frame = animation.GetFrameAtTime(-0.1f); // Should wrap to 0.2
- // Assert
- Assert.Equal(11, frame.TileId);
- }
- [Fact]
- public void GetFrameAtTime_WithEmptyFrames_ThrowsInvalidOperationException()
- {
- // Arrange
- var animation = new TilemapTileAnimation(Array.Empty<TilemapTileAnimationFrame>());
- // Act & Assert
- Assert.Throws<InvalidOperationException>(() => animation.GetFrameAtTime(0f));
- }
- [Fact]
- public void GetFrameAtTime_DoesNotAffectCurrentState()
- {
- // Arrange
- var frames = new TilemapTileAnimationFrame[]
- {
- new(10, 0.1f),
- new(11, 0.2f)
- };
- var animation = new TilemapTileAnimation(frames);
- // Act
- var frame1 = animation.GetFrameAtTime(0.15f);
- var frame2 = animation.GetFrameAtTime(0.25f);
- // Assert - CurrentFrameIndex should still be 0
- Assert.Equal(11, frame1.TileId);
- Assert.Equal(11, frame2.TileId);
- Assert.Equal(0, animation.CurrentFrameIndex);
- Assert.Equal(10, animation.CurrentFrame.TileId);
- }
- }
- }
|