using System.Collections.Generic; using System.IO; using CounterDrone.Core; using CounterDrone.Core.Algorithms; using CounterDrone.Core.Models; using CounterDrone.Core.Repository; using CounterDrone.Core.Services; using UnityEngine; using UVector3 = UnityEngine.Vector3; namespace CounterDrone.Unity { /// 一键启动:创建想定 → 推荐方案 → 运行仿真 public class SimulationBootstrap : MonoBehaviour { [Header("Simulation Settings")] [SerializeField] private float _droneSpeed = 200f; [SerializeField] private float _routeLength = 20000f; [SerializeField] private float _timeScale = 4f; public SimulationRunner Runner { get; private set; } void Start() { var paths = new UnityPathProvider(); // 清理上次残留数据 if (System.IO.File.Exists(paths.GetMainDbPath())) System.IO.File.Delete(paths.GetMainDbPath()); if (System.IO.Directory.Exists(paths.GetFramesDir())) System.IO.Directory.Delete(paths.GetFramesDir(), true); var dbm = new DatabaseManager(paths); var db = dbm.OpenMainDb(); // 弹药规格 db.Insert(new AmmunitionSpec { AerosolType = (int)AerosolType.InertGas, Name = "惰性气体弹", InitialRadius = 50, CoreDensity = 1.0, DispersionRateBase = 5, MaxRadius = 500, MaxDuration = 120, EffectiveConcentration = 0.05, }); // 创建想定 var scenario = new ScenarioService( new SimTaskRepository(db), new CombatSceneRepository(db), new ControlZoneRepository(db), new TargetConfigRepository(db), new EquipmentDeploymentRepository(db), new CloudDispersalRepository(db), new RoutePlanRepository(db), new WaypointRepository(db)); var task = scenario.CreateTask("Unity Demo", ""); var taskId = task.Id; scenario.SaveScene(taskId, new CombatScene { SceneType = (int)SceneType.Plain, WindSpeed = 3, WindDirection = (int)WindDirection.W, Temperature = 22, }); scenario.SaveTarget(taskId, new TargetConfig { TargetType = (int)TargetType.Piston, PowerType = (int)PowerType.Piston, Quantity = 1, TypicalSpeed = _droneSpeed, TypicalAltitude = 500, }); scenario.SaveRoute(taskId, new RoutePlan { FormationMode = (int)FormationMode.Single }, new List { new Waypoint { PosX = 0, PosY = 500, PosZ = 0, Speed = _droneSpeed }, new Waypoint { PosX = _routeLength, PosY = 500, PosZ = 0, Speed = _droneSpeed }, }); // 推荐方案 var detail = scenario.GetTaskDetail(taskId); var ammoCatalog = db.Table().ToList(); var rec = new DefaultDefenseAdvisor(ammoCatalog).Recommend(new ThreatProfile { Environment = detail.Scene, Targets = detail.Targets, Route = detail.Route, Waypoints = detail.Waypoints, }); scenario.SaveCloudDispersal(taskId, rec.Best.RecommendedCloud); var equips = new List(); foreach (var p in rec.Best.Platforms) equips.Add(new EquipmentDeployment { EquipmentRole = (int)EquipmentRole.LaunchPlatform, PlatformType = (int)p.Type, Quantity = 1, PositionX = p.Position.X, PositionY = p.Position.Y, PositionZ = p.Position.Z, AerosolType = (int)rec.Best.RecommendedAerosolType, MunitionCount = 1, Cooldown = p.Cooldown, MuzzleVelocity = p.MuzzleVelocity, }); scenario.SaveDeployment(taskId, equips); db.Close(); Debug.Log($"推荐方案: {rec.Best.RecommendedAerosolType}, {rec.Best.Platforms.Count}门炮, 拦截概率 {rec.Best.InterceptProbability:P0}"); // 运行仿真 Runner = GetComponent(); if (Runner == null) Runner = gameObject.AddComponent(); else Runner.enabled = true; Runner.LoadAndStart(taskId); // 设置倍速 if (Runner.Engine != null) { Runner.Engine.TimeScale = _timeScale; Debug.Log($"Engine state: {Runner.Engine.State}, drones: {Runner.Engine.Drones.Count}"); } // 移动 Game 视图相机 var cam = Camera.main; if (cam == null) { var cgo = new GameObject("MainCamera"); cgo.AddComponent(); cam = cgo.GetComponent(); cam.tag = "MainCamera"; } cam.transform.position = new UVector3(_routeLength / 2f, 2000, -3000); cam.transform.LookAt(new UVector3(_routeLength / 2f, 500, 0)); Debug.Log("Camera positioned. In Scene view, double-click 'Simulation' in Hierarchy to focus."); } } }