コード例 #1
0
    /// <summary>
    ///     Times the attacking cycle for a sword style attack
    /// </summary>
    /// <remark>
    ///    The total time this coroutine takes to execute is equal to chargeTime+enableLength+pauseTime+retreatLength
    /// </remark>
    ///     <param name="vectorToTarget">The vector from the center of this gameObject's transform to the target</param>
    ///     <param name="chargeTime">The time this enemy waits before attacking</param>
    ///     <param name="enableLength">How long to enable the weapon's collider for</param>
    ///     <param name="pauseTime">The time after turning off the collider this enemy waits before retreating</param>
    ///     <param name="retreatLength">The length of time over which this enemy retreats</param>
    private IEnumerator SwordAttack(Vector2 vectorToTarget, float chargeTime, float pauseTime, float retreatLength)
    {
        attacking = true;
        WC.AimInDirection(vectorToTarget);
        float     startTime = Time.time;
        Coroutine retreat   = null;
        bool      doing     = true;
        bool      shot      = false;

        while (doing)
        {
            Vector3 startingPosition = transform.position;
            if (Time.time < startTime + chargeTime) // CHARGING ATTACK
            {
                attackIndicator.SetActive(true);
                // do animations here
                yield return(new WaitForSeconds(.05f));
            }
            else if (Time.time < startTime + chargeTime + pauseTime) // ATTACKING
            {
                attackIndicator.SetActive(false);
                if (!shot)
                {
                    WC.Fire(vectorToTarget);
                    shot = true;
                }
                else
                {
                    if (WC.IsFiring())
                    {
                        startTime += Time.deltaTime;// increment start time to prevent from moving on and waiting until the attack is complete
                    }
                }
                // do animations here
                yield return(new WaitForEndOfFrame());
            }
            else if (Time.time > startTime + chargeTime + pauseTime && doing) // RETREATING (jumping backwards)
            {
                if (retreat == null)
                {
                    //AimWeaponAtTarget(-vectorToTarget);
                    WC.AimInDirection(-vectorToTarget);
                    retreat = MoveTo(-vectorToTarget.normalized * retreatLength);
                }
                else if (!RunningThisRoutine(retreat))
                {
                    doing = false;
                }
                // do animations here
                yield return(new WaitForFixedUpdate());
            }
        }
        attacking = false;
    }