forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.rs
170 lines (157 loc) · 5.16 KB
/
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
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
use bevy::prelude::*;
/// This example illustrates how to use States to control transitioning from a Menu state to an InGame state.
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.init_resource::<ButtonMaterials>()
.insert_resource(State::new(AppState::Menu))
.add_stage_after(CoreStage::Update, Stage, StateStage::<AppState>::default())
.on_state_enter(Stage, AppState::Menu, setup_menu.system())
.on_state_update(Stage, AppState::Menu, menu.system())
.on_state_exit(Stage, AppState::Menu, cleanup_menu.system())
.on_state_enter(Stage, AppState::InGame, setup_game.system())
.on_state_update(Stage, AppState::InGame, movement.system())
.on_state_update(Stage, AppState::InGame, change_color.system())
.run();
}
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
struct Stage;
#[derive(Clone)]
enum AppState {
Menu,
InGame,
}
struct MenuData {
button_entity: Entity,
}
fn setup_menu(
mut commands: Commands,
asset_server: Res<AssetServer>,
button_materials: Res<ButtonMaterials>,
) {
commands
// ui camera
.spawn(UiCameraBundle::default())
.spawn(ButtonBundle {
style: Style {
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
// center button
margin: Rect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
..Default::default()
},
material: button_materials.normal.clone(),
..Default::default()
})
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text::with_section(
"Play",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
Default::default(),
),
..Default::default()
});
});
commands.insert_resource(MenuData {
button_entity: commands.current_entity().unwrap(),
});
}
fn menu(
mut state: ResMut<State<AppState>>,
button_materials: Res<ButtonMaterials>,
mut interaction_query: Query<
(&Interaction, &mut Handle<ColorMaterial>),
(Mutated<Interaction>, With<Button>),
>,
) {
for (interaction, mut material) in interaction_query.iter_mut() {
match *interaction {
Interaction::Clicked => {
*material = button_materials.pressed.clone();
state.set_next(AppState::InGame).unwrap();
}
Interaction::Hovered => {
*material = button_materials.hovered.clone();
}
Interaction::None => {
*material = button_materials.normal.clone();
}
}
}
}
fn cleanup_menu(mut commands: Commands, menu_data: Res<MenuData>) {
commands.despawn_recursive(menu_data.button_entity);
}
fn setup_game(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let texture_handle = asset_server.load("branding/icon.png");
commands
.spawn(OrthographicCameraBundle::new_2d())
.spawn(SpriteBundle {
material: materials.add(texture_handle.into()),
..Default::default()
});
}
const SPEED: f32 = 100.0;
fn movement(
time: Res<Time>,
input: Res<Input<KeyCode>>,
mut query: Query<&mut Transform, With<Sprite>>,
) {
for mut transform in query.iter_mut() {
let mut direction = Vec3::default();
if input.pressed(KeyCode::Left) {
direction.x += 1.0;
}
if input.pressed(KeyCode::Right) {
direction.x -= 1.0;
}
if input.pressed(KeyCode::Up) {
direction.y += 1.0;
}
if input.pressed(KeyCode::Down) {
direction.y -= 1.0;
}
if direction != Vec3::default() {
transform.translation += direction.normalize() * SPEED * time.delta_seconds();
}
}
}
fn change_color(
time: Res<Time>,
mut assets: ResMut<Assets<ColorMaterial>>,
query: Query<&Handle<ColorMaterial>, With<Sprite>>,
) {
for handle in query.iter() {
let material = assets.get_mut(handle).unwrap();
material
.color
.set_b((time.seconds_since_startup() * 5.0).sin() as f32 + 2.0);
}
}
struct ButtonMaterials {
normal: Handle<ColorMaterial>,
hovered: Handle<ColorMaterial>,
pressed: Handle<ColorMaterial>,
}
impl FromWorld for ButtonMaterials {
fn from_world(world: &mut World) -> Self {
let mut materials = world.get_resource_mut::<Assets<ColorMaterial>>().unwrap();
ButtonMaterials {
normal: materials.add(Color::rgb(0.15, 0.15, 0.15).into()),
hovered: materials.add(Color::rgb(0.25, 0.25, 0.25).into()),
pressed: materials.add(Color::rgb(0.35, 0.75, 0.35).into()),
}
}
}