revival/game/Assets/Scripts/Player/PlayerInputHandler.cs

73 lines
2.1 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerInputHandler : MonoBehaviour
{
[SerializeField]
private PlayerInputSettings _inputSettings;
2021-02-15 19:09:32 +01:00
public class PlayerInputState
{
public BufferedInput Jump;
2021-02-15 19:09:32 +01:00
public BufferedInput Blink;
public BufferedInput Use;
2021-01-27 13:06:17 +01:00
public CameraRelativeInput Move;
public Vector2 Look;
2021-01-27 13:06:17 +01:00
public float MoveRotation;
2021-02-15 19:09:32 +01:00
public PlayerInputState( float jumpBuffer, float blinkBuffer, float useBuffer )
{
Jump = new BufferedInput( jumpBuffer );
Blink = new BufferedInput( blinkBuffer );
Use = new BufferedInput( useBuffer );
Move = new CameraRelativeInput();
Look = Vector2.zero;
}
public void Update( float deltaTime )
{
Jump.Update(deltaTime);
Blink.Update(deltaTime);
Use.Update(deltaTime);
}
}
public PlayerInputState InputState => _state;
private PlayerInputState _state;
private void Awake()
{
2021-02-15 19:09:32 +01:00
_state = new PlayerInputState(
_inputSettings.JumpBufferTime,
_inputSettings.BlinkBufferTime,
_inputSettings.UseBufferTime
);
}
private void FixedUpdate()
{
2021-02-15 19:09:32 +01:00
_state.Update( Time.fixedDeltaTime );
}
2021-01-27 13:06:17 +01:00
public void SetCameraRotation( float angle )
=> _state.Move.SetAngle( angle );
public void Look( InputAction.CallbackContext context )
=> _state.Look = context.ReadValue<Vector2>();
public void Move( InputAction.CallbackContext context )
2021-01-27 13:06:17 +01:00
=> _state.Move.SetValue( context.ReadValue<Vector2>() );
2021-02-15 19:09:32 +01:00
public void Blink( InputAction.CallbackContext context )
=> _state.Blink.Set( context.ReadValueAsButton() );
public void Jump( InputAction.CallbackContext context )
2021-02-15 19:09:32 +01:00
=> _state.Jump.Set( context.ReadValueAsButton() );
public void Use( InputAction.CallbackContext context )
2021-02-15 19:09:32 +01:00
=> _state.Use.Set( context.ReadValueAsButton() );
}