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

47 lines
1.6 KiB
C#
Raw Normal View History

2024-09-12 00:17:21 -07:00
using System;
using System.Collections.Generic;
using UnityEngine;
// The assignment class is an interface for assigning a target to each missile.
2024-09-13 22:45:25 -07:00
public interface IAssignment {
// Assignment item type.
// The first element corresponds to the missile index, and the second element
// corresponds to the target index.
public struct AssignmentItem {
public int MissileIndex;
public int TargetIndex;
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
public AssignmentItem(int missileIndex, int targetIndex) {
MissileIndex = missileIndex;
TargetIndex = targetIndex;
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-13 22:45:25 -07:00
// A list containing the missile-target assignments.
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Assign a target to each missile that has not been assigned a target yet.
public abstract IEnumerable<AssignmentItem> Assign(List<Agent> missiles, List<Agent> targets);
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Get the list of assignable missile indices.
protected static List<int> GetAssignableMissileIndices(List<Agent> missiles) {
List<int> assignableMissileIndices = new List<int>();
for (int missileIndex = 0; missileIndex < missiles.Count; missileIndex++) {
if (missiles[missileIndex].IsAssignable()) {
assignableMissileIndices.Add(missileIndex);
}
2024-09-12 00:17:21 -07:00
}
2024-09-13 22:45:25 -07:00
return assignableMissileIndices;
}
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Get the list of active target indices.
protected static List<int> GetActiveTargetIndices(List<Agent> targets) {
List<int> activeTargetIndices = new List<int>();
for (int targetIndex = 0; targetIndex < targets.Count; targetIndex++) {
if (!targets[targetIndex].IsHit()) {
activeTargetIndices.Add(targetIndex);
}
2024-09-12 00:17:21 -07:00
}
2024-09-13 22:45:25 -07:00
return activeTargetIndices;
}
2024-09-12 00:17:21 -07:00
}