Skip to content

Commit

Permalink
Flow improvements, hide debug UI by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Leinnan committed Feb 16, 2024
1 parent 579d712 commit c91b170
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 52 deletions.
2 changes: 1 addition & 1 deletion bevy_forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ serde_json = "1.0"
futures-lite = "2.2.0"
bevy_pkv = "0.9.1"
bevy_button_released_plugin = "0.3.1"
bevy_simple_text_input = {version = "0.3.2", git="https://github.com/Leinnan/bevy_simple_text_input"}
bevy_simple_text_input = {git="https://github.com/Leinnan/bevy_simple_text_input"}
bevy_args = "1.2.0"
clap = { version = "4.4", features = ["derive"] }

Expand Down
14 changes: 11 additions & 3 deletions bevy_forge/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, git_info)
.add_plugins(EguiPlugin)
.add_plugins(WorldInspectorPlugin::new().run_if(input_toggle_active(true, KeyCode::F1)))
.add_plugins(StateInspectorPlugin::<crate::beam::state::BeamableInitStatus>::default())
.add_plugins(StateInspectorPlugin::<crate::states::MainGameState>::default());
.add_plugins(
WorldInspectorPlugin::new().run_if(input_toggle_active(false, KeyCode::F1)),
)
.add_plugins(
StateInspectorPlugin::<crate::beam::state::BeamableInitStatus>::default()
.run_if(input_toggle_active(false, KeyCode::F1)),
)
.add_plugins(
StateInspectorPlugin::<crate::states::MainGameState>::default()
.run_if(input_toggle_active(false, KeyCode::F1)),
);
}
}

Expand Down
54 changes: 7 additions & 47 deletions bevy_forge/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::time::Duration;

use beam::{context::BeamContext, BeamPlugin};

use bevy::{asset::AssetMetaCheck, prelude::*, time::common_conditions::on_timer};
use beam::BeamPlugin;
use bevy::{asset::AssetMetaCheck, prelude::*};
use bevy_args::BevyArgsPlugin;
use debug::DebugPlugin;
use game::components::RequestText;
use utils::GameArgs;

pub mod beam;
Expand All @@ -20,53 +16,17 @@ pub mod utils;
fn main() {
App::new()
.insert_resource(ClearColor(consts::MY_BG_COLOR))
.add_plugins(DefaultPlugins)
.add_plugins(BevyArgsPlugin::<GameArgs>::default())
.insert_resource(bevy_pkv::PkvStore::new("Beamable", "AiForge"))
// Never attempts to look up meta files. The default meta configuration will be used for each asset.
.insert_resource(AssetMetaCheck::Never)
.add_plugins((
DefaultPlugins,
DebugPlugin,
BeamPlugin,
microservice::MicroservicePlugin,
game::GamePlugin,
states::GameStatesPlugin,
))
.add_plugins(BevyArgsPlugin::<GameArgs>::default())
.insert_resource(bevy_pkv::PkvStore::new("Beamable", "AiForge"))
// Never attempts to look up meta files. The default meta configuration will be used for each asset.
.insert_resource(AssetMetaCheck::Never)
.add_systems(Update, handle_request)
.add_systems(
Update,
send_request.run_if(on_timer(Duration::from_secs(5))),
)
.run();
}

fn handle_request(
mut event_completed: EventReader<microservice::SayHiEventCompleted>,
mut text_query: Query<&mut Text, With<RequestText>>,
) {
let Ok(mut text) = text_query.get_single_mut() else {
return;
};
for result in event_completed.read() {
let txt = match result.as_deref() {
Ok(response) => response.to_string(),
Err(error) => format!("{:#?}", error),
};
text.sections[0].value = format!("Microservice request result:\n{}", txt);
text.sections[0].style.color = if result.is_ok() {
Color::YELLOW_GREEN
} else {
Color::ORANGE_RED
};
}
}

fn send_request(mut ev: EventWriter<microservice::SayHiEvent>, context: Option<Res<BeamContext>>) {
let Some(ctx) = context else {
return;
};

ev.send(microservice::SayHiEvent(
beam_microservice::models::SayHiRequestArgs::new(ctx.get_name().to_owned()),
));
}
4 changes: 3 additions & 1 deletion bevy_forge/src/states/login_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ fn handle_buttons(
mut beam: ResMut<BeamContext>,
) {
if let Ok(text) = text.get_single() {
beam.name = Some((**text).clone());
if !text.0.is_empty() {
beam.name = Some((**text).clone());
}
}
for event in reader.read() {
let Ok(button) = q.get(**event) else {
Expand Down
41 changes: 41 additions & 0 deletions bevy_forge/src/states/menu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::game::components::RequestText;
use bevy::prelude::*;
use bevy::time::common_conditions::on_timer;
use std::time::Duration;

use crate::microservice;
use crate::{consts, game::components::*, utils::despawn_recursive_by_component};

pub struct MenuStatePlugin;
Expand All @@ -9,6 +13,13 @@ impl Plugin for MenuStatePlugin {
app.add_systems(
OnEnter(super::MainGameState::Menu),
(setup, despawn_recursive_by_component::<GameLogoText>),
)
.add_systems(Update, handle_request)
.add_systems(
Update,
send_request.run_if(
on_timer(Duration::from_secs(5)).and_then(in_state(super::MainGameState::Menu)),
),
);
}
}
Expand Down Expand Up @@ -80,3 +91,33 @@ fn setup(
});
});
}

fn handle_request(
mut event_completed: EventReader<microservice::SayHiEventCompleted>,
mut text_query: Query<&mut Text, With<RequestText>>,
) {
let Ok(mut text) = text_query.get_single_mut() else {
return;
};
for result in event_completed.read() {
let txt = match result.as_deref() {
Ok(response) => response.to_string(),
Err(error) => format!("{:#?}", error),
};
text.sections[0].value = format!("Microservice request result:\n{}", txt);
text.sections[0].style.color = if result.is_ok() {
Color::YELLOW_GREEN
} else {
Color::ORANGE_RED
};
}
}

fn send_request(
mut ev: EventWriter<microservice::SayHiEvent>,
context: Res<crate::beam::context::BeamContext>,
) {
ev.send(microservice::SayHiEvent(
beam_microservice::models::SayHiRequestArgs::new(context.get_name().to_owned()),
));
}

0 comments on commit c91b170

Please sign in to comment.