feat: add connecting to the server

This commit is contained in:
2026-08-21 14:16:34 +02:00
parent c0cfb67eec
commit 20d922c56e
9 changed files with 99 additions and 22 deletions

View File

@@ -11,6 +11,7 @@ use crate::ui::{
state::{
GameContext,
GuiState::{self},
connect_to_server,
},
};
@@ -30,25 +31,25 @@ pub async fn draw_dificulty_selection(state: &mut GuiState, context: &mut GameCo
.ui(&mut root_ui(), |ui| {
menu_builder.label(ui, "Difficulty Selection");
// TODO! refactor this ugly ass code
if menu_builder.button(ui, "Easy") {
context.difficulty = crate::super_advanced_ai::BotDificulty::Easy;
*state = GuiState::Game;
*state = GuiState::Connecting(connect_to_server(context.clone()));
}
if menu_builder.button(ui, "Medium") {
context.difficulty = crate::super_advanced_ai::BotDificulty::Normal;
*state = GuiState::Game;
*state = GuiState::Connecting(connect_to_server(context.clone()));
}
if menu_builder.button(ui, "Hard") {
context.difficulty = crate::super_advanced_ai::BotDificulty::Hard;
*state = GuiState::Game;
*state = GuiState::Connecting(connect_to_server(context.clone()));
}
if menu_builder.button(ui, "Go back") {
*state = GuiState::ModeSelection;
}
});
next_frame().await;
}

38
src/ui/macroquad/game.rs Normal file
View File

@@ -0,0 +1,38 @@
use tokio::sync::mpsc::Receiver;
use crate::{
logic::board::board_view::BoardView,
network::{client::Client, message::ServerMessage},
ui::state::GameContext,
};
pub struct GameClient {
client: Client,
update_receiver: Receiver<ServerMessage>,
board: BoardView,
player_name: String,
enemy_name: String, // TODO! Server should give this info to us (it doesn't rn)
}
impl GameClient {
pub async fn new(context: &GameContext) -> Option<Self> {
let Some(mut client) = Client::new(None).await else {
return None;
};
let Some(update_receiver) = client.get_update_receiver() else {
return None;
};
let board = BoardView::default();
Some(Self {
client,
update_receiver,
board,
player_name: "Morbius".to_string(),
enemy_name: "Milo".to_string(),
})
}
}

View File

@@ -8,7 +8,6 @@ use macroquad::{
use crate::ui::{macroquad::menu_builder::MenuBuilder, state::GuiState};
pub async fn draw_main_menu(state: &mut GuiState) {
let menu_size = vec2(screen_width() * 0.5, screen_height() * 0.5);
let menu_pos = vec2(
@@ -37,6 +36,4 @@ pub async fn draw_main_menu(state: &mut GuiState) {
*state = GuiState::Exit;
}
});
next_frame().await;
}

View File

@@ -1,4 +1,5 @@
pub mod difficulty_selection;
pub mod game;
pub mod main_menu;
mod menu_builder;
pub mod mode_selection;

View File

@@ -43,6 +43,4 @@ pub async fn draw_mode_selection(state: &mut GuiState, context: &mut GameContext
*state = GuiState::MainMenu;
}
});
next_frame().await;
}

View File

@@ -11,6 +11,7 @@ use crate::ui::{
state::{
GameContext,
GuiState::{self},
connect_to_server,
},
};
@@ -40,7 +41,7 @@ pub async fn draw_server_selection(state: &mut GuiState, context: &mut GameConte
if menu_builder.button(ui, "Connect") {
context.gamemode = crate::ui::state::GameMode::Multiplayer { is_hosting: false };
*state = GuiState::Game;
*state = GuiState::Connecting(connect_to_server(context.clone()));
// TODO Validate url before connecting or something like that
}
@@ -48,6 +49,4 @@ pub async fn draw_server_selection(state: &mut GuiState, context: &mut GameConte
*state = GuiState::ModeSelection;
}
});
next_frame().await;
}