53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.InputSystem;
|
||
|
|
||
|
public class PlayerInputHandler : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField]
|
||
|
private PlayerInputSettings _inputSettings;
|
||
|
|
||
|
public struct PlayerInputState
|
||
|
{
|
||
|
public BufferedInput Jump;
|
||
|
public BufferedInput Use;
|
||
|
public Vector2 Move;
|
||
|
public Vector2 Look;
|
||
|
}
|
||
|
|
||
|
public PlayerInputState InputState => _state;
|
||
|
|
||
|
private PlayerInputState _state;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
_state = new PlayerInputState();
|
||
|
|
||
|
_state.Jump = new BufferedInput( _inputSettings.JumpBufferTime );
|
||
|
_state.Use = new BufferedInput( _inputSettings.UseBufferTime );
|
||
|
_state.Look = Vector2.zero;
|
||
|
_state.Move = Vector2.zero;
|
||
|
}
|
||
|
|
||
|
private void FixedUpdate()
|
||
|
{
|
||
|
float deltaTime = Time.fixedDeltaTime;
|
||
|
|
||
|
_state.Jump.Update( deltaTime );
|
||
|
_state.Use.Update( deltaTime );
|
||
|
}
|
||
|
|
||
|
public void Look( InputAction.CallbackContext context )
|
||
|
=> _state.Look = context.ReadValue<Vector2>();
|
||
|
|
||
|
public void Move( InputAction.CallbackContext context )
|
||
|
=> _state.Move = context.ReadValue<Vector2>();
|
||
|
|
||
|
public void Jump( InputAction.CallbackContext context )
|
||
|
=> _state.Jump.Set( context.ReadValue<float>() > 0.5f );
|
||
|
|
||
|
public void Use( InputAction.CallbackContext context )
|
||
|
=> _state.Use.Set( context.ReadValue<float>() > 0.5f );
|
||
|
}
|