From 6149d8c783aaa2480e03b027d16073ea0b3ff223 Mon Sep 17 00:00:00 2001 From: Daniel Lovell Date: Sat, 14 Sep 2024 11:40:58 -0700 Subject: [PATCH] Document SimManager, fix bug in RestartSimulation causing crash --- Assets/Scripts/SimManager.cs | 43 ++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/SimManager.cs b/Assets/Scripts/SimManager.cs index 710308d..7aea577 100644 --- a/Assets/Scripts/SimManager.cs +++ b/Assets/Scripts/SimManager.cs @@ -3,10 +3,19 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; +/// +/// Manages the simulation by handling missiles, targets, and their assignments. +/// Implements the Singleton pattern to ensure only one instance exists. +/// public class SimManager : MonoBehaviour { - // Singleton instance + /// + /// Singleton instance of SimManager. + /// public static SimManager Instance { get; private set; } + /// + /// Configuration settings for the simulation. + /// [SerializeField] public SimulationConfig simulationConfig; @@ -19,6 +28,10 @@ public class SimManager : MonoBehaviour { private IAssignment _assignmentScheme; + /// + /// Gets the elapsed simulation time. + /// + /// The elapsed time in seconds. public double GetElapsedSimulationTime() { return _elapsedSimulationTime; } @@ -62,9 +75,8 @@ public class SimManager : MonoBehaviour { } _assignmentScheme = new ThreatAssignment(); - // Perform initial assignment } - + public void AssignMissilesToTargets() { AssignMissilesToTargets(_missiles); } @@ -73,6 +85,10 @@ public class SimManager : MonoBehaviour { _unassignedTargets.Add(target); } + /// + /// Assigns the specified list of missiles to available targets based on the assignment scheme. + /// + /// The list of missiles to assign. public void AssignMissilesToTargets(List missilesToAssign) { // Convert Missile and Target lists to Agent lists List missileAgents = new List(missilesToAssign.ConvertAll(m => m as Agent)); @@ -97,6 +113,11 @@ public class SimManager : MonoBehaviour { target => missilesToAssign.Any(missile => missile.GetAssignedTarget() == target)); } + /// + /// Creates a missile based on the provided configuration. + /// + /// Configuration settings for the missile. + /// The created Missile instance, or null if creation failed. public Missile CreateMissile(AgentConfig config) { string prefabName = config.missile_type switch { MissileType.HYDRA_70 => "Hydra70", MissileType.MICROMISSILE => "Micromissile", @@ -132,6 +153,11 @@ public class SimManager : MonoBehaviour { return missileObject.GetComponent(); } + /// + /// Creates a target based on the provided configuration. + /// + /// Configuration settings for the target. + /// The created Target instance, or null if creation failed. private Target CreateTarget(AgentConfig config) { string prefabName = config.target_type switch { TargetType.DRONE => "DroneTarget", TargetType.MISSILE => "MissileTarget", @@ -158,6 +184,12 @@ public class SimManager : MonoBehaviour { return targetObject.GetComponent(); } + /// + /// Creates an agent (missile or target) based on the provided configuration and prefab name. + /// + /// Configuration settings for the agent. + /// Name of the prefab to instantiate. + /// The created GameObject instance, or null if creation failed. public GameObject CreateAgent(AgentConfig config, string prefabName) { GameObject prefab = Resources.Load($"Prefabs/{prefabName}"); if (prefab == null) { @@ -181,6 +213,7 @@ public class SimManager : MonoBehaviour { return agentObject; } + private void RestartSimulation() { // Reset simulation time _elapsedSimulationTime = 0f; @@ -192,14 +225,16 @@ public class SimManager : MonoBehaviour { Destroy(missile.gameObject); } } - _missiles.Clear(); foreach (var target in _targets) { if (target != null) { Destroy(target.gameObject); } } + + _missiles.Clear(); _targets.Clear(); + _unassignedTargets.Clear(); InitializeSimulation(); }