micromissiles-unity/Assets/Scripts/Assignment/Assignment.cs

47 lines
1.6 KiB
C#
Raw Permalink Normal View History

2024-09-12 00:17:21 -07:00
using System;
using System.Collections.Generic;
using UnityEngine;
2024-09-24 19:59:25 -07:00
// The assignment class is an interface for assigning a threat to each interceptor.
2024-09-13 22:45:25 -07:00
public interface IAssignment {
// Assignment item type.
2024-09-24 19:59:25 -07:00
// The first element corresponds to the interceptor index, and the second element
2024-09-24 19:24:50 -07:00
// corresponds to the threat index.
2024-09-13 22:45:25 -07:00
public struct AssignmentItem {
2024-09-24 19:59:25 -07:00
public int InterceptorIndex;
2024-09-24 19:24:50 -07:00
public int ThreatIndex;
2024-09-12 00:17:21 -07:00
2024-09-24 19:24:50 -07:00
public AssignmentItem(int missileIndex, int threatIndex) {
2024-09-24 19:59:25 -07:00
InterceptorIndex = missileIndex;
2024-09-24 19:24:50 -07:00
ThreatIndex = threatIndex;
2024-09-12 00:17:21 -07:00
}
2024-09-13 22:45:25 -07:00
}
2024-09-12 00:17:21 -07:00
2024-09-24 19:59:25 -07:00
// A list containing the interceptor-target assignments.
2024-09-12 00:17:21 -07:00
2024-09-24 19:59:25 -07:00
// Assign a target to each interceptor that has not been assigned a target yet.
2024-09-13 22:45:25 -07:00
public abstract IEnumerable<AssignmentItem> Assign(List<Agent> missiles, List<Agent> targets);
2024-09-12 00:17:21 -07:00
2024-09-24 19:59:25 -07:00
// Get the list of assignable interceptor indices.
protected static List<int> GetAssignableInterceptorIndices(List<Agent> missiles) {
List<int> assignableInterceptorIndices = new List<int>();
2024-09-13 22:45:25 -07:00
for (int missileIndex = 0; missileIndex < missiles.Count; missileIndex++) {
if (missiles[missileIndex].IsAssignable()) {
2024-09-24 19:59:25 -07:00
assignableInterceptorIndices.Add(missileIndex);
2024-09-13 22:45:25 -07:00
}
2024-09-12 00:17:21 -07:00
}
2024-09-24 19:59:25 -07:00
return assignableInterceptorIndices;
2024-09-13 22:45:25 -07:00
}
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Get the list of active target indices.
2024-09-24 19:24:50 -07:00
protected static List<int> GetActiveThreatIndices(List<Agent> threats) {
List<int> activeThreatIndices = new List<int>();
for (int threatIndex = 0; threatIndex < threats.Count; threatIndex++) {
if (!threats[threatIndex].IsHit()) {
activeThreatIndices.Add(threatIndex);
2024-09-13 22:45:25 -07:00
}
2024-09-12 00:17:21 -07:00
}
2024-09-24 19:24:50 -07:00
return activeThreatIndices;
2024-09-13 22:45:25 -07:00
}
2024-09-12 00:17:21 -07:00
}