From ae23ed4a30ad3ec42a9f4b813dee7e39dd182218 Mon Sep 17 00:00:00 2001 From: Paul Berruti Date: Mon, 22 Jul 2024 23:18:02 -0700 Subject: [PATCH] Main menu --- src/main.rs | 13 ++----------- src/ui.rs | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 75f82e7..c1d4d8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,9 +55,9 @@ fn main() { .add_systems( Update, ( - sys_draw_border, - (sys_start_game_on_click).run_if(in_state(GameState::MainMenu)), + (ui::main_menu).run_if(in_state(GameState::MainMenu)), ( + sys_draw_border, sys_spawn_on_click, plant_roots::sys_plant_move, sys_harvester_look_for_fruit, @@ -90,15 +90,6 @@ pub fn sys_draw_border(mut gizmos: Gizmos, bounds: Res) { gizmos.line_2d(bounds.max, bounds.max.with_x(bounds.min.x), Color::WHITE); } -pub fn sys_start_game_on_click( - buttons: Res>, - mut next_state: ResMut>, -) { - if buttons.just_pressed(MouseButton::Left) { - next_state.set(GameState::Playing); - } -} - pub fn sys_harvester_look_for_fruit( mut commands: Commands, spatial_tree: Res>, diff --git a/src/ui.rs b/src/ui.rs index 762bdf2..e63042c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,12 +1,34 @@ use bevy::prelude::*; use bevy_egui::{ - egui::{self, RichText}, + egui::{self, Align, Layout, RichText}, EguiContexts, }; -use crate::Score; +use crate::{GameState, Score}; -pub fn scoreboard(mut contexts: EguiContexts, score: Res) { +pub fn main_menu(mut contexts: EguiContexts, mut next_state: ResMut>) { + egui::CentralPanel::default() + .frame(egui::Frame::none()) + .show(contexts.ctx_mut(), |ui| { + ui.add_space(40.0); + ui.with_layout(Layout::top_down(Align::Center), |ui| { + ui.heading("Welcome to Fruitstar!"); + ui.add_space(10.0); + if ui + .button(RichText::new("Start").text_style(egui::TextStyle::Heading)) + .clicked() + { + next_state.set(GameState::Playing); + } + }); + }); +} + +pub fn scoreboard( + mut contexts: EguiContexts, + mut score: ResMut, + mut next_state: ResMut>, +) { let score_label = format!("Score: {}", score.to_string()); egui::Window::new("Fruitstar") .collapsible(false) @@ -15,5 +37,13 @@ pub fn scoreboard(mut contexts: EguiContexts, score: Res) { .resizable(false) .show(contexts.ctx_mut(), |ui| { ui.label(RichText::new(score_label).text_style(egui::TextStyle::Heading)); + if ui.button("Reset").clicked() { + **score = 0; + } + if ui.button("End").clicked() { + next_state.set(GameState::GameOver); + } }); } + +// High scores