| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
use std::marker::PhantomData; |
| 15 |
|
| 16 |
use bevy::{ |
| 17 |
dev_tools::states::*, |
| 18 |
ecs::schedule::ScheduleLabel, |
| 19 |
picking::hover::Hovered, |
| 20 |
prelude::*, |
| 21 |
ui::Pressed, |
| 22 |
ui_widgets::{Activate, ActivateOnPress, Button}, |
| 23 |
}; |
| 24 |
|
| 25 |
use custom_transitions::*; |
| 26 |
|
| 27 |
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] |
| 28 |
enum AppState { |
| 29 |
#[default] |
| 30 |
Menu, |
| 31 |
InGame, |
| 32 |
} |
| 33 |
|
| 34 |
fn main() { |
| 35 |
App::new() |
| 36 |
|
| 37 |
.add_plugins(( |
| 38 |
DefaultPlugins, |
| 39 |
IdentityTransitionsPlugin::<AppState>::default(), |
| 40 |
)) |
| 41 |
.init_state::<AppState>() |
| 42 |
.add_systems(Startup, setup) |
| 43 |
.add_systems(OnEnter(AppState::Menu), setup_menu) |
| 44 |
.add_observer(on_activate_start_game.run_if(in_state(AppState::Menu))) |
| 45 |
.add_systems(Update, hover_style) |
| 46 |
.add_systems(OnExit(AppState::Menu), cleanup_menu) |
| 47 |
|
| 48 |
.add_observer(trigger_game_restart.run_if(in_state(AppState::InGame))) |
| 49 |
.add_systems(OnReenter(AppState::InGame), setup_game) |
| 50 |
.add_systems(OnReexit(AppState::InGame), teardown_game) |
| 51 |
|
| 52 |
.add_systems( |
| 53 |
Update, |
| 54 |
(movement, change_color).run_if(in_state(AppState::InGame)), |
| 55 |
) |
| 56 |
.add_systems(Update, log_transitions::<AppState>) |
| 57 |
.run(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
mod custom_transitions { |
| 62 |
use crate::*; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[derive(Default)] |
| 70 |
pub struct IdentityTransitionsPlugin<S: States>(PhantomData<S>); |
| 71 |
|
| 72 |
impl<S: States> Plugin for IdentityTransitionsPlugin<S> { |
| 73 |
fn build(&self, app: &mut App) { |
| 74 |
app.add_systems( |
| 75 |
StateTransition, |
| 76 |
|
| 77 |
|
| 78 |
last_transition::<S> |
| 79 |
|
| 80 |
.pipe(run_reenter::<S>) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
.in_set(EnterSchedules::<S>::default()), |
| 88 |
) |
| 89 |
.add_systems( |
| 90 |
StateTransition, |
| 91 |
last_transition::<S> |
| 92 |
.pipe(run_reexit::<S>) |
| 93 |
.in_set(ExitSchedules::<S>::default()), |
| 94 |
); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] |
| 100 |
pub struct OnReenter<S: States>(pub S); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
fn run_reenter<S: States>(transition: In<Option<StateTransitionEvent<S>>>, world: &mut World) { |
| 105 |
|
| 106 |
let Some(transition) = transition.0 else { |
| 107 |
return; |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
let Some(entered) = transition.entered else { |
| 118 |
return; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
let _ = world.try_run_schedule(OnReenter(entered)); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] |
| 137 |
pub struct OnReexit<S: States>(pub S); |
| 138 |
|
| 139 |
fn run_reexit<S: States>(transition: In<Option<StateTransitionEvent<S>>>, world: &mut World) { |
| 140 |
let Some(transition) = transition.0 else { |
| 141 |
return; |
| 142 |
}; |
| 143 |
let Some(exited) = transition.exited else { |
| 144 |
return; |
| 145 |
}; |
| 146 |
|
| 147 |
let _ = world.try_run_schedule(OnReexit(exited)); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
fn on_activate_start_game(_: On<Activate>, mut next_state: ResMut<NextState<AppState>>) { |
| 152 |
next_state.set(AppState::InGame); |
| 153 |
} |
| 154 |
|
| 155 |
fn hover_style( |
| 156 |
mut button_query: Query<(&Hovered, &mut BackgroundColor), (Changed<Hovered>, With<Button>)>, |
| 157 |
) { |
| 158 |
for (hovered, mut color) in &mut button_query { |
| 159 |
if hovered.get() { |
| 160 |
*color = HOVERED_BUTTON.into(); |
| 161 |
} else { |
| 162 |
*color = NORMAL_BUTTON.into(); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
fn cleanup_menu(mut commands: Commands, menu_data: Res<MenuData>) { |
| 168 |
commands.entity(menu_data.button_entity).despawn(); |
| 169 |
} |
| 170 |
|
| 171 |
const SPEED: f32 = 100.0; |
| 172 |
fn movement( |
| 173 |
time: Res<Time>, |
| 174 |
input: Res<ButtonInput<KeyCode>>, |
| 175 |
mut query: Query<&mut Transform, With<Sprite>>, |
| 176 |
) { |
| 177 |
for mut transform in &mut query { |
| 178 |
let mut direction = Vec3::ZERO; |
| 179 |
if input.pressed(KeyCode::ArrowLeft) { |
| 180 |
direction.x -= 1.0; |
| 181 |
} |
| 182 |
if input.pressed(KeyCode::ArrowRight) { |
| 183 |
direction.x += 1.0; |
| 184 |
} |
| 185 |
if input.pressed(KeyCode::ArrowUp) { |
| 186 |
direction.y += 1.0; |
| 187 |
} |
| 188 |
if input.pressed(KeyCode::ArrowDown) { |
| 189 |
direction.y -= 1.0; |
| 190 |
} |
| 191 |
|
| 192 |
if direction != Vec3::ZERO { |
| 193 |
transform.translation += direction.normalize() * SPEED * time.delta_secs(); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) { |
| 199 |
for mut sprite in &mut query { |
| 200 |
let new_color = LinearRgba { |
| 201 |
blue: ops::sin(time.elapsed_secs() * 0.5) + 2.0, |
| 202 |
..LinearRgba::from(sprite.color) |
| 203 |
}; |
| 204 |
|
| 205 |
sprite.color = new_color.into(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
fn trigger_game_restart(_: On<Activate>, mut next_state: ResMut<NextState<AppState>>) { |
| 213 |
|
| 214 |
|
| 215 |
next_state.set(AppState::InGame); |
| 216 |
} |
| 217 |
|
| 218 |
fn setup(mut commands: Commands) { |
| 219 |
commands.spawn(Camera2d); |
| 220 |
} |
| 221 |
|
| 222 |
fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 223 |
commands.spawn(Sprite::from_image(asset_server.load("branding/icon.png"))); |
| 224 |
commands.spawn_scene(bsn! { |
| 225 |
Node { |
| 226 |
width: percent(100), |
| 227 |
height: percent(100), |
| 228 |
} |
| 229 |
Children [ |
| 230 |
Node { |
| 231 |
position_type: PositionType::Absolute, |
| 232 |
left: px(10), |
| 233 |
top: px(10), |
| 234 |
} |
| 235 |
Children [ |
| 236 |
Text("Move with arrow keys.") |
| 237 |
] |
| 238 |
-- |
| 239 |
Node { |
| 240 |
position_type: PositionType::Absolute, |
| 241 |
left: px(10), |
| 242 |
bottom: px(10), |
| 243 |
padding: px(5), |
| 244 |
justify_content: JustifyContent::Center, |
| 245 |
align_items: AlignItems::Center, |
| 246 |
} |
| 247 |
Button |
| 248 |
Hovered::default() |
| 249 |
BackgroundColor(NORMAL_BUTTON) |
| 250 |
on(|event: On<Add<Pressed>>, |
| 251 |
mut commands: Commands| { |
| 252 |
commands.entity(event.entity).insert(BackgroundColor(PRESSED_BUTTON)); |
| 253 |
}) |
| 254 |
on(|event: On<Remove<Pressed>>, |
| 255 |
is_hovered: Query<&Hovered>, |
| 256 |
mut commands: Commands| { |
| 257 |
if is_hovered.get(event.entity).is_ok_and(Hovered::get) { |
| 258 |
commands.entity(event.entity).insert(BackgroundColor(HOVERED_BUTTON)); |
| 259 |
} else { |
| 260 |
commands.entity(event.entity).insert(BackgroundColor(NORMAL_BUTTON)); |
| 261 |
} |
| 262 |
}) |
| 263 |
Children [ |
| 264 |
Text("Restart Game") |
| 265 |
TextFont { |
| 266 |
font_size: FontSize::Px(33.0), |
| 267 |
} |
| 268 |
TextColor(Color::srgb(0.9, 0.9, 0.9)) |
| 269 |
] |
| 270 |
] |
| 271 |
}); |
| 272 |
info!("Setup game"); |
| 273 |
} |
| 274 |
|
| 275 |
fn teardown_game(mut commands: Commands, player: Single<Entity, With<Sprite>>) { |
| 276 |
commands.entity(*player).despawn(); |
| 277 |
info!("Teardown game"); |
| 278 |
} |
| 279 |
|
| 280 |
#[derive(Resource)] |
| 281 |
struct MenuData { |
| 282 |
pub button_entity: Entity, |
| 283 |
} |
| 284 |
|
| 285 |
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); |
| 286 |
const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25); |
| 287 |
const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35); |
| 288 |
|
| 289 |
fn setup_menu(mut commands: Commands) { |
| 290 |
let button_entity = commands |
| 291 |
.spawn(( |
| 292 |
Node { |
| 293 |
|
| 294 |
width: percent(100), |
| 295 |
height: percent(100), |
| 296 |
justify_content: JustifyContent::Center, |
| 297 |
align_items: AlignItems::Center, |
| 298 |
..default() |
| 299 |
}, |
| 300 |
children![( |
| 301 |
Button, |
| 302 |
ActivateOnPress, |
| 303 |
Hovered::default(), |
| 304 |
Node { |
| 305 |
width: px(150), |
| 306 |
height: px(65), |
| 307 |
|
| 308 |
justify_content: JustifyContent::Center, |
| 309 |
|
| 310 |
align_items: AlignItems::Center, |
| 311 |
..default() |
| 312 |
}, |
| 313 |
BackgroundColor(NORMAL_BUTTON), |
| 314 |
children![( |
| 315 |
Text::new("Play"), |
| 316 |
TextFont { |
| 317 |
font_size: FontSize::Px(33.0), |
| 318 |
..default() |
| 319 |
}, |
| 320 |
TextColor(Color::srgb(0.9, 0.9, 0.9)), |
| 321 |
)] |
| 322 |
)], |
| 323 |
)) |
| 324 |
.id(); |
| 325 |
commands.insert_resource(MenuData { button_entity }); |
| 326 |
} |