| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using JetBrains.Annotations; |
| | 4 | | using UnityEngine; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | public class CarrierInterceptor : Interceptor { |
| 0 | 8 | | private bool _submunitionsLaunched = false; |
| | 9 | |
|
| 0 | 10 | | public override void SetDynamicAgentConfig(DynamicAgentConfig config) { |
| 0 | 11 | | base.SetDynamicAgentConfig(config); |
| 0 | 12 | | if (!HasAssignedTarget()) { |
| | 13 | | // Create a DummyTarget that is projected 100km out from the initialRotation |
| 0 | 14 | | Vector3 initialPosition = config.initial_state.position; |
| 0 | 15 | | Quaternion initialRotation = Quaternion.Euler(config.initial_state.rotation); |
| 0 | 16 | | Vector3 forwardDirection = initialRotation * Vector3.forward; |
| 0 | 17 | | Vector3 dummyTargetPosition = |
| | 18 | | initialPosition + forwardDirection * 100000f; // 100km in meters |
| | 19 | |
|
| | 20 | | // Calculate a reasonable velocity for the dummy target |
| 0 | 21 | | Vector3 dummyTargetVelocity = Vector3.zero; // Assuming 1000 m/s speed |
| | 22 | |
|
| | 23 | | // Create the dummy agent using SimManager |
| 0 | 24 | | Agent dummyAgent = |
| | 25 | | SimManager.Instance.CreateDummyAgent(dummyTargetPosition, dummyTargetVelocity); |
| | 26 | |
|
| | 27 | | // Assign the dummy agent as the target |
| 0 | 28 | | AssignTarget(dummyAgent); |
| 0 | 29 | | } |
| 0 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | public override bool IsAssignable() { |
| 0 | 33 | | return false; |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | protected override void FixedUpdate() { |
| 0 | 37 | | base.FixedUpdate(); |
| 0 | 38 | | float launchTimeVariance = 0.5f; |
| 0 | 39 | | float launchTimeNoise = Random.Range(-launchTimeVariance, launchTimeVariance); |
| 0 | 40 | | double launchTimeWithNoise = |
| | 41 | | dynamicAgentConfig.submunitions_config.dispense_time + launchTimeNoise; |
| | 42 | | // Check if it's time to launch submunitions |
| 0 | 43 | | if (!_submunitionsLaunched && |
| | 44 | | (GetFlightPhase() == FlightPhase.MIDCOURSE || GetFlightPhase() == FlightPhase.BOOST) && |
| 0 | 45 | | SimManager.Instance.GetElapsedSimulationTime() >= launchTimeWithNoise) { |
| 0 | 46 | | SpawnSubmunitions(); |
| 0 | 47 | | _submunitionsLaunched = true; |
| 0 | 48 | | } |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | protected override void UpdateMidCourse(double deltaTime) { |
| 0 | 52 | | base.UpdateMidCourse(deltaTime); |
| 0 | 53 | | } |
| | 54 | |
|
| 0 | 55 | | protected override void DrawDebugVectors() { |
| 0 | 56 | | base.DrawDebugVectors(); |
| 0 | 57 | | if (_acceleration != null) { |
| 0 | 58 | | Debug.DrawRay(transform.position, _acceleration * 1f, Color.green); |
| 0 | 59 | | } |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | public void SpawnSubmunitions() { |
| 0 | 63 | | List<Interceptor> submunitions = new List<Interceptor>(); |
| 0 | 64 | | for (int i = 0; i < dynamicAgentConfig.submunitions_config.num_submunitions; i++) { |
| 0 | 65 | | DynamicAgentConfig convertedConfig = DynamicAgentConfig.FromSubmunitionDynamicAgentConfig( |
| | 66 | | dynamicAgentConfig.submunitions_config.dynamic_agent_config); |
| 0 | 67 | | convertedConfig.initial_state.position = transform.position; |
| 0 | 68 | | convertedConfig.initial_state.velocity = GetComponent<Rigidbody>().linearVelocity; |
| 0 | 69 | | Interceptor submunition = SimManager.Instance.CreateInterceptor(convertedConfig); |
| 0 | 70 | | submunition.SetFlightPhase(FlightPhase.READY); |
| | 71 | | // Force the submunition to be launched with the same velocity as the carrier |
| 0 | 72 | | submunition.SetVelocity(GetComponent<Rigidbody>().linearVelocity); |
| 0 | 73 | | submunitions.Add(submunition); |
| 0 | 74 | | } |
| 0 | 75 | | IADS.Instance.RequestThreatAssignment(submunitions); |
| | 76 | |
|
| 0 | 77 | | SimManager.Instance.AddSubmunitionsSwarm( |
| 0 | 78 | | submunitions.ConvertAll(submunition => submunition as Agent)); |
| 0 | 79 | | } |
| | 80 | | } |