Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Woah!! All this just for more accurate deadzones??
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Nov 19, 2023
1 parent b755586 commit dcc613f
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 111 deletions.
14 changes: 14 additions & 0 deletions Client/ClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class General {
public double _deadzoneRight = 0;
[DataMember(Name = "deadzone_left")]
public double _deadzoneLeft = 0;
[DataMember(Name = "snap_right")]
public double _snapRight = 0.25;
[DataMember(Name = "snap_left")]
public double _snapLeft = 0.25;

[DataMember(Name = "fov")]
public float _fov = 45;
Expand All @@ -52,6 +56,16 @@ public static double deadzoneLeft {
get => instance.general._deadzoneLeft;
set => instance.general._deadzoneLeft = value;
}

public static double snapRight {
get => instance.general._snapRight;
set => instance.general._snapRight = value;
}

public static double snapLeft {
get => instance.general._snapLeft;
set => instance.general._snapLeft = value;
}

public static float fov {
get => instance.general._fov;
Expand Down
160 changes: 115 additions & 45 deletions Client/Keybinding/Button.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// TODO: Separate this out into different files ffs
// I have no clue why we haven't done this yet lmao
// But right now it's more work than it's worth

using System;
using System.Collections.Generic;
using GlmSharp;
Expand All @@ -17,7 +21,8 @@ public abstract class Button {
"Key" => KeyButton.FromString(second),
"Mouse" => MouseButton.FromString(second),
"Button" => ControllerButton.FromString(second),
"Axis" => ControllerAxisButton.FromString(second),
"Trigger" => ControllerTriggerButton.FromString(second),
"Joystick" => ControllerJoystickButton.FromString(second),
_ => null,
};
}
Expand All @@ -26,6 +31,8 @@ public abstract class Button {

public virtual double strength => isPressed ? 1 : 0;

public virtual dvec2 axis => new(strength, 0);

public abstract override string ToString();
}

Expand Down Expand Up @@ -96,71 +103,134 @@ public static ControllerButton Get(GamepadButton button) {

public override bool isPressed => VoxelClient.Instance.InputManager.IsButtonPressed(Button);

public ControllerButton(GamepadButton button) {
private ControllerButton(GamepadButton button) {
Button = button;
}

public override string ToString()
=> $"Button.{Button}";
}

public class ControllerAxisButton : Button {
private static readonly Dictionary<GamepadAxis, ControllerAxisButton> Cache = new();
public class ControllerTriggerButton : Button {
private static readonly Dictionary<GamepadTrigger, ControllerTriggerButton> Cache = new();

public new static ControllerTriggerButton? FromString(string value)
=> Enum.TryParse(value, out GamepadTrigger trigger) ? Get(trigger) : null;

public new static ControllerAxisButton? FromString(string value)
=> Enum.TryParse(value, out GamepadAxis axis) ? Get(axis) : null;
public static ControllerTriggerButton Get(GamepadTrigger gamepadTrigger) {
if (!Cache.ContainsKey(gamepadTrigger))
Cache[gamepadTrigger] = new(gamepadTrigger);

return Cache[gamepadTrigger];
}

public readonly GamepadTrigger Trigger;

public override double strength => VoxelClient.Instance.InputManager.GetAxisStrength(Trigger.GetAxis());

public static ControllerAxisButton Get(GamepadAxis axis) {
public override bool isPressed => strength > 0.25;

private ControllerTriggerButton(GamepadTrigger trigger) {
Trigger = trigger;
}

public override string ToString()
=> $"Trigger.{Trigger}";

public enum GamepadTrigger {
Left,
Right
}
}

public class ControllerJoystickButton : Button {
private static readonly Dictionary<GamepadJoystick, ControllerJoystickButton> Cache = new();

public new static ControllerJoystickButton? FromString(string value)
=> Enum.TryParse(value, out GamepadJoystick axis) ? Get(axis) : null;

public static ControllerJoystickButton Get(GamepadJoystick axis) {
if (!Cache.ContainsKey(axis))
Cache[axis] = new(axis);

return Cache[axis];
}

public readonly GamepadAxis Axis;
public readonly GamepadJoystick Joystick;

public override double strength => GetAxisStrength(Axis);
public override bool isPressed => GetAxisStrength().LengthSqr > 0;

public override bool isPressed => strength > 0.25;
public override double strength => GetAxisStrength().Length;

public ControllerAxisButton(GamepadAxis axis) {
Axis = axis;
}
public override dvec2 axis => GetAxisStrength();

private ControllerJoystickButton(GamepadJoystick joystick) {
Joystick = joystick;
}

public override string ToString()
=> $"Axis.{Axis}";

private static double GetAxisStrength(GamepadAxis axis) {
var inputManager = VoxelClient.Instance.InputManager;
if (axis == GamepadAxis.RightX || axis == GamepadAxis.RightY) {
int i = (int)axis - 2;
var vec = new dvec2(
inputManager.GetAxisStrength(GamepadAxis.RightX),
inputManager.GetAxisStrength(GamepadAxis.RightY)
);
if (vec.Length < ClientConfig.General.deadzoneRight)
return 0;

if (vec[i] < ClientConfig.General.deadzoneRight * 0.5)
return 0;

return vec[i];
}
if (axis == GamepadAxis.LeftX || axis == GamepadAxis.LeftY) {
int i = (int)axis;
var vec = new dvec2(
inputManager.GetAxisStrength(GamepadAxis.LeftX),
inputManager.GetAxisStrength(GamepadAxis.LeftY)
);
if (vec.Length < ClientConfig.General.deadzoneLeft)
return 0;

if (vec[i] < ClientConfig.General.deadzoneLeft * 0.5)
return 0;

return vec[i];
=> $"Joystick.{Joystick}";

private dvec2 GetAxisStrength() {
double deadzone;
double snap;
if (Joystick == GamepadJoystick.Left) {
deadzone = ClientConfig.General.deadzoneLeft;
snap = ClientConfig.General.snapLeft;
} else {
deadzone = ClientConfig.General.deadzoneRight;
snap = ClientConfig.General.snapRight;
}

return inputManager.GetAxisStrength(axis);
var GetAxisStrength = VoxelClient.Instance.InputManager.GetAxisStrength;

var vec = new dvec2(
GetAxisStrength(Joystick.GetAxisX()),
GetAxisStrength(Joystick.GetAxisY())
);

for (int i = 0; i < 2; i++)
if (Math.Abs(vec[i]) < snap)
vec[i] = 0;

if (deadzone <= 0)
return vec;
if (deadzone > 1)
return new(0);

if (vec.LengthSqr < deadzone * deadzone)
return new(0);

vec -= (dvec2) dvec2.Sign(vec) * deadzone;

vec *= 1 / (1 - deadzone);

return vec;
}

public enum GamepadJoystick {
Left,
Right
}
}

public static class AxisExtensions {
public static GamepadAxis GetAxis(this ControllerTriggerButton.GamepadTrigger gamepadTrigger)
=> gamepadTrigger switch {
ControllerTriggerButton.GamepadTrigger.Left => GamepadAxis.LeftTrigger,
ControllerTriggerButton.GamepadTrigger.Right => GamepadAxis.RightTrigger,
_ => GamepadAxis.Invalid
};
public static GamepadAxis GetAxisX(this ControllerJoystickButton.GamepadJoystick gamepadTrigger)
=> gamepadTrigger switch {
ControllerJoystickButton.GamepadJoystick.Left => GamepadAxis.LeftX,
ControllerJoystickButton.GamepadJoystick.Right => GamepadAxis.RightX,
_ => GamepadAxis.Invalid
};
public static GamepadAxis GetAxisY(this ControllerJoystickButton.GamepadJoystick gamepadTrigger)
=> gamepadTrigger switch {
ControllerJoystickButton.GamepadJoystick.Left => GamepadAxis.LeftY,
ControllerJoystickButton.GamepadJoystick.Right => GamepadAxis.RightY,
_ => GamepadAxis.Invalid
};
}
6 changes: 6 additions & 0 deletions Client/Keybinding/Keybind.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GlmSharp;

namespace Voxel.Client.Keybinding;

Expand All @@ -17,6 +18,11 @@ public class Keybind {

public double strength => currentButtons.Aggregate<Button, double>(0, (current, button) => Math.Max(current, button.strength));

public dvec2 axis => currentButtons.Aggregate<Button, dvec2>(new(0, 0), (current, button) => {
var buttonAxis = button.axis;
return current.LengthSqr > buttonAxis.LengthSqr ? current : buttonAxis;
});

public Keybind(string name, params Button[] defaultButtons) {
DefaultButtons = defaultButtons;
currentButtons = defaultButtons.ToList();
Expand Down
38 changes: 20 additions & 18 deletions Client/Keybinding/Keybinds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,29 @@ public static class Keybinds {
ControllerButton.Get(GamepadButton.Start)
);

public static readonly Keybind Move = new(
"movement.full",
ControllerJoystickButton.Get(ControllerJoystickButton.GamepadJoystick.Left)
);

public static readonly Keybind Forward = new(
"movement.forward",
KeyButton.Get(Key.W),
ControllerAxisButton.Get(GamepadAxis.LeftYNegative)
KeyButton.Get(Key.W)
);

public static readonly Keybind Backward = new(
"movement.backward",
KeyButton.Get(Key.S),
ControllerAxisButton.Get(GamepadAxis.LeftYPositive)
KeyButton.Get(Key.S)
);

public static readonly Keybind StrafeLeft = new(
"movement.strafe.left",
KeyButton.Get(Key.A),
ControllerAxisButton.Get(GamepadAxis.LeftXNegative)
KeyButton.Get(Key.A)
);

public static readonly Keybind StrafeRight = new(
"movement.strafe.right",
KeyButton.Get(Key.D),
ControllerAxisButton.Get(GamepadAxis.LeftXPositive)
KeyButton.Get(Key.D)
);

public static readonly Keybind Jump = new(
Expand All @@ -49,41 +50,42 @@ public static class Keybinds {
KeyButton.Get(Key.ShiftLeft),
ControllerButton.Get(GamepadButton.RightStick)
);

public static readonly Keybind Look = new(
"camera.full",
ControllerJoystickButton.Get(ControllerJoystickButton.GamepadJoystick.Right)
);

public static readonly Keybind LookUp = new(
"camera.up",
KeyButton.Get(Key.Up),
ControllerAxisButton.Get(GamepadAxis.RightYNegative)
KeyButton.Get(Key.Up)
);

public static readonly Keybind LookDown = new(
"camera.down",
KeyButton.Get(Key.Down),
ControllerAxisButton.Get(GamepadAxis.RightYPositive)
KeyButton.Get(Key.Down)
);

public static readonly Keybind LookLeft = new(
"camera.left",
KeyButton.Get(Key.Left),
ControllerAxisButton.Get(GamepadAxis.RightXNegative)
KeyButton.Get(Key.Left)
);

public static readonly Keybind LookRight = new(
"camera.right",
KeyButton.Get(Key.Right),
ControllerAxisButton.Get(GamepadAxis.RightXPositive)
KeyButton.Get(Key.Right)
);

public static readonly Keybind Use = new(
"action.use",
MouseButton.Get(VMouseButton.Right),
ControllerAxisButton.Get(GamepadAxis.LeftTrigger)
ControllerTriggerButton.Get(ControllerTriggerButton.GamepadTrigger.Left)
);

public static readonly Keybind Attack = new(
"action.attack",
MouseButton.Get(VMouseButton.Left),
ControllerAxisButton.Get(GamepadAxis.RightTrigger)
ControllerTriggerButton.Get(ControllerTriggerButton.GamepadTrigger.Right)
);

public static readonly Keybind Refresh = new(
Expand Down
4 changes: 3 additions & 1 deletion Client/Rendering/CameraStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public void SetToCamera(Camera c, double timeSinceLastTick) {
currentCameraPosition = dvec3.Lerp(c.oldPosition, c.position, deltaTicks);

var data = new CameraData();
var cRotation = c.oldRotation == c.rotation ? c.rotation : quat.SLerp(c.oldRotation, c.rotation, (float)deltaTicks);
var cRotation = quat.SLerp(c.oldRotation, c.rotation, (float)deltaTicks);
if (quat.IsNaN(cRotation).Any)
cRotation = c.rotation;
data.VPMatrix = cRotation.ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed;
CameraBuffer.value = data;
}
Expand Down
10 changes: 9 additions & 1 deletion Client/VoxelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public override void OnTick() {
inputDir.y -= 1;
if (Keybinds.Jump.isPressed)
inputDir.y += 1;

var move = Keybinds.Move.axis;

inputDir.x += move.x;
inputDir.z += move.y;

if (Keybinds.Refresh.isPressed)
GameRenderer.WorldRenderer.ChunkRenderer.Reload();
Expand All @@ -78,7 +83,10 @@ public override void OnTick() {
if (Keybinds.LookUp.isPressed)
camera.rotationVec.x += 0.125f * (float)Keybinds.LookUp.strength;
if (Keybinds.LookDown.isPressed)
camera.rotationVec.x -= 0.25f * (float)Keybinds.LookDown.strength;
camera.rotationVec.x -= 0.125f * (float)Keybinds.LookDown.strength;

camera.rotationVec += -(vec2)Keybinds.Look.axis.swizzle.yx * 0.125f;

if (camera.rotationVec.x < -MathF.PI/2)
camera.rotationVec.x = -MathF.PI/2;
if (camera.rotationVec.x > MathF.PI/2)
Expand Down
8 changes: 0 additions & 8 deletions Core/Input/Gamepad/Buttons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ public enum GamepadAxis {
LeftTrigger,
RightTrigger,
Max,
LeftXPositive,
LeftXNegative,
LeftYPositive,
LeftYNegative,
RightXPositive,
RightXNegative,
RightYPositive,
RightYNegative
}

public static class ButtonsExtensions {
Expand Down
Loading

0 comments on commit dcc613f

Please sign in to comment.