| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using CounterDrone.Core.Algorithms; |
| | | 4 | | using CounterDrone.Core.Models; |
| | | 5 | | |
| | | 6 | | namespace CounterDrone.Core.Simulation |
| | | 7 | | { |
| | | 8 | | public class DroneEntity |
| | | 9 | | { |
| | 5555 | 10 | | public string Id { get; } |
| | 0 | 11 | | public string GroupId { get; } |
| | 25 | 12 | | public TargetType TargetType { get; } |
| | 25 | 13 | | public PowerType PowerType { get; } |
| | 0 | 14 | | public float Wingspan { get; } |
| | 5545 | 15 | | public float TypicalSpeed { get; } // km/h |
| | 16640 | 16 | | public List<Waypoint> Route { get; } |
| | 11105 | 17 | | public int CurrentWaypointIndex { get; private set; } |
| | 33759 | 18 | | public float PosX { get; private set; } |
| | 33753 | 19 | | public float PosY { get; private set; } |
| | 33710 | 20 | | public float PosZ { get; private set; } |
| | 11193 | 21 | | public float Hp { get; private set; } = 1.0f; |
| | 33359 | 22 | | public DroneStatus Status { get; private set; } = DroneStatus.Flying; |
| | 75 | 23 | | public float ExposureTime { get; set; } // 在云团中的累计暴露时间 |
| | 20 | 24 | | public float FormationOffsetX { get; set; } |
| | 21 | 25 | | public float FormationOffsetY { get; set; } |
| | | 26 | | |
| | 20 | 27 | | public DroneEntity(string id, string groupId, TargetConfig config, List<Waypoint> route, int formationIndex, flo |
| | 20 | 28 | | { |
| | 20 | 29 | | Id = id; |
| | 20 | 30 | | GroupId = groupId; |
| | 20 | 31 | | TargetType = (TargetType)config.TargetType; |
| | 20 | 32 | | PowerType = (PowerType)config.PowerType; |
| | 20 | 33 | | Wingspan = (float)config.Wingspan; |
| | 20 | 34 | | TypicalSpeed = (float)config.TypicalSpeed; |
| | 20 | 35 | | Route = route; |
| | | 36 | | |
| | 20 | 37 | | if (route.Count > 0) |
| | 20 | 38 | | { |
| | 20 | 39 | | PosX = (float)route[0].PosX; |
| | 20 | 40 | | PosY = (float)route[0].PosY; |
| | 20 | 41 | | PosZ = (float)route[0].PosZ; |
| | 20 | 42 | | } |
| | | 43 | | |
| | | 44 | | // 编队偏移 |
| | 20 | 45 | | ApplyFormationOffset(formationIndex, spacing, mode); |
| | 20 | 46 | | PosX += FormationOffsetX; |
| | 20 | 47 | | PosY += FormationOffsetY; |
| | 20 | 48 | | } |
| | | 49 | | |
| | | 50 | | private void ApplyFormationOffset(int index, float spacing, FormationMode mode) |
| | 20 | 51 | | { |
| | 39 | 52 | | if (index == 0) return; |
| | 1 | 53 | | var offset = spacing * index; |
| | 1 | 54 | | switch (mode) |
| | | 55 | | { |
| | | 56 | | case FormationMode.Formation: |
| | 1 | 57 | | FormationOffsetY = offset; // 沿 Y 轴横队排列 |
| | 1 | 58 | | break; |
| | | 59 | | case FormationMode.Swarm: |
| | 0 | 60 | | var rng = new Random(index * 137); |
| | 0 | 61 | | FormationOffsetX = (float)(rng.NextDouble() - 0.5) * spacing * 3; |
| | 0 | 62 | | FormationOffsetY = (float)(rng.NextDouble() - 0.5) * spacing * 3; |
| | 0 | 63 | | break; |
| | | 64 | | } |
| | 20 | 65 | | } |
| | | 66 | | |
| | | 67 | | public void Update(float deltaTime, float windSpeed, WindDirection windDir) |
| | 5545 | 68 | | { |
| | 5545 | 69 | | if (Status != DroneStatus.Flying) return; |
| | 5545 | 70 | | if (Route.Count == 0) return; |
| | 5545 | 71 | | if (CurrentWaypointIndex >= Route.Count - 1) |
| | 0 | 72 | | { |
| | 0 | 73 | | Status = DroneStatus.ReachedTarget; |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | 5545 | 77 | | var target = Route[CurrentWaypointIndex + 1]; |
| | 5545 | 78 | | var speedMs = TypicalSpeed / 3.6f; |
| | 5545 | 79 | | var dx = (float)(target.PosX - PosX); |
| | 5545 | 80 | | var dy = (float)(target.PosY - PosY); |
| | 5545 | 81 | | var dz = (float)(target.PosZ - PosZ); |
| | 5545 | 82 | | var dist = (float)Math.Sqrt(dx * dx + dy * dy + dz * dz); |
| | 5545 | 83 | | var step = speedMs * deltaTime; |
| | | 84 | | |
| | 5545 | 85 | | if (dist < 1.0f || step >= dist) |
| | 5 | 86 | | { |
| | 5 | 87 | | PosX = (float)target.PosX; |
| | 5 | 88 | | PosY = (float)target.PosY; |
| | 5 | 89 | | PosZ = (float)target.PosZ; |
| | 5 | 90 | | CurrentWaypointIndex++; |
| | 5 | 91 | | if (CurrentWaypointIndex >= Route.Count - 1) |
| | 5 | 92 | | Status = DroneStatus.ReachedTarget; |
| | 5 | 93 | | return; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // 移动方向 |
| | 5540 | 97 | | var ratio = step / dist; |
| | 5540 | 98 | | PosX += dx * ratio; |
| | 5540 | 99 | | PosY += dy * ratio; |
| | 5540 | 100 | | PosZ += dz * ratio; |
| | | 101 | | |
| | | 102 | | // 风偏 |
| | 5540 | 103 | | (float wx, float wy, float wz) = Kinematics.WindToVector(windDir, windSpeed); |
| | 5540 | 104 | | PosX += wx * deltaTime; |
| | 5540 | 105 | | PosY += wy * deltaTime; |
| | 5540 | 106 | | PosZ += wz * deltaTime; |
| | 5545 | 107 | | } |
| | | 108 | | |
| | | 109 | | public void ApplyDamage(float damage) |
| | 27 | 110 | | { |
| | 28 | 111 | | if (Status != DroneStatus.Flying) return; |
| | 26 | 112 | | Hp -= damage; |
| | 26 | 113 | | if (Hp <= 0) |
| | 3 | 114 | | { |
| | 3 | 115 | | Hp = 0; |
| | 3 | 116 | | Status = DroneStatus.Destroyed; |
| | 3 | 117 | | } |
| | 27 | 118 | | } |
| | | 119 | | |
| | | 120 | | public void MarkZoneIntruded() |
| | 1 | 121 | | { |
| | 1 | 122 | | Status = DroneStatus.ZoneIntruded; |
| | 1 | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |