Refactored input

This commit is contained in:
2026-06-23 13:00:48 +02:00
parent 540cfe16ab
commit 378d6bc7a7
8 changed files with 1685 additions and 1555 deletions

View File

@@ -21,22 +21,24 @@
private InputMethod inputMethod;
private Direction resultInput;
private Direction inputHorizontal;
private Direction inputVertical;
private float horizontalValue;
private float verticalValue;
private Vector2 analogInput;
private bool dirty;
private HandType lastUsedHand;
private bool horizontalPriority;
public void Initialize(GameManager gameManager)
{
this.gameManager = gameManager;
resultInput = Direction.Zero;
inputHorizontal = Direction.Zero;
inputVertical = Direction.Zero;
horizontalPriority = false;
player = Networking.LocalPlayer;
inputMethod = InputMethod.KeyboardMouse;
SubscribeToEvent(NetworkEventType.PacManTurn);
resultInput = Direction.Zero;
analogInput = Vector2.zero;
dirty = false;
horizontalPriority = false;
SubscribeToEvent(NetworkEventType.InputChange);
}
void Update()
@@ -67,6 +69,14 @@
}
}
public override void SyncedUpdate()
{
if (dirty) // Update now to ensure input feedback is performed timely
{
UpdateResultInput();
}
}
public void Activate()
{
Debug.Log($"{gameObject} got activated, {player} was immobilized");
@@ -109,36 +119,9 @@
return;
}
var previousInputHorizontal = inputHorizontal;
horizontalValue = Math.Abs(value);
if (value < -0.5)
{
inputHorizontal = Direction.Left;
if (horizontalValue >= verticalValue)
{
SetPriority(true);
}
}
else if (value > 0.5)
{
inputHorizontal = Direction.Right;
if (horizontalValue >= verticalValue)
{
SetPriority(true);
}
}
else
{
inputHorizontal = Direction.Zero;
SetPriority(false);
}
if (previousInputHorizontal != inputHorizontal)
{
UpdateResultInput();
}
// Debug.Log("Horizontal Input Event: " + value + " | Direction: " + direction + " | lastDirection: " + lastDirection + " | Left: " + left + " | Right: " + right);
analogInput.x = value;
lastUsedHand = args.handType;
dirty = true;
}
public override void InputMoveVertical(float value, UdonInputEventArgs args)
@@ -148,37 +131,9 @@
return;
}
var previousInputVertical = inputVertical;
verticalValue = Math.Abs(value);
if (value > 0.5)
{
inputVertical = Direction.Up;
if (verticalValue >= horizontalValue)
{
SetPriority(false);
}
}
else if (value < -0.5)
{
inputVertical = Direction.Down;
if (verticalValue >= horizontalValue)
{
SetPriority(false);
}
}
else
{
inputVertical = Direction.Zero;
SetPriority(true);
}
if (previousInputVertical != inputVertical)
{
UpdateResultInput();
}
// Debug.Log("Vertical Input Event: " + value + " | Direction: " + direction + " | lastDirection : " + lastDirection);
analogInput.y = value;
lastUsedHand = args.handType;
dirty = true;
}
public override void OnInputMethodChanged(VRCInputMethod inputMethod)
@@ -195,33 +150,108 @@
}
}
private void SetPriority(bool horizontalPriority)
{
if (this.horizontalPriority != horizontalPriority)
{
if (inputMethod != InputMethod.KeyboardMouse)
{
player.PlayHapticEventInHand(VRC_Pickup.PickupHand.Left, 0.1f, 0.15f, 75);
}
this.horizontalPriority = horizontalPriority;
}
}
private void UpdateResultInput()
{
resultInput = horizontalPriority ? inputHorizontal : inputVertical;
networkManager.SendEventSoon(NetworkEventType.PacManTurn);
dirty = false;
var newResult = GetResultInput(analogInput);
if (newResult == resultInput)
{
return;
}
Debug.Log($"Switched to input direction {newResult} from analogInput {analogInput} with HorizontalPriority {horizontalPriority}");
resultInput = newResult;
networkManager.SendEventSoon(NetworkEventType.InputChange, true);
PlayHaptics();
}
private void PlayHaptics()
{
if (inputMethod == InputMethod.KeyboardMouse)
{
return;
}
VRC_Pickup.PickupHand hand;
switch (lastUsedHand)
{
case HandType.LEFT:
hand = VRC_Pickup.PickupHand.Left;
break;
case HandType.RIGHT:
hand = VRC_Pickup.PickupHand.Right;
break;
default:
return;
}
player.PlayHapticEventInHand(hand, 0.1f, 0.15f, 75);
}
private Direction GetResultInput(Vector2 analogInput)
{
if (analogInput.magnitude < 0.8)
{
return Direction.Zero;
}
var normalized = analogInput.normalized;
Debug.Log($"Checking analogInput {analogInput} with HorizontalPriority {horizontalPriority}, normalized {normalized}");
const float directionDivider = 0.72f;
if (normalized.x > directionDivider)
{
horizontalPriority = false;
return Direction.Right;
}
else if (normalized.x < -directionDivider)
{
horizontalPriority = false;
return Direction.Left;
}
else if (normalized.y > directionDivider)
{
horizontalPriority = true;
return Direction.Up;
}
else if (normalized.y < -directionDivider)
{
horizontalPriority = true;
return Direction.Down;
}
if (horizontalPriority)
{
if (normalized.x > 0)
{
return Direction.Right;
}
return Direction.Left;
}
if (normalized.y > 0)
{
return Direction.Up;
}
return Direction.Down;
}
public Direction GetDirection()
{
if (dirty) // Update now to reduce input delay
{
UpdateResultInput();
}
return resultInput;
}
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
if (eventType != NetworkEventType.InputChange)
{
return;
}
@@ -231,7 +261,7 @@
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
if (eventType != NetworkEventType.InputChange)
{
return true;
}