-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_state.rs
97 lines (83 loc) · 2.93 KB
/
split_state.rs
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
//! A split state is a basic enum state that can be split between the modules of a crate.
//! It's a useful organizational tool for cross-cutting states in a plugin-based codebase.
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
use input::InputMode;
use pyri_state::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, StatePlugin))
.insert_resource(StateDebugSettings {
log_flush: true,
..default()
})
.insert_state(NextStateBuffer::enabled(InputMode::Move))
.add_systems(
Update,
// Every state added to InputMode can be accessed normally.
(
// While in `Move` mode, move with A/D or enter `Attack` mode with W.
InputMode::Move.on_update((
move_left.run_if(input_just_pressed(KeyCode::KeyA)),
move_right.run_if(input_just_pressed(KeyCode::KeyD)),
InputMode::Attack
.enter()
.run_if(input_just_pressed(KeyCode::KeyW)),
)),
// While in `Attack` mode, attack with A/D and return to `Move` mode.
InputMode::Attack.on_update((
(attack_left, InputMode::Move.enter())
.run_if(input_just_pressed(KeyCode::KeyA)),
(attack_right, InputMode::Move.enter())
.run_if(input_just_pressed(KeyCode::KeyD)),
)),
// Enter `Menu` mode on Escape press.
InputMode::with(|x| x != &InputMode::Menu).on_update(
InputMode::Menu
.enter()
.run_if(input_just_pressed(KeyCode::Escape)),
),
// While in `Menu` mode, cancel / confirm with Escape / Enter.
InputMode::Menu.on_update((
(menu_cancel, InputMode::Move.enter())
.run_if(input_just_pressed(KeyCode::Escape)),
menu_confirm.run_if(input_just_pressed(KeyCode::Enter)),
)),
),
)
.run();
}
mod input {
use super::*;
// InputMode is defined as a split state in `mod input`.
#[derive(State, Clone, PartialEq, Eq, Debug)]
#[state(log_flush)]
pub struct InputMode(pub SplitState);
}
mod game {
use super::*;
// The Move and Attack states are added to InputMode in `mod game`.
add_to_split_state!(InputMode, Move, Attack);
}
mod ui {
use super::*;
// The Menu state is added to InputMode in `mod ui`.
add_to_split_state!(InputMode, Menu);
}
fn move_left() {
info!("Moved left.");
}
fn move_right() {
info!("Moved right.");
}
fn attack_left() {
info!("Attacked left.");
}
fn attack_right() {
info!("Attacked right.");
}
fn menu_cancel() {
info!("Canceled menu.");
}
fn menu_confirm() {
info!("Confirmed menu.");
}