quakeball/Assets/Scripts/Player/LocalPlayer.cs

79 lines
3.1 KiB
C#
Raw Normal View History

2020-08-02 02:52:38 +02:00
using UnityEngine;
using UnityEngine.InputSystem;
namespace NeonTea.Quakeball.Player {
/// <summary>A controller class for a local player. Handles input and updates the relevant components.</summary>
[RequireComponent(typeof(Player))]
public class LocalPlayer : MonoBehaviour {
public Transform Camera;
2020-08-06 19:58:10 +02:00
public float PlayerUpdateFrequency = 1;
2020-08-06 23:37:23 +02:00
public bool DisableInput = false;
2020-08-02 02:52:38 +02:00
private float Lean = 0;
2020-08-02 02:52:38 +02:00
private Player Player;
private InputAction LookAction;
private InputAction MoveAction;
private InputAction CrouchAction;
private InputAction JumpAction;
2020-08-02 02:52:38 +02:00
2020-08-06 19:58:10 +02:00
private float PreviousPlayerUpdate = -1;
private bool WantsToJump = false;
2020-08-02 02:52:38 +02:00
private void Awake() {
Player = GetComponent<Player>();
CrouchAction = new InputAction("crouch", binding: "<Gamepad>/buttonEast");
CrouchAction.AddBinding("<Keyboard>/leftShift");
2020-08-02 02:52:38 +02:00
CrouchAction.Enable();
JumpAction = new InputAction("crouch", binding: "<Gamepad>/buttonSouth");
JumpAction.AddBinding("<Keyboard>/space");
2020-08-06 19:58:10 +02:00
JumpAction.performed += _ => {
WantsToJump = true;
};
JumpAction.Enable();
2020-08-02 02:52:38 +02:00
LookAction = new InputAction("look", binding: "<Gamepad>/leftStick");
LookAction.AddBinding("<Mouse>/delta");
LookAction.Enable();
MoveAction = new InputAction("move", binding: "<Gamepad>/rightStick");
MoveAction.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
MoveAction.Enable();
}
2020-08-02 02:52:38 +02:00
private void Start() {
2020-08-02 02:52:38 +02:00
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
2020-08-02 02:52:38 +02:00
}
private void Update() {
OptionsData Opts = Options.Get();
Vector2 LookInput = LookAction.ReadValue<Vector2>() * Opts.LookSensitivity;
Player.Pitch = Mathf.Clamp(Player.Pitch - LookInput.y * (Opts.InvertVerticalLook ? -1 : 1), -90, 90);
Player.Yaw += LookInput.x;
float TargetLean = -Vector3.Dot(Player.GroundVelocity / Player.MoveStyle.TargetVelocity, Camera.right) * Player.MoveStyle.LeanDegrees;
2020-08-05 20:07:51 +02:00
Lean = Mathf.Lerp(Lean, TargetLean, 30f * Time.deltaTime);
Camera.localEulerAngles = new Vector3(Player.Pitch, Player.Yaw, Lean);
2020-08-06 19:58:10 +02:00
if (Time.time - PreviousPlayerUpdate >= 1f / PlayerUpdateFrequency) {
Vector2 MovementInput = MoveAction.ReadValue<Vector2>();
Vector3 Move = new Vector3(MovementInput.x, 0, MovementInput.y);
Move = Quaternion.Euler(0, Player.Yaw, 0) * Move;
Player.MoveDirection = Move;
Player.CurrentMoveStyle = CrouchAction.ReadValue<float>() > 0 ? (byte)1 : (byte)0;
Player.Jumping = WantsToJump;
PreviousPlayerUpdate = Time.time;
WantsToJump = false;
}
2020-08-02 02:52:38 +02:00
}
}
}