| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | // Base implementation of a carrier. |
| | | 7 | | // |
| | | 8 | | // A carrier carries other interceptors, such as a launcher or a carrier interceptor. |
| | | 9 | | public abstract class CarrierBase : InterceptorBase { |
| | | 10 | | // Time between checking whether to release sub-interceptors. |
| | | 11 | | private const float _releasePeriod = 0.2f; |
| | | 12 | | |
| | | 13 | | // Coroutine for releasing sub-interceptors. |
| | | 14 | | private Coroutine _releaseCoroutine; |
| | | 15 | | |
| | | 16 | | // Release strategy for sub-interceptors. |
| | 42 | 17 | | public IReleaseStrategy ReleaseStrategy { get; set; } |
| | | 18 | | |
| | 14 | 19 | | protected override void Awake() { |
| | 14 | 20 | | base.Awake(); |
| | | 21 | | |
| | 14 | 22 | | EscapeDetector = new GeometricEscapeDetector(this); |
| | 14 | 23 | | } |
| | | 24 | | |
| | 14 | 25 | | protected override void Start() { |
| | 14 | 26 | | base.Start(); |
| | | 27 | | |
| | 14 | 28 | | _releaseCoroutine = StartCoroutine(ReleaseManager(_releasePeriod)); |
| | 14 | 29 | | } |
| | | 30 | | |
| | 13 | 31 | | protected override void OnDestroy() { |
| | 13 | 32 | | base.OnDestroy(); |
| | | 33 | | |
| | 26 | 34 | | if (_releaseCoroutine != null) { |
| | 13 | 35 | | StopCoroutine(_releaseCoroutine); |
| | 13 | 36 | | _releaseCoroutine = null; |
| | 13 | 37 | | } |
| | 13 | 38 | | } |
| | | 39 | | |
| | 14 | 40 | | private IEnumerator ReleaseManager(float period) { |
| | 28 | 41 | | while (NumSubInterceptorsRemaining > 0) { |
| | | 42 | | // Determine whether to release the sub-interceptors. |
| | 28 | 43 | | if (ReleaseStrategy != null) { |
| | 14 | 44 | | List<IAgent> releasedAgents = ReleaseStrategy.Release(); |
| | 14 | 45 | | NumSubInterceptorsRemaining -= releasedAgents.Count; |
| | | 46 | | |
| | 42 | 47 | | foreach (var agent in releasedAgents) { |
| | 0 | 48 | | if (agent is IInterceptor subInterceptor) { |
| | 0 | 49 | | subInterceptor.OnAssignSubInterceptor += AssignSubInterceptor; |
| | 0 | 50 | | subInterceptor.OnReassignTarget += ReassignTarget; |
| | 0 | 51 | | if (subInterceptor.Movement is MissileMovement movement) { |
| | 0 | 52 | | movement.FlightPhase = Simulation.FlightPhase.Boost; |
| | 0 | 53 | | } |
| | 0 | 54 | | } |
| | 0 | 55 | | } |
| | 14 | 56 | | } |
| | | 57 | | |
| | 14 | 58 | | yield return new WaitForSeconds(period); |
| | 0 | 59 | | } |
| | 0 | 60 | | _releaseCoroutine = null; |
| | 0 | 61 | | } |
| | | 62 | | } |