80 lines
2.0 KiB
C#
80 lines
2.0 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
public class PlayerInput : MonoBehaviour
|
|
{
|
|
public static PlayerInput Instance { get; private set; }
|
|
|
|
// Continuous axes
|
|
public float MouseX { get; private set; }
|
|
public float MouseY { get; private set; }
|
|
public float Horizontal { get; private set; }
|
|
public float Vertical { get; private set; }
|
|
|
|
// Continuous states
|
|
public bool Crouch { get; private set; }
|
|
public bool Run { get; private set; }
|
|
|
|
// Events for discrete actions
|
|
public event Action OnToggleInventory;
|
|
public event Action OnDropPressed;
|
|
public event Action OnPickupPressed;
|
|
public event Action OnJumpPressed;
|
|
public event Action<int> OnHotbarSelect;
|
|
|
|
public bool Enabled { get; set; } = true;
|
|
|
|
void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// Always allow toggle inventory (Tab/Escape) even when input is disabled
|
|
if (Input.GetKeyDown(KeyCode.Tab) || Input.GetKeyDown(KeyCode.Escape))
|
|
OnToggleInventory?.Invoke();
|
|
|
|
if (!Enabled) return;
|
|
|
|
// Axes
|
|
MouseX = Input.GetAxis("Mouse X");
|
|
MouseY = Input.GetAxis("Mouse Y");
|
|
Horizontal = Input.GetAxisRaw("Horizontal");
|
|
Vertical = Input.GetAxisRaw("Vertical");
|
|
|
|
// Continuous keys
|
|
Crouch = Input.GetKey(KeyCode.C);
|
|
Run = Input.GetKey(KeyCode.LeftShift);
|
|
|
|
// Discrete actions
|
|
if (Input.GetButtonDown("Jump"))
|
|
OnJumpPressed?.Invoke();
|
|
|
|
if (Input.GetKeyDown(KeyCode.Tab))
|
|
OnToggleInventory?.Invoke();
|
|
|
|
if (Input.GetKeyDown(KeyCode.Q))
|
|
OnDropPressed?.Invoke();
|
|
|
|
if (Input.GetKeyDown(KeyCode.E))
|
|
OnPickupPressed?.Invoke();
|
|
|
|
for (int i = 0; i < 9; i++)
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Alpha1 + i))
|
|
{
|
|
OnHotbarSelect?.Invoke(i);
|
|
}
|
|
}
|
|
}
|
|
}
|