-
Notifications
You must be signed in to change notification settings - Fork 1
/
bird.rs
220 lines (209 loc) · 7.02 KB
/
bird.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use bevy::prelude::*;
use moonshine_behavior::prelude::*;
const HELP_TEXT: &str = "This example simulates the state of a bird.
Each button corresponds to an action available to the bird. A red button means the action is not allowed.
When idle, the bird can sleep, chirp, or fly. When sleeping, the bird cannot chirp, or fly.
The bird may only chirp if flying or idle.";
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Bird Behavior".to_string(),
resolution: (800., 300.).into(),
..default()
}),
..default()
}))
// Add the BehaviorPlugin for Bird behavior.
// This plugin is required for the behavior system to work with a behavior type.
.add_plugins(BehaviorPlugin::<Bird>::default())
// Add the transition system for Bird behavior
// Behavior changes happen in this system. Register your systems before or after it as needed.
.add_systems(Update, transition::<Bird>)
// ... other systems ...
.add_systems(Startup, setup)
.add_systems(
Update,
(update_text, update_buttons).after(transition::<Bird>),
)
.add_systems(Update, on_button_clicked.before(transition::<Bird>))
.run();
}
// Define Bird behavior as an enum with all of its possible states.
#[derive(Component, Default, Debug, Reflect)]
#[reflect(Component)]
enum Bird {
#[default]
Idle,
Fly,
Sleep,
Chirp,
}
// Implement Behavior for Bird to describe behavior transitions as required:
// 1. When idle, the bird can sleep, chirp, or fly.
// 2. When sleeping, the bird cannot chirp, or fly.
// 3. The bird may only chirp if flying or idle.
impl Behavior for Bird {
fn allows_next(&self, next: &Self) -> bool {
use Bird::*;
match self {
Idle => matches!(next, Sleep | Fly | Chirp),
Fly => matches!(next, Chirp),
Sleep | Chirp => false,
}
}
}
// A marker component for the message text.
#[derive(Component)]
struct Message;
// A marker component for the buttons.
#[derive(Component)]
enum Action {
Fly,
Sleep,
Chirp,
Stop,
Reset,
}
// Spawn a Bird and setup UI.
fn setup(mut commands: Commands) {
commands.spawn((Bird::Idle, Transition::<Bird>::default()));
commands.spawn(Camera2d);
commands
.spawn((
Node {
width: Val::Percent(100.),
height: Val::Percent(100.),
flex_direction: FlexDirection::Column,
align_items: AlignItems::Start,
padding: UiRect::all(Val::Px(20.)),
..Default::default()
},
BackgroundColor(bevy::color::palettes::css::GRAY.into()),
))
.with_children(|root| {
root.spawn((
Text::new(HELP_TEXT),
Node {
margin: UiRect::bottom(Val::Px(20.)),
..default()
},
TextFont {
font_size: 20.0,
..default()
},
TextColor(Color::WHITE),
));
root.spawn((
Message,
Text::new(""),
TextFont {
font_size: 25.0,
..default()
},
TextColor(Color::WHITE),
));
root.spawn(Node {
width: Val::Percent(100.),
height: Val::Percent(100.),
flex_direction: FlexDirection::Row,
align_items: AlignItems::Start,
..default()
})
.with_children(|parent| {
spawn_button(parent, "Fly", Action::Fly);
spawn_button(parent, "Sleep", Action::Sleep);
spawn_button(parent, "Chirp", Action::Chirp);
spawn_button(parent, "Stop", Action::Stop);
spawn_button(parent, "Reset", Action::Reset);
});
});
}
// Update the message text based on the current state of the Bird.
fn update_text(
query: Query<(&Bird, Previous<Bird>), Changed<Bird>>,
mut message: Query<&mut Text, With<Message>>,
) {
use Bird::*;
if let Ok((bird, previous)) = query.get_single() {
let mut text = message.single_mut();
text.0 = match bird {
Idle => "Bird is idle.",
Fly => "Bird is flying.",
Sleep => "Bird is sleeping.",
Chirp => {
if let Some(Fly) = previous.get() {
"Bird is chirping while flying."
} else {
"Bird is chirping."
}
}
}
.to_string();
}
}
// Update the button colors based on the current state of the Bird.
fn update_buttons(
query: Query<&Bird, Changed<Bird>>,
mut buttons: Query<(&Action, &mut BackgroundColor), With<Button>>,
) {
use Bird::*;
if let Ok(behavior) = query.get_single() {
for (action, mut color) in buttons.iter_mut() {
let is_allowed = match action {
Action::Fly => behavior.allows_next(&Fly),
Action::Sleep => behavior.allows_next(&Sleep),
Action::Chirp => behavior.allows_next(&Chirp),
_ => true,
};
color.0 = if is_allowed {
bevy::color::palettes::css::DARK_GREEN.into()
} else {
bevy::color::palettes::css::RED.into()
};
}
}
}
// Modify the Bird behavior based on button clicks.
fn on_button_clicked(
query: Query<(&Action, &Interaction), Changed<Interaction>>,
mut bird: Query<&mut Transition<Bird>>,
) {
use Bird::*;
let mut transition = bird.single_mut();
for (action, interaction) in query.iter() {
if let Interaction::Pressed = interaction {
match action {
Action::Fly => transition.try_start(Fly).forget(),
Action::Sleep => transition.try_start(Sleep).forget(),
Action::Chirp => transition.try_start(Chirp).forget(),
Action::Stop => transition.stop(),
Action::Reset => transition.reset(),
}
}
}
}
// Spawn a button with the given text and action.
fn spawn_button(parent: &mut ChildBuilder, text: impl Into<String>, action: Action) {
parent
.spawn((
action,
Button,
BackgroundColor(bevy::color::palettes::css::DARK_GRAY.into()),
Node {
margin: UiRect::all(Val::Px(5.)),
padding: UiRect::new(Val::Px(10.), Val::Px(10.), Val::Px(5.), Val::Px(5.)),
..default()
},
))
.with_children(|fly_button| {
fly_button.spawn((
Text::new(text),
TextFont {
font_size: 20.0,
..default()
},
TextColor(Color::WHITE),
));
});
}