From 14282ee35ea8052d0f6a0fd9c2f4768abbaa0232 Mon Sep 17 00:00:00 2001 From: Maurice Wangleng Tan Date: Mon, 21 Aug 2023 00:34:18 +0800 Subject: [PATCH] Clean up end UI --- src/end_ui.rs | 157 ++++++++++++++++++++++++++++---------------- src/main_menu_ui.rs | 8 +-- src/scores.rs | 3 + 3 files changed, 106 insertions(+), 62 deletions(-) diff --git a/src/end_ui.rs b/src/end_ui.rs index ed8c0e4..b07a316 100644 --- a/src/end_ui.rs +++ b/src/end_ui.rs @@ -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, }; @@ -14,74 +15,114 @@ impl Plugin for EndUiPlugin { } } -fn setup_end_ui(mut commands: Commands, scores: Res) { - 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, font_assets: Res) { 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>, mut next_state: ResMut>) { diff --git a/src/main_menu_ui.rs b/src/main_menu_ui.rs index dcd9cee..5d781be 100644 --- a/src/main_menu_ui.rs +++ b/src/main_menu_ui.rs @@ -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, @@ -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() }, )); @@ -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() }, @@ -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() }, diff --git a/src/scores.rs b/src/scores.rs index e8cd472..e88ad39 100644 --- a/src/scores.rs +++ b/src/scores.rs @@ -6,6 +6,7 @@ pub struct ScoresPlugin; pub struct Scores { pub end_score: i32, pub best_score: i32, + pub new_record: bool, } impl Default for Scores { @@ -13,12 +14,14 @@ impl Default for Scores { 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); }