private IEnumerator ActionSequence() { // Determine turn order using our custom compare method List <EntityController> turnOrder = new List <EntityController>(); foreach (EntityController ec in entities) { turnOrder.Add(ec); } turnOrder.Sort((x, y) => x.CompareTo(y)); // Execute each entity's action foreach (EntityController ec in turnOrder) { // We need to check for target death later. if (ec.dead) { continue; } // Tweak later so that the target can change if (ec.target.dead) { continue; } // Cast the spell SpellCast spellCast = ec.action.Cast(ec, ec.target); ec.actionResult = spellCast; ec.target.IncreaseDamageTaken(spellCast.GetDamageApplied()); // NOTE: Replace with a spell specific method later because "Player casts attack!" sounds awful. string dialogueSeq = ec.param.entityName + " casts " + spellCast.spell.name + " on " + ec.target.param.entityName + "!"; EventManager.Instance.RaiseStringEvent(EventConstants.ON_DIALOGUE_QUEUE, dialogueSeq); // If spell is successful, queue up its animation if (spellCast.success) { Sequence animationSeq = new AnimationSequence(spellCast.spell.spellAnimation, ec, ec.target, spellCast); EventManager.Instance.RaiseSequenceGameEvent(EventConstants.ON_SEQUENCE_QUEUE, animationSeq); } // Otherwise, output a failure message else { // NOTE: Replace with a spell specific method later so we can output more varied flavor text here EventManager.Instance.RaiseStringEvent(EventConstants.ON_DIALOGUE_QUEUE, "Failed!"); } // Start the sequence sequencer.StartSequence(); // Output damage messages if (spellCast.GetDamageApplied() > 0) { if (spellCast.critical) { string critSeq = "Critical Hit!"; EventManager.Instance.RaiseStringEvent(EventConstants.ON_DIALOGUE_QUEUE, critSeq); } string damageSeq = ec.target.param.entityName + " takes " + spellCast.GetDamageApplied() + " damage!"; EventManager.Instance.RaiseStringEvent(EventConstants.ON_DIALOGUE_QUEUE, damageSeq); } // Wait until sequence is done while (sequencer.active) { yield return(null); } // apply effects from the spell if the target is still alive. List <EffectInstance> effects = spellCast.GetEffects(); foreach (EffectInstance ef in effects) { if (ef.castSuccess) { ef.OnActivate(); } } while (sequencer.active) { yield return(null); } } yield return(null); StartCoroutine(InvokeOnSequenceEnd(EventConstants.ON_TURN_END)); }