Ghosts synced with some issues

This commit is contained in:
2026-01-24 15:04:54 +01:00
parent 9554d1c512
commit 1433b9bdfc
11 changed files with 1489 additions and 1168 deletions

View File

@@ -24,9 +24,12 @@ namespace Marro.PacManUdon
private int elroy1PelletCount;
private int elroy2PelletCount;
private float powerPelletDuration;
private float[] scatterPattern;
private float pelletTimeoutLimit;
// Power Pellet logic
private bool powerPelletActive;
private float powerPelletDuration;
private float powerPelletCountdown;
private int powerPelletMultiplier;
@@ -40,7 +43,6 @@ namespace Marro.PacManUdon
// Scattering logic
private float scatterCounter;
private float[] scatterPattern;
private int scatterPatternIndex;
// Elroy logic
@@ -51,12 +53,10 @@ namespace Marro.PacManUdon
private int sharedPelletCounter;
private readonly int[] sharedPelletCounterReleaseValues = { 0, 7, 17, 32 };
private float pelletTimeout;
private float pelletTimeoutLimit;
private bool frozen;
private bool kinematic;
// This should be called once when the game is initialized
public void Initialize(Transform[] ghostStarts, Transform[] ghostTargets, PacMan pacMan, PelletManager pelletManager, GameManager gameController)
{
this.gameController = gameController;
@@ -72,12 +72,11 @@ namespace Marro.PacManUdon
Vector2 idlePosition2 = ghostTargets[2 + ghostIndex * 3].localPosition;
Vector2 cornerPosition = ghostTargets[3 + ghostIndex * 3].localPosition;
ghosts[ghostIndex].Initialize(pacMan, blinky, startTransform, homePosition, idlePosition1, idlePosition2, cornerPosition);
ghosts[ghostIndex].Initialize(pacMan, blinky, startTransform, homePosition, idlePosition1, idlePosition2, cornerPosition, ghostIndex);
}
}
// This should be called every time the level is reset
public void Reset(bool afterLifeLost = false)
public void RestartLevel(bool afterLifeLost = false)
{
ghostScaredQueue = new DataList();
powerPelletActive = false;
@@ -192,7 +191,9 @@ namespace Marro.PacManUdon
gameController.GhostCaught(0);
return;
}
// Debug.Log($"{gameObject} GhostCaughtQueue with ghost {ghost}");
networkManager.SendEventSoon(NetworkEventType.GhostUpdate);
ghostScaredQueue.Add(ghost);
GhostCaughtExecute(ghost);
}
@@ -314,6 +315,18 @@ namespace Marro.PacManUdon
public void SetLevel(int level)
{
Debug.Log($"GhostManager: SetLevel {level}");
SetLevelConstants(level);
int[] privatePelletCounterReleaseValues = PacManConstants.GetGhostHousePrivatePelletCounterLimitForLevel(level);
for (int i = 0; i < ghosts.Length; i++)
{
ghosts[i].SetHousePelletCounterLimit(privatePelletCounterReleaseValues[i]);
RestartLevel(); // Reset needed to properly apply level
}
}
private void SetLevelConstants(int level)
{
speedDefault = PacManConstants.GetGhostDefaultSpeedForLevel(level);
speedScared = PacManConstants.GetGhostScaredSpeedForLevel(level);
speedReturn = 15f;
@@ -326,13 +339,6 @@ namespace Marro.PacManUdon
powerPelletDuration = PacManConstants.GetScaredDurationForLevel(level);
scatterPattern = PacManConstants.GetScatterPatternForLevel(level);
pelletTimeoutLimit = PacManConstants.GetGhostHousePelletTimeoutLimitForLevel(level);
int[] privatePelletCounterReleaseValues = PacManConstants.GetGhostHousePrivatePelletCounterLimitForLevel(level);
for (int i = 0; i < ghosts.Length; i++)
{
ghosts[i].SetHousePelletCounterLimit(privatePelletCounterReleaseValues[i]);
Reset(); // Reset needed to properly apply level
}
}
public float GetTargetSpeed(Ghost ghost, PacManGhostState ghostState, bool isScared, bool inTunnel)
@@ -478,11 +484,100 @@ namespace Marro.PacManUdon
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.GhostUpdate)
{
return;
}
// Power Pellet logic
data.Append(powerPelletActive, ref index);
data.Append(powerPelletCountdown, ref index);
data.AppendAsByte(powerPelletMultiplier, ref index);
// Blink logic
data.Append(blinkingActivated, ref index);
data.Append(blinkCountdown, ref index);
data.Append(blinkCurrentlyWhite, ref index);
// Scattering logic
data.Append(scatterCounter, ref index);
data.AppendAsByte(scatterPatternIndex, ref index);
// Elroy logic
data.AppendAsByte(elroyLevel, ref index);
// Ghost house logic
data.Append(sharedPelletCounterActive, ref index);
data.AppendAsByte(sharedPelletCounter, ref index);
data.Append(pelletTimeout, ref index);
data.Append(frozen, ref index);
data.Append(kinematic, ref index);
data.AppendAsByte(gameController.Level, ref index);
var ghostScaredQueueArray = new byte[ghosts.Length];
for (int i = 0; i < ghostScaredQueueArray.Length; i++)
{
var add = ghostScaredQueue.TryGetValue(i, out var ghost);
if (!add)
{
ghostScaredQueueArray[i] = byte.MaxValue; // Add a terminator
break;
}
ghostScaredQueueArray[i] = (byte)((Ghost)ghost.Reference).Index;
}
Debug.Log($"{gameObject} Sent a ghostScareQueue of length {ghostScaredQueue.Count}");
data.Append(ghostScaredQueueArray, ref index);
}
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.GhostUpdate)
{
return true;
}
// Power Pellet logic
powerPelletActive = data.ReadBool(ref index);
powerPelletCountdown = data.ReadFloat(ref index);
powerPelletMultiplier = data.ReadByte(ref index);
// Blink logic
blinkingActivated = data.ReadBool(ref index);
blinkCountdown = data.ReadFloat(ref index);
blinkCurrentlyWhite = data.ReadBool(ref index);
// Scattering logic
scatterCounter = data.ReadFloat(ref index);
scatterPatternIndex = data.ReadByte(ref index);
// Elroy logic
elroyLevel = data.ReadByte(ref index);
// Ghost house logic
sharedPelletCounterActive = data.ReadBool(ref index);
sharedPelletCounter = data.ReadByte(ref index);
pelletTimeout = data.ReadFloat(ref index);
frozen = data.ReadBool(ref index);
kinematic = data.ReadBool(ref index);
var level = data.ReadByte(ref index);
SetLevelConstants(level);
ghostScaredQueue.Clear();
for (int i = 0; i < ghosts.Length; i++)
{
var ghostIndex = data[index + i];
if (ghostIndex > ghosts.Length) // Reached terminator
{
break;
}
ghostScaredQueue.Add(ghosts[ghostIndex]);
}
Debug.Log($"{gameObject} Read back a ghostScareQueue of length {ghostScaredQueue.Count}");
return true;
}