mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: add connecting to singleplayer
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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<Option<Client>> {
|
||||
|
||||
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());
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user