Skip to content

Commit

Permalink
Title to playing state transition
Browse files Browse the repository at this point in the history
  • Loading branch information
bas-ie committed Dec 21, 2024
1 parent b04dee5 commit 0b63dfd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/screens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod loading;
mod playing;
mod splash;
mod title;

Expand All @@ -8,14 +9,19 @@ pub fn plugin(app: &mut App) {
app.init_state::<Screen>();
app.enable_state_scoped_entities::<Screen>();

app.add_plugins((loading::plugin, splash::plugin, title::plugin));
app.add_plugins((
loading::plugin,
playing::plugin,
splash::plugin,
title::plugin,
));
}

#[derive(States, Debug, Hash, PartialEq, Eq, Clone, Default)]
pub enum Screen {
Loading,
// Over,
// Playing,
Playing,
#[default]
Splash,
Title,
Expand Down
60 changes: 60 additions & 0 deletions src/screens/playing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use bevy::{input::common_conditions::input_just_pressed, prelude::*};

use crate::screens::Screen;

pub(super) fn plugin(app: &mut App) {
// app.add_systems(OnEnter(Screen::Playing), spawn_level);

// app.load_resource::<PlayingMusic>();
// app.add_systems(OnEnter(Screen::Playing), play_gameplay_music);
// app.add_systems(OnExit(Screen::Playing), stop_music);

app.add_systems(
Update,
return_to_title_screen
.run_if(in_state(Screen::Playing).and(input_just_pressed(KeyCode::Escape))),
);
}

// fn spawn_level(mut commands: Commands) {
// commands.queue(spawn_level_command);
// }

// #[derive(Resource, Asset, Reflect, Clone)]
// pub struct PlayingMusic {
// #[dependency]
// handle: Handle<AudioSource>,
// entity: Option<Entity>,
// }
//
// impl FromWorld for PlayingMusic {
// fn from_world(world: &mut World) -> Self {
// let assets = world.resource::<AssetServer>();
// Self {
// handle: assets.load("audio/music/Fluffing A Duck.ogg"),
// entity: None,
// }
// }
// }
//
// fn play_gameplay_music(mut commands: Commands, mut music: ResMut<PlayingMusic>) {
// music.entity = Some(
// commands
// .spawn((
// AudioPlayer(music.handle.clone()),
// PlaybackSettings::LOOP,
// Music,
// ))
// .id(),
// );
// }
//
// fn stop_music(mut commands: Commands, mut music: ResMut<PlayingMusic>) {
// if let Some(entity) = music.entity.take() {
// commands.entity(entity).despawn_recursive();
// }
// }

fn return_to_title_screen(mut next_screen: ResMut<NextState<Screen>>) {
next_screen.set(Screen::Title);
}
34 changes: 20 additions & 14 deletions src/screens/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use bevy::prelude::*;

use crate::screens::Screen;

pub(super) fn plugin(app: &mut App) {
pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::Title), spawn_title_screen);
}

#[derive(Component)]
pub struct Button;

fn spawn_title_screen(mut commands: Commands) {
commands
.spawn((StateScoped(Screen::Title), Name::new("Title"), Node {
Expand All @@ -23,18 +26,21 @@ fn spawn_title_screen(mut commands: Commands) {
font_size: 30.,
..default()
}));

p.spawn((Name::new("Start Button"), Button, Node {
align_items: AlignItems::Center,
height: Val::Px(65.0),
justify_content: JustifyContent::Center,
width: Val::Px(200.0),
..default()
}))
.with_children(|p| {
p.spawn((Name::new("Button Text"), Text::new("Start")));
})
.observe(
|_ev: Trigger<Pointer<Click>>, mut next_state: ResMut<NextState<Screen>>| {
next_state.set(Screen::Playing);
},
);
});
}

// fn enter_gameplay_screen(_trigger: Trigger<OnPress>, mut next_screen: ResMut<NextState<Screen>>) {
// next_screen.set(Screen::Playing);
// }
//
// fn enter_credits_screen(_trigger: Trigger<OnPress>, mut next_screen: ResMut<NextState<Screen>>) {
// next_screen.set(Screen::Credits);
// }
//
// #[cfg(not(target_family = "wasm"))]
// fn exit_app(_trigger: Trigger<OnPress>, mut app_exit: EventWriter<AppExit>) {
// app_exit.send(AppExit::Success);
// }

0 comments on commit 0b63dfd

Please sign in to comment.