154 lines
4.3 KiB
C#
154 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BlueWater.Interfaces;
|
|
using BlueWater.Uis;
|
|
using BlueWater.Utility;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace BlueWater.Players.Tycoons
|
|
{
|
|
public class TycoonInput : MonoBehaviour
|
|
{
|
|
// variables
|
|
#region variables
|
|
|
|
// Components
|
|
[SerializeField]
|
|
private PlayerInput _playerInput;
|
|
|
|
[SerializeField]
|
|
protected float InteractionRadius = 2f;
|
|
|
|
private List<IPlayerInteraction> _playerInteractions = new();
|
|
private IPlayerInteraction _closestInteraction;
|
|
private IPlayerInteraction _previousInteraction;
|
|
|
|
// Events
|
|
public event Action<Vector2> OnMoveInputReceived;
|
|
|
|
#endregion
|
|
|
|
#region Unity events
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
// 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.Tycoon);
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Player input methods
|
|
#region Player input methods
|
|
|
|
// Tycoon
|
|
public void OnMove(InputAction.CallbackContext context)
|
|
{
|
|
var movementInput = context.ReadValue<Vector2>();
|
|
OnMoveInputReceived?.Invoke(movementInput);
|
|
}
|
|
|
|
public void OnInteraction(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
_closestInteraction?.Interaction();
|
|
}
|
|
}
|
|
|
|
public void OnDevelopKey01(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
DataManager.Instance.TestData();
|
|
}
|
|
}
|
|
|
|
// TycoonUi
|
|
public void OnCancel(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
TycoonUiManager.Instance.CloseLastPopup();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#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)
|
|
{
|
|
if (!interaction.CanInteraction()) continue;
|
|
|
|
var distance = Vector3.Distance(transform.position, interaction.Transform.position);
|
|
if (distance > interaction.InteractionRadius || distance >= closestDistance) continue;
|
|
|
|
closestDistance = distance;
|
|
closestInteraction = interaction;
|
|
}
|
|
|
|
return closestInteraction;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |