Files
PacManUdon/Assets/Scripts/PlayerInput.cs
2026-06-22 13:35:32 +02:00

216 lines
6.0 KiB
C#

namespace Marro.PacManUdon
{
using System;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon.Common;
enum InputMethod
{
KeyboardMouse,
Other,
}
public class PlayerInput : SyncedObject
{
public bool active;
private GameManager gameManager;
private VRCPlayerApi player;
private InputMethod inputMethod;
private Direction resultInput;
private Direction inputHorizontal;
private Direction inputVertical;
private float horizontalValue;
private float verticalValue;
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;
SubscribeToEvent(NetworkEventType.PacManTurn);
}
public void Activate()
{
Debug.Log($"{gameObject} got activated, {player} was immobilized");
active = true;
player.SetWalkSpeed(0);
player.SetRunSpeed(0);
player.SetStrafeSpeed(0);
gameManager.JoystickGrabbed();
}
public void Deactivate()
{
Debug.Log($"{gameObject} got deactivated, {player} was released");
player.SetWalkSpeed(2);
player.SetRunSpeed(4);
player.SetStrafeSpeed(2);
active = false;
gameManager.JoystickReleased();
}
public override void InputJump(bool pressed, UdonInputEventArgs args)
{
if (!active)
{
return;
}
if (pressed)
{
Deactivate();
}
}
public override void InputMoveHorizontal(float value, UdonInputEventArgs args)
{
if (!active)
{
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);
}
public override void InputMoveVertical(float value, UdonInputEventArgs args)
{
if (!active)
{
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);
}
public override void OnInputMethodChanged(VRCInputMethod inputMethod)
{
switch (inputMethod)
{
case VRCInputMethod.Keyboard:
case VRCInputMethod.Mouse:
this.inputMethod = InputMethod.KeyboardMouse;
return;
default:
this.inputMethod = InputMethod.Other;
return;
}
}
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);
}
public Direction GetDirection()
{
return resultInput;
}
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
{
return;
}
ByteUtils.AppendAsByte(data, (int)resultInput, ref index);
}
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
{
return true;
}
resultInput = (Direction)ByteUtils.ReadByte(data, ref index);
return true;
}
}
}