quakeball/Assets/Scripts/Players/LocalPlayer.cs

125 lines
5.0 KiB
C#
Raw Normal View History

2020-08-02 02:52:38 +02:00
using UnityEngine;
using UnityEngine.InputSystem;
2020-08-08 01:14:23 +02:00
using NeonTea.Quakeball.Interface;
using NeonTea.Quakeball.Networking;
using NeonTea.Quakeball.Networking.Packets;
2020-08-02 02:52:38 +02:00
2020-08-07 20:23:21 +02:00
namespace NeonTea.Quakeball.Players {
2020-08-02 02:52:38 +02:00
/// <summary>A controller class for a local player. Handles input and updates the relevant components.</summary>
[RequireComponent(typeof(Player))]
public class LocalPlayer : MonoBehaviour {
2020-08-06 23:37:23 +02:00
public bool DisableInput = false;
2020-08-07 23:59:51 +02:00
public float FullSyncFrequency = 1;
2020-08-02 02:52:38 +02:00
public Player Player { private set; get; }
2020-08-06 21:58:31 +02:00
public InputAction LookAction { private set; get; }
2020-08-02 02:52:38 +02:00
private InputAction MoveAction;
private InputAction CrouchAction;
private InputAction JumpAction;
2020-08-08 04:26:17 +02:00
private InputAction ShootAction;
2020-08-02 02:52:38 +02:00
2020-08-06 19:58:10 +02:00
private float PreviousPlayerUpdate = -1;
2020-08-07 23:59:51 +02:00
private float PreviousPlayerFullSync = -1;
2020-08-06 19:58:10 +02:00
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
JumpAction = new InputAction("crouch", binding: "<Gamepad>/buttonSouth");
JumpAction.AddBinding("<Keyboard>/space");
2020-08-06 19:58:10 +02:00
JumpAction.performed += _ => {
WantsToJump = true;
};
2020-08-02 02:52:38 +02:00
LookAction = new InputAction("look", binding: "<Gamepad>/leftStick");
LookAction.AddBinding("<Mouse>/delta");
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");
2020-08-08 01:14:23 +02:00
2020-08-08 04:26:17 +02:00
ShootAction = new InputAction("Shoot", binding: "<Mouse>/leftButton");
ShootAction.performed += _ => {
2020-08-08 05:20:23 +02:00
if (Networking.Net.Singleton.Instance != null) {
ShootData shootData = new ShootData(Player.Pitch, Player.Yaw, Player.CreateSyncPacket());
Networking.Net.Singleton.Instance.LocalPlayerAction(PlayerAction.Shoot, shootData);
}
2020-08-08 04:26:17 +02:00
Player.Shoot();
};
2020-08-08 01:14:23 +02:00
Terminal.Singleton.RegisterCommand("tp", args => {
if (args.Length != 3) {
return false;
}
transform.position = new Vector3(float.Parse(args[0]), float.Parse(args[1]), float.Parse(args[2]));
return true;
}, "tp x y z - Teleports the local player to the specified coordinates.");
Terminal.Singleton.RegisterCommand("hit", args => {
Player.Hit();
return true;
});
2020-08-02 02:52:38 +02:00
}
private void Update() {
2020-08-06 23:42:39 +02:00
if (DisableInput) {
CrouchAction.Disable();
JumpAction.Disable();
LookAction.Disable();
MoveAction.Disable();
2020-08-08 04:26:17 +02:00
ShootAction.Disable();
2020-08-06 23:42:39 +02:00
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
} else {
CrouchAction.Enable();
JumpAction.Enable();
LookAction.Enable();
MoveAction.Enable();
2020-08-08 04:26:17 +02:00
ShootAction.Enable();
2020-08-06 23:42:39 +02:00
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
2020-08-02 02:52:38 +02:00
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;
2020-08-06 19:58:10 +02:00
if (Time.time - PreviousPlayerUpdate >= 1f / Player.UpdateFrequency) {
PreviousPlayerUpdate = Time.time;
2020-08-06 19:58:10 +02:00
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;
if (Networking.Net.Singleton.Instance != null) {
Networking.Net.Singleton.Instance.UpdateLocalPlayer();
2020-08-07 05:24:46 +02:00
}
2020-08-08 01:16:51 +02:00
if (WantsToJump) {
bool Jumped = Player.Jump();
if (Jumped && Networking.Net.Singleton.Instance != null) {
Networking.Net.Singleton.Instance.LocalPlayerAction(PlayerAction.Jump);
2020-08-08 01:16:51 +02:00
}
}
WantsToJump = false;
2020-08-06 19:58:10 +02:00
}
2020-08-07 23:59:51 +02:00
if (Time.time - PreviousPlayerFullSync >= 1f / FullSyncFrequency) {
2020-08-08 00:44:07 +02:00
PreviousPlayerFullSync = Time.time;
if (Networking.Net.Singleton.Instance != null) {
Networking.Net.Singleton.Instance.SendPlayerSync();
}
2020-08-07 23:59:51 +02:00
}
2020-08-02 02:52:38 +02:00
}
}
}