2021-01-27 12:27:56 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
2021-02-19 21:38:45 +01:00
|
|
|
using Ktyl.Util;
|
2021-01-27 12:27:56 +01:00
|
|
|
|
2021-03-11 21:25:09 +01:00
|
|
|
[RequireComponent(typeof(PlayerInput))]
|
2021-01-27 12:27:56 +01:00
|
|
|
public class PlayerInputHandler : MonoBehaviour
|
|
|
|
{
|
2021-02-14 16:08:59 +01:00
|
|
|
//to get the artifact id you are near to
|
2021-02-19 21:38:45 +01:00
|
|
|
[SerializeField] private SerialInt _nearbyArtefactID;
|
2021-02-14 16:08:59 +01:00
|
|
|
|
2021-01-27 12:27:56 +01:00
|
|
|
[SerializeField]
|
|
|
|
private PlayerInputSettings _inputSettings;
|
|
|
|
|
2021-03-02 19:28:13 +01:00
|
|
|
[SerializeField]
|
|
|
|
private Camera _camera;
|
|
|
|
|
2021-03-11 21:25:09 +01:00
|
|
|
[SerializeField] private DialogueSystem _dialogue;
|
|
|
|
|
2021-02-15 19:09:32 +01:00
|
|
|
public class PlayerInputState
|
2021-01-27 12:27:56 +01:00
|
|
|
{
|
|
|
|
public BufferedInput Jump;
|
2021-02-15 19:09:32 +01:00
|
|
|
public BufferedInput Blink;
|
2021-01-27 12:27:56 +01:00
|
|
|
public BufferedInput Use;
|
2021-03-08 15:16:20 +01:00
|
|
|
public BufferedInput Freeze;
|
2021-01-27 13:06:17 +01:00
|
|
|
public CameraRelativeInput Move;
|
2021-01-27 12:27:56 +01:00
|
|
|
public Vector2 Look;
|
2021-01-27 13:06:17 +01:00
|
|
|
public float MoveRotation;
|
2021-02-15 19:09:32 +01:00
|
|
|
|
2021-03-08 15:16:20 +01:00
|
|
|
public PlayerInputState( float jumpBuffer, float blinkBuffer, float useBuffer , float freezeBuffer)
|
2021-02-15 19:09:32 +01:00
|
|
|
{
|
|
|
|
Jump = new BufferedInput( jumpBuffer );
|
|
|
|
Blink = new BufferedInput( blinkBuffer );
|
|
|
|
Use = new BufferedInput( useBuffer );
|
2021-03-08 15:16:20 +01:00
|
|
|
Freeze = new BufferedInput( freezeBuffer );
|
2021-02-15 19:09:32 +01:00
|
|
|
Move = new CameraRelativeInput();
|
|
|
|
Look = Vector2.zero;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Update( float deltaTime )
|
|
|
|
{
|
|
|
|
Jump.Update(deltaTime);
|
|
|
|
Blink.Update(deltaTime);
|
|
|
|
Use.Update(deltaTime);
|
2021-03-08 15:16:20 +01:00
|
|
|
Freeze.Update(deltaTime);
|
2021-02-15 19:09:32 +01:00
|
|
|
}
|
2021-01-27 12:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerInputState InputState => _state;
|
|
|
|
|
|
|
|
private PlayerInputState _state;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
2021-03-08 15:16:20 +01:00
|
|
|
_state = new PlayerInputState(
|
|
|
|
_inputSettings.JumpBufferTime,
|
|
|
|
_inputSettings.BlinkBufferTime,
|
|
|
|
_inputSettings.UseBufferTime,
|
|
|
|
_inputSettings.FreezeBufferTime
|
|
|
|
) ;
|
2021-03-11 21:25:09 +01:00
|
|
|
|
|
|
|
// run a first time update to ensure the dialogue system has an updated
|
|
|
|
// value for the player's input control scheme
|
|
|
|
_dialogue.UpdateControlPrompts(GetComponent<PlayerInput>());
|
2021-01-27 12:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
2021-03-08 15:16:20 +01:00
|
|
|
_state.Update( Time.fixedDeltaTime);
|
2021-03-02 19:28:13 +01:00
|
|
|
|
|
|
|
float cameraRotation = _camera.transform.rotation.eulerAngles.y;
|
|
|
|
_state.Move.SetAngle(-cameraRotation);
|
2021-01-27 12:27:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:08:59 +01:00
|
|
|
public void SetCameraRotation(float angle)
|
|
|
|
=> _state.Move.SetAngle(angle);
|
2021-01-27 13:06:17 +01:00
|
|
|
|
2021-02-14 16:08:59 +01:00
|
|
|
public void Look(InputAction.CallbackContext context)
|
2021-01-27 12:27:56 +01:00
|
|
|
=> _state.Look = context.ReadValue<Vector2>();
|
|
|
|
|
2021-02-14 16:08:59 +01:00
|
|
|
public void Move(InputAction.CallbackContext context)
|
|
|
|
=> _state.Move.SetValue(context.ReadValue<Vector2>());
|
2021-01-27 12:27:56 +01:00
|
|
|
|
2021-02-15 19:09:32 +01:00
|
|
|
public void Blink( InputAction.CallbackContext context )
|
|
|
|
=> _state.Blink.Set( context.ReadValueAsButton() );
|
|
|
|
|
2021-01-27 12:27:56 +01:00
|
|
|
public void Jump( InputAction.CallbackContext context )
|
2021-02-15 19:09:32 +01:00
|
|
|
=> _state.Jump.Set( context.ReadValueAsButton() );
|
2021-01-27 12:27:56 +01:00
|
|
|
|
2021-02-14 16:08:59 +01:00
|
|
|
public void Use(InputAction.CallbackContext context)
|
|
|
|
{
|
2021-02-22 10:01:33 +01:00
|
|
|
_state.Use.Set( context.ReadValueAsButton() );
|
|
|
|
|
2021-02-19 16:14:14 +01:00
|
|
|
if(context.started)
|
2021-02-19 21:38:45 +01:00
|
|
|
EventHandler.current.ArtefactPickUp(_nearbyArtefactID.Value);
|
2021-02-14 16:08:59 +01:00
|
|
|
}
|
2021-03-08 15:16:20 +01:00
|
|
|
|
|
|
|
public void Freeze(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
_state.Freeze.Set( context.ReadValueAsButton() );
|
|
|
|
|
|
|
|
}
|
2021-01-27 12:27:56 +01:00
|
|
|
}
|