Files
ARPGCastleDefence/Assets/Scripts/Input/PlayerInputReader.cs

192 lines
6.1 KiB
C#
Raw Permalink Normal View History

2026-03-31 17:04:13 +01:00
using System;
using EasyTalk.Controller;
using UnityEngine;
using UnityEngine.InputSystem;
namespace ActionRPG.Input
{
public class PlayerInputReader : MonoBehaviour, InputSystem_Actions.IPlayerActions
{
// ── Raw values ────────────────────────────────────────────────
public Vector2 MoveInput { get; private set; }
public Vector2 LookInput { get; private set; }
// ── Dialogue state ────────────────────────────────────────────
public bool IsInDialogue { get; private set; }
// ── Movement events ───────────────────────────────────────────
public event Action OnJumpPerformed;
public event Action OnSprintStarted;
public event Action OnSprintCanceled;
public event Action OnCrouchStarted;
public event Action OnCrouchCanceled;
// ── Combat events ─────────────────────────────────────────────
public event Action OnAttackPerformed;
public event Action OnKickPerformed;
public event Action OnBlockStarted;
public event Action OnBlockCanceled;
// Aim: add "Aim" Button action to InputSystem_Actions.inputactions, then uncomment:
// public event Action OnAimStarted;
// public event Action OnAimCanceled;
// ── Interaction events ────────────────────────────────────────
public event Action OnInteractStarted; // fires on tap (context.started)
public event Action OnInteractPerformed; // fires after hold threshold met
// ── Navigation / UI events ────────────────────────────────────
public event Action OnNextPerformed;
public event Action OnPreviousPerformed;
// ── Lock-on: add "LockOn" Button action to InputSystem_Actions.inputactions, then uncomment:
// public event Action OnLockOnToggled;
// ── Toggle walk: add "ToggleWalk" Button action to InputSystem_Actions.inputactions, then uncomment:
// public event Action OnWalkToggled;
private InputSystem_Actions _controls;
private void Start()
{
if (DialogueController.Instance != null)
{
DialogueController.Instance.onPlay.AddListener(OnDialogueStarted);
DialogueController.Instance.onStop.AddListener(OnDialogueStopped);
}
}
private void OnDestroy()
{
if (DialogueController.Instance != null)
{
DialogueController.Instance.onPlay.RemoveListener(OnDialogueStarted);
DialogueController.Instance.onStop.RemoveListener(OnDialogueStopped);
}
}
private void OnDialogueStarted() => IsInDialogue = true;
private void OnDialogueStopped() => IsInDialogue = false;
private void OnEnable()
{
if (_controls == null)
{
_controls = new InputSystem_Actions();
_controls.Player.SetCallbacks(this);
}
_controls.Player.Enable();
}
private void OnDisable()
{
_controls.Player.Disable();
}
// ── InputSystem_Actions.IPlayerActions ────────────────────────
public void OnMove(InputAction.CallbackContext context)
{
MoveInput = context.ReadValue<Vector2>();
}
public void OnLook(InputAction.CallbackContext context)
{
LookInput = context.ReadValue<Vector2>();
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed && !IsInDialogue)
{
OnJumpPerformed?.Invoke();
}
}
public void OnSprint(InputAction.CallbackContext context)
{
if (context.started)
{
OnSprintStarted?.Invoke();
}
else if (context.canceled)
{
OnSprintCanceled?.Invoke();
}
}
public void OnCrouch(InputAction.CallbackContext context)
{
if (context.started)
{
OnCrouchStarted?.Invoke();
}
else if (context.canceled)
{
OnCrouchCanceled?.Invoke();
}
}
public void OnAttack(InputAction.CallbackContext context)
{
if (context.performed)
{
OnAttackPerformed?.Invoke();
}
}
public void OnInteract(InputAction.CallbackContext context)
{
if (context.started)
{
OnInteractStarted?.Invoke();
}
else if (context.performed)
{
OnInteractPerformed?.Invoke();
}
}
public void OnNext(InputAction.CallbackContext context)
{
if (context.performed)
{
OnNextPerformed?.Invoke();
}
}
public void OnPrevious(InputAction.CallbackContext context)
{
if (context.performed)
{
OnPreviousPerformed?.Invoke();
}
}
public void OnKick(InputAction.CallbackContext context)
{
if (context.performed)
{
OnKickPerformed?.Invoke();
}
}
public void OnBlock(InputAction.CallbackContext context)
{
if (context.started)
{
OnBlockStarted?.Invoke();
}
else if (context.canceled)
{
OnBlockCanceled?.Invoke();
}
}
}
}