Skip to content

Commit

Permalink
Clean up end UI
Browse files Browse the repository at this point in the history
  • Loading branch information
yamgent committed Aug 20, 2023
1 parent f95fb6f commit 14282ee
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 62 deletions.
157 changes: 99 additions & 58 deletions src/end_ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bevy::prelude::*;
use bevy::{core_pipeline::clear_color::ClearColorConfig, prelude::*};

use crate::{
app_state::{AppState, StateOwner},
game_assets::FontAssets,
scores::Scores,
};

Expand All @@ -14,74 +15,114 @@ impl Plugin for EndUiPlugin {
}
}

fn setup_end_ui(mut commands: Commands, scores: Res<Scores>) {
commands.spawn((Camera2dBundle::default(), StateOwner(AppState::End)));
const BACKGROUND_COLOR: Color = Color::rgb(40.0 / 255.0, 40.0 / 255.0, 63.0 / 255.0);

fn setup_end_ui(mut commands: Commands, scores: Res<Scores>, font_assets: Res<FontAssets>) {
commands.spawn((
TextBundle::from_section(
"Game Over",
TextStyle {
font_size: 32.0,
color: Color::GREEN,
Camera2dBundle {
camera_2d: Camera2d {
clear_color: ClearColorConfig::Custom(BACKGROUND_COLOR),
..Default::default()
},
),
StateOwner(AppState::End),
));

commands.spawn((
TextBundle::from_section(
format!("Final Score: {}m", scores.end_score),
TextStyle {
font_size: 32.0,
color: Color::GREEN,
..Default::default()
},
)
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(32.0),
left: Val::Px(0.0),
..Default::default()
}),
},
StateOwner(AppState::End),
));

commands.spawn((
TextBundle::from_section(
format!("Your Best: {}m", scores.best_score),
TextStyle {
font_size: 32.0,
color: Color::GREEN,
commands
.spawn((
NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
row_gap: Val::Px(40.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
..Default::default()
},
)
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(128.0),
left: Val::Px(0.0),
..Default::default()
}),
StateOwner(AppState::End),
));
StateOwner(AppState::End),
))
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Good Flight!",
TextStyle {
font_size: 96.0,
color: Color::CYAN,
..Default::default()
},
));

commands.spawn((
TextBundle::from_section(
"Press SPACE to restart",
TextStyle {
font_size: 32.0,
color: Color::GREEN,
..Default::default()
},
)
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(256.0),
left: Val::Px(0.0),
..Default::default()
}),
StateOwner(AppState::End),
));
parent
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(16.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
..Default::default()
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
format!("Final Score: {}m", scores.end_score),
TextStyle {
font: font_assets.font_fira.clone(),
font_size: 32.0,
color: Color::WHITE,
..Default::default()
},
));

parent
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(4.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
..Default::default()
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
format!("Your Best: {}m", scores.best_score),
TextStyle {
font: font_assets.font_fira.clone(),
font_size: 32.0,
color: Color::WHITE,
..Default::default()
},
));

if scores.new_record {
parent.spawn(TextBundle::from_section(
"(NEW BEST!)",
TextStyle {
font: font_assets.font_fira.clone(),
font_size: 28.0,
color: Color::YELLOW,
..Default::default()
},
));
}
});
});

parent.spawn(TextBundle::from_section(
"Press [SPACE] to try again",
TextStyle {
font: font_assets.font_fira.clone(),
font_size: 40.0,
color: Color::GREEN,
..Default::default()
},
));
});
}

fn restart_game(keyboard_input: Res<Input<KeyCode>>, mut next_state: ResMut<NextState<AppState>>) {
Expand Down
8 changes: 4 additions & 4 deletions src/main_menu_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Plugin for MainMenuUiPlugin {
}
}

pub const BACKGROUND_COLOR: Color = Color::rgb(40.0 / 255.0, 40.0 / 255.0, 63.0 / 255.0);
const BACKGROUND_COLOR: Color = Color::rgb(40.0 / 255.0, 40.0 / 255.0, 63.0 / 255.0);

fn setup_main_menu_ui(
mut commands: Commands,
Expand Down Expand Up @@ -76,7 +76,7 @@ fn setup_main_menu_ui(
"Coin in the Sky",
TextStyle {
font_size: 48.0,
color: Color::WHITE,
color: Color::CYAN,
..Default::default()
},
));
Expand All @@ -86,7 +86,7 @@ fn setup_main_menu_ui(
"Flip your coin into the sky, and maintain it as high as possible!",
TextStyle {
font: font_assets.font_fira.clone(),
font_size: 20.0,
font_size: 28.0,
color: Color::WHITE,
..Default::default()
},
Expand Down Expand Up @@ -175,7 +175,7 @@ fn setup_main_menu_ui(
"Press [SPACE] to start",
TextStyle {
font: font_assets.font_fira.clone(),
font_size: 24.0,
font_size: 32.0,
color: Color::GREEN,
..Default::default()
},
Expand Down
3 changes: 3 additions & 0 deletions src/scores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ pub struct ScoresPlugin;
pub struct Scores {
pub end_score: i32,
pub best_score: i32,
pub new_record: bool,
}

impl Default for Scores {
fn default() -> Self {
Self {
end_score: 0,
best_score: 0,
new_record: false,
}
}
}

impl Scores {
pub fn register_score(&mut self, new_score: i32) {
self.new_record = new_score > self.best_score;
self.end_score = new_score;
self.best_score = self.best_score.max(self.end_score);
}
Expand Down

0 comments on commit 14282ee

Please sign in to comment.