C# 实现简单状态机

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
126
127
128
129
130
131
132
using System;
namespace StateMachine2.State {
public enum AnimationState {
Walk = 1,
Dead,
}

public abstract class State {
abstract public int GetStateId{ get; }
abstract public void Enter(StateEvent data);
abstract public void Execute(StateEvent data);
abstract public void Exit(StateEvent data);
}

//状态运行时候的参数
public class StateEvent {
public string data;
}

//行走状态
public class State_Walk : State {
public const int ID = 1;

public override int GetStateId {
get {
return ID;
}
}

public override void Enter(StateEvent data) {
Console.WriteLine("角色行走-进入");
}

public override void Execute(StateEvent data) {
Console.WriteLine("角色行走-执行中");
}

public override void Exit(StateEvent data) {
Console.WriteLine("角色行走-退出");
}
}

//死亡状态
public class State_Dead : State {
public const int ID = 2;
public override int GetStateId {
get {
return ID;
}
}

public override void Enter(StateEvent data) {
Console.WriteLine("角色死亡-进入");
}

public override void Execute(StateEvent data) {
Console.WriteLine("角色死亡-执行中");
}

public override void Exit(StateEvent data) {
Console.WriteLine("角色死亡-退出");
}
}

public class StateMachine {
private State currentState = null;
private State previousState = null;
private StateEvent dataEvent = null;
private bool isStop;

public State CurrentState {
get {
return currentState;
}
}

public State PreviousState {
get {
return previousState;
}
}

public bool IsStop {
get {
return isStop;
}

set {
isStop = value;
}
}

private State GetState(AnimationState animationState) {
switch (animationState) {
case AnimationState.Walk: return new State_Walk();
case AnimationState.Dead: return new State_Dead();
}
return new State_Walk();
}

public void ChangeState(AnimationState animationState, StateEvent data,StateEvent previousData = null) {
ChangeState(GetState(animationState),data, previousData);
}

public void ChangeState(State state,StateEvent data, StateEvent previousData = null) {
//如果切换的状态就是本状态,就退出
if (currentState != null && state.GetStateId == currentState.GetStateId)
return;

//退出上一个状态
if (previousState != null)
previousState.Exit(previousData);

//设置进状态,进入新状态
currentState = state;
dataEvent = data;
currentState.Enter(data);
}

public void Update() {
if (currentState == null) {
Console.WriteLine("当前没有状态可以执行");
return;
} else if (IsStop) {
Console.WriteLine("状态机已经停止");
return;
} else {
currentState.Execute(dataEvent);
}
}
}
}

执行代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using StateMachine2.State;
using System;

namespace StateMachine2 {
class Program {
static void Main(string[] args) {
StateMachine sm = new StateMachine();
sm.ChangeState(AnimationState.Walk, new StateEvent() { data = "行走需要的参数" });
sm.Update(); //执行行走状态
sm.Update(); //执行行走状态

sm.ChangeState(AnimationState.Dead, new StateEvent() { data = "死亡需要的参数" });
sm.Update();
sm.IsStop = true; //停止状态机

sm.Update(); //再次执行状态

Console.WriteLine("结束程序");
Console.ReadLine();
}
}
}

本文来自:https://www.cnblogs.com/plateFace/p/5018686.html