250 lines
7.4 KiB
C#
250 lines
7.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BlueWater.Interfaces;
|
|
using BlueWater.Maps;
|
|
using BlueWater.Uis;
|
|
using BlueWater.Utility;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace BlueWater.Players.Combat
|
|
{
|
|
public class CombatInput : MonoBehaviour
|
|
{
|
|
// variables
|
|
#region variables
|
|
|
|
// Components
|
|
[SerializeField]
|
|
private PlayerInput _playerInput;
|
|
|
|
[SerializeField]
|
|
protected float InteractionRadius = 2f;
|
|
|
|
[SerializeField]
|
|
private Vector3 _interactionUiOffset = new(0f, 1.5f, 0f);
|
|
|
|
private List<IPlayerInteraction> _playerInteractions = new();
|
|
private IPlayerInteraction _closestInteraction;
|
|
private IPlayerInteraction _previousInteraction;
|
|
|
|
// Events
|
|
public event Action<Vector2> OnMoveInputReceived;
|
|
public event Action OnDashInputReceived;
|
|
public event Action<bool> OnAttackInputReceived;
|
|
public event Action<string> OnActivateMainSkillInputReceived;
|
|
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
InitializeComponents();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_closestInteraction = GetClosestInteraction();
|
|
if (_closestInteraction != null)
|
|
{
|
|
_closestInteraction.ShowInteractionUi();
|
|
if (_previousInteraction != null && _closestInteraction != _previousInteraction)
|
|
{
|
|
_previousInteraction.HideInteractionUi();
|
|
}
|
|
_previousInteraction = _closestInteraction;
|
|
}
|
|
else
|
|
{
|
|
_previousInteraction?.HideInteractionUi();
|
|
_previousInteraction = null;
|
|
}
|
|
}
|
|
|
|
// Initialize methods
|
|
#region Initialize methods
|
|
|
|
[Button("컴포넌트 초기화")]
|
|
private void InitializeComponents()
|
|
{
|
|
_playerInput = GetComponent<PlayerInput>();
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
_playerInput.enabled = true;
|
|
PlayerInputKeyManager.Instance.SetCurrentPlayerInput(_playerInput);
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.Combat);
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Player input methods
|
|
#region Player input methods
|
|
|
|
public void OnMove(InputAction.CallbackContext context)
|
|
{
|
|
var movementInput = context.ReadValue<Vector2>();
|
|
OnMoveInputReceived?.Invoke(movementInput);
|
|
}
|
|
|
|
public void OnDash(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
OnDashInputReceived?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void OnAttack(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
var device = context.control.device;
|
|
bool usedMouse;
|
|
|
|
switch (device)
|
|
{
|
|
case Keyboard:
|
|
usedMouse = false;
|
|
break;
|
|
case Mouse:
|
|
usedMouse = true;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
OnAttackInputReceived?.Invoke(usedMouse);
|
|
}
|
|
}
|
|
|
|
public void OnActivateMainSkill(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
OnActivateMainSkillInputReceived?.Invoke(null);
|
|
}
|
|
}
|
|
|
|
public void OnInteraction(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
_closestInteraction?.Interaction();
|
|
}
|
|
}
|
|
|
|
public void OnOpenItemInventory(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
CombatUiManager.Instance.CombatItemInventoryUi.Open();
|
|
}
|
|
}
|
|
|
|
public void OnCloseItemInventory(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
if (!CombatUiManager.Instance.CombatItemInventoryUi.gameObject.activeSelf) return;
|
|
|
|
CombatUiManager.Instance.CombatItemInventoryUi.Close();
|
|
}
|
|
}
|
|
|
|
public void OnOpenMenu(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
CombatUiManager.Instance.CombatMenuPopupUi.Open();
|
|
}
|
|
}
|
|
|
|
public void OnOpenDevelopMenu(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
CombatUiManager.Instance.DevelopMenuPopupUi.Open();
|
|
}
|
|
}
|
|
|
|
public void OnForceKillBoss(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
MapManager.Instance.ForceKillCurrentBoss();
|
|
}
|
|
}
|
|
|
|
public void OnHealthPointMax(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
GameManager.Instance.CurrentCombatPlayer.SetCurrentHealthPointMax();
|
|
}
|
|
}
|
|
|
|
public void OnCancel(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
PopupUiController.CloseLastPopup();
|
|
}
|
|
}
|
|
|
|
public void OnInteractionUi(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
if (CombatUiManager.Instance.CombatTutorialUi.CurrentTutorialState == TutorialState.First)
|
|
{
|
|
CombatUiManager.Instance.CombatTutorialUi.SecondFirstTutorialUi();
|
|
}
|
|
else if (CombatUiManager.Instance.CombatTutorialUi.CurrentTutorialState == TutorialState.Second)
|
|
{
|
|
CombatUiManager.Instance.CombatTutorialUi.EndTutorialUi();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Methods
|
|
#region Methods
|
|
|
|
public void RegisterPlayerInteraction(IPlayerInteraction playerInteraction)
|
|
{
|
|
Utils.RegisterList(_playerInteractions, playerInteraction);
|
|
}
|
|
|
|
public void UnregisterPlayerInteraction(IPlayerInteraction playerInteraction)
|
|
{
|
|
Utils.UnregisterList(_playerInteractions, playerInteraction);
|
|
}
|
|
|
|
private IPlayerInteraction GetClosestInteraction()
|
|
{
|
|
IPlayerInteraction closestInteraction = null;
|
|
var closestDistance = float.MaxValue;
|
|
|
|
foreach (var interaction in _playerInteractions)
|
|
{
|
|
var distance = Vector3.Distance(transform.position, interaction.CenterTransform.position);
|
|
if (distance > InteractionRadius || distance >= closestDistance) continue;
|
|
|
|
closestDistance = distance;
|
|
closestInteraction = interaction;
|
|
}
|
|
|
|
return closestInteraction;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |