Assignment system overhaul to fix many bugs
This commit is contained in:
25
Assets/Tests/SanityTest.cs
Normal file
25
Assets/Tests/SanityTest.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
using System.Collections;
|
||||
|
||||
public class SanityTest
|
||||
{
|
||||
[Test]
|
||||
public void SanityTestSimplePasses()
|
||||
{
|
||||
// Use the Assert class to test conditions
|
||||
Assert.Pass("This test passes.");
|
||||
}
|
||||
|
||||
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
|
||||
// `yield return null;` to skip a frame.
|
||||
[UnityTest]
|
||||
public IEnumerator SanityTestWithEnumeratorPasses()
|
||||
{
|
||||
// Use the Assert class to test conditions.
|
||||
// Use yield to skip a frame.
|
||||
yield return null;
|
||||
Assert.Pass("This test passes after skipping a frame.");
|
||||
}
|
||||
}
|
||||
2
Assets/Tests/SanityTest.cs.meta
Normal file
2
Assets/Tests/SanityTest.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b48cf6e434723a449a49186eeda32d3f
|
||||
24
Assets/Tests/Tests.asmdef
Normal file
24
Assets/Tests/Tests.asmdef
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "Tests",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"UnityEngine.TestRunner",
|
||||
"UnityEditor.TestRunner",
|
||||
"MicromissileAssembly"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [
|
||||
"nunit.framework.dll"
|
||||
],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [
|
||||
"UNITY_INCLUDE_TESTS"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
7
Assets/Tests/Tests.asmdef.meta
Normal file
7
Assets/Tests/Tests.asmdef.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d15c92e585e721749b63d85007276dbe
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
138
Assets/Tests/ThreatAssignmentTests.cs
Normal file
138
Assets/Tests/ThreatAssignmentTests.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
using System.Linq;
|
||||
|
||||
public class ThreatAssignmentTests
|
||||
{
|
||||
[Test]
|
||||
public void Assign_Should_Assign_All_Interceptors_And_Threats()
|
||||
{
|
||||
// Arrange
|
||||
ThreatAssignment threatAssignment = new ThreatAssignment();
|
||||
|
||||
// Create interceptors
|
||||
List<Interceptor> interceptors = new List<Interceptor>
|
||||
{
|
||||
new GameObject("Interceptor 1").AddComponent<Micromissile>(),
|
||||
new GameObject("Interceptor 2").AddComponent<Micromissile>(),
|
||||
new GameObject("Interceptor 3").AddComponent<Micromissile>()
|
||||
};
|
||||
|
||||
// Create threats
|
||||
Threat threat1 = new GameObject("Threat 1").AddComponent<DroneTarget>();
|
||||
Threat threat2 = new GameObject("Threat 2").AddComponent<DroneTarget>();
|
||||
Threat threat3 = new GameObject("Threat 3").AddComponent<DroneTarget>();
|
||||
|
||||
// Add Rigidbody components to threats to set velocities
|
||||
Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>();
|
||||
Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>();
|
||||
Rigidbody rb3 = threat3.gameObject.AddComponent<Rigidbody>();
|
||||
|
||||
// Set positions and velocities
|
||||
threat1.transform.position = Vector3.forward * -20f;
|
||||
threat2.transform.position = Vector3.forward * -20f;
|
||||
threat3.transform.position = Vector3.forward * -20f;
|
||||
|
||||
rb1.linearVelocity = Vector3.forward * 5f;
|
||||
rb2.linearVelocity = Vector3.forward * 10f;
|
||||
rb3.linearVelocity = Vector3.forward * 15f;
|
||||
|
||||
// Create threat data
|
||||
List<ThreatData> threats = new List<ThreatData>
|
||||
{
|
||||
new ThreatData(threat1, "Threat1ID"),
|
||||
new ThreatData(threat2, "Threat2ID"),
|
||||
new ThreatData(threat3, "Threat3ID")
|
||||
};
|
||||
|
||||
// Act
|
||||
IEnumerable<IAssignment.AssignmentItem> assignments = threatAssignment.Assign(interceptors, threats);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(3, assignments.Count(), "All interceptors should be assigned");
|
||||
|
||||
HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
|
||||
HashSet<Threat> assignedThreats = new HashSet<Threat>();
|
||||
|
||||
foreach (var assignment in assignments)
|
||||
{
|
||||
Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null");
|
||||
Assert.IsNotNull(assignment.Threat, "Threat should not be null");
|
||||
assignedInterceptors.Add(assignment.Interceptor);
|
||||
assignedThreats.Add(assignment.Threat);
|
||||
}
|
||||
|
||||
Assert.AreEqual(3, assignedInterceptors.Count, "All interceptors should be unique");
|
||||
Assert.AreEqual(3, assignedThreats.Count, "All threats should be assigned");
|
||||
|
||||
// Verify that threats are assigned in order of their threat level (based on velocity and distance)
|
||||
var orderedAssignments = assignments.OrderByDescending(a => a.Threat.GetVelocity().magnitude / Vector3.Distance(a.Threat.transform.position, Vector3.zero)).ToList();
|
||||
Assert.AreEqual(threat3, orderedAssignments[0].Threat, "Highest threat should be assigned first");
|
||||
Assert.AreEqual(threat2, orderedAssignments[1].Threat, "Second highest threat should be assigned second");
|
||||
Assert.AreEqual(threat1, orderedAssignments[2].Threat, "Lowest threat should be assigned last");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Assign_Should_Handle_More_Interceptors_Than_Threats()
|
||||
{
|
||||
// Arrange
|
||||
ThreatAssignment threatAssignment = new ThreatAssignment();
|
||||
|
||||
// Create interceptors
|
||||
List<Interceptor> interceptors = new List<Interceptor>
|
||||
{
|
||||
new GameObject("Interceptor 1").AddComponent<Micromissile>(),
|
||||
new GameObject("Interceptor 2").AddComponent<Micromissile>(),
|
||||
new GameObject("Interceptor 3").AddComponent<Micromissile>()
|
||||
};
|
||||
|
||||
// Create threats
|
||||
Threat threat1 = new GameObject("Threat 1").AddComponent<DroneTarget>();
|
||||
Threat threat2 = new GameObject("Threat 2").AddComponent<DroneTarget>();
|
||||
|
||||
// Add Rigidbody components to threats to set velocities
|
||||
Rigidbody rb1 = threat1.gameObject.AddComponent<Rigidbody>();
|
||||
Rigidbody rb2 = threat2.gameObject.AddComponent<Rigidbody>();
|
||||
|
||||
// Set positions and velocities
|
||||
threat1.transform.position = Vector3.up * 10f;
|
||||
threat2.transform.position = Vector3.right * 5f;
|
||||
|
||||
rb1.linearVelocity = Vector3.forward * 10f;
|
||||
rb2.linearVelocity = Vector3.forward * 15f;
|
||||
|
||||
// Create threat data
|
||||
List<ThreatData> threats = new List<ThreatData>
|
||||
{
|
||||
new ThreatData(threat1, "Threat1ID"),
|
||||
new ThreatData(threat2, "Threat2ID")
|
||||
};
|
||||
|
||||
// Act
|
||||
IEnumerable<IAssignment.AssignmentItem> assignments = threatAssignment.Assign(interceptors, threats);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(2, assignments.Count(), "All threats should be assigned");
|
||||
|
||||
HashSet<Interceptor> assignedInterceptors = new HashSet<Interceptor>();
|
||||
HashSet<Threat> assignedThreats = new HashSet<Threat>();
|
||||
|
||||
foreach (var assignment in assignments)
|
||||
{
|
||||
Assert.IsNotNull(assignment.Interceptor, "Interceptor should not be null");
|
||||
Assert.IsNotNull(assignment.Threat, "Threat should not be null");
|
||||
assignedInterceptors.Add(assignment.Interceptor);
|
||||
assignedThreats.Add(assignment.Threat);
|
||||
}
|
||||
|
||||
Assert.AreEqual(2, assignedInterceptors.Count, "Two interceptors should be assigned");
|
||||
Assert.AreEqual(2, assignedThreats.Count, "Both threats should be assigned");
|
||||
|
||||
// Verify that threats are assigned in order of their threat level (based on velocity and distance)
|
||||
var orderedAssignments = assignments.OrderByDescending(a => a.Threat.GetVelocity().magnitude / Vector3.Distance(a.Threat.transform.position, Vector3.zero)).ToList();
|
||||
Assert.AreEqual(threat2, orderedAssignments[0].Threat, "Higher threat should be assigned first");
|
||||
Assert.AreEqual(threat1, orderedAssignments[1].Threat, "Lower threat should be assigned second");
|
||||
}
|
||||
}
|
||||
2
Assets/Tests/ThreatAssignmentTests.cs.meta
Normal file
2
Assets/Tests/ThreatAssignmentTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ddf271569a78ee4e995192d4df0ef3f
|
||||
Reference in New Issue
Block a user