using System.Collections.Generic; using System.IO; using System.Linq; using NUnit.Framework; using UnityEngine; namespace DiamondArrow.Tests { public sealed class ArrowMotionTests { [Test] public void BentArrowTailFollowsOriginalTrackWhileHeadLeavesStraight() { var path=new ArrowMotionPath(new[]{Vector2.zero,new Vector2(0,2),new Vector2(2,2),new Vector2(2,4)}); var line=new List();path.Window(1,line); Assert.That(line[0],Is.EqualTo(new Vector2(0,1)));Assert.That(line[line.Count-1],Is.EqualTo(new Vector2(2,5))); Assert.That(line,Does.Contain(new Vector2(0,2)));Assert.That(line,Does.Contain(new Vector2(2,2))); path.Window(3,line);Assert.That(line[0],Is.EqualTo(new Vector2(1,2)));CollectionAssert.DoesNotContain(line,new Vector2(0,2)); Assert.That(line,Does.Contain(new Vector2(2,2)));Assert.That(line[line.Count-1],Is.EqualTo(new Vector2(2,7))); } [Test] public void EveryArrowKeepsItsLengthAtEveryPartOfItsExit() { var book=LevelBook.Parse(File.ReadAllText("Assets/DiamondArrow/Data/Levels.json"));var line=new List(); foreach(var level in book.levels)foreach(var arrow in level.arrows) { var path=new ArrowMotionPath(arrow.cells.Select(c=>new Vector2(c.x,c.y)).ToArray()); for(int i=0;i<=30;i++){float travel=(path.Length+level.size)*i/30f;path.Window(travel,line);float length=0;for(int k=1;k();path.Window(3,line); Assert.That(line.Count,Is.EqualTo(2));Assert.That(line[1]-line[0],Is.EqualTo(Vector2.right*2)); } [Test] public void BlockedArrowAdvancesHoldsAndReturnsWithoutOvershoot() { Assert.That(ArrowMotionPath.Bump(0,30),Is.Zero);Assert.That(ArrowMotionPath.Bump(.11f,30),Is.EqualTo(30));Assert.That(ArrowMotionPath.Bump(.16f,30),Is.EqualTo(30)); Assert.That(ArrowMotionPath.Bump(.28f,30),Is.InRange(0f,30f));Assert.That(ArrowMotionPath.Bump(.38f,30),Is.Zero);Assert.That(ArrowMotionPath.Bump(-1,30),Is.Zero); for(int i=0;i<60;i++)Assert.That(ArrowMotionPath.Bump(i/100f,30),Is.InRange(0f,30f)); } [Test] public void ExitMotionAcceleratesAndEndsExactlyAtTarget() { float prev=0;for(int i=0;i<=100;i++){float now=ArrowMotionPath.Travel(i/100f,.8f,900);Assert.That(now,Is.GreaterThanOrEqualTo(prev));prev=now;} Assert.That(prev,Is.EqualTo(900));Assert.That(ArrowMotionPath.Travel(-1,.8f,900),Is.Zero); float early=ArrowMotionPath.Travel(.2f,.8f,900)-ArrowMotionPath.Travel(.1f,.8f,900);float late=ArrowMotionPath.Travel(.7f,.8f,900)-ArrowMotionPath.Travel(.6f,.8f,900);Assert.That(late,Is.GreaterThan(early)); } } }