using System; using Amphora; using Azimuth; using UnitTestSharp; namespace Konis.UnitTests { public class StretchedFlipbookTrackTests : TestFixture { private static FlipbookDef EvenSteps(int stepCount = 3) { return FlipbookTrackTests.Def(FlipbookLoop.Once, stepCount, timing: FlipbookTiming.StretchToLifetime); } private static FlipbookDef WeightedSteps(double first, double second) { SheetSpriteDef[] sprites = SheetSpriteDef.Run(PixelSheetDefTests.Sheet(), row: 0, firstColumn: 0, count: 2); return new FlipbookDef { Timing = FlipbookTiming.StretchToLifetime, Steps = new[] { new FlipbookStep { Sprite = sprites[0], Duration = first }, new FlipbookStep { Sprite = sprites[1], Duration = second }, }, }; } private static int FrameAt(StretchedFlipbookTrack track, float age, float lifetime) { var store = new ParticleStore(capacity: 1, columnCount: 0); store.Add(new Particle { Age = age, Lifetime = lifetime }); return track.FrameOf(store, 0); } public class CtorTests : TestFixture { public void ANullDef_Throws() { CheckThrow(typeof(ArgumentNullException)); new StretchedFlipbookTrack(null); } public void AFlipbookOfOneStep_Throws() { CheckThrow(typeof(ArgumentException)); new StretchedFlipbookTrack(EvenSteps(stepCount: 1)); } } public class IsConstantTests : TestFixture { public void IsFalse() { CheckFalse(new StretchedFlipbookTrack(EvenSteps()).IsConstant); } } public class NeedsStepTests : TestFixture { public void IsFalse() { CheckFalse(new StretchedFlipbookTrack(EvenSteps()).NeedsStep); } } public class BirthTests : TestFixture { public void DrawsNothing() { var track = new StretchedFlipbookTrack(EvenSteps()); var store = new ParticleStore(capacity: 1, columnCount: 0); store.Add(new Particle()); ScriptedGenerator random = ScriptedGenerator.OfUnits(0.5); track.Birth(store, 0, random); CheckEqual(1, random.Remaining); } } public class FrameOfTests : TestFixture { public void AtBirth_IsTheFirstStep() { CheckEqual(0, FrameAt(new StretchedFlipbookTrack(EvenSteps()), age: 0f, lifetime: 2f)); } public void SharesTheLifeEvenlyBetweenEvenSteps() { var track = new StretchedFlipbookTrack(EvenSteps()); CheckEqual(0, FrameAt(track, age: 0.5f, lifetime: 2f)); CheckEqual(1, FrameAt(track, age: 1f, lifetime: 2f)); CheckEqual(2, FrameAt(track, age: 1.5f, lifetime: 2f)); } public void LongerAndShorterLives_ReachTheSameStepAtTheSameFraction() { var track = new StretchedFlipbookTrack(EvenSteps()); CheckEqual(1, FrameAt(track, age: 0.5f, lifetime: 1f)); CheckEqual(1, FrameAt(track, age: 5f, lifetime: 10f)); } public void AtTheEndOfTheLife_IsTheLastStep() { CheckEqual(2, FrameAt(new StretchedFlipbookTrack(EvenSteps()), age: 2f, lifetime: 2f)); } public void PastTheEndOfTheLife_StaysOnTheLastStep() { CheckEqual(2, FrameAt(new StretchedFlipbookTrack(EvenSteps()), age: 9f, lifetime: 2f)); } public void ALifetimeOfZero_IsTheLastStep() { CheckEqual(2, FrameAt(new StretchedFlipbookTrack(EvenSteps()), age: 0f, lifetime: 0f)); } public void ALongerStep_TakesALargerShareOfTheLife() { var track = new StretchedFlipbookTrack(WeightedSteps(first: 1, second: 3)); CheckEqual(0, FrameAt(track, age: 0.2f, lifetime: 1f)); CheckEqual(1, FrameAt(track, age: 0.3f, lifetime: 1f)); } public void ARangeOfDurations_WeighsByItsMiddle() { SheetSpriteDef[] sprites = SheetSpriteDef.Run( PixelSheetDefTests.Sheet(), row: 0, firstColumn: 0, count: 2); var def = new FlipbookDef { Timing = FlipbookTiming.StretchToLifetime, Steps = new[] { new FlipbookStep { Sprite = sprites[0], Duration = 1 }, new FlipbookStep { Sprite = sprites[1], Duration = new Variation(1, 5) }, }, }; var track = new StretchedFlipbookTrack(def); CheckEqual(0, FrameAt(track, age: 0.2f, lifetime: 1f)); CheckEqual(1, FrameAt(track, age: 0.3f, lifetime: 1f)); } } } }