Skip to content

Commit

Permalink
Add EndOfRoundEvent and game ui
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmauro committed Apr 8, 2024
1 parent 464c75c commit efbd85f
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 65 deletions.
11 changes: 11 additions & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl Plugin for GamePlugin {
.add_plugins(TilePlugin)
.add_plugins(InputPlugin)
.add_plugins(GameButtonPlugin)
.add_event::<EndOfRoundEvent>()
.add_systems(OnEnter(AppState::Game), setup)
.add_systems(
PreUpdate,
Expand Down Expand Up @@ -183,7 +184,13 @@ fn timer_system(time: Res<Time>, mut query: Query<(&mut CueTimer, &GameState)>)
}
}

#[derive(Event)]
pub struct EndOfRoundEvent {
pub round: usize,
}

fn end_of_round_system(
mut events: EventWriter<EndOfRoundEvent>,
mut query: Query<(
&mut CueEngine,
&mut Round,
Expand Down Expand Up @@ -253,6 +260,10 @@ fn end_of_round_system(
*sound = new_sound;
}

events.send(EndOfRoundEvent {
round: round.current,
});

round.current += 1;
}
}
Expand Down
189 changes: 124 additions & 65 deletions src/game/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@ use bevy_egui::{egui, EguiContexts};

use crate::state::{AppState, OnGameScreen};

use self::button::{GameButtonBundle, Shortcut};
use self::{
button::{GameButtonBundle, Shortcut},
text::{round_system, CurrentRoundText},
};

use super::core::{cue::CueEngine, round::Round, score::Score, state::GameState};
use super::{
core::{cue::CueEngine, round::Round, score::Score, state::GameState},
settings::GameSettings,
};

pub mod button;
pub mod text;

pub struct UiPlugin;

impl Plugin for UiPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::Game), setup)
.add_systems(Update, debug_ui.run_if(in_state(AppState::Game)));
.add_systems(Update, round_system);
}
}

pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
pub fn setup(mut commands: Commands, settings: Res<GameSettings>, asset_server: Res<AssetServer>) {
commands
.spawn((
NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::End,
justify_content: JustifyContent::Center,
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
..default()
},
..default()
Expand All @@ -35,87 +42,139 @@ pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
))
.with_children(|parent| {
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
.spawn((
NodeBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
flex_grow: 1.0,
flex_direction: FlexDirection::Row,
align_items: AlignItems::Start,
justify_content: JustifyContent::SpaceBetween,
width: Val::Percent(100.0),
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyA),
})
OnGameScreen,
))
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Position (A)",
format!("{}-Back", settings.n),
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyS),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Sound (S)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
parent.spawn((
TextBundle::from_section(
"11/24",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
),
CurrentRoundText,
));
});

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
.spawn((
NodeBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
flex_grow: 1.0,
flex_direction: FlexDirection::Row,
align_items: AlignItems::End,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyD),
})
OnGameScreen,
))
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Color (D)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyA),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Position (A)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyS),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Sound (S)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyD),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Color (D)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
});
});
}
Expand Down
19 changes: 19 additions & 0 deletions src/game/ui/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use bevy::prelude::*;

use crate::game::{settings::GameSettings, EndOfRoundEvent};

#[derive(Component)]
pub struct CurrentRoundText;

#[allow(clippy::type_complexity)]
pub fn round_system(
settings: Res<GameSettings>,
mut events: EventReader<EndOfRoundEvent>,
mut query: Query<(&mut Text, &CurrentRoundText)>,
) {
for (text, _) in &mut query.get_single_mut() {
for e in events.read() {
text.sections[0].value = format!("{}/{}", e.round, settings.rounds);
}
}
}

0 comments on commit efbd85f

Please sign in to comment.