Skip to content

Commit

Permalink
Add global simulator.json SimulatorConfig
Browse files Browse the repository at this point in the history
toggle logging, particle effects, set the physics update rate
  • Loading branch information
daniellovell committed Oct 12, 2024
1 parent ecfbdb1 commit 529eae2
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 27 deletions.
6 changes: 6 additions & 0 deletions Assets/Scripts/Config/ConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public static StaticAgentConfig LoadStaticAgentConfig(string configFileName) {
});
}

public static SimulatorConfig LoadSimulatorConfig() {
string relativePath = "simulator.json"; // Path relative to StreamingAssets
string fileContent = LoadFromStreamingAssets(relativePath);
return JsonConvert.DeserializeObject<SimulatorConfig>(fileContent);
}

public static void PrintSimulationConfig(SimulationConfig config) {
if (config == null) {
Debug.Log("SimulationConfig is null");
Expand Down
39 changes: 29 additions & 10 deletions Assets/Scripts/Managers/ParticleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ParticleManager : MonoBehaviour {
[SerializeField]
private Queue<GameObject> _missileExplosionPool;


private void Awake() {
if (Instance == null) {
Instance = this;
Expand All @@ -21,34 +22,52 @@ private void Awake() {
}

private void Start() {

_missileTrailPool = new Queue<GameObject>();
_missileExplosionPool = new Queue<GameObject>();

// Grab from Resources/Prefabs/Effects
if (SimManager.Instance.simulatorConfig.enableMissileTrailEffect) {
InitializeMissileTrailParticlePool();
}
if (SimManager.Instance.simulatorConfig.enableExplosionEffect) {
InitializeMissileExplosionParticlePool();
}

SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor;
}

private void InitializeMissileTrailParticlePool() {
// Grab from Resources/Prefabs/Effects
GameObject missileTrailPrefab =
Resources.Load<GameObject>("Prefabs/Effects/InterceptorSmokeEffect");
GameObject missileExplosionPrefab =
Resources.Load<GameObject>("Prefabs/Effects/InterceptExplosionEffect");


// Pre-instantiate 10 missile trail particles
for (int i = 0; i < 10; i++) {
InstantiateMissileTrail(missileTrailPrefab);
InstantiateMissileExplosion(missileExplosionPrefab);
}

}
// Instantiate over an interval
StartCoroutine(InstantiateMissileTrailsOverTime(missileTrailPrefab, 200, 0.05f));
StartCoroutine(InstantiateMissileExplosionsOverTime(missileExplosionPrefab, 200, 0.05f));
}

SimManager.Instance.OnNewInterceptor += RegisterNewInterceptor;
private void InitializeMissileExplosionParticlePool() {
GameObject missileExplosionPrefab =
Resources.Load<GameObject>("Prefabs/Effects/InterceptExplosionEffect");
// Pre-instantiate 10 missile trail particles
for (int i = 0; i < 10; i++) {
InstantiateMissileExplosion(missileExplosionPrefab);
}
StartCoroutine(InstantiateMissileExplosionsOverTime(missileExplosionPrefab, 200, 0.05f));
}

private void RegisterNewInterceptor(Interceptor interceptor) {
interceptor.OnInterceptHit += RegisterInterceptorHit;
}

private void RegisterInterceptorHit(Interceptor interceptor, Threat threat) {
PlayMissileExplosion(interceptor.transform.position);
if (SimManager.Instance.simulatorConfig.enableExplosionEffect) {
PlayMissileExplosion(interceptor.transform.position);
}
}

/// <summary>
Expand Down Expand Up @@ -138,7 +157,7 @@ private IEnumerator InstantiateMissileExplosionsOverTime(GameObject prefab, int
/// </summary>
/// <returns></returns>
public GameObject RequestMissileTrailParticle() {
if (_missileTrailPool.Count > 0) {
if (_missileTrailPool.Count > 0 && SimManager.Instance.simulatorConfig.enableMissileTrailEffect) {
GameObject trail = _missileTrailPool.Dequeue();

return trail;
Expand Down
36 changes: 24 additions & 12 deletions Assets/Scripts/Monitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,20 @@ private void WriteEventsToFile() {
}

private void RegisterSimulationStarted() {
InitializeLogFiles();
_monitorRoutine = StartCoroutine(MonitorRoutine());
if (SimManager.Instance.simulatorConfig.enableLogging) {
InitializeLogFiles();
_monitorRoutine = StartCoroutine(MonitorRoutine());
}
}

private void RegisterSimulationEnded() {
StopCoroutine(_monitorRoutine);
CloseLogFiles();
WriteEventsToFile();
StartCoroutine(ConvertBinaryTelemetryToCsvCoroutine(
if (SimManager.Instance.simulatorConfig.enableLogging) {
StopCoroutine(_monitorRoutine);
CloseLogFiles();
WriteEventsToFile();
StartCoroutine(ConvertBinaryTelemetryToCsvCoroutine(
_telemetryBinPath, Path.ChangeExtension(_telemetryBinPath, ".csv")));
}
}

private IEnumerator ConvertBinaryTelemetryToCsvCoroutine(string binaryFilePath,
Expand All @@ -191,13 +195,17 @@ private IEnumerator ConvertBinaryTelemetryToCsvCoroutine(string binaryFilePath,
}

public void RegisterNewThreat(Threat threat) {
RegisterNewAgent(threat, "NEW_THREAT");
if (SimManager.Instance.simulatorConfig.enableLogging) {
RegisterNewAgent(threat, "NEW_THREAT");
}
}

public void RegisterNewInterceptor(Interceptor interceptor) {
RegisterNewAgent(interceptor, "NEW_INTERCEPTOR");
interceptor.OnInterceptMiss += RegisterInterceptorMiss;
interceptor.OnInterceptHit += RegisterInterceptorHit;
if (SimManager.Instance.simulatorConfig.enableLogging) {
RegisterNewAgent(interceptor, "NEW_INTERCEPTOR");
interceptor.OnInterceptMiss += RegisterInterceptorMiss;
interceptor.OnInterceptHit += RegisterInterceptorHit;
}
}

private void RegisterNewAgent(Agent agent, string eventType) {
Expand All @@ -209,11 +217,15 @@ private void RegisterNewAgent(Agent agent, string eventType) {
}

public void RegisterInterceptorHit(Interceptor interceptor, Threat threat) {
RegisterInterceptEvent(interceptor, threat, true);
if (SimManager.Instance.simulatorConfig.enableLogging) {
RegisterInterceptEvent(interceptor, threat, true);
}
}

public void RegisterInterceptorMiss(Interceptor interceptor, Threat threat) {
RegisterInterceptEvent(interceptor, threat, false);
if (SimManager.Instance.simulatorConfig.enableLogging) {
RegisterInterceptEvent(interceptor, threat, false);
}
}

public void RegisterInterceptEvent(Interceptor interceptor, Threat threat, bool hit) {
Expand Down
19 changes: 18 additions & 1 deletion Assets/Scripts/SimManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
/// Implements the Singleton pattern to ensure only one instance exists.
/// </summary>
public class SimManager : MonoBehaviour {

public SimulatorConfig simulatorConfig;

/// <summary>
/// Singleton instance of SimManager.
/// </summary>
Expand Down Expand Up @@ -76,6 +79,10 @@ void Awake() {
}
simulationConfig = ConfigLoader.LoadSimulationConfig("1_salvo_1_hydra_7_drones.json");
Debug.Log(simulationConfig);

// Load the simulator config
simulatorConfig = ConfigLoader.LoadSimulatorConfig();
Time.fixedDeltaTime = (float)(1.0f / simulatorConfig.physicsUpdateRate);
}

void Start() {
Expand All @@ -88,8 +95,9 @@ void Start() {

public void SetTimeScale(float timeScale) {
Time.timeScale = timeScale;
Time.fixedDeltaTime = Time.timeScale * 0.01f;
Time.maximumDeltaTime = Time.timeScale * 0.05f;

// Time.fixedDeltaTime is set in the simulator.json
}

public void StartSimulation() {
Expand Down Expand Up @@ -376,3 +384,12 @@ void FixedUpdate() {
}
}
}

[System.Serializable]
public class SimulatorConfig
{
public bool enableLogging;
public bool enableMissileTrailEffect;
public bool enableExplosionEffect;
public int physicsUpdateRate;
}
8 changes: 4 additions & 4 deletions Assets/StreamingAssets/Configs/1_salvo_4_sfrj_10_brahmos.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"agent_model": "sfrj.json",
"initial_state": {
"position": { "x": 0, "y": 20, "z": 0 },
"rotation": { "x": -85, "y": 0, "z": 0 },
"velocity": { "x": 0, "y": 50, "z": 10 }
"rotation": { "x": -45, "y": 0, "z": 0 },
"velocity": { "x": 0, "y": 30, "z": 50 }
},
"standard_deviation": {
"position": { "x": 5, "y": 0, "z": 5 },
Expand All @@ -23,7 +23,7 @@
},
"submunitions_config": {
"num_submunitions": 7,
"launch_config": { "launch_time": 45 },
"launch_config": { "launch_time": 20 },
"dynamic_agent_config": {
"agent_model": "micromissile.json",
"initial_state": {
Expand Down Expand Up @@ -54,7 +54,7 @@
"agent_model": "brahmos.json",
"attack_behavior": "brahmos_direct_attack.json",
"initial_state": {
"position": { "x": 0, "y": 5000, "z": 100000 },
"position": { "x": 0, "y": 50, "z": 50000 },
"rotation": { "x": 90, "y": 0, "z": 0 },
"velocity": { "x": 0, "y": 0, "z": -800 }
},
Expand Down
6 changes: 6 additions & 0 deletions Assets/StreamingAssets/simulator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"enableLogging": false,
"enableMissileTrailEffect": false,
"enableExplosionEffect": false,
"physicsUpdateRate": 100
}
7 changes: 7 additions & 0 deletions Assets/StreamingAssets/simulator.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 529eae2

Please sign in to comment.