Exemple #1
0
    IEnumerator _FireWeapon(int weaponNumber)
    {
        //we're firing, dont fire again until we're done
        firing = true;
        Transform weapon     = transform.Find("Equipment").Find("WeaponSlot").GetChild(weaponNumber).transform.GetChild(0);
        string    weaponType = weapon.tag;

        WeaponBehavior weaponBehavior = weapon.GetComponent <WeaponBehavior>();
        Vector4        weaponInfo     = weaponBehavior.WeaponInfo();

        float _rotationSpeed    = stats.rotationSpeed;
        float fireSpeed         = weaponInfo.x;
        float chargeSpeed       = weaponInfo.y;
        float playerRotSlow     = weaponInfo.z;
        float energyDrainAmount = weaponInfo.w;

        Animator weaponAnimator = null;

        if (weapon.GetComponent <Animator>())
        {
            weaponAnimator = weapon.GetComponent <Animator>();
        }

        //swap weapons on fire
        if (activeWeapon != weaponNumber)
        {
            GameObject oldWeaponSlot = equipment.equipmentSlots[activeWeapon].gameObject;
            oldWeaponSlot.SetActive(false);

            GameObject weaponSlot = equipment.equipmentSlots[weaponNumber].gameObject;
            weaponSlot.SetActive(true);

            activeWeapon = weaponNumber;
        }

        playerAnimator.SetFloat("FireSpeed", fireSpeed);
        playerAnimator.SetFloat("ChargeSpeed", chargeSpeed);

        if (weaponType == "Staff")
        {
            Animate(Casting_Animation);
        }
        if (weaponType == "Sword" || weaponType == "Dagger") // or any melee type..
        {
            Animate(Meleeing_Animation);                     // animate appropriate weapon type animation
        }

        if (weaponAnimator != null)
        {
            weaponAnimator.SetBool("Firing", true);
            weaponAnimator.SetFloat("ChargeSpeed", chargeSpeed);
            weaponAnimator.SetFloat("FireSpeed", fireSpeed);
        }

        //slow player rotation by weapon amount
        stats.rotationSpeed = stats.rotationSpeed - (stats.rotationSpeed * playerRotSlow / 100);

        float lastFireTime = 0;

        while (firingWeapon == weaponNumber && stats.energy - energyDrainAmount >= 0)
        {
            if (Time.time >= lastFireTime + (1 / fireSpeed) && charged == true)
            {
                lastFireTime = Time.time;
                playerAnimator.SetTrigger("Fire");

                if (weaponAnimator != null)
                {
                    weaponAnimator.SetTrigger("Fire");
                }
            }
            yield return(new WaitForFixedUpdate());
        }

        //set rotation speed to normal
        stats.rotationSpeed = _rotationSpeed;
        Animate(Idle_Animation);
        playerAnimator.SetFloat("FireSpeed", 1);

        if (weaponAnimator != null)
        {
            weaponAnimator.SetFloat("FireSpeed", 1);
            weaponAnimator.SetBool("Firing", false);
        }

        firing  = false;
        charged = false;
    }