feat: add connecting to singleplayer

This commit is contained in:
2026-08-24 19:04:52 +02:00
parent 7f92435f89
commit ba7100083e
2 changed files with 60 additions and 10 deletions

View File

@@ -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());
}