Skip to content

Commit 3930098

Browse files
committed
Additional tests for NavMeshLink
1 parent 751ed67 commit 3930098

1 file changed

Lines changed: 70 additions & 29 deletions

File tree

Assets/Tests/NavMeshSurfaceLinkTests.cs

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,105 @@
44
using NUnit.Framework;
55
using System.Collections;
66

7+
[TestFixture]
78
public class NavMeshSurfaceLinkTests
89
{
9-
[Test]
10-
public void NavMeshLinkCanConnectTwoSurfaces()
10+
public GameObject plane1, plane2;
11+
public NavMeshLink link;
12+
public NavMeshSurface surface;
13+
14+
[SetUp]
15+
public void CreatesPlanesAndLink()
1116
{
12-
var plane1 = GameObject.CreatePrimitive(PrimitiveType.Plane);
13-
var plane2 = GameObject.CreatePrimitive(PrimitiveType.Plane);
17+
plane1 = GameObject.CreatePrimitive(PrimitiveType.Plane);
18+
plane2 = GameObject.CreatePrimitive(PrimitiveType.Plane);
1419
plane1.transform.position = 11.0f * Vector3.right;
1520

16-
var go = new GameObject();
17-
var surface = go.AddComponent<NavMeshSurface>();
21+
surface = new GameObject().AddComponent<NavMeshSurface>();
1822
surface.BuildNavMesh();
1923

2024
Assert.IsFalse(HasPathConnecting(plane1, plane2));
25+
Assert.IsFalse(HasPathConnecting(plane2, plane1));
2126

22-
var linkGO = new GameObject();
23-
var link = linkGO.AddComponent<NavMeshLink>();
27+
link = new GameObject().AddComponent<NavMeshLink>();
2428
link.startPoint = plane1.transform.position;
2529
link.endPoint = plane2.transform.position;
26-
link.UpdateLink();
2730

2831
Assert.IsTrue(HasPathConnecting(plane1, plane2));
32+
Assert.IsTrue(HasPathConnecting(plane2, plane1));
33+
}
2934

30-
GameObject.DestroyImmediate(go);
35+
[TearDown]
36+
public void DestroyPlanesAndLink()
37+
{
38+
GameObject.DestroyImmediate(surface.gameObject);
39+
GameObject.DestroyImmediate(link.gameObject);
3140
GameObject.DestroyImmediate(plane1);
3241
GameObject.DestroyImmediate(plane2);
33-
GameObject.DestroyImmediate(linkGO);
3442
}
3543

3644
[Test]
37-
public void ChangingCostModifierAffectsRoute()
45+
public void NavMeshLinkCanConnectTwoSurfaces()
3846
{
39-
var plane1 = GameObject.CreatePrimitive(PrimitiveType.Plane);
40-
var plane2 = GameObject.CreatePrimitive(PrimitiveType.Plane);
41-
plane1.transform.position = 11.0f * Vector3.right;
47+
Assert.IsTrue(HasPathConnecting(plane1, plane2));
48+
}
4249

43-
var go = new GameObject();
44-
var surface = go.AddComponent<NavMeshSurface>();
45-
surface.BuildNavMesh();
50+
[Test]
51+
public void DisablingBidirectionalMakesTheLinkOneWay()
52+
{
53+
link.bidirectional = false;
54+
Assert.IsTrue(HasPathConnecting(plane1, plane2));
55+
Assert.IsFalse(HasPathConnecting(plane2, plane1));
56+
}
4657

47-
Assert.IsFalse(HasPathConnecting(plane1, plane2));
58+
[Test]
59+
public void ChangingAreaTypeCanBlockPath()
60+
{
61+
var areaMask = ~(1 << 4);
62+
Assert.IsTrue(HasPathConnecting(plane1, plane2, areaMask));
4863

49-
var linkGO = new GameObject();
64+
link.area = 4;
65+
Assert.IsFalse(HasPathConnecting(plane1, plane2, areaMask));
66+
}
5067

51-
var link1 = linkGO.AddComponent<NavMeshLink>();
68+
[Test]
69+
public void EndpointsMoveRelativeToLinkOnUpdate()
70+
{
71+
link.transform.position += Vector3.forward;
72+
Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
73+
Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
74+
75+
link.UpdateLink();
76+
77+
Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
78+
Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
79+
}
80+
81+
[UnityTest]
82+
public IEnumerator EndpointsMoveRelativeToLinkNextFrameWhenAutoUpdating()
83+
{
84+
link.transform.position += Vector3.forward;
85+
link.autoUpdate = true;
86+
87+
Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
88+
Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
89+
90+
yield return null;
91+
92+
Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
93+
Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
94+
}
95+
96+
[Test]
97+
public void ChangingCostModifierAffectsRoute()
98+
{
99+
var link1 = link;
52100
link1.startPoint = plane1.transform.position;
53101
link1.endPoint = plane2.transform.position + Vector3.forward;
54-
link1.UpdateLink();
55102

56-
var link2 = linkGO.AddComponent<NavMeshLink>();
103+
var link2 = link.gameObject.AddComponent<NavMeshLink>();
57104
link2.startPoint = plane1.transform.position;
58105
link2.endPoint = plane2.transform.position - Vector3.forward;
59-
link2.UpdateLink();
60106

61107
link1.costModifier = -1;
62108
link2.costModifier = 100;
@@ -67,11 +113,6 @@ public void ChangingCostModifierAffectsRoute()
67113
link2.costModifier = -1;
68114
Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, link1.endPoint));
69115
Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, link2.endPoint));
70-
71-
GameObject.DestroyImmediate(go);
72-
GameObject.DestroyImmediate(plane1);
73-
GameObject.DestroyImmediate(plane2);
74-
GameObject.DestroyImmediate(linkGO);
75116
}
76117

77118
public static bool HasPathConnecting(GameObject a, GameObject b, int areaMask = NavMesh.AllAreas, int agentTypeID = 0)

0 commit comments

Comments
 (0)