This commit is contained in:
unknown
2026-04-15 20:04:28 +05:00
parent 0837589fa2
commit 057f5d3d1e
6 changed files with 198 additions and 45 deletions

View File

@@ -0,0 +1,79 @@
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);
}
}
}
}