Fixed desync issue for PacMan moves

This commit is contained in:
2026-06-20 14:12:18 +02:00
parent a4733b33b8
commit cb975c24b2
12 changed files with 2024 additions and 2109 deletions

View File

@@ -1,15 +1,15 @@
namespace Marro.PacManUdon
{
using System;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon.Common;
public class PlayerInput : UdonSharpBehaviour
public class PlayerInput : SyncedObject
{
public bool active;
private GameManager gameManager;
Direction resultInput;
Direction inputHorizontal;
Direction inputVertical;
float horizontalValue;
@@ -20,10 +20,13 @@
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()
@@ -67,6 +70,9 @@
{
return;
}
var previousInputHorizontal = inputHorizontal;
horizontalValue = Math.Abs(value);
if (value < -0.5)
{
@@ -90,6 +96,10 @@
SetPriority(false);
}
if (previousInputHorizontal != inputHorizontal)
{
UpdateResultInput();
}
// Debug.Log("Horizontal Input Event: " + value + " | Direction: " + direction + " | lastDirection: " + lastDirection + " | Left: " + left + " | Right: " + right);
}
@@ -99,6 +109,9 @@
{
return;
}
var previousInputVertical = inputVertical;
verticalValue = Math.Abs(value);
if (value > 0.5)
{
@@ -122,6 +135,11 @@
SetPriority(true);
}
if (previousInputVertical != inputVertical)
{
UpdateResultInput();
}
// Debug.Log("Vertical Input Event: " + value + " | Direction: " + direction + " | lastDirection : " + lastDirection);
}
@@ -134,16 +152,37 @@
}
}
private void UpdateResultInput()
{
resultInput = horizontalPriority ? inputHorizontal : inputVertical;
networkManager.SendEventSoon(NetworkEventType.PacManTurn);
}
public Direction GetDirection()
{
if (horizontalPriority)
return resultInput;
}
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
{
return inputHorizontal;
return;
}
else
ByteUtils.AppendAsByte(data, (int)resultInput, ref index);
}
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
{
return inputVertical;
return true;
}
resultInput = (Direction)ByteUtils.ReadByte(data, ref index);
return true;
}
}
}