A => +89 -0
@@ 0,0 1,89 @@
/*
* This file is part of sdbclient
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::{
prelude::*,
tasks::{AsyncComputeTaskPool, Task},
};
use bevy_console::PrintConsoleLine;
use futures_lite::future;
use crate::{
api::{self, game::ResponseCreateGame},
cfg::{CfgDev, CfgUser},
runtime::menu::{PlayMenuUIState, RTDMenu},
};
use super::CreateGameEvent;
struct CreateGameCallResponse {
game: ResponseCreateGame,
}
#[derive(Component)]
pub(super) struct CreateGameCall(Task<CreateGameCallResponse>);
pub(super) fn start(
mut events: EventReader<CreateGameEvent>,
mut commands: Commands,
cfg_dev: Res<CfgDev>,
cfg_user: Res<CfgUser>,
mut rtdmenu: ResMut<RTDMenu>,
) {
for _event in events.iter() {
let api_address = cfg_dev.api_server.clone();
let token = cfg_user.user_token.clone();
let thread_pool = AsyncComputeTaskPool::get();
let task = thread_pool.spawn(async move {
let create_game_response = api::game::creategame(api_address, token);
CreateGameCallResponse {
game: create_game_response,
}
});
commands.spawn(CreateGameCall(task));
rtdmenu.waiting_for_create_game_call = true;
}
}
pub(super) fn handle(
mut commands: Commands,
mut create_game_call_tasks: Query<(Entity, &mut CreateGameCall)>,
mut rtdmenu: ResMut<RTDMenu>,
mut console: EventWriter<PrintConsoleLine>,
) {
if create_game_call_tasks.is_empty() {
return;
}
let (entity, mut task) = create_game_call_tasks.single_mut();
if let Some(create_game_call_response) = future::block_on(future::poll_once(&mut task.0)) {
rtdmenu.waiting_for_create_game_call = false;
match create_game_call_response.game {
ResponseCreateGame::Valid(res) => {
rtdmenu.cur_game_id = res.id.clone();
rtdmenu.play_menu_ui_state = PlayMenuUIState::InLobbyHost;
console.send(PrintConsoleLine::new(
format!("Created game with id: '{}'", res.id).into(),
));
}
ResponseCreateGame::Error(error) => {
console.send(PrintConsoleLine::new(
format!("Game creation failed, got error: '{}'", error).into(),
));
}
}
// Remove the task, since it's done now
commands.entity(entity).remove::<CreateGameCall>();
commands.entity(entity).despawn_recursive();
}
}
M => +13 -0
@@ 9,20 9,29 @@
use bevy::prelude::*;
use iyes_loopless::prelude::*;
use crate::runtime::menu::RTDMenu;
use super::MenuState;
mod ui;
mod creategamecall;
struct CreateGameEvent;
pub(super) struct PlayMenuPlugin;
impl Plugin for PlayMenuPlugin {
fn build(&self, app: &mut App) {
app.add_loopless_state(PlayMenuState::None)
.add_event::<CreateGameEvent>()
.add_enter_system(MenuState::Play, play_menu_setup)
.add_system_set(
ConditionSet::new()
.run_in_state(PlayMenuState::Visible)
.with_system(ui::show)
.with_system(creategamecall::start.run_on_event::<CreateGameEvent>())
.with_system(creategamecall::handle.run_if(waiting_for_create_game_call))
.into(),
);
}
@@ 38,3 47,7 @@ pub(super) enum PlayMenuState {
fn play_menu_setup(mut commands: Commands) {
commands.insert_resource(NextState(PlayMenuState::Visible))
}
fn waiting_for_create_game_call(rtdmenu: Res<RTDMenu>) -> bool {
rtdmenu.waiting_for_create_game_call
}
M => +10 -7
@@ 15,12 15,13 @@ use crate::{
runtime::menu::{PlayMenuUIState, RTDMenu},
};
use super::PlayMenuState;
use super::{CreateGameEvent, PlayMenuState};
pub(super) fn show(
mut commands: Commands,
mut egui_context: ResMut<EguiContext>,
mut rtdmenu: ResMut<RTDMenu>,
mut creategame_ev_w: EventWriter<CreateGameEvent>,
) {
egui::Window::new(rtdmenu.play_menu_ui_state.display())
.resizable(false)
@@ 46,13 47,15 @@ pub(super) fn show(
}
PlayMenuUIState::CreateGame => {
ui.vertical_centered(|ui| {
if ui.button("Confirm").clicked() {
todo!();
}
ui.add_enabled_ui(!rtdmenu.waiting_for_create_game_call, |ui| {
if ui.button("Confirm").clicked() {
creategame_ev_w.send(CreateGameEvent);
}
if ui.button("Cancel").clicked() {
rtdmenu.play_menu_ui_state = PlayMenuUIState::Main
}
if ui.button("Cancel").clicked() {
rtdmenu.play_menu_ui_state = PlayMenuUIState::Main
}
});
});
}
PlayMenuUIState::BrowseGames => {
M => +2 -0
@@ 12,6 12,7 @@ use bevy::prelude::Resource;
#[derive(Resource)]
pub(crate) struct RTDMenu {
pub play_menu_ui_state: PlayMenuUIState,
pub waiting_for_create_game_call: bool,
/// Current game ID, for showing lobby data etc
pub cur_game_id: String,
}
@@ 20,6 21,7 @@ impl Default for RTDMenu {
fn default() -> Self {
Self {
play_menu_ui_state: PlayMenuUIState::Main,
waiting_for_create_game_call: false,
cur_game_id: String::from(""),
}
}