134 lines
5.6 KiB
C#
134 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using ActiveProtect.Models;
|
|
|
|
namespace ActiveProtect.SimulationEnvironment
|
|
{
|
|
public class SimulationManager : ISimulationManager
|
|
{
|
|
public List<SimulationElement> Elements { get; private set; }
|
|
public double CurrentTime { get; private set; }
|
|
public bool IsSimulationEnded { get; private set; }
|
|
private readonly SimulationConfig config;
|
|
|
|
public SimulationManager(SimulationConfig config)
|
|
{
|
|
this.config = config;
|
|
Elements = [];
|
|
CurrentTime = 0;
|
|
IsSimulationEnded = false;
|
|
InitializeSimulation();
|
|
}
|
|
|
|
private void InitializeSimulation()
|
|
{
|
|
// 创建坦克(保持不变)
|
|
for (int i = 0; i < config.TankConfigs.Count; i++)
|
|
{
|
|
var tankConfig = config.TankConfigs[i];
|
|
Elements.Add(new Tank(
|
|
$"Tank_{i + 1}",
|
|
tankConfig.InitialPosition,
|
|
tankConfig.InitialOrientation,
|
|
tankConfig.InitialSpeed,
|
|
tankConfig.MaxSpeed,
|
|
tankConfig.MaxArmor
|
|
));
|
|
}
|
|
|
|
// 创建导弹(修改这部分)
|
|
for (int i = 0; i < config.MissileConfigs.Count; i++)
|
|
{
|
|
var missileConfig = config.MissileConfigs[i];
|
|
Missile missile = missileConfig.Type switch
|
|
{
|
|
MissileType.SemiActiveLaserGuidance => new LaserSemiActiveGuidedMissile(
|
|
$"LSGM_{i + 1}",
|
|
missileConfig.InitialPosition,
|
|
missileConfig.InitialOrientation,
|
|
missileConfig.InitialSpeed,
|
|
missileConfig.MaxSpeed,
|
|
$"Tank_{missileConfig.TargetIndex + 1}",
|
|
missileConfig.MaxFlightTime,
|
|
missileConfig.MaxFlightDistance,
|
|
missileConfig.DistanceParams,
|
|
missileConfig.StageConfig,
|
|
this,
|
|
missileConfig.ThrustAcceleration,
|
|
missileConfig.MaxEngineBurnTime,
|
|
missileConfig.MaxAcceleration,
|
|
missileConfig.ProportionalNavigationCoefficient
|
|
),
|
|
_ => new Missile(
|
|
$"NM_{i + 1}",
|
|
missileConfig.InitialPosition,
|
|
missileConfig.InitialOrientation,
|
|
missileConfig.InitialSpeed,
|
|
missileConfig.MaxSpeed,
|
|
$"Tank_{missileConfig.TargetIndex + 1}",
|
|
missileConfig.MaxFlightTime,
|
|
missileConfig.MaxFlightDistance,
|
|
missileConfig.DistanceParams,
|
|
missileConfig.StageConfig,
|
|
this,
|
|
missileConfig.ThrustAcceleration,
|
|
missileConfig.MaxEngineBurnTime,
|
|
missileConfig.MaxAcceleration,
|
|
missileConfig.ProportionalNavigationCoefficient
|
|
),
|
|
};
|
|
Elements.Add(missile);
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (IsSimulationEnded) return;
|
|
|
|
CurrentTime += config.SimulationTimeStep;
|
|
|
|
foreach (var element in Elements.ToList())
|
|
{
|
|
element.Update(config.SimulationTimeStep);
|
|
}
|
|
|
|
// 移除不活跃的元素
|
|
Elements.RemoveAll(e => (e is Tank tank && !tank.IsActive) || (e is Missile missile && !missile.IsActive));
|
|
|
|
// 检查是否所有导弹都结束飞行
|
|
if (!Elements.Any(e => e is Missile))
|
|
{
|
|
EndSimulation();
|
|
}
|
|
}
|
|
|
|
public void PrintStatus()
|
|
{
|
|
Console.WriteLine($"仿真时间: {CurrentTime:F2}");
|
|
foreach (var element in Elements)
|
|
{
|
|
Console.WriteLine(element.GetStatus());
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
|
|
private void EndSimulation()
|
|
{
|
|
IsSimulationEnded = true;
|
|
Console.WriteLine("仿真结束");
|
|
Console.WriteLine($"总仿真时间: {CurrentTime:F2} 秒");
|
|
Console.WriteLine($"剩余坦克数量: {Elements.Count(e => e is Tank)}");
|
|
}
|
|
|
|
public Vector3D GetElementPosition(string elementId)
|
|
{
|
|
var element = Elements.FirstOrDefault(e => e.Id == elementId);
|
|
if (element != null)
|
|
{
|
|
return element.Position;
|
|
}
|
|
throw new ArgumentException($"Element with id {elementId} not found");
|
|
}
|
|
}
|
|
} |