Simplified PacMan state

This commit is contained in:
2026-06-22 14:52:53 +02:00
parent ca116ac77f
commit 99556b81b1
4 changed files with 103 additions and 60 deletions

View File

@@ -227,7 +227,6 @@ namespace Marro.PacManUdon
AddScore(scoreBonus);
StartTimeSequence(PacManTimeSequence.GhostCaught);
pacMan.HideUntilUnfrozen();
}
public void PacManCaught()

View File

@@ -12,29 +12,40 @@ namespace Marro.PacManUdon
Dead,
}
enum PacManState
{
Idle,
Normal,
PowerPellet,
Dead,
Big,
}
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Renderer))]
public class PacMan : GridMover
{
private PlayerInput input;
private CollisionManager collisionManager;
private Animator animator;
private new Renderer renderer;
private Vector2 startPosition;
private float defaultSpeed;
private float powerPelletSpeed;
private PacManState state;
private float speed;
private Vector2 startPosition;
private Animator animator;
new Renderer renderer;
private bool hideUntilUnfrozen;
private bool dead;
private float freezeSeconds;
private bool frozen;
private bool kinematic;
private bool followingPredefinedPath;
private Direction[] predefinedPath;
private int predefinedPathIndex;
private float freezeSeconds;
private bool frozen;
private readonly int animatorKeyState = Animator.StringToHash("State");
private readonly int animatorKeyDirection = Animator.StringToHash("Direction");
@@ -45,7 +56,6 @@ namespace Marro.PacManUdon
animator = GetComponent<Animator>();
renderer = GetComponent<Renderer>();
frozen = false;
hideUntilUnfrozen = false;
startPosition = startTransform.localPosition;
}
@@ -54,13 +64,13 @@ namespace Marro.PacManUdon
SetPosition(startPosition);
direction = Direction.Left;
targetDirection = Direction.Left;
speed = defaultSpeed;
kinematic = false;
followingPredefinedPath = false;
SetDead(false);
state = PacManState.Idle;
SetAnimatorState((int)PacManAnimatorState.Idle);
UpdateSpeed();
UpdateAnimator();
}
public override void SyncedUpdate()
@@ -72,12 +82,6 @@ namespace Marro.PacManUdon
return;
}
if (hideUntilUnfrozen)
{
hideUntilUnfrozen = false;
SetVisibility(true);
}
float speed = this.speed;
if (freezeSeconds > 0)
{
@@ -220,11 +224,19 @@ namespace Marro.PacManUdon
if (!gameObject.activeInHierarchy)
return;
if (dead)
switch (state)
{
SetAnimatorState((int)PacManAnimatorState.Dead);
animator.speed = 1;
return;
case PacManState.Dead:
SetAnimatorState((int)PacManAnimatorState.Dead);
animator.speed = 1;
return;
case PacManState.Idle:
SetAnimatorState((int)PacManAnimatorState.Idle);
return;
case PacManState.Big:
SetAnimatorState((int)PacManAnimatorState.Big);
animator.speed = 1;
return;
}
animator.speed = frozen ? 0 : 1;
@@ -251,22 +263,22 @@ namespace Marro.PacManUdon
private void SetAnimatorState(int value) =>
animator.SetFloat(animatorKeyState, value);
public void SetDead(bool dead)
public void Die()
{
this.dead = dead;
state = PacManState.Dead;
UpdateAnimator();
}
public void SetFrozen(bool frozen)
{
this.frozen = frozen;
UpdateAnimator();
}
public void HideUntilUnfrozen()
{
hideUntilUnfrozen = true;
SetVisibility(false);
if (!frozen && state == PacManState.Idle)
{
state = PacManState.Normal;
}
UpdateAnimator();
}
public void SetLevel(int level)
@@ -278,7 +290,13 @@ namespace Marro.PacManUdon
public void SetPowerPellet(bool powerPellet)
{
if (powerPellet)
state = powerPellet ? PacManState.PowerPellet : PacManState.Normal;
UpdateSpeed();
}
private void UpdateSpeed()
{
if (state == PacManState.PowerPellet)
{
speed = powerPelletSpeed;
}
@@ -312,12 +330,32 @@ namespace Marro.PacManUdon
public void BecomeBig()
{
SetAnimatorState((int)PacManAnimatorState.Big);
state = PacManState.Big;
UpdateAnimator();
}
void SetVisibility(bool visible)
public void SetVisibility(bool visible)
{
renderer.enabled = visible;
}
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
data.AppendAsByte((int)state, ref index);
data.Append(freezeSeconds, ref index);
data.Append(frozen, ref index);
base.CollectSyncedData(data, ref index, eventType);
}
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
state = (PacManState)data.ReadByte(ref index);
freezeSeconds = data.ReadFloat(ref index);
frozen = data.ReadBool(ref index);
UpdateSpeed();
return base.WriteSyncedData(data, ref index, eventType);
}
}
}

View File

@@ -279,11 +279,17 @@ MonoBehaviour:
Entry: 1
Data: inputMethod
- Name: <UserType>k__BackingField
Entry: 6
Entry: 7
Data: 16|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: Marro.PacManUdon.InputMethod, Assembly-CSharp
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 7
Data: 16|System.RuntimeType, mscorlib
Data: 17|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Int32, mscorlib
@@ -304,7 +310,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 17|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 18|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -328,13 +334,13 @@ MonoBehaviour:
Data: resultInput
- Name: $v
Entry: 7
Data: 18|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 19|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: resultInput
- Name: <UserType>k__BackingField
Entry: 7
Data: 19|System.RuntimeType, mscorlib
Data: 20|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: Marro.PacManUdon.Direction, Assembly-CSharp
@@ -343,7 +349,7 @@ MonoBehaviour:
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 16
Data: 17
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -358,7 +364,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 20|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 21|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -382,16 +388,16 @@ MonoBehaviour:
Data: inputHorizontal
- Name: $v
Entry: 7
Data: 21|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 22|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: inputHorizontal
- Name: <UserType>k__BackingField
Entry: 9
Data: 19
Data: 20
- Name: <SystemType>k__BackingField
Entry: 9
Data: 16
Data: 17
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -406,7 +412,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 22|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 23|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -430,16 +436,16 @@ MonoBehaviour:
Data: inputVertical
- Name: $v
Entry: 7
Data: 23|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 24|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: inputVertical
- Name: <UserType>k__BackingField
Entry: 9
Data: 19
Data: 20
- Name: <SystemType>k__BackingField
Entry: 9
Data: 16
Data: 17
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -454,7 +460,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 24|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 25|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -478,13 +484,13 @@ MonoBehaviour:
Data: horizontalValue
- Name: $v
Entry: 7
Data: 25|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 26|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: horizontalValue
- Name: <UserType>k__BackingField
Entry: 7
Data: 26|System.RuntimeType, mscorlib
Data: 27|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Single, mscorlib
@@ -493,7 +499,7 @@ MonoBehaviour:
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 26
Data: 27
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -508,7 +514,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 27|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 28|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -532,16 +538,16 @@ MonoBehaviour:
Data: verticalValue
- Name: $v
Entry: 7
Data: 28|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 29|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: verticalValue
- Name: <UserType>k__BackingField
Entry: 9
Data: 26
Data: 27
- Name: <SystemType>k__BackingField
Entry: 9
Data: 26
Data: 27
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -556,7 +562,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 29|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 30|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -580,7 +586,7 @@ MonoBehaviour:
Data: horizontalPriority
- Name: $v
Entry: 7
Data: 30|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 31|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: horizontalPriority
@@ -604,7 +610,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 31|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 32|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0

View File

@@ -14,7 +14,7 @@ namespace Marro.PacManUdon
case 1:
// Hide ghosts, start pacman death animation
SetGhostsActive(false);
pacMan.SetDead(true);
pacMan.Die();
break;
case 2:
// Start playing death sound