From ba7100083e511f56992f9cdead7c7c706536519e Mon Sep 17 00:00:00 2001 From: szymon Date: Mon, 24 Aug 2026 19:04:52 +0200 Subject: [PATCH] feat: add connecting to singleplayer --- src/ui/macroquad/difficulty_selection.rs | 43 +++++++++++++++++++----- src/ui/state.rs | 27 ++++++++++++++- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/ui/macroquad/difficulty_selection.rs b/src/ui/macroquad/difficulty_selection.rs index 5107df1..9fa91fe 100644 --- a/src/ui/macroquad/difficulty_selection.rs +++ b/src/ui/macroquad/difficulty_selection.rs @@ -1,3 +1,5 @@ +use std::sync::mpsc; + use macroquad::{ color::GRAY, math::vec2, @@ -6,12 +8,18 @@ use macroquad::{ window::{next_frame, screen_height, screen_width}, }; -use crate::ui::{ - macroquad::menu_builder::MenuBuilder, - state::{ - GameContext, - GuiState::{self}, - connect_to_server, +use crate::{ + settings::{ + client_settings::ClientSettings, + general_settings::{DEFAULT_URL, SettingsLike}, + }, + ui::{ + macroquad::menu_builder::MenuBuilder, + state::{ + GameContext, + GuiState::{self}, + connect_bot, connect_to_server, run_server, + }, }, }; @@ -35,17 +43,17 @@ pub async fn draw_dificulty_selection(state: &mut GuiState, context: &mut GameCo if menu_builder.button(ui, "Easy") { context.difficulty = crate::super_advanced_ai::BotDificulty::Easy; - *state = GuiState::Connecting(connect_to_server()); + start_single(state, context); } if menu_builder.button(ui, "Medium") { context.difficulty = crate::super_advanced_ai::BotDificulty::Normal; - *state = GuiState::Connecting(connect_to_server()); + start_single(state, context); } if menu_builder.button(ui, "Hard") { context.difficulty = crate::super_advanced_ai::BotDificulty::Hard; - *state = GuiState::Connecting(connect_to_server()); + start_single(state, context); } if menu_builder.button(ui, "Go back") { @@ -53,3 +61,20 @@ pub async fn draw_dificulty_selection(state: &mut GuiState, context: &mut GameCo } }); } + +fn start_single(state: &mut GuiState, context: &GameContext) { + let mut client_settings = ClientSettings::new(); + client_settings.server_url = DEFAULT_URL; // we overwrite server_url since we connect locally + + let (tx, rx) = mpsc::channel(); + + run_server(tx); + + let result = rx.recv(); + + if let Ok(result) = result { + connect_bot(context.difficulty); + } + + *state = GuiState::Connecting(connect_to_server()); +} diff --git a/src/ui/state.rs b/src/ui/state.rs index 7a5d9c1..49184bc 100644 --- a/src/ui/state.rs +++ b/src/ui/state.rs @@ -1,7 +1,9 @@ use std::sync::mpsc::{self, Receiver, Sender}; use crate::{ - network::client::Client, super_advanced_ai::BotDificulty, ui::macroquad::game::game::GameClient, + network::{client::Client, server::Server}, + super_advanced_ai::{Bot, BotDificulty}, + ui::macroquad::game::game::GameClient, }; #[derive(Default)] @@ -62,3 +64,26 @@ pub fn connect_to_server() -> Receiver> { rc } + +pub fn run_server(sender: Sender<()>) { + std::thread::spawn(move || { + let rt = tokio::runtime::Runtime::new().unwrap(); + + let mut server = rt.block_on(Server::new()); + let _ = rt.block_on(server.start()); + + let _ = sender.send(()); + + rt.block_on(server.update()); + }); +} + +pub fn connect_bot(dificluty: BotDificulty) { + std::thread::spawn(move || { + let rt = tokio::runtime::Runtime::new().unwrap(); + + let bot = rt.block_on(Bot::new(dificluty)); + + rt.block_on(bot.game_loop()); + }); +}