74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class EquipmentManager : MonoBehaviour
|
|
{
|
|
[Header("Controllers")]
|
|
[SerializeField] private MeleeCombatLayerController _meleeController;
|
|
[SerializeField] private BowAimLayerController _bowController;
|
|
|
|
private enum EquipState { None, Melee, Bow }
|
|
private EquipState _currentState = EquipState.None;
|
|
|
|
private bool _isTransitioning;
|
|
|
|
private void Update()
|
|
{
|
|
if (_isTransitioning) return;
|
|
|
|
if (Keyboard.current != null)
|
|
{
|
|
if (Keyboard.current.digit1Key.wasPressedThisFrame)
|
|
{
|
|
SwitchToWeapon(EquipState.Bow);
|
|
}
|
|
else if (Keyboard.current.digit2Key.wasPressedThisFrame)
|
|
{
|
|
SwitchToWeapon(EquipState.Melee);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SwitchToWeapon(EquipState targetState)
|
|
{
|
|
// If we press the same key, it toggles it off
|
|
if (_currentState == targetState)
|
|
{
|
|
StartCoroutine(TransitionRoutine(EquipState.None));
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(TransitionRoutine(targetState));
|
|
}
|
|
}
|
|
|
|
private IEnumerator TransitionRoutine(EquipState targetState)
|
|
{
|
|
_isTransitioning = true;
|
|
|
|
// 1. Unequip Current Weapon
|
|
if (_currentState == EquipState.Melee && _meleeController != null)
|
|
{
|
|
yield return StartCoroutine(_meleeController.UnequipWeapon());
|
|
}
|
|
else if (_currentState == EquipState.Bow && _bowController != null)
|
|
{
|
|
yield return StartCoroutine(_bowController.UnequipWeapon());
|
|
}
|
|
|
|
// 2. Equip New Weapon
|
|
if (targetState == EquipState.Melee && _meleeController != null)
|
|
{
|
|
yield return StartCoroutine(_meleeController.EquipWeapon());
|
|
}
|
|
else if (targetState == EquipState.Bow && _bowController != null)
|
|
{
|
|
yield return StartCoroutine(_bowController.EquipWeapon());
|
|
}
|
|
|
|
_currentState = targetState;
|
|
_isTransitioning = false;
|
|
}
|
|
}
|