Rework of SimulationConfig to load JSON from StreamingAssets\Configs
This commit is contained in:
117
Assets/Scripts/Config/ConfigLoader.cs
Normal file
117
Assets/Scripts/Config/ConfigLoader.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
public static class ConfigLoader {
|
||||
public static SimulationConfig LoadSimulationConfig(string configFileName) {
|
||||
string configFilePath = Path.Combine(Application.streamingAssetsPath, "Configs", configFileName);
|
||||
if (File.Exists(configFilePath)) {
|
||||
string json = File.ReadAllText(configFilePath);
|
||||
SimulationConfig config = JsonConvert.DeserializeObject<SimulationConfig>(json, new JsonSerializerSettings {
|
||||
Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
|
||||
});
|
||||
return config;
|
||||
} else {
|
||||
Debug.LogError($"Configuration file not found at path: {configFilePath}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void PrintSimulationConfig(SimulationConfig config)
|
||||
{
|
||||
if (config == null)
|
||||
{
|
||||
Debug.Log("SimulationConfig is null");
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("SimulationConfig:");
|
||||
Debug.Log($"Time Scale: {config.timeScale}");
|
||||
|
||||
Debug.Log("Missile Swarm Configurations:");
|
||||
for (int i = 0; i < config.missile_swarm_configs.Count; i++)
|
||||
{
|
||||
PrintSwarmConfig(config.missile_swarm_configs[i], $"Missile Swarm {i + 1}");
|
||||
}
|
||||
|
||||
Debug.Log("Target Swarm Configurations:");
|
||||
for (int i = 0; i < config.target_swarm_configs.Count; i++)
|
||||
{
|
||||
PrintSwarmConfig(config.target_swarm_configs[i], $"Target Swarm {i + 1}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void PrintSwarmConfig(SwarmConfig swarmConfig, string swarmName)
|
||||
{
|
||||
Debug.Log($"{swarmName}:");
|
||||
Debug.Log($" Number of Agents: {swarmConfig.num_agents}");
|
||||
PrintAgentConfig(swarmConfig.agent_config);
|
||||
}
|
||||
|
||||
private static void PrintAgentConfig(AgentConfig agentConfig)
|
||||
{
|
||||
Debug.Log(" Agent Configuration:");
|
||||
Debug.Log($" Missile Type: {agentConfig.missile_type}");
|
||||
Debug.Log($" Target Type: {agentConfig.target_type}");
|
||||
PrintInitialState(agentConfig.initial_state);
|
||||
PrintStandardDeviation(agentConfig.standard_deviation);
|
||||
PrintDynamicConfig(agentConfig.dynamic_config);
|
||||
PrintPlottingConfig(agentConfig.plotting_config);
|
||||
PrintSubmunitionsConfig(agentConfig.submunitions_config);
|
||||
}
|
||||
|
||||
private static void PrintInitialState(InitialState initialState)
|
||||
{
|
||||
Debug.Log(" Initial State:");
|
||||
Debug.Log($" Position: {initialState.position}");
|
||||
Debug.Log($" Rotation: {initialState.rotation}");
|
||||
Debug.Log($" Velocity: {initialState.velocity}");
|
||||
}
|
||||
|
||||
private static void PrintStandardDeviation(StandardDeviation standardDeviation)
|
||||
{
|
||||
Debug.Log(" Standard Deviation:");
|
||||
Debug.Log($" Position: {standardDeviation.position}");
|
||||
Debug.Log($" Velocity: {standardDeviation.velocity}");
|
||||
}
|
||||
|
||||
private static void PrintDynamicConfig(DynamicConfig dynamicConfig)
|
||||
{
|
||||
Debug.Log(" Dynamic Configuration:");
|
||||
Debug.Log($" Launch Time: {dynamicConfig.launch_config.launch_time}");
|
||||
Debug.Log($" Sensor Type: {dynamicConfig.sensor_config.type}");
|
||||
Debug.Log($" Sensor Frequency: {dynamicConfig.sensor_config.frequency}");
|
||||
}
|
||||
|
||||
private static void PrintPlottingConfig(PlottingConfig plottingConfig)
|
||||
{
|
||||
Debug.Log(" Plotting Configuration:");
|
||||
Debug.Log($" Color: {plottingConfig.color}");
|
||||
Debug.Log($" Line Style: {plottingConfig.linestyle}");
|
||||
Debug.Log($" Marker: {plottingConfig.marker}");
|
||||
}
|
||||
|
||||
private static void PrintSubmunitionsConfig(SubmunitionsConfig submunitionsConfig)
|
||||
{
|
||||
if (submunitionsConfig == null)
|
||||
{
|
||||
Debug.Log(" Submunitions Configuration: None");
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log(" Submunitions Configuration:");
|
||||
Debug.Log($" Number of Submunitions: {submunitionsConfig.num_submunitions}");
|
||||
Debug.Log($" Launch Time: {submunitionsConfig.launch_config.launch_time}");
|
||||
PrintSubmunitionAgentConfig(submunitionsConfig.agent_config);
|
||||
}
|
||||
|
||||
private static void PrintSubmunitionAgentConfig(SubmunitionAgentConfig agentConfig)
|
||||
{
|
||||
Debug.Log(" Submunition Agent Configuration:");
|
||||
Debug.Log($" Missile Type: {agentConfig.missile_type}");
|
||||
PrintInitialState(agentConfig.initial_state);
|
||||
PrintStandardDeviation(agentConfig.standard_deviation);
|
||||
PrintDynamicConfig(agentConfig.dynamic_config);
|
||||
PrintPlottingConfig(agentConfig.plotting_config);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Config/ConfigLoader.cs.meta
Normal file
11
Assets/Scripts/Config/ConfigLoader.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da3b9eead33b94a4f8df7a4af7a79d49
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,119 +1,123 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
[CreateAssetMenu(fileName = "SimulationConfig", menuName = "Simulation/Config", order = 1)]
|
||||
public class SimulationConfig : ScriptableObject {
|
||||
[Header("Simulation Settings")]
|
||||
public float timeScale = 0.05f;
|
||||
|
||||
[Header("Missile Swarm Configurations")]
|
||||
public List<SwarmConfig> missile_swarm_configs = new List<SwarmConfig>();
|
||||
[Serializable]
|
||||
public class SimulationConfig {
|
||||
[Header("Simulation Settings")]
|
||||
public float timeScale = 0.05f;
|
||||
|
||||
[Header("Target Swarm Configurations")]
|
||||
public List<SwarmConfig> target_swarm_configs = new List<SwarmConfig>();
|
||||
[Header("Missile Swarm Configurations")]
|
||||
public List<SwarmConfig> missile_swarm_configs = new List<SwarmConfig>();
|
||||
|
||||
[Header("Target Swarm Configurations")]
|
||||
public List<SwarmConfig> target_swarm_configs = new List<SwarmConfig>();
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class DynamicConfig {
|
||||
public LaunchConfig launch_config;
|
||||
public SensorConfig sensor_config;
|
||||
public LaunchConfig launch_config;
|
||||
public SensorConfig sensor_config;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class SwarmConfig {
|
||||
public int num_agents;
|
||||
public AgentConfig agent_config;
|
||||
public int num_agents;
|
||||
public AgentConfig agent_config;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class AgentConfig {
|
||||
public MissileType missile_type;
|
||||
public TargetType target_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
public SubmunitionsConfig submunitions_config;
|
||||
public MissileType missile_type;
|
||||
public TargetType target_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
public SubmunitionsConfig submunitions_config;
|
||||
|
||||
public static AgentConfig FromSubmunitionAgentConfig(SubmunitionAgentConfig submunitionConfig) {
|
||||
return new AgentConfig {
|
||||
missile_type = submunitionConfig.missile_type,
|
||||
initial_state = submunitionConfig.initial_state,
|
||||
standard_deviation = submunitionConfig.standard_deviation,
|
||||
dynamic_config = submunitionConfig.dynamic_config,
|
||||
plotting_config = submunitionConfig.plotting_config,
|
||||
|
||||
// Set other fields as needed, using default values if not present in SubmunitionAgentConfig
|
||||
target_type = TargetType.DRONE, // Or another default value
|
||||
submunitions_config = null // Or a default value if needed
|
||||
};
|
||||
}
|
||||
public static AgentConfig FromSubmunitionAgentConfig(SubmunitionAgentConfig submunitionConfig) {
|
||||
return new AgentConfig {
|
||||
missile_type = submunitionConfig.missile_type,
|
||||
initial_state = submunitionConfig.initial_state,
|
||||
standard_deviation = submunitionConfig.standard_deviation,
|
||||
dynamic_config = submunitionConfig.dynamic_config,
|
||||
plotting_config = submunitionConfig.plotting_config,
|
||||
// Set other fields as needed, using default values if not present in SubmunitionAgentConfig
|
||||
target_type = TargetType.DRONE, // Or another default value
|
||||
submunitions_config = null // Or a default value if needed
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class InitialState {
|
||||
public Vector3 position;
|
||||
public Vector3 rotation;
|
||||
public Vector3 velocity;
|
||||
public Vector3 position;
|
||||
public Vector3 rotation;
|
||||
public Vector3 velocity;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class StandardDeviation {
|
||||
public Vector3 position;
|
||||
public Vector3 velocity;
|
||||
public Vector3 position;
|
||||
public Vector3 velocity;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class LaunchConfig {
|
||||
public float launch_time;
|
||||
public float launch_time;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class PlottingConfig {
|
||||
public Color color;
|
||||
public LineStyle linestyle;
|
||||
public Marker marker;
|
||||
public ConfigColor color;
|
||||
public LineStyle linestyle;
|
||||
public Marker marker;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class SubmunitionsConfig {
|
||||
public int num_submunitions;
|
||||
public LaunchConfig launch_config;
|
||||
public SubmunitionAgentConfig agent_config;
|
||||
public int num_submunitions;
|
||||
public LaunchConfig launch_config;
|
||||
public SubmunitionAgentConfig agent_config;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class SubmunitionAgentConfig {
|
||||
public MissileType missile_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
public MissileType missile_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class SensorConfig {
|
||||
public SensorType type;
|
||||
public float frequency;
|
||||
public SensorType type;
|
||||
public float frequency;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class TargetConfig {
|
||||
public TargetType target_type;
|
||||
public InitialState initial_state;
|
||||
public PlottingConfig plotting_config;
|
||||
public string prefabName;
|
||||
public TargetType target_type;
|
||||
public InitialState initial_state;
|
||||
public PlottingConfig plotting_config;
|
||||
public string prefabName;
|
||||
}
|
||||
|
||||
// Enums
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum MissileType { HYDRA_70, MICROMISSILE }
|
||||
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum TargetType { DRONE, MISSILE }
|
||||
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum ConfigColor { BLUE, GREEN, RED }
|
||||
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum LineStyle { DOTTED, SOLID }
|
||||
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum Marker { TRIANGLE_UP, TRIANGLE_DOWN, SQUARE }
|
||||
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum SensorType { IDEAL }
|
||||
@@ -61,6 +61,8 @@ public class SimManager : MonoBehaviour {
|
||||
} else {
|
||||
Destroy(gameObject);
|
||||
}
|
||||
simulationConfig = ConfigLoader.LoadSimulationConfig("seven_missiles_seven_drone_targets.json");
|
||||
Debug.Log(simulationConfig);
|
||||
}
|
||||
|
||||
void Start() {
|
||||
|
||||
Reference in New Issue
Block a user