121 lines
3.8 KiB
C#
121 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using BlueWater.Interfaces;
|
|
using BlueWater.Utility;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace BlueWater.Players.Tycoons
|
|
{
|
|
public class TycoonInput : MonoBehaviour
|
|
{
|
|
// variables
|
|
#region variables
|
|
|
|
private List<IPlayerInteraction> _playerInteractions = new();
|
|
private IPlayerInteraction _closestInteraction;
|
|
private IPlayerInteraction _previousInteraction;
|
|
private InputAction _interactionAction;
|
|
private InputAction _developKey01Action;
|
|
|
|
#endregion
|
|
|
|
#region Unity events
|
|
|
|
private void Start()
|
|
{
|
|
_interactionAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.Tycoon, "Interaction");
|
|
_developKey01Action = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.Tycoon, "DevelopKey01");
|
|
|
|
_interactionAction.performed += OnInteraction;
|
|
_interactionAction.canceled += OnInteraction;
|
|
_developKey01Action.performed += OnDevelopKey01;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_closestInteraction = GetClosestInteraction();
|
|
if (_closestInteraction != null)
|
|
{
|
|
_closestInteraction.ShowInteractionUi();
|
|
if (_previousInteraction != null && _closestInteraction != _previousInteraction)
|
|
{
|
|
_previousInteraction.HideInteractionUi();
|
|
}
|
|
_previousInteraction = _closestInteraction;
|
|
}
|
|
else
|
|
{
|
|
_previousInteraction?.CancelInteraction();
|
|
_previousInteraction?.HideInteractionUi();
|
|
_previousInteraction = null;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_interactionAction.performed -= OnInteraction;
|
|
_interactionAction.canceled -= OnInteraction;
|
|
_developKey01Action.performed -= OnDevelopKey01;
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Player input methods
|
|
#region Player input methods
|
|
|
|
// Tycoon
|
|
public void OnInteraction(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
_closestInteraction?.Interaction();
|
|
}
|
|
else if (context.canceled)
|
|
{
|
|
_closestInteraction?.CancelInteraction();
|
|
}
|
|
}
|
|
|
|
public void OnDevelopKey01(InputAction.CallbackContext context)
|
|
{
|
|
EventManager.InvokeCreateServerCrew();
|
|
EventManager.InvokeCreateCleanerCrew();
|
|
EventManager.InvokeCreateBartenderCrew();
|
|
}
|
|
|
|
#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.CenterTransform.position);
|
|
if (distance > interaction.InteractionRadius || distance >= closestDistance) continue;
|
|
|
|
closestDistance = distance;
|
|
closestInteraction = interaction;
|
|
}
|
|
|
|
return closestInteraction;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |