| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using UnityEngine; |
| | | 3 | | |
| | | 4 | | // Base implementation of a single release strategy. |
| | | 5 | | // |
| | | 6 | | // The single release strategy checks whether a sub-interceptor should be released for every |
| | | 7 | | // sub-hierarchical object of the target hierarchical object. No assignment is performed as a result |
| | | 8 | | // of the per-target check. |
| | | 9 | | public abstract class SingleReleaseStrategyBase : ReleaseStrategyBase { |
| | | 10 | | // The sub-interceptors are launched with a nonzero speed for their rotation to be set correctly. |
| | | 11 | | private const float _initialSpeed = 1e-3f; |
| | | 12 | | |
| | 42 | 13 | | public SingleReleaseStrategyBase(IAgent agent) : base(agent) {} |
| | | 14 | | |
| | 14 | 15 | | protected override List<IAgent> Release(IEnumerable<IHierarchical> hierarchicals) { |
| | 14 | 16 | | var carrier = Agent as CarrierBase; |
| | 14 | 17 | | var releasedAgents = new List<IAgent>(); |
| | 42 | 18 | | foreach (var hierarchical in hierarchicals) { |
| | 0 | 19 | | if (carrier.NumSubInterceptorsRemaining - releasedAgents.Count <= 0) { |
| | 0 | 20 | | break; |
| | | 21 | | } |
| | 0 | 22 | | IAgent releasedAgent = ReleaseSingle(hierarchical); |
| | 0 | 23 | | if (releasedAgent != null) { |
| | 0 | 24 | | releasedAgents.Add(releasedAgent); |
| | 0 | 25 | | } |
| | 0 | 26 | | } |
| | 14 | 27 | | return releasedAgents; |
| | 14 | 28 | | } |
| | | 29 | | |
| | | 30 | | // Plan the release for the given target. |
| | | 31 | | protected abstract LaunchPlan PlanRelease(IHierarchical target); |
| | | 32 | | |
| | 0 | 33 | | private IAgent ReleaseSingle(IHierarchical hierarchical) { |
| | 0 | 34 | | IHierarchical target = hierarchical.Target; |
| | 0 | 35 | | if (target == null || hierarchical.LaunchedHierarchicals.Count != 0) { |
| | 0 | 36 | | return null; |
| | | 37 | | } |
| | 0 | 38 | | LaunchPlan launchPlan = PlanRelease(target); |
| | 0 | 39 | | if (!launchPlan.ShouldLaunch) { |
| | 0 | 40 | | return null; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | Simulation.State initialState = new Simulation.State() { |
| | | 44 | | Position = Coordinates3.ToProto(Agent.Position), |
| | | 45 | | Velocity = |
| | | 46 | | Coordinates3.ToProto(launchPlan.NormalizedLaunchVector(Agent.Position) * _initialSpeed), |
| | | 47 | | }; |
| | 0 | 48 | | IAgent subInterceptor = SimManager.Instance.CreateInterceptor( |
| | | 49 | | Agent.AgentConfig.SubAgentConfig.AgentConfig, initialState); |
| | 0 | 50 | | if (subInterceptor is not IInterceptor subInterceptorInterceptor) { |
| | 0 | 51 | | return null; |
| | | 52 | | } |
| | 0 | 53 | | subInterceptor.HierarchicalAgent.Target = target; |
| | 0 | 54 | | hierarchical.AddLaunchedHierarchical(subInterceptor.HierarchicalAgent); |
| | | 55 | | |
| | 0 | 56 | | Debug.Log( |
| | | 57 | | $"Launching a {subInterceptor.StaticConfig.AgentType} from {Agent} at an elevation of {launchPlan.LaunchAngle} d |
| | 0 | 58 | | UIManager.Instance.LogActionMessage( |
| | | 59 | | $"[IADS] Launching a {subInterceptor.StaticConfig.AgentType} from {Agent} at an elevation of {launchPlan.LaunchA |
| | 0 | 60 | | return subInterceptor; |
| | 0 | 61 | | } |
| | | 62 | | } |