From 9a91544519822149a733849f3700cfb69ae9e803 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Wed, 16 Oct 2024 11:07:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .cursorrules | 69 ------- Design.md | 29 +++ Models/Common.cs | 116 ++++++++++- Models/ILaserIlluminatable.cs | 20 +- Models/LaserDesignator.cs | 76 ++++++- Models/LaserJammer.cs | 70 ++++++- Models/LaserSemiActiveGuidedMissile.cs | 34 +++- Models/LaserWarner.cs | 54 ++++- Models/Missile.cs | 202 +++++++++++++++++-- Models/Tank.cs | 48 ++++- Program.cs | 7 +- SimulationEnvironment/SimulationConfig.cs | 222 +++++++++++++++++++-- SimulationEnvironment/SimulationElement.cs | 40 ++++ SimulationEnvironment/SimulationEvents.cs | 71 ++++++- SimulationEnvironment/SimulationManager.cs | 94 ++++++++- 15 files changed, 1026 insertions(+), 126 deletions(-) delete mode 100644 .cursorrules diff --git a/.cursorrules b/.cursorrules deleted file mode 100644 index bad2367..0000000 --- a/.cursorrules +++ /dev/null @@ -1,69 +0,0 @@ - - # C 开发规则 - - 您是一名C#开发专家。 - - ## 代码风格和结构 - - 编写简洁、符合习惯的 C# 代码,并提供准确的示例。 - - 根据需要使用面向对象和函数式编程模式。 - - 优先使用 LINQ 和 lambda 表达式进行集合操作。 - - 使用描述性变量和方法名称(例如,'IsUserSignedIn', 'CalculateTotal')。 - - ## 命名约定 - - 类名、方法名和公共成员使用 PascalCase。 - - 局部变量和私有字段使用 camelCase。 - - 常量使用 UPPERCASE。 - - 接口名称以 "I" 开头(例如,'IUserService')。 - - ## C# 使用 - - 在适当的情况下使用 C# 10+ 特性(例如,记录类型、模式匹配、空合并赋值)。 - - 有效使用 Entity Framework Core 进行数据库操作。 - - 使用依赖注入实现松耦合和可测试性。 - - 类的构造函数使用主构造函数,成员变量使用属性初始化。 - - ## 语法和格式 - - 遵循 C# 编码约定(https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions) - - 使用 C# 的表达式语法(例如,空条件运算符、字符串插值) - - 当类型明显时,使用 'var' 进行隐式类型声明。 - - ## 错误处理和验证 - - 对于异常情况使用异常,而不是控制流。 - - 使用内置的 .NET 日志记录或第三方日志记录器实现适当的错误日志记录。 - - 使用数据注解或 Fluent Validation 进行模型验证。 - - 实现全局异常处理的中间件。 - - 返回适当的 HTTP 状态码和一致的错误响应。 - - ## API 设计 - - 遵循 RESTful API 设计原则。 - - 在控制器中使用属性路由。 - - 为您的 API 实现版本控制。 - - 使用操作过滤器处理跨切关注点。 - - ## 性能优化 - - 对于 I/O 密集型操作,使用 async/await 进行异步编程。 - - 使用 IMemoryCache 或分布式缓存实现缓存策略。 - - 使用高效的 LINQ 查询,避免 N+1 查询问题。 - - 对于大型数据集实现分页。 - - ## 关键约定 - - 使用依赖注入实现松耦合和可测试性。 - - 根据复杂性实现仓储模式或直接使用 Entity Framework Core。 - - 如有需要,使用 AutoMapper 进行对象到对象的映射。 - - 使用 IHostedService 或 BackgroundService 实现后台任务。 - - ## 测试 - - 使用 xUnit、NUnit 或 MSTest 编写单元测试。 - - 使用 Moq 或 NSubstitute 模拟依赖项。 - - 为 API 端点实现集成测试。 - - ## 安全性 - - 使用身份验证和授权中间件。 - - 实现 JWT 身份验证以进行无状态 API 身份验证。 - - 使用 HTTPS 并强制 SSL。 - - 实现适当的 CORS 策略。 - - ## API 文档 - - 使用 Swagger/OpenAPI 进行 API 文档(根据安装的 Swashbuckle.AspNetCore 包)。 - - 为控制器和模型提供 XML 注释,以增强 Swagger 文档。 - - 遵循官方 Microsoft 文档,以获取路由、控制器、模型和其他 API 组件的最佳实践。 diff --git a/Design.md b/Design.md index 629efda..d2001ed 100644 --- a/Design.md +++ b/Design.md @@ -119,3 +119,32 @@ public class FireMissileCommand : ICommand - 实现观察者模式,处理告警通知。 - 使用命令模式封装关键操作。 - 进行单元测试和集成测试,确保系统的正确性和稳定性。 + +## 数据库访问架构 + +在Unity3D环境中,考虑到兼容性和易用性,我推荐使用第二种方法:值对象模式。这种方式有以下几个优点: + +1. 分离关注点:数据库实体(MissileConfigEntity)和游戏中使用的配置对象(MissileConfig)是分开的,这样可以避免 ORM 相关的属性和特性污染游戏逻辑。 +2. 灵活性:可以轻松地在数据库实体和游戏配置对象之间进行转换,而不影响现有的游戏逻辑。 +3. 序列化友好:Unity 经常需要序列化对象,使用纯粹的 C# 对象(如 MissileConfig)更容易进行序列化和反序列化。 +4. 兼容性好:这种方法不依赖于特定的 ORM 框架,如果将来需要更换数据库或 ORM 系统,影响会较小。 + +数据访问层使用了 Dapper(一个轻量级的 ORM),它在 Unity 中使用较为简单。 + +数据库设计 + +### 1. 导弹数据库设计 + +导弹数据库设计需要考虑导弹的属性、状态、行为等。 + +#### 1.1 导弹属性 + +导弹属性包括导弹的初始位置、初始速度、最大速度、目标索引、最大飞行时间、最大飞行距离、飞行阶段配置、推力加速度、最大发动机燃烧时间、最大加速度、距离参数、比例导引系数、类型等。 + +#### 1.2 导弹状态 + +导弹状态包括导弹的当前位置、当前速度、当前加速度、当前飞行阶段、当前目标索引、当前最大速度、当前最大加速度、当前最大飞行距离、当前最大飞行时间、当前比例导引系数等。 + +#### 1.3 导弹行为 + +导弹行为包括导弹的飞行行为、攻击行为、躲避行为、跟踪行为、锁定行为、发射行为、爆炸行为等。 diff --git a/Models/Common.cs b/Models/Common.cs index 45a254d..0280965 100644 --- a/Models/Common.cs +++ b/Models/Common.cs @@ -2,54 +2,105 @@ using System; namespace ActiveProtect.Models { + /// + /// 表示三维空间中的向量 + /// public class Vector3D(double x, double y, double z) { + /// + /// X 坐标 + /// public double X { get; set; } = x; + + /// + /// Y 坐标 + /// public double Y { get; set; } = y; + + /// + /// Z 坐标 + /// public double Z { get; set; } = z; + /// + /// 计算两个向量之间的距离 + /// + /// 第一个向量 + /// 第二个向量 + /// 两个向量之间的距离 public static double Distance(Vector3D v1, Vector3D v2) { return Math.Sqrt(Math.Pow(v1.X - v2.X, 2) + Math.Pow(v1.Y - v2.Y, 2) + Math.Pow(v1.Z - v2.Z, 2)); } + /// + /// 零向量 + /// public static Vector3D Zero => new(0, 0, 0); + /// + /// 将向量转换为字符串表示 + /// + /// 向量的字符串表示 public override string ToString() { return $"({X:F2}, {Y:F2}, {Z:F2})"; } + /// + /// 向量减法运算符重载 + /// public static Vector3D operator -(Vector3D a, Vector3D b) { return new Vector3D(a.X - b.X, a.Y - b.Y, a.Z - b.Z); } + /// + /// 向量加法运算符重载 + /// public static Vector3D operator +(Vector3D a, Vector3D b) { return new Vector3D(a.X + b.X, a.Y + b.Y, a.Z + b.Z); } + /// + /// 向量与标量乘法运算符重载 + /// public static Vector3D operator *(Vector3D a, double scalar) { return new Vector3D(a.X * scalar, a.Y * scalar, a.Z * scalar); } + /// + /// 向量与标量除法运算符重载 + /// public static Vector3D operator /(Vector3D a, double scalar) { return new Vector3D(a.X / scalar, a.Y / scalar, a.Z / scalar); } + /// + /// 计算向量的模长 + /// + /// 向量的模长 public double Magnitude() { return Math.Sqrt(X * X + Y * Y + Z * Z); } + /// + /// 计算向量模长的平方 + /// + /// 向量模长的平方 public double MagnitudeSquared() { return X * X + Y * Y + Z * Z; } + /// + /// 向量归一化 + /// + /// 归一化后的向量 public Vector3D Normalize() { double mag = Magnitude(); @@ -60,6 +111,12 @@ namespace ActiveProtect.Models return new Vector3D(0, 0, 0); } + /// + /// 计算两个向量的叉积 + /// + /// 第一个向量 + /// 第二个向量 + /// 叉积结果 public static Vector3D CrossProduct(Vector3D a, Vector3D b) { return new Vector3D( @@ -69,23 +126,60 @@ namespace ActiveProtect.Models ); } + /// + /// 计算两个向量的点积 + /// + /// 第一个向量 + /// 第二个向量 + /// 点积结果 public static double DotProduct(Vector3D a, Vector3D b) { return a.X * b.X + a.Y * b.Y + a.Z * b.Z; } + + /// + /// 向量取反 + /// + /// 输入向量 + /// 取反后的向量 + public static Vector3D Negate(Vector3D a) + { + return new Vector3D(-a.X, -a.Y, -a.Z); + } } + /// + /// 表示三维空间中的方向 + /// public struct Orientation(double yaw, double pitch, double roll) { + /// + /// 偏航角(绕Y轴旋转) + /// public double Yaw { get; set; } = yaw; + + /// + /// 俯仰角(绕X轴旋转) + /// public double Pitch { get; set; } = pitch; + + /// + /// 滚转角(绕Z轴旋转) + /// public double Roll { get; set; } = roll; + /// + /// 将方向转换为字符串表示 + /// + /// 方向的字符串表示 public override readonly string ToString() { return $"(Yaw: {Yaw:F2}, Pitch: {Pitch:F2}, Roll: {Roll:F2})"; } + /// + /// 将角度归一化到 [-π, π] 范围内 + /// public void Normalize() { Yaw = NormalizeAngle(Yaw); @@ -93,6 +187,11 @@ namespace ActiveProtect.Models Roll = NormalizeAngle(Roll); } + /// + /// 将单个角度归一化到 [-π, π] 范围内 + /// + /// 输入角度 + /// 归一化后的角度 private static double NormalizeAngle(double angle) { while (angle > Math.PI) angle -= 2 * Math.PI; @@ -100,6 +199,11 @@ namespace ActiveProtect.Models return angle; } + /// + /// 根据给定的方向向量创建方向 + /// + /// 方向向量 + /// 对应的方向 public static Orientation LookAt(Vector3D direction) { double yaw = Math.Atan2(direction.Z, direction.X); @@ -107,6 +211,10 @@ namespace ActiveProtect.Models return new Orientation(yaw, pitch, 0); } + /// + /// 将方向转换为单位向量 + /// + /// 对应的单位向量 public readonly Vector3D ToVector() { double cosYaw = Math.Cos(Yaw); @@ -121,6 +229,11 @@ namespace ActiveProtect.Models ); } + /// + /// 从向量创建方向 + /// + /// 输入向量 + /// 对应的方向 public static Orientation FromVector(Vector3D vector) { Vector3D normalized = vector.Normalize(); @@ -137,7 +250,4 @@ namespace ActiveProtect.Models return new Orientation(yaw, pitch, 0); } } - - - } diff --git a/Models/ILaserIlluminatable.cs b/Models/ILaserIlluminatable.cs index 45e8962..f5994ef 100644 --- a/Models/ILaserIlluminatable.cs +++ b/Models/ILaserIlluminatable.cs @@ -2,11 +2,29 @@ using ActiveProtect.SimulationEnvironment; namespace ActiveProtect.Models { + /// + /// 定义可被激光照射的实体接口 + /// public interface ILaserIlluminatable { + /// + /// 获取实体是否正在被激光照射 + /// bool IsIlluminated { get; } + + /// + /// 开始激光照射 + /// void Illuminate(); + + /// + /// 停止激光照射 + /// void StopIllumination(); + + /// + /// 获取实体的位置 + /// Vector3D Position { get; } } -} \ No newline at end of file +} diff --git a/Models/LaserDesignator.cs b/Models/LaserDesignator.cs index 9fb00af..7cf5059 100644 --- a/Models/LaserDesignator.cs +++ b/Models/LaserDesignator.cs @@ -3,15 +3,49 @@ using System; namespace ActiveProtect.Models { + /// + /// 激光指示器类,用于对目标进行激光照射 + /// public class LaserDesignator : SimulationElement { + /// + /// 目标ID + /// public string TargetId { get; private set; } + + /// + /// 导弹ID + /// public string MissileId { get; private set; } - public double JammingThreshold { get; private set; } = 0.0; // 调整干扰阈值 + + /// + /// 干扰阈值(分贝) + /// + public double JammingThreshold { get; private set; } = 0.0; + + /// + /// 是否被干扰 + /// public bool IsJammed { get; private set; } = false; + + /// + /// 最大照射范围(米) + /// public double MaxIlluminationRange { get; private set; } + + /// + /// 是否正在照射 + /// private bool IsIlluminating { get; set; } = false; + /// + /// 构造函数 + /// + /// 激光指示器ID + /// 目标ID + /// 导弹ID + /// 激光指示器配置 + /// 仿真管理器 public LaserDesignator(string id, string targetId, string missileId, LaserDesignatorConfig config, ISimulationManager simulationManager) : base(id, config.InitialPosition, new Orientation(), simulationManager) { @@ -23,6 +57,10 @@ namespace ActiveProtect.Models SimulationManager.SubscribeToEvent(OnEntityDeactivatedEvent); } + /// + /// 更新激光指示器状态 + /// + /// 时间步长 public override void Update(double deltaTime) { if (IsActive && !IsJammed) @@ -35,6 +73,9 @@ namespace ActiveProtect.Models } } + /// + /// 更新照射状态 + /// private void UpdateIllumination() { if (SimulationManager.GetEntityById(TargetId) is ILaserIlluminatable target) @@ -67,6 +108,10 @@ namespace ActiveProtect.Models } } + /// + /// 开始照射目标 + /// + /// 目标 private void StartIllumination(ILaserIlluminatable target) { target.Illuminate(); @@ -75,6 +120,9 @@ namespace ActiveProtect.Models PublishIlluminationEvent(); } + /// + /// 停止照射目标 + /// private void StopIllumination() { if (SimulationManager.GetEntityById(TargetId) is ILaserIlluminatable target) @@ -86,6 +134,10 @@ namespace ActiveProtect.Models PublishIlluminationStopEvent(); } + /// + /// 处理激光干扰事件 + /// + /// 激光干扰事件 private void OnLaserJamming(LaserJammingEvent evt) { if (evt.TargetId == TargetId) @@ -119,6 +171,9 @@ namespace ActiveProtect.Models } } + /// + /// 激活激光指示器 + /// public void ActivateLaser() { IsActive = true; @@ -127,6 +182,9 @@ namespace ActiveProtect.Models UpdateIllumination(); } + /// + /// 停用激光指示器 + /// public void DeactivateLaser() { if (IsActive) @@ -137,6 +195,9 @@ namespace ActiveProtect.Models } } + /// + /// 发布激光照射事件 + /// private void PublishIlluminationEvent() { PublishEvent(new LaserIlluminationEvent @@ -145,6 +206,9 @@ namespace ActiveProtect.Models }); } + /// + /// 发布激光照射停止事件 + /// private void PublishIlluminationStopEvent() { PublishEvent(new LaserIlluminationStopEvent @@ -153,6 +217,10 @@ namespace ActiveProtect.Models }); } + /// + /// 处理实体停用事件 + /// + /// 实体停用事件 private void OnEntityDeactivatedEvent(EntityDeactivatedEvent evt) { if (evt.DeactivatedEntityId == TargetId || evt.DeactivatedEntityId == MissileId) @@ -162,6 +230,10 @@ namespace ActiveProtect.Models } } + /// + /// 获取激光指示器状态信息 + /// + /// 状态信息字符串 public override string GetStatus() { return $"激光目标指示器 {Id}:\n" + @@ -173,4 +245,4 @@ namespace ActiveProtect.Models $" 最大照射范围: {MaxIlluminationRange}"; } } -} \ No newline at end of file +} diff --git a/Models/LaserJammer.cs b/Models/LaserJammer.cs index ccb9acd..f79165c 100644 --- a/Models/LaserJammer.cs +++ b/Models/LaserJammer.cs @@ -3,17 +3,60 @@ using ActiveProtect.SimulationEnvironment; namespace ActiveProtect.Models { + /// + /// 激光干扰器类,用于对抗激光制导武器 + /// public class LaserJammer : SimulationElement { + /// + /// 是否正在进行干扰 + /// public bool IsJamming { get; private set; } + + /// + /// 当前干扰功率(瓦特) + /// public double JammingPower { get; private set; } + + /// + /// 当前冷却时间(秒) + /// private double jammingCooldown = 0; + + /// + /// 最大冷却时间(秒) + /// private readonly double maxJammingCooldown; + + /// + /// 最大干扰功率(瓦特) + /// private readonly double maxJammingPower; + + /// + /// 初始干扰功率(瓦特) + /// private readonly double initialJammingPower; + + /// + /// 功率增加速率(瓦特/秒) + /// private readonly double powerIncreaseRate; + + /// + /// 被监视实体的ID + /// public string MonitoredEntityId { get; private set; } + /// + /// 构造函数 + /// + /// 激光干扰器ID + /// 初始位置 + /// 初始朝向 + /// 仿真管理器 + /// 被监视实体的ID + /// 激光干扰器配置 public LaserJammer(string id, Vector3D position, Orientation orientation, ISimulationManager simulationManager, string monitoredEntityId, LaserJammerConfig config) : base(id, position, orientation, simulationManager) { @@ -28,6 +71,10 @@ namespace ActiveProtect.Models SimulationManager.SubscribeToEvent(OnLaserWarnerAlarmStopEvent); } + /// + /// 更新激光干扰器状态 + /// + /// 时间步长 public override void Update(double deltaTime) { if (!IsActive) return; @@ -61,6 +108,10 @@ namespace ActiveProtect.Models } } + /// + /// 处理激光告警器警报事件 + /// + /// 激光告警器警报事件 private void OnLaserWarnerAlarmEvent(LaserWarnerAlarmEvent evt) { if (evt.TargetId == MonitoredEntityId) @@ -69,6 +120,10 @@ namespace ActiveProtect.Models } } + /// + /// 处理激光告警器警报停止事件 + /// + /// 激光告警器警报停止事件 private void OnLaserWarnerAlarmStopEvent(LaserWarnerAlarmStopEvent evt) { if (evt.TargetId == MonitoredEntityId) @@ -77,6 +132,9 @@ namespace ActiveProtect.Models } } + /// + /// 开始干扰 + /// private void StartJamming() { if (!IsJamming) @@ -88,6 +146,9 @@ namespace ActiveProtect.Models } } + /// + /// 停止干扰 + /// private void StopJamming() { if (IsJamming) @@ -99,6 +160,10 @@ namespace ActiveProtect.Models } } + /// + /// 获取激光干扰器状态信息 + /// + /// 状态信息字符串 public override string GetStatus() { return $"激光干扰器 {Id}:\n" + @@ -108,6 +173,9 @@ namespace ActiveProtect.Models $" 冷却时间: {jammingCooldown:F2}/{maxJammingCooldown:F2}"; } + /// + /// 停用激光干扰器 + /// protected override void Deactivate() { base.Deactivate(); @@ -115,4 +183,4 @@ namespace ActiveProtect.Models SimulationManager.UnsubscribeFromEvent(OnLaserWarnerAlarmStopEvent); } } -} \ No newline at end of file +} diff --git a/Models/LaserSemiActiveGuidedMissile.cs b/Models/LaserSemiActiveGuidedMissile.cs index b054aaa..d3971c1 100644 --- a/Models/LaserSemiActiveGuidedMissile.cs +++ b/Models/LaserSemiActiveGuidedMissile.cs @@ -3,10 +3,22 @@ using System; namespace ActiveProtect.Models { + /// + /// 激光半主动制导导弹类,继承自基础导弹类 + /// public class LaserSemiActiveGuidedMissile : Missile { - private const double LaserLockDistance = 1500; // 锁定距离,单位:米 + /// + /// 激光锁定距离(米) + /// + private const double LaserLockDistance = 1500; + /// + /// 构造函数 + /// + /// 导弹ID + /// 导弹配置 + /// 仿真管理器 public LaserSemiActiveGuidedMissile( string id, MissileConfig missileConfig, @@ -18,6 +30,9 @@ namespace ActiveProtect.Models simulationManager.SubscribeToEvent(OnLaserIlluminationStop); } + /// + /// 更新导弹的制导状态 + /// protected override void UpdateGuidanceStatus() { bool previousHasGuidance = HasGuidance; @@ -53,6 +68,10 @@ namespace ActiveProtect.Models base.UpdateGuidanceStatus(); } + /// + /// 处理激光照射事件 + /// + /// 激光照射事件 private void OnLaserIllumination(LaserIlluminationEvent evt) { if (evt.TargetId == TargetId) @@ -62,6 +81,10 @@ namespace ActiveProtect.Models } } + /// + /// 处理激光照射停止事件 + /// + /// 激光照射停止事件 private void OnLaserIlluminationStop(LaserIlluminationStopEvent evt) { if (evt.TargetId == TargetId) @@ -71,6 +94,10 @@ namespace ActiveProtect.Models } } + /// + /// 获取导弹状态信息 + /// + /// 状态信息字符串 public override string GetStatus() { string baseStatus = base.GetStatus().Replace("导弹", "激光半主动制导导弹"); @@ -78,6 +105,9 @@ namespace ActiveProtect.Models return baseStatus + additionalStatus; } + /// + /// 停用导弹 + /// protected override void Deactivate() { base.Deactivate(); @@ -85,4 +115,4 @@ namespace ActiveProtect.Models SimulationManager.UnsubscribeFromEvent(OnLaserIlluminationStop); } } -} \ No newline at end of file +} diff --git a/Models/LaserWarner.cs b/Models/LaserWarner.cs index f7a9236..8cb054f 100644 --- a/Models/LaserWarner.cs +++ b/Models/LaserWarner.cs @@ -3,13 +3,40 @@ using System; namespace ActiveProtect.Models { + /// + /// 激光告警器类,用于检测激光照射并发出警报 + /// public class LaserWarner : SimulationElement { + /// + /// 是否正在发出警报 + /// public bool IsAlarming { get; private set; } + + /// + /// 警报持续时间(秒) + /// private readonly double alarmDuration; + + /// + /// 当前警报计时器 + /// private double alarmTimer = 0; + + /// + /// 被监视实体的ID + /// public string MonitoredEntityId { get; private set; } + /// + /// 构造函数 + /// + /// 激光告警器ID + /// 初始位置 + /// 初始朝向 + /// 仿真管理器 + /// 被监视实体的ID + /// 激光告警器配置 public LaserWarner(string id, Vector3D position, Orientation orientation, ISimulationManager simulationManager, string monitoredEntityId, LaserWarnerConfig config) : base(id, position, orientation, simulationManager) { @@ -20,6 +47,10 @@ namespace ActiveProtect.Models SimulationManager.SubscribeToEvent(OnLaserIlluminationStop); } + /// + /// 更新激光告警器状态 + /// + /// 时间步长 public override void Update(double deltaTime) { if (!IsActive) return; @@ -41,6 +72,10 @@ namespace ActiveProtect.Models } } + /// + /// 处理激光照射事件 + /// + /// 激光照射事件 private void OnLaserIllumination(LaserIlluminationEvent evt) { if (evt.TargetId == MonitoredEntityId) @@ -49,6 +84,10 @@ namespace ActiveProtect.Models } } + /// + /// 处理激光照射停止事件 + /// + /// 激光照射停止事件 private void OnLaserIlluminationStop(LaserIlluminationStopEvent evt) { if (evt.TargetId == MonitoredEntityId) @@ -57,6 +96,9 @@ namespace ActiveProtect.Models } } + /// + /// 触发警报 + /// public void TriggerAlarm() { if (!IsAlarming) @@ -68,6 +110,9 @@ namespace ActiveProtect.Models } } + /// + /// 停止警报 + /// public void StopAlarm() { if (IsAlarming) @@ -79,6 +124,10 @@ namespace ActiveProtect.Models } } + /// + /// 获取激光告警器状态信息 + /// + /// 状态信息字符串 public override string GetStatus() { return $"激光告警器 {Id}:\n" + @@ -87,6 +136,9 @@ namespace ActiveProtect.Models $" 警报持续时间: {(IsAlarming ? alarmTimer : 0):F2}/{alarmDuration:F2}"; } + /// + /// 停用激光告警器 + /// protected override void Deactivate() { base.Deactivate(); @@ -94,4 +146,4 @@ namespace ActiveProtect.Models SimulationManager.UnsubscribeFromEvent(OnLaserIlluminationStop); } } -} \ No newline at end of file +} diff --git a/Models/Missile.cs b/Models/Missile.cs index c1dbb50..65497f9 100644 --- a/Models/Missile.cs +++ b/Models/Missile.cs @@ -12,44 +12,142 @@ namespace ActiveProtect.Models /// 导弹类型 /// public MissileType Type { get; protected set; } + + /// + /// 当前速度(米/秒) + /// public double Speed { get; protected set; } + + /// + /// 最大速度(米/秒) + /// public double MaxSpeed { get; protected set; } + + /// + /// 目标ID + /// public string TargetId { get; protected set; } + + /// + /// 最大飞行时间(秒) + /// public double MaxFlightTime { get; protected set; } + + /// + /// 最大飞行距离(米) + /// public double MaxFlightDistance { get; protected set; } + + /// + /// 当前飞行时间(秒) + /// public double FlightTime { get; protected set; } + + /// + /// 当前飞行距离(米) + /// public double FlightDistance { get; protected set; } + + /// + /// 与目标的距离(米) + /// public double DistanceToTarget { get; protected set; } + + /// + /// 导弹距离参数 + /// public MissileDistanceParams DistanceParams { get; protected set; } + + /// + /// 飞行阶段配置 + /// public FlightStageConfig StageConfig { get; protected set; } + + /// + /// 当前飞行阶段 + /// public FlightStage CurrentStage { get; protected set; } + + /// + /// 导弹速度向量 + /// protected Vector3D Velocity; + + /// + /// 上一帧目标位置 + /// private Vector3D LastTargetPosition; - private const double N = 3; // 比例导引系数 + + /// + /// 比例导引系数 + /// + private const double N = 3; + /// + /// 推力加速度(米/秒²) + /// public double ThrustAcceleration { get; protected set; } + + /// + /// 当前发动机燃烧时间(秒) + /// public double EngineBurnTime { get; protected set; } + + /// + /// 最大发动机燃烧时间(秒) + /// public double MaxEngineBurnTime { get; protected set; } - public double MaxAcceleration { get; protected set; } = 100; // m/s^2 + /// + /// 最大加速度(米/秒²) + /// + public double MaxAcceleration { get; protected set; } = 100; + /// + /// 比例导引系数 + /// public double ProportionalNavigationCoefficient { get; set; } + /// + /// 是否有制导 + /// public bool HasGuidance { get; protected set; } = false; + + /// + /// 失去制导的时间(秒) + /// protected double LostGuidanceTime { get; set; } = 0; + + /// + /// 最后已知的速度向量 + /// protected Vector3D LastKnownVelocity = Vector3D.Zero; - public const double LAUNCH_SPEED = 10; // 发射速度,单位:m/s - public const double LAUNCH_DURATION = 0.5; // 发射阶持续时间,单位:秒 + + /// + /// 发射速度(米/秒) + /// + public const double LAUNCH_SPEED = 10; + + /// + /// 发射阶段持续时间(秒) + /// + public const double LAUNCH_DURATION = 0.5; private IMissileStageStrategy currentStage; private Dictionary stageStrategies; - // 添加质量属性 - public double Mass { get; protected set; } = 100; // 默认质量为100kg,可以根据需要调整 + /// + /// 导弹质量(千克) + /// + public double Mass { get; protected set; } = 100; + /// + /// 构造函数 + /// public Missile(string id, MissileConfig missileConfig, ISimulationManager simulationManager) : base(id, missileConfig.InitialPosition, missileConfig.InitialOrientation, simulationManager) { + // 初始化导弹属性 Speed = missileConfig.InitialSpeed; MaxSpeed = missileConfig.MaxSpeed; MaxFlightTime = missileConfig.MaxFlightTime; @@ -84,11 +182,14 @@ namespace ActiveProtect.Models currentStage = stageStrategies[CurrentStage]; - LastTargetPosition = Position; // 初始化 LastTargetPosition + LastTargetPosition = Position; DistanceToTarget = Vector3D.Distance(Position, SimulationManager.GetEntityById(TargetId).Position); } + /// + /// 设置导弹的初始飞行阶段 + /// private void SetInitialStage() { if (StageConfig.EnableLaunch) CurrentStage = FlightStage.Launch; @@ -100,6 +201,9 @@ namespace ActiveProtect.Models Console.WriteLine($"导弹 {Id} 的初始阶段: {CurrentStage}"); } + /// + /// 更新导弹状态 + /// public override void Update(double deltaTime) { if (!IsActive) return; @@ -129,6 +233,9 @@ namespace ActiveProtect.Models currentStage.Update(deltaTime); } + /// + /// 切换导弹飞行阶段 + /// public void ChangeStage(FlightStage newStage) { if (stageStrategies.TryGetValue(newStage, out var strategy)) @@ -151,6 +258,9 @@ namespace ActiveProtect.Models } } + /// + /// 检查指定飞行阶段是否启用 + /// private bool IsStageEnabled(FlightStage stage) { return stage switch @@ -164,6 +274,9 @@ namespace ActiveProtect.Models }; } + /// + /// 尝试切换到下一个可用的飞行阶段 + /// private void TryChangeToNextAvailableStage(FlightStage startStage) { FlightStage[] stageOrder = [FlightStage.Launch, FlightStage.Acceleration, FlightStage.Cruise, FlightStage.TerminalGuidance, FlightStage.Attack]; @@ -183,6 +296,9 @@ namespace ActiveProtect.Models SelfDestruct(); } + /// + /// 计算导弹的加速度 + /// private (Vector3D, Vector3D) CalculateDerivatives(Vector3D position, Vector3D velocity, double deltaTime) { Vector3D guidanceAcceleration = Vector3D.Zero; @@ -241,9 +357,11 @@ namespace ActiveProtect.Models return (velocity, totalAcceleration); } + /// + /// 计算空气阻力 + /// private static double CalculateDrag(double speed) { - // 修改空气阻力计算 const double dragCoefficient = 0.1; // 减小阻力系数 const double airDensity = 1.225; // 海平面空气密度,kg/m^3 const double referenceArea = 0.01; // 减小导弹的参考面积,m^2 @@ -251,7 +369,9 @@ namespace ActiveProtect.Models return 0.5 * dragCoefficient * airDensity * referenceArea * speed * speed; } - // 使用 Runge-Kutta 方法更新位置和速度 + /// + /// 使用 Runge-Kutta 方法更新位置和速度 + /// private (Vector3D, Vector3D) UpdatePositionAndVelocityRK4(double deltaTime) { Vector3D k1, k2, k3, k4; @@ -274,13 +394,17 @@ namespace ActiveProtect.Models return (newPosition, newVelocity); } - // 检查是否命中目标 + /// + /// 检查是否命中目标 + /// private bool CheckHit() { return DistanceToTarget <= DistanceParams.ExplosionDistance; } - // 检查是否应该自毁 + /// + /// 检查是否应该自毁 + /// private bool ShouldSelfDestruct() { if (FlightTime >= MaxFlightTime) @@ -301,7 +425,9 @@ namespace ActiveProtect.Models return false; } - // 更新发动机燃烧时间 + /// + /// 更新发动机燃烧时间 + /// private void UpdateEngineBurnTime(double deltaTime) { if (CurrentStage == FlightStage.Acceleration && EngineBurnTime < MaxEngineBurnTime) @@ -310,8 +436,9 @@ namespace ActiveProtect.Models } } - - // 计算比例导引加速度 + /// + /// 计算比例导引加速度 + /// private Vector3D CalculateProportionalNavigation(Vector3D position) { Vector3D targetPosition = SimulationManager.GetEntityById(TargetId).Position; @@ -323,7 +450,9 @@ namespace ActiveProtect.Models * ProportionalNavigationCoefficient * Velocity.Magnitude(); } - // 爆炸 + /// + /// 导弹爆炸 + /// public void Explode() { Deactivate(); @@ -331,7 +460,9 @@ namespace ActiveProtect.Models Console.WriteLine($"导弹 {Id} 在 {Position} 爆炸,命中目标!"); } - // 自毁 + /// + /// 导弹自毁 + /// public void SelfDestruct() { if (IsActive) @@ -346,14 +477,18 @@ namespace ActiveProtect.Models } } - // 设置比例导引系数 + /// + /// 设置比例导引系数 + /// public void SetProportionalNavigationCoefficient(double newCoefficient) { ProportionalNavigationCoefficient = newCoefficient; Console.WriteLine($"导弹 {Id} 的比例导引系数已更新为 {newCoefficient}"); } - // 获取导弹状态 + /// + /// 获取导弹状态 + /// public override string GetStatus() { return $"导弹 {Id}:\n" + @@ -368,24 +503,34 @@ namespace ActiveProtect.Models $" 失去引导时间: {LostGuidanceTime:F2}"; } - // 添加新的方法来更新引导状态 + /// + /// 更新导弹的制导状态 + /// protected virtual void UpdateGuidanceStatus() { // 基类中的默认实现 } + /// + /// 获取当前飞行阶段的名称 + /// public string GetCurrentStateName() { return currentStage.GetType().Name; } - } + /// + /// 导弹飞行阶段策略接口 + /// public interface IMissileStageStrategy { void Update(double deltaTime); } + /// + /// 发射阶段策略 + /// public class LaunchStageStrategy : IMissileStageStrategy { private readonly Missile missile; @@ -406,6 +551,9 @@ namespace ActiveProtect.Models } } + /// + /// 加速阶段策略 + /// public class AccelerationStageStrategy(Missile missile) : IMissileStageStrategy { private readonly Missile missile = missile; @@ -419,6 +567,9 @@ namespace ActiveProtect.Models } } + /// + /// 巡航阶段策略 + /// public class CruiseStageStrategy(Missile missile) : IMissileStageStrategy { private readonly Missile missile = missile; @@ -432,6 +583,9 @@ namespace ActiveProtect.Models } } + /// + /// 终端制导阶段策略 + /// public class TerminalGuidanceStageStrategy(Missile missile) : IMissileStageStrategy { private readonly Missile missile = missile; @@ -445,6 +599,9 @@ namespace ActiveProtect.Models } } + /// + /// 攻击阶段策略 + /// public class AttackStageStrategy(Missile missile) : IMissileStageStrategy { private readonly Missile missile = missile; @@ -458,6 +615,9 @@ namespace ActiveProtect.Models } } + /// + /// 无制导阶段策略 + /// public class UnguidedStageStrategy(Missile missile) : IMissileStageStrategy { private readonly Missile missile = missile; @@ -483,4 +643,4 @@ namespace ActiveProtect.Models } } } -} \ No newline at end of file +} diff --git a/Models/Tank.cs b/Models/Tank.cs index 235e979..0a099b1 100644 --- a/Models/Tank.cs +++ b/Models/Tank.cs @@ -3,22 +3,51 @@ using System; namespace ActiveProtect.Models { + /// + /// 表示仿真中的坦克 + /// public class Tank(string id, TankConfig tankConfig, ISimulationManager simulationManager) : SimulationElement(id, tankConfig.InitialPosition, tankConfig.InitialOrientation, simulationManager), ILaserIlluminatable { + /// + /// 当前速度(米/秒) + /// public double Speed { get; set; } = tankConfig.InitialSpeed; + + /// + /// 最大速度(米/秒) + /// public double MaxSpeed { get; private set; } = tankConfig.MaxSpeed; + + /// + /// 最大装甲值 + /// public double MaxArmor { get; private set; } = tankConfig.MaxArmor; + + /// + /// 当前装甲值 + /// public double CurrentArmor { get; private set; } = tankConfig.MaxArmor; + + /// + /// 是否被激光照射 + /// public bool IsIlluminated { get; private set; } = false; + /// + /// 更新坦克状态 + /// + /// 时间步长(秒) public override void Update(double deltaTime) { if (!IsActive) return; UpdatePosition(deltaTime); - // 移除发布坦克位置更新事件的代码 } + /// + /// 更新坦克位置 + /// + /// 时间步长(秒) private void UpdatePosition(double deltaTime) { Vector3D direction = Orientation.ToVector(); @@ -26,6 +55,11 @@ namespace ActiveProtect.Models Position += movement; } + /// + /// 坦克受到伤害 + /// + /// 伤害值 + /// 是否为导弹造成的伤害 public void TakeDamage(double damage, bool isMissileDamage = false) { if (isMissileDamage) @@ -40,6 +74,9 @@ namespace ActiveProtect.Models } } + /// + /// 被激光照射 + /// public void Illuminate() { if (!IsIlluminated) @@ -49,6 +86,9 @@ namespace ActiveProtect.Models } } + /// + /// 停止激光照射 + /// public void StopIllumination() { if (IsIlluminated) @@ -58,6 +98,10 @@ namespace ActiveProtect.Models } } + /// + /// 获取坦克状态信息 + /// + /// 坦克状态字符串 public override string GetStatus() { return $"坦克 {Id}:\n" + @@ -68,4 +112,4 @@ namespace ActiveProtect.Models $" 激光照射: {(IsIlluminated ? "是" : "否")}\n"; } } -} \ No newline at end of file +} diff --git a/Program.cs b/Program.cs index f09f5d2..c3b3309 100644 --- a/Program.cs +++ b/Program.cs @@ -12,6 +12,7 @@ namespace ActiveProtect // 创建仿真配置 var config = new SimulationConfig { + // 配置坦克 TankConfigs = [ new() { @@ -24,10 +25,12 @@ namespace ActiveProtect HasLaserJammer = true } ], + // 配置激光告警器 LaserWarnerConfig = new LaserWarnerConfig { AlarmDuration = 5.0 }, + // 配置激光干扰器 LaserJammerConfig = new LaserJammerConfig { MaxJammingCooldown = 5.0, @@ -35,6 +38,7 @@ namespace ActiveProtect InitialJammingPower = 4000.0, PowerIncreaseRate = 2000.0 }, + // 配置导弹 MissileConfigs = [ // 标准导弹配置 @@ -110,6 +114,7 @@ namespace ActiveProtect Type = MissileType.LaserSemiActiveGuidance } ], + // 配置激光指示器 LaserDesignatorConfigs = [ new LaserDesignatorConfig @@ -138,4 +143,4 @@ namespace ActiveProtect Console.WriteLine("仿真结束"); } } -} \ No newline at end of file +} diff --git a/SimulationEnvironment/SimulationConfig.cs b/SimulationEnvironment/SimulationConfig.cs index f47e184..93c767d 100644 --- a/SimulationEnvironment/SimulationConfig.cs +++ b/SimulationEnvironment/SimulationConfig.cs @@ -3,15 +3,44 @@ using ActiveProtect.Models; namespace ActiveProtect.SimulationEnvironment { + /// + /// 仿真配置类,包含整个仿真所需的所有配置信息 + /// public class SimulationConfig { + /// + /// 坦克配置列表 + /// public List TankConfigs { get; set; } + + /// + /// 导弹配置列表 + /// public List MissileConfigs { get; set; } + + /// + /// 激光指示器配置列表 + /// public List LaserDesignatorConfigs { get; set; } + + /// + /// 激光告警器配置 + /// public LaserWarnerConfig LaserWarnerConfig { get; set; } + + /// + /// 激光干扰器配置 + /// public LaserJammerConfig LaserJammerConfig { get; set; } + + /// + /// 仿真时间步长(秒) + /// public double SimulationTimeStep { get; set; } + /// + /// 构造函数,初始化默认配置 + /// public SimulationConfig() { TankConfigs = []; @@ -23,17 +52,49 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 坦克配置类 + /// public class TankConfig { + /// + /// 初始位置 + /// public Vector3D InitialPosition { get; set; } + + /// + /// 初始朝向 + /// public Orientation InitialOrientation { get; set; } + + /// + /// 初始速度(米/秒) + /// public double InitialSpeed { get; set; } + + /// + /// 最大速度(米/秒) + /// public double MaxSpeed { get; set; } + + /// + /// 最大装甲值 + /// public double MaxArmor { get; set; } + /// + /// 是否装备激光告警器 + /// public bool HasLaserWarner { get; set; } + + /// + /// 是否装备激光干扰器 + /// public bool HasLaserJammer { get; set; } + /// + /// 构造函数,设置默认值 + /// public TankConfig() { InitialPosition = new Vector3D(0, 0, 0); @@ -41,47 +102,126 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 导弹配置类 + /// public class MissileConfig { + /// + /// 初始位置 + /// public Vector3D InitialPosition { get; set; } + + /// + /// 初始朝向 + /// public Orientation InitialOrientation { get; set; } + + /// + /// 初始速度(米/秒) + /// public double InitialSpeed { get; set; } + + /// + /// 最大速度(米/秒) + /// public double MaxSpeed { get; set; } + + /// + /// 目标索引 + /// public int TargetIndex { get; set; } + + /// + /// 最大飞行时间(秒) + /// public double MaxFlightTime { get; set; } + + /// + /// 最大飞行距离(米) + /// public double MaxFlightDistance { get; set; } + + /// + /// 最大加速度(米/秒²) + /// public double MaxAcceleration { get; set; } + + /// + /// 比例导引系数 + /// public double ProportionalNavigationCoefficient { get; set; } + + /// + /// 飞行阶段配置 + /// public FlightStageConfig StageConfig { get; set; } + + /// + /// 距离参数 + /// public MissileDistanceParams DistanceParams { get; set; } + + /// + /// 推力加速度(米/秒²) + /// public double ThrustAcceleration { get; set; } + + /// + /// 最大发动机燃烧时间(秒) + /// public double MaxEngineBurnTime { get; set; } + + /// + /// 导弹质量(千克) + /// public double Mass { get; set; } + + /// + /// 导弹类型 + /// public MissileType Type { get; set; } + /// + /// 构造函数,设置默认值 + /// public MissileConfig() { - InitialPosition = new Vector3D(0, 0, 0); // 初始位 - InitialOrientation = new Orientation(0, 0, 0); // 初始方向 - InitialSpeed = 0; // 初始速度 - MaxSpeed = 0; // 最大速度 - TargetIndex = 0; // 目标索引 - MaxFlightTime = 0; // 最大飞行时间 - MaxFlightDistance = 0; // 最大飞行距离 - StageConfig = FlightStageConfig.StandardMissile; // 飞行阶段配置 - ThrustAcceleration = 0; // 推力加速度 - MaxEngineBurnTime = 0; // 最大发动机燃烧时间 - MaxAcceleration = 0; // 最大加速度 - DistanceParams = new MissileDistanceParams(0, 0, 0); // 距离参数 - ProportionalNavigationCoefficient = 0; // 比例导引系数 - Type = MissileType.StandardMissile; // 导弹类型 + InitialPosition = new Vector3D(0, 0, 0); + InitialOrientation = new Orientation(0, 0, 0); + InitialSpeed = 0; + MaxSpeed = 0; + TargetIndex = 0; + MaxFlightTime = 0; + MaxFlightDistance = 0; + StageConfig = FlightStageConfig.StandardMissile; + ThrustAcceleration = 0; + MaxEngineBurnTime = 0; + MaxAcceleration = 0; + DistanceParams = new MissileDistanceParams(0, 0, 0); + ProportionalNavigationCoefficient = 0; + Type = MissileType.StandardMissile; } } + /// + /// 激光指示器配置类 + /// public class LaserDesignatorConfig { + /// + /// 初始位置 + /// public Vector3D InitialPosition { get; set; } + + /// + /// 最大照射范围(米) + /// public double MaxIlluminationRange { get; set; } + + /// + /// 构造函数,设置默认值 + /// public LaserDesignatorConfig() { InitialPosition = new Vector3D(0, 0, 0); @@ -89,6 +229,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 导弹类型枚举 + /// public enum MissileType { StandardMissile, // 标准导弹 @@ -99,15 +242,29 @@ namespace ActiveProtect.SimulationEnvironment MillimeterWaveTerminalGuidance // 毫米波末制导 } + /// + /// 导弹距离参数结构 + /// public struct MissileDistanceParams(double terminalGuidanceDistance, double attackDistance, double explosionDistance) { + /// + /// 终端制导距离(米) + /// public double TerminalGuidanceDistance { get; set; } = terminalGuidanceDistance; + + /// + /// 攻击距离(米) + /// public double AttackDistance { get; set; } = attackDistance; + + /// + /// 爆炸距离(米) + /// public double ExplosionDistance { get; set; } = explosionDistance; } /// - /// 导弹飞行的各个阶段 + /// 导弹飞行阶段枚举 /// public enum FlightStage { @@ -120,11 +277,8 @@ namespace ActiveProtect.SimulationEnvironment } /// - /// 导弹飞行阶段的配置结构 + /// 导弹飞行阶段配置结构 /// - /// - /// 初始化飞行阶段配置 - /// public struct FlightStageConfig(bool enableLaunch = true, bool enableAcceleration = true, bool enableCruise = true, bool enableTerminalGuidance = true, bool enableAttack = true) { @@ -165,23 +319,53 @@ namespace ActiveProtect.SimulationEnvironment public static FlightStageConfig MillimeterWaveTerminalGuidance => new(true, true, true, true, true); } + /// + /// 激光告警器配置类 + /// public class LaserWarnerConfig { + /// + /// 警报持续时间(秒) + /// public double AlarmDuration { get; set; } + /// + /// 构造函数,设置默认值 + /// public LaserWarnerConfig() { AlarmDuration = 5.0; // 默认警报持续5秒 } } + /// + /// 激光干扰器配置类 + /// public class LaserJammerConfig { + /// + /// 最大干扰冷却时间(秒) + /// public double MaxJammingCooldown { get; set; } + + /// + /// 最大干扰功率(瓦特) + /// public double MaxJammingPower { get; set; } + + /// + /// 初始干扰功率(瓦特) + /// public double InitialJammingPower { get; set; } + + /// + /// 功率增加速率(瓦特/秒) + /// public double PowerIncreaseRate { get; set; } + /// + /// 构造函数,设置默认值 + /// public LaserJammerConfig() { MaxJammingCooldown = 5.0; diff --git a/SimulationEnvironment/SimulationElement.cs b/SimulationEnvironment/SimulationElement.cs index 92fcc89..adafeb4 100644 --- a/SimulationEnvironment/SimulationElement.cs +++ b/SimulationEnvironment/SimulationElement.cs @@ -2,21 +2,55 @@ using ActiveProtect.Models; namespace ActiveProtect.SimulationEnvironment { + /// + /// 仿真元素的抽象基类,所有仿真中的实体都继承自此类 + /// public abstract class SimulationElement(string id, Vector3D position, Orientation orientation, ISimulationManager simulationManager) { + /// + /// 仿真元素的唯一标识符 + /// public string Id { get; set; } = id; + + /// + /// 仿真元素的当前位置 + /// public Vector3D Position { get; set; } = position; + + /// + /// 仿真元素的当前朝向 + /// public Orientation Orientation { get; set; } = orientation; + + /// + /// 仿真管理器的引用 + /// public ISimulationManager SimulationManager { get; set; } = simulationManager; + + /// + /// 仿真元素是否处于活动状态 + /// public bool IsActive { get; protected set; } = true; + /// + /// 更新仿真元素的状态 + /// + /// 时间步长 public abstract void Update(double deltaTime); + /// + /// 获取仿真元素的状态信息 + /// + /// 状态信息字符串 public virtual string GetStatus() { return $"{GetType().Name} {Id} at {Position}, Orientation: {Orientation}, Active: {IsActive}"; } + /// + /// 发布仿真事件 + /// + /// 要发布的事件 protected void PublishEvent(SimulationEvent evt) { evt.SenderId = Id; @@ -24,12 +58,18 @@ namespace ActiveProtect.SimulationEnvironment SimulationManager.PublishEvent(evt); } + /// + /// 激活仿真元素 + /// protected virtual void Activate() { IsActive = true; PublishEvent(new EntityActivatedEvent { ActivatedEntityId = Id }); } + /// + /// 停用仿真元素 + /// protected virtual void Deactivate() { IsActive = false; diff --git a/SimulationEnvironment/SimulationEvents.cs b/SimulationEnvironment/SimulationEvents.cs index 1cc5bde..2822a4b 100644 --- a/SimulationEnvironment/SimulationEvents.cs +++ b/SimulationEnvironment/SimulationEvents.cs @@ -1,55 +1,122 @@ namespace ActiveProtect.SimulationEnvironment { + /// + /// 仿真事件的基类 + /// public class SimulationEvent { + /// + /// 事件发送者的ID + /// public string? SenderId { get; set; } + + /// + /// 事件发生的时间戳 + /// public double Timestamp { get; set; } } + /// + /// 导弹发射事件 + /// public class MissileFireEvent : SimulationEvent { + /// + /// 目标ID + /// public string? TargetId { get; set; } } + /// + /// 激光照射事件 + /// public class LaserIlluminationEvent : SimulationEvent { + /// + /// 目标ID + /// public string? TargetId { get; set; } } + /// + /// 激光干扰事件 + /// public class LaserJammingEvent : SimulationEvent { + /// + /// 目标ID + /// public string? TargetId { get; set; } + + /// + /// 干扰功率 + /// public double JammingPower { get; set; } } + /// + /// 实体销毁事件 + /// public class EntityDestroyedEvent : SimulationEvent { + /// + /// 被销毁实体的ID + /// public string? DestroyedEntityId { get; set; } } + /// + /// 激光照射停止事件 + /// public class LaserIlluminationStopEvent : SimulationEvent { + /// + /// 目标ID + /// public string? TargetId { get; set; } } + /// + /// 激光告警器警报事件 + /// public class LaserWarnerAlarmEvent : SimulationEvent { + /// + /// 目标ID + /// public string? TargetId { get; set; } } + /// + /// 激光告警器警报停止事件 + /// public class LaserWarnerAlarmStopEvent : SimulationEvent { + /// + /// 目标ID + /// public string? TargetId { get; set; } } - + /// + /// 实体激活事件 + /// public class EntityActivatedEvent : SimulationEvent { + /// + /// 被激活实体的ID + /// public string? ActivatedEntityId { get; set; } } + /// + /// 实体停用事件 + /// public class EntityDeactivatedEvent : SimulationEvent { + /// + /// 被停用实体的ID + /// public string? DeactivatedEntityId { get; set; } } -} \ No newline at end of file +} diff --git a/SimulationEnvironment/SimulationManager.cs b/SimulationEnvironment/SimulationManager.cs index ecd48c7..4d5b7ef 100644 --- a/SimulationEnvironment/SimulationManager.cs +++ b/SimulationEnvironment/SimulationManager.cs @@ -5,28 +5,79 @@ using ActiveProtect.Models; namespace ActiveProtect.SimulationEnvironment { + /// + /// 仿真管理器接口,定义了仿真管理器的基本功能 + /// public interface ISimulationManager { + /// + /// 当前仿真时间 + /// double CurrentTime { get; } + /// + /// 添加仿真元素 + /// void AddElement(SimulationElement element); + + /// + /// 根据ID获取仿真实体 + /// SimulationElement GetEntityById(string id); + + /// + /// 处理目标被击中事件 + /// void HandleTargetHit(string targetId, string missileId); + + /// + /// 发布仿真事件 + /// void PublishEvent(SimulationEvent evt); + + /// + /// 订阅仿真事件 + /// void SubscribeToEvent(Action handler) where T : SimulationEvent; + + /// + /// 取消订阅仿真事件 + /// void UnsubscribeFromEvent(Action handler) where T : SimulationEvent; + + /// + /// 取消所有事件订阅 + /// void UnsubscribeAllEvents(SimulationElement element); } + /// + /// 仿真管理器类,负责管理整个仿真过程 + /// public class SimulationManager : ISimulationManager { + /// + /// 仿真元素列表 + /// public List Elements { get; private set; } + + /// + /// 当前仿真时间 + /// public double CurrentTime { get; private set; } + + /// + /// 仿真是否结束 + /// public bool IsSimulationEnded { get; private set; } + private readonly SimulationConfig config; private Dictionary> eventHandlers = new Dictionary>(); private Dictionary> elementSubscriptions = new Dictionary>(); + /// + /// 构造函数 + /// public SimulationManager(SimulationConfig config) { this.config = config; @@ -36,6 +87,9 @@ namespace ActiveProtect.SimulationEnvironment InitializeSimulation(); } + /// + /// 初始化仿真 + /// private void InitializeSimulation() { // 创建坦克 @@ -74,7 +128,7 @@ namespace ActiveProtect.SimulationEnvironment } } - // 创建导弹(修改这部分) + // 创建导弹 for (int i = 0; i < config.MissileConfigs.Count; i++) { var missileConfig = config.MissileConfigs[i]; @@ -112,6 +166,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 发布仿真事件 + /// public void PublishEvent(SimulationEvent evt) { var eventType = evt.GetType(); @@ -124,6 +181,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 订阅仿真事件 + /// public void SubscribeToEvent(Action handler) where T : SimulationEvent { var eventType = typeof(T); @@ -149,6 +209,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 取消订阅仿真事件 + /// public void UnsubscribeFromEvent(Action handler) where T : SimulationEvent { var eventType = typeof(T); @@ -165,6 +228,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 取消所有事件订阅 + /// public void UnsubscribeAllEvents(SimulationElement element) { if (elementSubscriptions.ContainsKey(element)) @@ -180,6 +246,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 更新仿真状态 + /// public void Update() { if (IsSimulationEnded) return; @@ -208,6 +277,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 打印仿真状态 + /// public void PrintStatus() { Console.WriteLine($"仿真时间: {CurrentTime:F2}"); @@ -218,6 +290,9 @@ namespace ActiveProtect.SimulationEnvironment Console.WriteLine(); } + /// + /// 结束仿真 + /// private void EndSimulation() { IsSimulationEnded = true; @@ -226,11 +301,17 @@ namespace ActiveProtect.SimulationEnvironment Console.WriteLine($"剩余坦克数量: {Elements.Count(e => e is Tank)}"); } + /// + /// 根据ID获取仿真实体 + /// public SimulationElement GetEntityById(string id) { return Elements.FirstOrDefault(e => e.Id == id) ?? throw new InvalidOperationException($"Entity with id {id} not found"); } + /// + /// 处理目标被击中事件 + /// public void HandleTargetHit(string targetId, string missileId) { if (GetEntityById(targetId) is Tank tank && GetEntityById(missileId) is Missile missile) @@ -246,6 +327,9 @@ namespace ActiveProtect.SimulationEnvironment } } + /// + /// 计算导弹造成的伤害 + /// private double CalculateMissileDamage(Missile missile) { // 这里可以根据导弹类型、速度等因素计算伤害 @@ -253,14 +337,20 @@ namespace ActiveProtect.SimulationEnvironment return 50; } + /// + /// 记录击中事件 + /// private static void LogHitEvent(string targetId, string missileId, double damage) { Console.WriteLine($"目标 {targetId} 被导弹 {missileId} 击中,造成 {damage} 点伤害"); } + /// + /// 添加仿真元素 + /// public void AddElement(SimulationElement element) { Elements.Add(element); } } -} \ No newline at end of file +}