115 lines
3.9 KiB
C#
115 lines
3.9 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, this));
|
|
}
|
|
|
|
// 创建导弹(修改这部分)
|
|
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,
|
|
this
|
|
),
|
|
_ => new Missile(
|
|
$"NM_{i + 1}",
|
|
missileConfig,
|
|
this
|
|
),
|
|
};
|
|
Elements.Add(missile);
|
|
}
|
|
|
|
// 创建激光目标指示器
|
|
for (int i = 0; i < config.LaserDesignatorConfigs.Count; i++)
|
|
{
|
|
var laserDesignatorConfig = config.LaserDesignatorConfigs[i];
|
|
Elements.Add(new LaserDesignator($"LD_{i + 1}", laserDesignatorConfig, this));
|
|
}
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
public SimulationElement GetEntityById(string id)
|
|
{
|
|
return Elements.FirstOrDefault(e => e.Id == id) ?? throw new InvalidOperationException($"Entity with id {id} not found");
|
|
}
|
|
}
|
|
} |