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
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public struct PlayerInputState
|
|
|
|
{
|
|
|
|
public BufferedInput Jump;
|
|
|
|
public BufferedInput Use;
|
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-01-27 12:27:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerInputState InputState => _state;
|
|
|
|
|
|
|
|
private PlayerInputState _state;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
_state = new PlayerInputState();
|
|
|
|
|
2021-02-14 16:08:59 +01:00
|
|
|
_state.Jump = new BufferedInput(_inputSettings.JumpBufferTime);
|
|
|
|
_state.Use = new BufferedInput(_inputSettings.UseBufferTime);
|
2021-01-27 13:06:17 +01:00
|
|
|
_state.Move = new CameraRelativeInput();
|
2021-01-27 12:27:56 +01:00
|
|
|
_state.Look = Vector2.zero;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
float deltaTime = Time.fixedDeltaTime;
|
|
|
|
|
2021-02-14 16:08:59 +01:00
|
|
|
_state.Jump.Update(deltaTime);
|
|
|
|
_state.Use.Update(deltaTime);
|
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-14 16:08:59 +01:00
|
|
|
public void Jump(InputAction.CallbackContext context)
|
|
|
|
=> _state.Jump.Set(context.ReadValue<float>() > 0.5f);
|
2021-01-27 12:27:56 +01:00
|
|
|
|
2021-02-14 16:08:59 +01:00
|
|
|
public void Use(InputAction.CallbackContext context)
|
|
|
|
{
|
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-01-27 12:27:56 +01:00
|
|
|
}
|