| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 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<Vector2>();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<Vector2>();
- 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<line.Count;k++)length+=Vector2.Distance(line[k-1],line[k]);Assert.That(length,Is.EqualTo(path.Length).Within(.003f),"Level "+level.number);Assert.That(Vector2.Distance(line[line.Count-1],path.Points.Last()+path.Direction*travel),Is.LessThan(.003f));}
- }
- }
- [Test] public void ExitedArrowBecomesStraightOnlyAfterTailPassesLastBend()
- {
- var path=new ArrowMotionPath(new[]{Vector2.zero,Vector2.up,Vector2.one});var line=new List<Vector2>();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));
- }
- }
- }
|