1use yasm::*;
2
3mod network {
5 use yasm::*;
6
7 define_state_machine! {
8 name: NetworkConnection,
9 states: { Disconnected, Connecting, Connected, Reconnecting, Failed },
10 inputs: { Connect, Disconnect, Timeout, Success, Retry },
11 initial: Disconnected,
12 transitions: {
13 Disconnected + Connect => Connecting,
14 Connecting + Success => Connected,
15 Connecting + Timeout => Failed,
16 Connected + Disconnect => Disconnected,
17 Connected + Timeout => Reconnecting,
18 Reconnecting + Success => Connected,
19 Reconnecting + Timeout => Failed,
20 Failed + Retry => Connecting
21 }
22 }
23}
24
25mod game_character {
27 use yasm::*;
28
29 define_state_machine! {
30 name: CharacterState,
31 states: { Idle, Walking, Running, Jumping, Attacking, Dead },
32 inputs: { StartWalk, StartRun, Jump, Attack, Stop, Die, Respawn },
33 initial: Idle,
34 transitions: {
35 Idle + StartWalk => Walking,
36 Idle + StartRun => Running,
37 Idle + Jump => Jumping,
38 Idle + Attack => Attacking,
39 Walking + StartRun => Running,
40 Walking + Stop => Idle,
41 Walking + Jump => Jumping,
42 Walking + Attack => Attacking,
43 Running + Stop => Idle,
44 Running + Jump => Jumping,
45 Running + Attack => Attacking,
46 Jumping + Stop => Idle,
47 Attacking + Stop => Idle,
48 Idle + Die => Dead,
49 Walking + Die => Dead,
50 Running + Die => Dead,
51 Jumping + Die => Dead,
52 Attacking + Die => Dead,
53 Dead + Respawn => Idle
54 }
55 }
56}
57
58fn main() {
59 println!("=== Advanced State Machine Usage Examples ===\n");
60
61 demo_network_connection();
63
64 println!("\n{}\n", "=".repeat(60));
65
66 demo_game_character();
68
69 println!("\n{}\n", "=".repeat(60));
70
71 analyze_state_machines();
73}
74
75fn demo_network_connection() {
76 println!("🌐 Network Connection State Machine Demo");
77 println!("{}", "-".repeat(40));
78
79 let mut connection = StateMachineInstance::<network::NetworkConnection>::new();
80
81 println!("Initial state: {:?}", connection.current_state());
83
84 println!("\nStarting connection...");
85 connection.transition(network::Input::Connect).unwrap();
86 println!("State: {:?}", connection.current_state());
87
88 println!("\nConnection successful!");
89 connection.transition(network::Input::Success).unwrap();
90 println!("State: {:?}", connection.current_state());
91
92 println!("\nNetwork timeout, starting reconnection...");
93 connection.transition(network::Input::Timeout).unwrap();
94 println!("State: {:?}", connection.current_state());
95
96 println!("\nReconnection successful!");
97 connection.transition(network::Input::Success).unwrap();
98 println!("State: {:?}", connection.current_state());
99
100 println!("\nManually disconnecting");
101 connection.transition(network::Input::Disconnect).unwrap();
102 println!("State: {:?}", connection.current_state());
103
104 println!("\nConnection history: {:?}", connection.history());
105}
106
107fn demo_game_character() {
108 println!("🎮 Game Character State Machine Demo");
109 println!("{}", "-".repeat(36));
110
111 let mut character = StateMachineInstance::<game_character::CharacterState>::new();
112
113 println!("Character initial state: {:?}", character.current_state());
114
115 let actions = vec![
117 (game_character::Input::StartWalk, "Start walking"),
118 (game_character::Input::StartRun, "Start running"),
119 (game_character::Input::Jump, "Jump"),
120 (game_character::Input::Stop, "Stop"),
121 (game_character::Input::Attack, "Attack"),
122 (game_character::Input::Die, "Die"),
123 (game_character::Input::Respawn, "Respawn"),
124 ];
125
126 for (input, description) in actions {
127 println!("\n{}: {:?} -> ", description, character.current_state());
128 match character.transition(input) {
129 Ok(_) => {
130 println!("✅ {:?}", character.current_state());
131 println!(" Available actions: {:?}", character.valid_inputs());
132 }
133 Err(e) => println!("❌ {e}"),
134 }
135 }
136}
137
138fn analyze_state_machines() {
139 println!("📊 State Machine Analysis");
140 println!("{}", "-".repeat(26));
141
142 println!("Network connection state machine analysis:");
144 println!(
145 "- Total states: {}",
146 network::NetworkConnection::states().len()
147 );
148 println!(
149 "- Total inputs: {}",
150 network::NetworkConnection::inputs().len()
151 );
152
153 let all_states = network::NetworkConnection::states();
155 for state in &all_states {
156 let reachable = StateMachineQuery::<network::NetworkConnection>::reachable_states(state);
157 println!("- From {:?} can reach {} states", state, reachable.len());
158 }
159
160 println!("\nTerminal state analysis:");
162 for state in &all_states {
163 let valid_inputs = network::NetworkConnection::valid_inputs(state);
164 if valid_inputs.is_empty() {
165 println!("- {state:?} is a terminal state");
166 } else {
167 println!("- {state:?} has {} available inputs", valid_inputs.len());
168 }
169 }
170
171 println!("\nGame character state machine Mermaid diagram:");
172 println!(
173 "{}",
174 StateMachineDoc::<game_character::CharacterState>::generate_mermaid()
175 );
176}