Files
PacManUdon/Assets/Scripts/PlayerInput.cs

188 lines
5.2 KiB
C#

namespace Marro.PacManUdon
{
using System;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon.Common;
public class PlayerInput : SyncedObject
{
public bool active;
private GameManager gameManager;
Direction resultInput;
Direction inputHorizontal;
Direction inputVertical;
float horizontalValue;
float verticalValue;
bool horizontalPriority;
VRCPlayerApi player;
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);
}
private void SetPriority(bool horizontalPriority)
{
if (this.horizontalPriority != horizontalPriority)
{
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;
}
}
}