2024-06-06 11:07:07 +00:00
|
|
|
using System;
|
2024-10-29 06:45:18 +00:00
|
|
|
using System.Collections;
|
2024-07-02 18:27:56 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using BlueWater.Interfaces;
|
2024-08-14 10:52:35 +00:00
|
|
|
using BlueWater.Tycoons;
|
2024-06-06 11:07:07 +00:00
|
|
|
using BlueWater.Uis;
|
2024-07-02 18:27:56 +00:00
|
|
|
using BlueWater.Utility;
|
|
|
|
using Sirenix.OdinInspector;
|
2024-06-06 11:07:07 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
namespace BlueWater.Players.Tycoons
|
|
|
|
{
|
|
|
|
public class TycoonInput : MonoBehaviour
|
|
|
|
{
|
|
|
|
// variables
|
|
|
|
#region variables
|
|
|
|
|
|
|
|
// Components
|
2024-07-02 18:27:56 +00:00
|
|
|
[SerializeField]
|
2024-06-06 11:07:07 +00:00
|
|
|
private PlayerInput _playerInput;
|
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
private List<IPlayerInteraction> _playerInteractions = new();
|
|
|
|
private IPlayerInteraction _closestInteraction;
|
|
|
|
private IPlayerInteraction _previousInteraction;
|
|
|
|
|
2024-06-06 11:07:07 +00:00
|
|
|
// Events
|
|
|
|
public event Action<Vector2> OnMoveInputReceived;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
#region Unity events
|
2024-06-06 11:07:07 +00:00
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
private void Awake()
|
2024-06-06 11:07:07 +00:00
|
|
|
{
|
2024-07-02 18:27:56 +00:00
|
|
|
InitializeComponents();
|
|
|
|
}
|
2024-06-06 11:07:07 +00:00
|
|
|
|
2024-07-10 00:22:34 +00:00
|
|
|
private void Start()
|
|
|
|
{
|
2024-10-29 06:45:18 +00:00
|
|
|
StartCoroutine(nameof(Initialize));
|
2024-07-10 00:22:34 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
_closestInteraction = GetClosestInteraction();
|
|
|
|
if (_closestInteraction != null)
|
|
|
|
{
|
|
|
|
_closestInteraction.ShowInteractionUi();
|
|
|
|
if (_previousInteraction != null && _closestInteraction != _previousInteraction)
|
|
|
|
{
|
|
|
|
_previousInteraction.HideInteractionUi();
|
|
|
|
}
|
|
|
|
_previousInteraction = _closestInteraction;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-30 09:41:55 +00:00
|
|
|
_previousInteraction?.CancelInteraction();
|
2024-07-02 18:27:56 +00:00
|
|
|
_previousInteraction?.HideInteractionUi();
|
|
|
|
_previousInteraction = null;
|
|
|
|
}
|
2024-06-06 11:07:07 +00:00
|
|
|
}
|
2024-07-02 18:27:56 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Initialize methods
|
|
|
|
#region Initialize methods
|
2024-06-06 11:07:07 +00:00
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
[Button("컴포넌트 초기화")]
|
|
|
|
private void InitializeComponents()
|
2024-06-06 11:07:07 +00:00
|
|
|
{
|
2024-07-02 18:27:56 +00:00
|
|
|
_playerInput = GetComponent<PlayerInput>();
|
2024-06-06 11:07:07 +00:00
|
|
|
}
|
|
|
|
|
2024-10-29 06:45:18 +00:00
|
|
|
private IEnumerator Initialize()
|
2024-07-10 00:22:34 +00:00
|
|
|
{
|
|
|
|
PlayerInputKeyManager.Instance.SetCurrentPlayerInput(_playerInput);
|
2024-10-29 06:45:18 +00:00
|
|
|
yield return new WaitUntil(() => _playerInput.IsInitialized());
|
|
|
|
|
|
|
|
PlayerInputKeyManager.Instance.DisableAllActionMaps();
|
2024-07-10 00:22:34 +00:00
|
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.Tycoon);
|
2024-10-29 06:45:18 +00:00
|
|
|
EventManager.InvokeInitializedPlayerInput();
|
2024-07-10 00:22:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 11:07:07 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Player input methods
|
|
|
|
#region Player input methods
|
|
|
|
|
2024-06-07 17:31:08 +00:00
|
|
|
// Tycoon
|
2024-06-06 11:07:07 +00:00
|
|
|
public void OnMove(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
var movementInput = context.ReadValue<Vector2>();
|
|
|
|
OnMoveInputReceived?.Invoke(movementInput);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnInteraction(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
if (context.performed)
|
|
|
|
{
|
2024-07-02 18:27:56 +00:00
|
|
|
_closestInteraction?.Interaction();
|
2024-06-07 17:31:08 +00:00
|
|
|
}
|
2024-09-09 12:27:15 +00:00
|
|
|
else if (context.canceled)
|
|
|
|
{
|
|
|
|
_closestInteraction?.CancelInteraction();
|
|
|
|
}
|
2024-06-07 17:31:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 16:20:39 +00:00
|
|
|
public void OnDevelopKey01(InputAction.CallbackContext context)
|
2024-06-07 17:31:08 +00:00
|
|
|
{
|
|
|
|
if (context.performed)
|
|
|
|
{
|
2024-10-22 12:41:31 +00:00
|
|
|
EventManager.InvokeCreateServerCrew();
|
|
|
|
EventManager.InvokeCreateCleanerCrew();
|
|
|
|
EventManager.InvokeCreateBartenderCrew();
|
2024-06-07 17:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 09:09:18 +00:00
|
|
|
public void OnManual(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
if (context.performed)
|
|
|
|
{
|
2024-10-29 06:45:18 +00:00
|
|
|
TycoonUiManager.Instance.ManualBook.Open();
|
2024-10-28 09:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-07 17:31:08 +00:00
|
|
|
// TycoonUi
|
|
|
|
public void OnCancel(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
if (context.performed)
|
|
|
|
{
|
2024-10-29 06:45:18 +00:00
|
|
|
PopupUiController.CloseLastPopup();
|
2024-06-06 11:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 09:09:18 +00:00
|
|
|
public void OnCancelManual(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
if (context.performed)
|
|
|
|
{
|
|
|
|
TycoonUiManager.Instance.ManualBook.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 10:52:35 +00:00
|
|
|
// Bar
|
|
|
|
public void OnPour(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
var bar = FindAnyObjectByType<Bar>();
|
|
|
|
if (!bar) return;
|
|
|
|
|
|
|
|
if (context.performed)
|
|
|
|
{
|
|
|
|
bar.ActiveIsPouring();
|
|
|
|
}
|
|
|
|
else if (context.canceled)
|
|
|
|
{
|
|
|
|
bar.InActiveIsPouring();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 11:07:07 +00:00
|
|
|
#endregion
|
2024-07-02 18:27:56 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
2024-07-08 20:06:22 +00:00
|
|
|
if (!interaction.CanInteraction()) continue;
|
|
|
|
|
2024-07-20 11:32:54 +00:00
|
|
|
var distance = Vector3.Distance(transform.position, interaction.CenterTransform.position);
|
2024-07-16 16:05:53 +00:00
|
|
|
if (distance > interaction.InteractionRadius || distance >= closestDistance) continue;
|
2024-07-02 18:27:56 +00:00
|
|
|
|
|
|
|
closestDistance = distance;
|
|
|
|
closestInteraction = interaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return closestInteraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2024-06-06 11:07:07 +00:00
|
|
|
}
|
|
|
|
}
|