Ejemplo n.º 1
0
    private void Update()
    {
        // shoot handling
        WeaponController activeWeapon = GetActiveWeapon();

        if (activeWeapon && m_WeaponSwitchState == WeaponSwitchState.Up)
        {
            // handle aiming down sights
            // isAiming = m_InputHandler.GetAimInputHeld();

            // handle shooting
            bool hasFired = activeWeapon.HandleShootInputs(
                m_InputHandler.GetFireInputDown(),
                m_InputHandler.GetFireInputHeld(),
                m_InputHandler.GetFireInputReleased());

            bool hasAltFired = activeWeapon.HandleAltShootInputs(
                m_InputHandler.GetAltFireInputDown(),
                m_InputHandler.GetAltFireInputHeld(),
                m_InputHandler.GetAltFireInputReleased());

            // Handle accumulating recoil
            if (hasFired)
            {
                m_AccumulatedRecoil += Vector3.back * activeWeapon.recoilForce;
                m_AccumulatedRecoil  = Vector3.ClampMagnitude(m_AccumulatedRecoil, maxRecoilDistance);
            }

            // tell weaponcontroller to use lightning VFX
            if (m_InputHandler.GetFireInputDown())
            {
                activeWeapon.SetFX(true);
            }
            else if (m_InputHandler.GetFireInputReleased())
            {
                activeWeapon.SetFX(false);
            }
        }

        // weapon switch handling
        if (!isAiming &&
            (activeWeapon == null || !activeWeapon.isCharging) &&
            (m_WeaponSwitchState == WeaponSwitchState.Up || m_WeaponSwitchState == WeaponSwitchState.Down))
        {
            int switchWeaponInput = m_InputHandler.GetSwitchWeaponInput();
            if (switchWeaponInput != 0)
            {
                bool switchUp = switchWeaponInput > 0;
                SwitchWeapon(switchUp);
            }
            else
            {
                switchWeaponInput = m_InputHandler.GetSelectWeaponInput();
                if (switchWeaponInput != 0)
                {
                    if (GetWeaponAtSlotIndex(switchWeaponInput - 1) != null)
                    {
                        SwitchToWeaponIndex(switchWeaponInput - 1);
                    }
                }
            }
        }

        // Pointing at enemy handling
        isPointingAtEnemy = false;
        if (activeWeapon)
        {
            if (Physics.Raycast(weaponCamera.transform.position, weaponCamera.transform.forward, out RaycastHit hit, 1000, -1, QueryTriggerInteraction.Ignore))
            {
                if (hit.collider.GetComponentInParent <EnemyController>())
                {
                    isPointingAtEnemy = true;
                }
            }
        }

        // Selectable handling
        Collider selectableCollider = RaycastForSelectableCollider();

        if (selectableCollider)
        {
            Selectable selectable = selectableCollider.gameObject.GetComponentInParent <Selectable>();
            //if pointing at a selectable and it hasn't been selected, select it and unselect the last used selectable
            if (!selectable.selected)
            {
                // Debug.Log("select");
                selectable.Select(GetComponent <PlayerUpgradeManager>());
                m_LastSelectable = selectable;
            }
        }
        else if (m_LastSelectable)
        {
            //if not pointing at a selectable and we were in the last frame, then unset m_lastselectable and unselect it
            m_LastSelectable.Unselect();
            m_LastSelectable = null;
        }
    }