Fixed desync issue for PacMan moves
This commit is contained in:
@@ -16,14 +16,12 @@ namespace Marro.PacManUdon
|
||||
[RequireComponent(typeof(Renderer))]
|
||||
public class PacMan : GridMover
|
||||
{
|
||||
private GameManager gameManager;
|
||||
private PlayerInput input;
|
||||
private CollisionManager collisionManager;
|
||||
private float defaultSpeed;
|
||||
private float powerPelletSpeed;
|
||||
private float speed;
|
||||
private Vector3 startPosition;
|
||||
private Quaternion startRotation;
|
||||
private Vector2 startPosition;
|
||||
private Animator animator;
|
||||
new Renderer renderer;
|
||||
private bool hideUntilUnfrozen;
|
||||
@@ -37,15 +35,11 @@ namespace Marro.PacManUdon
|
||||
private float freezeSeconds;
|
||||
private bool frozen;
|
||||
|
||||
#region Animator constants
|
||||
private readonly int animatorKeyState = Animator.StringToHash("State");
|
||||
private readonly int animatorKeyDirection = Animator.StringToHash("Direction");
|
||||
#endregion
|
||||
|
||||
|
||||
public void Initialize(PlayerInput input, Transform startTransform, GameManager gameManager, CollisionManager collisionManager)
|
||||
{
|
||||
this.gameManager = gameManager;
|
||||
this.collisionManager = collisionManager;
|
||||
this.input = input;
|
||||
animator = GetComponent<Animator>();
|
||||
@@ -53,14 +47,11 @@ namespace Marro.PacManUdon
|
||||
frozen = false;
|
||||
hideUntilUnfrozen = false;
|
||||
startPosition = startTransform.localPosition;
|
||||
startRotation = startTransform.localRotation;
|
||||
|
||||
SubscribeToEvent(NetworkEventType.PacManTurn);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
transform.SetLocalPositionAndRotation(startPosition, startRotation);
|
||||
SetPosition(startPosition);
|
||||
direction = Direction.Left;
|
||||
targetDirection = Direction.Left;
|
||||
speed = defaultSpeed;
|
||||
@@ -95,11 +86,14 @@ namespace Marro.PacManUdon
|
||||
{
|
||||
freezeSeconds -= networkManager.SyncedDeltaTime;
|
||||
animator.speed = 0;
|
||||
return;
|
||||
speed = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
speed *= 1 - freezePart;
|
||||
animator.speed = 1 - freezePart;
|
||||
freezeSeconds = 0;
|
||||
}
|
||||
speed *= 1 - freezePart;
|
||||
animator.speed = 1 - freezePart;
|
||||
freezeSeconds = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -153,7 +147,7 @@ namespace Marro.PacManUdon
|
||||
{
|
||||
// Move in the requested direction, as well as perpundicular to it to get to the center of the tunnel
|
||||
if (IsHorizontal(inputDirection))
|
||||
{
|
||||
{
|
||||
var directionToCenter = VerticalToDirection(PositionToGrid(nextPosition).y - nextPosition.y);
|
||||
SetDirection((Direction)((int)inputDirection | (int)directionToCenter));
|
||||
}
|
||||
@@ -163,11 +157,6 @@ namespace Marro.PacManUdon
|
||||
SetDirection((Direction)((int)inputDirection | (int)directionToCenter));
|
||||
}
|
||||
SetTargetDirection(inputDirection); // This is the direction most logic should assume pacman is moving, the actual direction may be different due to cornering
|
||||
|
||||
if (!followingPredefinedPath)
|
||||
{
|
||||
networkManager.SendEventSoon(NetworkEventType.PacManTurn);
|
||||
}
|
||||
}
|
||||
|
||||
return nextPosition;
|
||||
@@ -221,7 +210,7 @@ namespace Marro.PacManUdon
|
||||
}
|
||||
else if (eatResult == EatResult.PowerPellet)
|
||||
{
|
||||
freezeSeconds = freezeSeconds = 0.05f;
|
||||
freezeSeconds = 0.05f;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,43 +319,5 @@ namespace Marro.PacManUdon
|
||||
{
|
||||
renderer.enabled = visible;
|
||||
}
|
||||
|
||||
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
|
||||
{
|
||||
if (eventType != NetworkEventType.PacManTurn)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (kinematic || frozen || !enabled)
|
||||
{
|
||||
index += 1;
|
||||
PadSyncedData(ref index);
|
||||
return;
|
||||
}
|
||||
|
||||
data.AppendAsByte((int)targetDirection, ref index);
|
||||
|
||||
base.CollectSyncedData(data, ref index, eventType);
|
||||
}
|
||||
|
||||
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
|
||||
{
|
||||
if (eventType != NetworkEventType.PacManTurn)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (kinematic || frozen || !enabled)
|
||||
{
|
||||
index += 1;
|
||||
PadSyncedData(ref index);
|
||||
return true;
|
||||
}
|
||||
|
||||
SetTargetDirection((Direction)data.ReadByte(ref index));
|
||||
|
||||
return base.WriteSyncedData(data, ref index, eventType);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user