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::{
|
use macroquad::{
|
||||||
color::GRAY,
|
color::GRAY,
|
||||||
math::vec2,
|
math::vec2,
|
||||||
@@ -6,12 +8,18 @@ use macroquad::{
|
|||||||
window::{next_frame, screen_height, screen_width},
|
window::{next_frame, screen_height, screen_width},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::ui::{
|
use crate::{
|
||||||
macroquad::menu_builder::MenuBuilder,
|
settings::{
|
||||||
state::{
|
client_settings::ClientSettings,
|
||||||
GameContext,
|
general_settings::{DEFAULT_URL, SettingsLike},
|
||||||
GuiState::{self},
|
},
|
||||||
connect_to_server,
|
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") {
|
if menu_builder.button(ui, "Easy") {
|
||||||
context.difficulty = crate::super_advanced_ai::BotDificulty::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") {
|
if menu_builder.button(ui, "Medium") {
|
||||||
context.difficulty = crate::super_advanced_ai::BotDificulty::Normal;
|
context.difficulty = crate::super_advanced_ai::BotDificulty::Normal;
|
||||||
*state = GuiState::Connecting(connect_to_server());
|
start_single(state, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if menu_builder.button(ui, "Hard") {
|
if menu_builder.button(ui, "Hard") {
|
||||||
context.difficulty = crate::super_advanced_ai::BotDificulty::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") {
|
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 std::sync::mpsc::{self, Receiver, Sender};
|
||||||
|
|
||||||
use crate::{
|
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)]
|
#[derive(Default)]
|
||||||
@@ -62,3 +64,26 @@ pub fn connect_to_server() -> Receiver<Option<Client>> {
|
|||||||
|
|
||||||
rc
|
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