/// <summary> Creates a coroutine and starts it. </summary>
        /// <param name="coroutine"> The coroutine to create a wrapper for. </param>
        /// <returns> The new coroutine. </returns>
        public AdvancedCoroutine CreateCoroutine(IEnumerator coroutine)
        {
            var createdCoroutine = new AdvancedCoroutine(this, coroutine);

            createdCoroutine.Start();
            return(createdCoroutine);
        }
        private IEnumerator StartFiringProjectiles()
        {
            while (true)
            {
                yield return(AdvancedCoroutine.Wait(_period));

                if (ProjectileTemplate != null)
                {
                    var projectileBehavior = ProjectileTemplate.GetComponent <ProjectileBehavior>();
                    projectileBehavior.CreateAndInitializeFrom(Owner.transform, ProjectileLayer);
                }
            }
        }
        /// <summary> Spawns a new entities. </summary>
        private IEnumerator Trigger()
        {
            while (true)
            {
                if (count < 10)
                {
                    var randomDirection = Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360));

                    var cloned = _instanceManager.Create(Template, _transform.position, randomDirection);

                    cloned.AddComponent <GroupChildBehavior>().Initialize(this);
                    cloned.SendFutureSignal(AllSignals.TargetChanged, new TargetAquiredSignal(CurrentTarget));
                }

                yield return(AdvancedCoroutine.Wait(TimeSpan.FromSeconds(Period)));
            }
        }
        void HandleTargetChanged(TargetAquiredSignal targetAquired)
        {
            var shouldFire = !targetAquired.TargetWasLost;

            if (shouldFire)
            {
                if (!_coroutine.IsValid)
                {
                    _coroutine = CreateCoroutine(StartFiringProjectiles());
                }
            }
            else
            {
                if (_coroutine.IsValid)
                {
                    _coroutine.Stop();
                    _coroutine = AdvancedCoroutine.Empty;
                }
            }
        }