Skip to content

Commit

Permalink
Implement join button, thanks to bevy_mod_picking
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 3, 2023
1 parent 8dca741 commit a81f9e1
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 20 deletions.
107 changes: 107 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bevy_asset_loader = { version = "0.18", features = ["2d", "progress_tracking"] }
bevy_editor_pls = { version = "0.6", optional = true }
bevy_kira_audio = { version = "0.18", features = ["mp3"] }
bevy_mod_debugdump = { version = "0.9", optional = true }
bevy_mod_picking = { version = "0.17", default-features = false, features = ["backend_bevy_ui"] }
bevy_rapier2d = "0.23"
iyes_progress = "0.10"
leafwing-input-manager = "0.11"
Expand Down Expand Up @@ -48,6 +49,7 @@ dev = [
"core",
"dep:bevy_editor_pls",
"bevy/dynamic_linking",
"bevy_mod_picking/debug",
"bevy_rapier2d/debug-render-2d",
]
native = ["core", "bevy_rapier2d/parallel"]
Expand Down
19 changes: 17 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct DebugPlugin {
pub entity_count_diagnostics: bool,
pub ambiguity_detection: bool,
pub debug_render: bool,
pub debug_picking: bool,
pub editor: bool,
pub start: AppState,
pub extend_loading_screen: f32,
Expand Down Expand Up @@ -65,7 +66,20 @@ impl Plugin for DebugPlugin {
app.world.resource_mut::<DebugRenderContext>().enabled = false;
app.add_systems(
Update,
toggle_debug_render.run_if(input_just_pressed(DEBUG_RENDER_TOGGLE_KEY)),
toggle_debug_render.run_if(input_just_pressed(DEBUG_TOGGLE_KEY)),
);
}

if self.debug_picking {
use bevy_mod_picking::debug::DebugPickingMode::*;
app.insert_resource(State::new(Disabled)).add_systems(
Update,
(
(|mut next: ResMut<NextState<_>>| next.set(Normal))
.run_if(in_state(Disabled).and_then(input_just_pressed(DEBUG_TOGGLE_KEY))),
(|mut next: ResMut<NextState<_>>| next.set(Disabled))
.run_if(in_state(Normal).and_then(input_just_pressed(DEBUG_TOGGLE_KEY))),
),
);
}

Expand Down Expand Up @@ -130,14 +144,15 @@ impl Default for DebugPlugin {
entity_count_diagnostics: true,
ambiguity_detection: true,
debug_render: true,
debug_picking: true,
editor: true,
extend_loading_screen: 0.0,
start: default(),
}
}
}

const DEBUG_RENDER_TOGGLE_KEY: KeyCode = KeyCode::F3;
const DEBUG_TOGGLE_KEY: KeyCode = KeyCode::F3;

fn toggle_debug_render(mut debug_render_context: ResMut<DebugRenderContext>) {
debug_render_context.enabled = !debug_render_context.enabled;
Expand Down
17 changes: 10 additions & 7 deletions src/state/title_screen.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use bevy_mod_picking::prelude::*;
use iyes_progress::prelude::*;

use crate::config::Config;
use crate::state::game::GameAssets;
use crate::state::AppState;
use crate::state::AppState::*;
use crate::ui::vh;
use crate::ui::vw;
Expand All @@ -23,7 +25,7 @@ impl Plugin for TitleScreenStatePlugin {
.add_plugins(ProgressPlugin::new(TitleScreen))
.add_systems(OnEnter(TitleScreen), enter_title_screen)
.add_systems(OnExit(TitleScreen), exit_title_screen)
.add_systems(Update, do_join_button.run_if(in_state(TitleScreen)));
.add_systems(Update, update_button.run_if(in_state(TitleScreen)));
}
}

Expand Down Expand Up @@ -192,6 +194,12 @@ fn enter_title_screen(mut commands: Commands, root: Res<AppRoot>, config: Res<Co
border_color: BUTTON_BORDER_COLOR.into(),
..default()
},
On::<Pointer<Click>>::run(
|mut next_state: ResMut<NextState<AppState>>, progress: Res<ProgressCounter>| {
let Progress { done, total } = progress.progress_complete();
next_state.set(if done >= total { Game } else { LoadingScreen });
},
),
))
.set_parent(container)
.id();
Expand All @@ -210,7 +218,7 @@ fn exit_title_screen(mut commands: Commands, root: Res<AppRoot>) {
commands.entity(root.ui).despawn_descendants();
}

fn do_join_button(
fn update_button(
mut interaction_query: Query<
(&Interaction, &mut BackgroundColor),
(Changed<Interaction>, With<Button>),
Expand All @@ -224,9 +232,4 @@ fn do_join_button(
}
.into()
}

// TODO: Use this to enter LoadingScreen or Game state when Join button gets pressed
// Show loading screen only if assets are still loading
//let Progress { done, total } = progress.progress_complete();
//next_state.set(if done >= total { Game } else { LoadingScreen });
}
13 changes: 2 additions & 11 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod font;

use bevy::prelude::*;
use bevy_mod_picking::prelude::*;

use crate::camera::CAMERA_HEIGHT;
use crate::camera::CAMERA_WIDTH;
Expand All @@ -12,8 +13,7 @@ pub struct UiPlugin;

impl Plugin for UiPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Selection>()
.add_plugins(font::FontPlugin);
app.add_plugins((DefaultPickingPlugins, font::FontPlugin));
}
}

Expand All @@ -27,12 +27,3 @@ pub fn vw(units: f32) -> Val {
pub fn vh(units: f32) -> Val {
VH_UNIT * units
}

#[derive(Component, Reflect)]
pub struct Selection(pub Entity);

impl Default for Selection {
fn default() -> Self {
Self(Entity::PLACEHOLDER)
}
}

0 comments on commit a81f9e1

Please sign in to comment.