Example #1
0
        public void Cast(int index, GameObject target)
        {
            Spell toCast = spells [index];

            //Check for errors that prevent spell-casting (only if player)
            if (holder is Wizard)
            {
                if (toCast == null)
                {
                    return;
                }
                if (toCast.OnCooldown())
                {
                    ErrorLogUI.Instance.LogError(toCast.Name + " is on cooldown.");
                    return;
                }
                if (toCast.RequiresTarget && target == null)
                {
                    ErrorLogUI.Instance.LogError("This spell requires a target to cast.");
                    return;
                }
                //Find out if there is enough mana for a spellcast and display eventual errors.
                if (mana < toCast.Cost)
                {
                    ErrorLogUI.Instance.LogError("There is not enough mana to cast " + toCast.Name);
                    return;
                }
                if (channeling)
                {
                    ErrorLogUI.Instance.LogError("Cannot cast spells while channeling");
                    return;
                }
            }

            //Check if the spell being cast is toggled and stop it if it is already active
            if (toCast.Toggled && SpellToggle [index])
            {
                ToggleOff(index);
                return;
            }

            //Actually cast the spell
            mana -= toCast.Cost;
            holder.PlayAnimation(toCast.AnimationType.ToString());

            //Determines which type of spell is being cast and chose the target accordingly.
            if (toCast.TargetsSelf)
            {
                toCast.Cast(holder.gameObject, holder.facingRight, DamageModifier);
            }
            else if (!toCast.RequiresTarget)
            {
                toCast.Cast(magicAttackPoint, holder.facingRight, DamageModifier);
            }
            else if (toCast.RequiresTarget)
            {
                //We know there is a target from the first check.
                toCast.Cast(target, holder.facingRight, DamageModifier);
            }

            if (toCast.Channeled)
            {
                channelManaDrain = drainMana(toCast.Cost);
                StartCoroutine(channelManaDrain);
                channeling = true;
            }
            else if (toCast.Toggled)
            {
                SpellToggle [index] = true;
            }
        }