149 lines
4.1 KiB
C#
149 lines
4.1 KiB
C#
namespace Marro.PacManUdon
|
|
{
|
|
using System;
|
|
using UdonSharp;
|
|
using UnityEngine;
|
|
using VRC.SDKBase;
|
|
using VRC.Udon.Common;
|
|
|
|
public class PlayerInput : UdonSharpBehaviour
|
|
{
|
|
public bool active;
|
|
private GameManager gameManager;
|
|
Direction inputHorizontal;
|
|
Direction inputVertical;
|
|
float horizontalValue;
|
|
float verticalValue;
|
|
bool horizontalPriority;
|
|
VRCPlayerApi player;
|
|
|
|
public void Initialize(GameManager gameManager)
|
|
{
|
|
this.gameManager = gameManager;
|
|
inputHorizontal = Direction.Zero;
|
|
inputVertical = Direction.Zero;
|
|
horizontalPriority = false;
|
|
player = Networking.LocalPlayer;
|
|
}
|
|
|
|
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;
|
|
}
|
|
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);
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
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);
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
public Direction GetDirection()
|
|
{
|
|
if (horizontalPriority)
|
|
{
|
|
return inputHorizontal;
|
|
}
|
|
else
|
|
{
|
|
return inputVertical;
|
|
}
|
|
}
|
|
}
|
|
} |