State Pattern

狀態模式(State pattern):

  • 當物件的內部狀態改變時,物件的行為也會隨之改變,即物件會根據當前狀態執行不同的行為。
  • 每個狀態的行為是獨立定義的。添加新狀態不會影響現有狀態的行為。

在遊戲中會需要不斷地追蹤遊戲中的各種狀態,例如玩家走路(Walking),跳躍(Jumping),待命(IdleStanding)等。若你把它畫成流程圖(flowchart)可能會發現

  • 一次只有一個狀態會被啟用(Active)
  • 狀態會依據不同的條件去轉換到另外一個狀態
  • 當轉換發生時,新的狀態會成為啟用狀態(Active state)
  • 這種狀態轉換圖被稱為有限狀態機(Finite state machineFSM)

以下是一個使用Enum去實作的簡單有限狀態機:

1
2
3
4
5
6
public enum PlayerControllerState
{
Idle,
Walk,
Jump
}

在這個Controller類中,使用switch轉換狀態,很快你會發現當狀態越來越多後,Controller會變得一團亂,例如增加幾個狀態,就必須要不斷的更改Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class UnrefactoredPlayerController : MonoBehaviour
{
private PlayerControllerState state;
private void Update()
{
GetInput();
switch (state)
{
case PlayerControllerState.Idle:
Idle();
break;
case PlayerControllerState.Walk:
Walk();
break;
case PlayerControllerState.Jump:
Jump();
break;
}
}
private void GetInput()
{
// 處理行走和跳躍控制
}
private void Walk()
{
// 行走邏輯
}
private void Idle()
{
// 待命邏輯
}
private void Jump()
{
// 跳躍邏輯
}
}

使用狀態模式,在遊戲中,你可以想像處理角色狀態需要:

  1. 當條件滿足時,第一次進入(Enter)這個狀態時需要處理一些事。
  2. 在遊戲迴圈中根據這個狀態不斷的更新(Update)角色的值。
  3. 當條件不滿足或是滿足某些離開條件時,離開(Exit)這個狀態時,要在離開前處理一些事。
    因此根據這樣的流程定義一個抽象介面(Interface),IState並定義它有以下方法:
  • Enter:第一次進入這個狀態時需要做的邏輯
  • Update: 在每一幀中需要執行的邏輯(有時也稱ExceuteTick)。它會根據這個狀態在每一幀中不斷的更新角色的值。
  • Exit:在離開這個狀態前要做的邏輯
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public interface IState
    {
    public void Enter()
    {
    // 當第一次進入狀態時運行的邏輯
    }
    public void Update()
    {
    // 每幀邏輯,包括轉換到新狀態的條件
    }
    public void Exit()
    {
    // 當離開狀態時運行的邏輯
    }
    }

行走狀態:在這個狀態中,Update()會不斷地移動這個角色,另外也會判斷是否要切換到其他狀態。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class WalkState : IState
{
private Color meshColor = Color.blue;
public Color MeshColor { get => meshColor; set => meshColor = value; }

private PlayerController player;

// 可以在建構子中傳遞任何需要的參數
public WalkState(PlayerController player)
{
this.player = player;
}

public void Enter()
{
//Debug.Log("Entering Walk State");
}

public void Update()
{
// 如果不再在地面上,轉換到跳躍狀態
if (!player.IsGrounded)
{
player.PlayerStateMachine.TransitionTo(player.PlayerStateMachine.jumpState);
}

// 如果速度降到最低臨界值以內,轉換到待命狀態
if (Mathf.Abs(player.CharController.velocity.x) < 0.1f && Mathf.Abs(player.CharController.velocity.z) < 0.1f)
{
player.PlayerStateMachine.TransitionTo(player.PlayerStateMachine.idleState);
}
}

public void Exit()
{
//Debug.Log("Exiting Walk State");
}

}

待命狀態

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class IdleState : IState
{
private PlayerController player;

private Color meshColor = Color.gray;
public Color MeshColor { get => meshColor; set => meshColor = value; }

public IdleState(PlayerController player)
{
this.player = player;
}

public void Enter()
{
Debug.Log("Entering Idle State");
}

public void Update()
{
// 如果不再在地面上,轉換到跳躍狀態
if (!player.IsGrounded)
{
player.PlayerStateMachine.TransitionTo(player.PlayerStateMachine.jumpState);
}

// 如果速度超過臨界值,轉換到移動狀態
if (Mathf.Abs(player.CharController.velocity.x) > 0.1f || Mathf.Abs(player.CharController.velocity.z) > 0.1f)
{
player.PlayerStateMachine.TransitionTo(player.PlayerStateMachine.walkState);
}
}

public void Exit()
{
Debug.Log("Exiting Idle State");
}
}

StateMachine負責管理這些狀態

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class StateMachine
{
public IState CurrentState { get; private set; } // 目前狀態

// 狀態物件的引用
public WalkState walkState;
public JumpState jumpState;
public IdleState idleState;

// 用於通知其他物件狀態改變的事件
public event Action<IState> stateChanged;

// 由於沒有繼承 MonoBehaviour,因此需要通過建構子將 PlayerController 傳入
public StateMachine(PlayerController player)
{
// 建立每個狀態的實體並傳遞 PlayerController
this.walkState = new WalkState(player);
this.jumpState = new JumpState(player);
this.idleState = new IdleState(player);
}

// 設置初始狀態
public void Initialize(IState state)
{
CurrentState = state;
state.Enter();

// 通知其他物件狀態已改變
stateChanged?.Invoke(state);
}

// 退出目前狀態並進入另一個狀態
public void TransitionTo(IState nextState)
{
CurrentState.Exit();
CurrentState = nextState;
nextState.Enter();

// 通知其他物件狀態已改變
stateChanged?.Invoke(nextState);
}

public void Update()
{
if (CurrentState != null)
{
CurrentState.Update();
}
}
}

在Controller中使用StateMachine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
[RequireComponent(typeof(PlayerInput), typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
[SerializeField] private PlayerInput playerInput;
private StateMachine playerStateMachine;

[Header("Movement")]
[Tooltip("Horizontal speed")]
[SerializeField] private float moveSpeed = 5f;
[Tooltip("Rate of change for move speed")]
[SerializeField] private float acceleration = 10f;
[Tooltip("Max height to jump")]
[SerializeField] private float jumpHeight = 1.25f;

[Tooltip("Custom gravity for player")]
[SerializeField] private float gravity = -15f;
[Tooltip("Time between jumps")]
[SerializeField] private float jumpTimeout = 0.1f;

[SerializeField] private bool isGrounded = true; // 是否在地面上
[SerializeField] private float groundedRadius = 0.5f; // 檢查是否在地面的半徑
[SerializeField] private float groundedOffset = 0.15f;
[SerializeField] private LayerMask groundLayers;

public CharacterController CharController => charController;
public bool IsGrounded => isGrounded;
public StateMachine PlayerStateMachine => playerStateMachine;

private CharacterController charController;
private float targetSpeed;
private float verticalVelocity;
private float jumpCooldown;

private void Awake()
{
playerInput = GetComponent<PlayerInput>();
charController = GetComponent<CharacterController>();

// 初始化狀態機
playerStateMachine = new StateMachine(this);
}

private void Start()
{
playerStateMachine.Initialize(playerStateMachine.idleState); // 設置初始狀態為 idleState
}

private void Update()
{
// 使用目前狀態更新角色資料
playerStateMachine.Update();
}

private void LateUpdate()
{
CalculateVertical();
Move();
}

private void Move()
{
Vector3 inputVector = playerInput.InputVector;

if (inputVector == Vector3.zero)
{
targetSpeed = 0;
}

float currentHorizontalSpeed = new Vector3(charController.velocity.x, 0.0f, charController.velocity.z).magnitude;
float tolerance = 0.1f;

if (currentHorizontalSpeed < targetSpeed - tolerance || currentHorizontalSpeed > targetSpeed + tolerance)
{
targetSpeed = Mathf.Lerp(currentHorizontalSpeed, targetSpeed, Time.deltaTime * acceleration);
targetSpeed = Mathf.Round(targetSpeed * 1000f) / 1000f;
}
else
{
targetSpeed = moveSpeed;
}

charController.Move((inputVector.normalized * targetSpeed * Time.deltaTime) + new Vector3(0f, verticalVelocity, 0f) * Time.deltaTime);
}

private void CalculateVertical()
{
if (isGrounded)
{
if (verticalVelocity < 0f)
{
verticalVelocity = -2f;
}

if (playerInput.IsJumping && jumpCooldown <= 0f)
{
verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * gravity);
}

if (jumpCooldown >= 0f)
{
jumpCooldown -= Time.deltaTime;
}
}
else
{
jumpCooldown = jumpTimeout;
playerInput.IsJumping = false;
}

verticalVelocity += gravity * Time.deltaTime;

Vector3 spherePosition = new Vector3(transform.position.x, transform.position.y + groundedOffset, transform.position.z);
isGrounded = Physics.CheckSphere(spherePosition, 0.5f, groundLayers, QueryTriggerInteraction.Ignore);
}

private void OnDrawGizmosSelected()
{
Color transparentGreen = new Color(0.0f, 1.0f, 0.0f, 0.35f);
Color transparentRed = new Color(1.0f, 0.0f, 0.0f, 0.35f);

Gizmos.color = isGrounded ? transparentGreen : transparentRed;

Gizmos.DrawSphere(new Vector3(transform.position.x, transform.position.y + groundedOffset, transform.position.z), groundedRadius);
}
}

Reference: https://github.com/Unity-Technologies/game-programming-patterns-demo/tree/main/Assets/10%20State/Scripts

評論