From ae6ca47c264a4fc3b15e99b1424e11472923d4be Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 16 Sep 2024 19:20:46 +0200 Subject: pc: ingame menu --- pixel-client/src/main.rs | 26 ++++++++------ pixel-client/src/menu/ingame.rs | 77 +++++++++++++++++++++++++++++++++++++++++ pixel-client/src/menu/main.rs | 6 ++-- pixel-client/src/menu/mod.rs | 1 + 4 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 pixel-client/src/menu/ingame.rs (limited to 'pixel-client/src') diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs index 8b0887e5..c43680ae 100644 --- a/pixel-client/src/main.rs +++ b/pixel-client/src/main.rs @@ -22,7 +22,7 @@ use config::Config; use game::Game; use hurrycurry_client_lib::network::sync::Network; use hurrycurry_protocol::glam::Vec2; -use menu::main::MainMenu; +use menu::{ingame::IngameMenu, main::MainMenu}; use profiler::ProfilerOverlay; use render::Renderer; use sdl2::{event::Event, keyboard::KeyboardState, mouse::MouseState, pixels::Color}; @@ -54,7 +54,7 @@ pub enum Action { } pub enum State { - Ingame(Box), + Ingame(IngameMenu), MainMenu(MainMenu), Quit, } @@ -94,7 +94,7 @@ fn main() -> Result<()> { let mut state = match args.action.unwrap_or_default() { Action::Menu => State::MainMenu(MainMenu::new(renderer.atlas_layout())), - Action::Join { server_address } => State::Ingame(Box::new(Game::new( + Action::Join { server_address } => State::Ingame(IngameMenu::new(Game::new( Network::connect(&server_address)?, &config, renderer.atlas_layout(), @@ -121,7 +121,9 @@ fn main() -> Result<()> { let dt = actual_dt.min(Duration::from_secs_f32(1. / 30.)); let next = match &mut state { - State::Ingame(x) => x.tick(dt.as_secs_f32(), &keyboard, renderer.atlas_layout()), + State::Ingame(x) => { + x.tick(dt.as_secs_f32(), &keyboard, &mouse, renderer.atlas_layout()) + } State::MainMenu(x) => { x.tick(dt.as_secs_f32(), &keyboard, &mouse, renderer.atlas_layout()) } @@ -133,7 +135,7 @@ fn main() -> Result<()> { renderer.set_ui_view(4.); match &mut state { - State::Ingame(x) => x.draw(&mut renderer), + State::Ingame(x) => x.draw(&mut renderer, &mut config), State::MainMenu(x) => x.draw(&mut renderer, &mut config), State::Quit => (), } @@ -151,19 +153,23 @@ fn main() -> Result<()> { keycode: Some(keycode), .. } => match &mut state { - State::Ingame(_) => (), - State::MainMenu(menu) => menu.keyboard_event(keycode, false), + State::Ingame(g) => g.keyboard_event(keycode, false), + State::MainMenu(g) => g.keyboard_event(keycode, false), _ => (), }, Event::KeyDown { keycode: Some(keycode), .. } => match &mut state { - State::Ingame(_) => (), - State::MainMenu(menu) => menu.keyboard_event(keycode, true), + State::Ingame(g) => g.keyboard_event(keycode, true), + State::MainMenu(g) => g.keyboard_event(keycode, true), + _ => (), + }, + Event::TextInput { text, .. } => match &mut state { + State::Ingame(g) => g.ui_state.text_input(text), + State::MainMenu(g) => g.ui_state.text_input(text), _ => (), }, - Event::TextInput { text, .. } => if let State::MainMenu(menu) = &mut state { menu.ui_state.text_input(text) }, _ => {} } } diff --git a/pixel-client/src/menu/ingame.rs b/pixel-client/src/menu/ingame.rs new file mode 100644 index 00000000..ed1ad1bb --- /dev/null +++ b/pixel-client/src/menu/ingame.rs @@ -0,0 +1,77 @@ +use super::main::MainMenu; +use crate::{ + config::Config, + game::Game, + render::{sprite::SpriteDraw, AtlasLayout, Renderer}, + ui::UiState, + State, +}; +use hurrycurry_protocol::glam::Vec2; +use sdl2::{ + keyboard::{KeyboardState, Keycode}, + mouse::MouseState, +}; + +pub struct IngameMenu { + game: Box, + pub ui_state: UiState, + overlay_shown: bool, + next_state: Option>, +} +impl IngameMenu { + pub fn new(game: Game) -> Self { + Self { + overlay_shown: false, + game: Box::new(game), + ui_state: UiState::default(), + next_state: None, + } + } + pub fn tick( + &mut self, + dt: f32, + keyboard: &KeyboardState, + mouse: &MouseState, + layout: &AtlasLayout, + ) -> Option> { + self.game.tick(dt, keyboard, layout); + self.ui_state.update(keyboard, mouse, dt); + self.next_state.take() + } + pub fn keyboard_event(&mut self, keycode: Keycode, down: bool) { + self.ui_state.keyboard_event(keycode, down); + if down && keycode == Keycode::Escape { + self.overlay_shown = !self.overlay_shown + } + } + pub fn draw(&mut self, ctx: &mut Renderer, _config: &mut Config) { + self.game.draw(ctx); + if self.overlay_shown { + let mut main_menu = false; + ctx.draw_ui(SpriteDraw::overlay( + ctx.misc_textures.solid, + Vec2::ZERO, + ctx.ui_size, + Some([0, 0, 0, 130]), + )); + self.ui_state.draw(ctx, |ui| { + ui.horizontal(|ui| { + ui.advance(Vec2::splat(20.)); + ui.vertical(|ui| { + ui.advance(Vec2::splat(20.)); + let w = 80.; + main_menu |= ui.button(w, "Resume"); + ui.advance(Vec2::Y * 10.); + main_menu |= ui.button(w, "Main menu"); + if ui.button(w, "Quit") { + self.next_state = Some(Box::new(State::Quit)) + } + }); + }); + }); + if main_menu { + self.next_state = Some(Box::new(State::MainMenu(MainMenu::new(ctx.atlas_layout())))) + } + } + } +} diff --git a/pixel-client/src/menu/main.rs b/pixel-client/src/menu/main.rs index ce5518e2..f5f951bf 100644 --- a/pixel-client/src/menu/main.rs +++ b/pixel-client/src/menu/main.rs @@ -15,7 +15,9 @@ along with this program. If not, see . */ -use super::{background::MenuBackground, credits::CreditsMenu, settings::SettingsMenu}; +use super::{ + background::MenuBackground, credits::CreditsMenu, ingame::IngameMenu, settings::SettingsMenu, +}; use crate::{ config::Config, game::Game, @@ -84,7 +86,7 @@ impl MainMenu { return; } if ui.button(80., "Join") { - self.next_state = Some(Box::new(State::Ingame(Box::new(Game::new( + self.next_state = Some(Box::new(State::Ingame(IngameMenu::new(Game::new( Network::connect(&self.server_address).unwrap(), config, ui.renderer.atlas_layout(), diff --git a/pixel-client/src/menu/mod.rs b/pixel-client/src/menu/mod.rs index 632e7ca2..167d38e0 100644 --- a/pixel-client/src/menu/mod.rs +++ b/pixel-client/src/menu/mod.rs @@ -19,3 +19,4 @@ pub mod main; pub mod background; pub mod settings; pub mod credits; +pub mod ingame; -- cgit v1.2.3-70-g09d2