diff --git a/src/drops.rs b/src/drops.rs index 18d1a2e..6b93ffc 100644 --- a/src/drops.rs +++ b/src/drops.rs @@ -10,7 +10,7 @@ use rand::Rng; use crate::{ player::{LifeChangeEvent, Player}, scene::SceneObj, - METER, + GameState, GameplaySet, METER, }; #[derive(Component)] @@ -28,7 +28,7 @@ pub fn spawner_plugin(app: &mut App) { app.add_plugins(EntropyPlugin::::default()) .add_event::() .add_event::() - .add_systems(Startup, add_timer) + .add_systems(OnEnter(GameState::InGame), add_timer.in_set(GameplaySet)) .add_systems( Update, ( @@ -37,7 +37,8 @@ pub fn spawner_plugin(app: &mut App) { crate_collisions, delete_on_env_coll, player_coll, - ), + ) + .in_set(GameplaySet), ); } diff --git a/src/game_state.rs b/src/game_state.rs new file mode 100644 index 0000000..5f462b1 --- /dev/null +++ b/src/game_state.rs @@ -0,0 +1,57 @@ +use bevy::{prelude::*, time::Stopwatch}; + +use crate::GameplaySet; + +#[derive(Component)] +pub struct GameState { + game_time: Stopwatch, +} + +#[derive(Component)] +struct StateText; + +pub fn state_and_ui_plugin(app: &mut App) { + app.add_systems( + OnEnter(crate::GameState::InGame), + (setup_gamestate, setup_ui).in_set(GameplaySet), + ) + .add_systems(Update, (update_time).in_set(GameplaySet)); +} + +fn setup_gamestate(mut commands: Commands) { + commands.spawn(GameState { + game_time: Stopwatch::new(), + }); +} + +fn setup_ui(mut commands: Commands) { + commands.spawn(( + TextBundle::from_sections([ + TextSection::new("Score: ", TextStyle::default()), + TextSection::new("0", TextStyle::default()), + ]) + .with_style(Style { + position_type: PositionType::Absolute, + top: Val::Px(5.0), + right: Val::Px(5.0), + ..default() + }), + StateText, + )); +} + +fn update_time( + mut state_txt: Query<&mut Text, With>, + mut game_state: Query<&mut GameState>, + time: Res