using System; using Amphora; using Azimuth; using UnitTestSharp; namespace Konis.UnitTests { public class FlipbookTrackTests : TestFixture { public static FlipbookDef Def( FlipbookLoop loop, int stepCount = 3, Variation? duration = null, Variation start = default(Variation), FlipbookTiming timing = FlipbookTiming.Durations) { SheetSpriteDef[] sprites = SheetSpriteDef.Run( PixelSheetDefTests.Sheet(), row: 0, firstColumn: 0, count: stepCount); return new FlipbookDef { Loop = loop, StartStep = start, Timing = timing, Steps = FlipbookStep.Each(duration ?? 1, sprites), }; } public static ParticleStore Store(int particles = 0) { var store = new ParticleStore(capacity: 4, columnCount: FlipbookTrack.ColumnCount); for (int i = 0; i < particles; i++) { store.Add(new Particle()); } return store; } public static void Advance(FlipbookTrack track, ParticleStore store, float seconds) { track.Step(store, seconds, new ScriptedGenerator()); } public class CtorTests : TestFixture { public void ANullDef_Throws() { CheckThrow(typeof(ArgumentNullException)); new FlipbookTrack(null, 0); } public void AFlipbookOfOneStep_Throws() { CheckThrow(typeof(ArgumentException)); new FlipbookTrack(Def(FlipbookLoop.Repeat, stepCount: 1), 0); } } public class IsConstantTests : TestFixture { public void IsFalse() { CheckFalse(new FlipbookTrack(Def(FlipbookLoop.Once), 0).IsConstant); } } public class NeedsStepTests : TestFixture { public void IsTrue() { CheckTrue(new FlipbookTrack(Def(FlipbookLoop.Once), 0).NeedsStep); } } public class StepAtTests : TestFixture { public void ForARepeatingFlipbook_IsThePosition() { var track = new FlipbookTrack(Def(FlipbookLoop.Repeat), 0); CheckEqual(0, track.StepAt(0)); CheckEqual(1, track.StepAt(1)); CheckEqual(2, track.StepAt(2)); } public void ForAPingPongFlipbook_ComesBackDownWithoutRepeatingTheEnds() { var track = new FlipbookTrack(Def(FlipbookLoop.PingPong, stepCount: 4), 0); CheckEqual(3, track.StepAt(3)); CheckEqual(2, track.StepAt(4)); CheckEqual(1, track.StepAt(5)); } } public class BirthTests : TestFixture { private static int BornAt(FlipbookDef def, ScriptedGenerator random = null) { var track = new FlipbookTrack(def, 0); ParticleStore store = Store(particles: 1); track.Birth(store, 0, random ?? new ScriptedGenerator()); return track.FrameOf(store, 0); } public void WithNoStart_BeginsOnTheFirstStep() { CheckEqual(0, BornAt(Def(FlipbookLoop.Repeat))); } public void BeginsOnTheStartStep() { CheckEqual(2, BornAt(Def(FlipbookLoop.Repeat, start: 2))); } public void ARoundedStart_RoundsDown() { CheckEqual(1, BornAt(Def(FlipbookLoop.Repeat, start: 1.9))); } public void AStartPastTheLastStep_BeginsOnTheLast() { CheckEqual(2, BornAt(Def(FlipbookLoop.Repeat, start: 5))); } public void ANegativeStart_BeginsOnTheFirstStep() { CheckEqual(0, BornAt(Def(FlipbookLoop.Repeat, start: -3))); } public void ARangeOfStarts_DrawsOne() { ScriptedGenerator random = ScriptedGenerator.OfUnits(0.25); CheckEqual(1, BornAt(Def(FlipbookLoop.Repeat, start: new Variation(0, 2)), random)); CheckEqual(0, random.Remaining); } public void WithFixedTimesAndNoStartRange_DrawsNothing() { ScriptedGenerator random = ScriptedGenerator.OfUnits(0.5); BornAt(Def(FlipbookLoop.Repeat), random); CheckEqual(1, random.Remaining); } public void ARangeOfDurations_DrawsTheFirstStepsTime() { ScriptedGenerator random = ScriptedGenerator.OfUnits(0.5); BornAt(Def(FlipbookLoop.Repeat, duration: new Variation(1, 2)), random); CheckEqual(0, random.Remaining); } public void OnTheLastStepOfAFlipbookThatHolds_DrawsNoDuration() { ScriptedGenerator random = ScriptedGenerator.OfUnits(0.5); BornAt(Def(FlipbookLoop.Once, start: 2, duration: new Variation(1, 2)), random); CheckEqual(1, random.Remaining); } } public class StepTests : TestFixture { private static int FrameAfter(FlipbookLoop loop, int stepCount, params float[] stepsOfSeconds) { var track = new FlipbookTrack(Def(loop, stepCount), 0); ParticleStore store = Store(particles: 1); track.Birth(store, 0, new ScriptedGenerator()); foreach (float seconds in stepsOfSeconds) { Advance(track, store, seconds); } return track.FrameOf(store, 0); } public void BeforeTheStepsTimeIsUp_StaysOnIt() { CheckEqual(0, FrameAfter(FlipbookLoop.Repeat, 3, 0.5f)); } public void WhenTheStepsTimeIsUp_MovesToTheNext() { CheckEqual(1, FrameAfter(FlipbookLoop.Repeat, 3, 1f)); } public void ALongFrame_MovesThroughSeveralSteps() { CheckEqual(2, FrameAfter(FlipbookLoop.Repeat, 3, 2.5f)); } public void TimeOverTheStepsEnd_CountsAgainstTheNextStep() { CheckEqual(1, FrameAfter(FlipbookLoop.Repeat, 3, 1.5f, 0.4f)); CheckEqual(2, FrameAfter(FlipbookLoop.Repeat, 3, 1.5f, 0.4f, 0.2f)); } public void ARepeatingFlipbook_ReturnsToTheFirstStep() { CheckEqual(0, FrameAfter(FlipbookLoop.Repeat, 3, 3f)); CheckEqual(1, FrameAfter(FlipbookLoop.Repeat, 3, 4f)); } public void AFlipbookThatHolds_StaysOnTheLastStep() { CheckEqual(2, FrameAfter(FlipbookLoop.Once, 3, 2f)); CheckEqual(2, FrameAfter(FlipbookLoop.Once, 3, 10f)); CheckEqual(2, FrameAfter(FlipbookLoop.Once, 3, 10f, 1000f)); } public void APingPongFlipbook_TurnsAroundAtTheEnds() { CheckEqual(2, FrameAfter(FlipbookLoop.PingPong, 3, 2f)); CheckEqual(1, FrameAfter(FlipbookLoop.PingPong, 3, 3f)); CheckEqual(0, FrameAfter(FlipbookLoop.PingPong, 3, 4f)); CheckEqual(1, FrameAfter(FlipbookLoop.PingPong, 3, 5f)); } public void ARangeOfDurations_DrawsEachStepsTimeAsItIsReached() { var track = new FlipbookTrack(Def(FlipbookLoop.Repeat, duration: new Variation(1, 2)), 0); ParticleStore store = Store(particles: 1); ScriptedGenerator random = ScriptedGenerator.OfUnits(0.5, 0.5); track.Birth(store, 0, random); track.Step(store, 1.4f, random); CheckEqual(0, track.FrameOf(store, 0)); CheckEqual(1, random.Remaining); track.Step(store, 0.2f, random); CheckEqual(1, track.FrameOf(store, 0)); CheckEqual(0, random.Remaining); } public void EveryParticleKeepsItsOwnTime() { var track = new FlipbookTrack(Def(FlipbookLoop.Repeat), 0); ParticleStore store = Store(particles: 2); track.Birth(store, 0, new ScriptedGenerator()); Advance(track, store, 0.75f); track.Birth(store, 1, new ScriptedGenerator()); Advance(track, store, 0.5f); CheckEqual(1, track.FrameOf(store, 0)); CheckEqual(0, track.FrameOf(store, 1)); } public void WithNoParticles_DrawsNothing() { var track = new FlipbookTrack(Def(FlipbookLoop.Repeat, duration: new Variation(1, 2)), 0); ScriptedGenerator random = ScriptedGenerator.OfUnits(0.5); track.Step(Store(), 5f, random); CheckEqual(1, random.Remaining); } public void UsesTheColumnsFromTheFirstColumnGiven() { var track = new FlipbookTrack(Def(FlipbookLoop.Repeat), 2); var store = new ParticleStore(capacity: 4, columnCount: 4); store.Add(new Particle()); track.Birth(store, 0, new ScriptedGenerator()); Advance(track, store, 1f); CheckEqual(1, track.FrameOf(store, 0)); CheckEqual(0f, store.Columns[0][0]); CheckEqual(0f, store.Columns[1][0]); } } public class FrameOfTests : TestFixture { public void ReadsTheStepFromTheParticlesPosition() { var track = new FlipbookTrack(Def(FlipbookLoop.PingPong), 0); ParticleStore store = Store(particles: 1); store.Columns[0][0] = 3; CheckEqual(1, track.FrameOf(store, 0)); } } } }