From e2e535137a2eaed790192359a9eccd07e0a66304 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Thu, 16 Nov 2023 22:20:32 -0800 Subject: [PATCH] Most of the way toward the final camera setup Two cameras only, one for 2d one for 3d -- none of this camera-per-state bs. Still have a bug with switching while in the menu, but otherwise things are working well. --- src/credits.rs | 13 ------------- src/display2d.rs | 15 +++++++++++++-- src/display3d.rs | 15 +++++++++++++-- src/game.rs | 9 +++++++-- src/loading.rs | 22 ---------------------- src/main.rs | 39 +++++++++++++++++++++++++-------------- src/menu.rs | 20 ++------------------ 7 files changed, 60 insertions(+), 73 deletions(-) diff --git a/src/credits.rs b/src/credits.rs index 7573ffb..147618b 100644 --- a/src/credits.rs +++ b/src/credits.rs @@ -72,19 +72,6 @@ fn parse_credits(bytes: &[u8]) -> Result { struct Credits; fn init_credits_ui(mut commands: Commands, server: Res) { - commands.spawn(( - Credits, - Camera2dBundle { - camera: Camera { - is_active: false, - ..default() - }, - ..default() - }, - UiCameraConfig { show_ui: true }, - Name::new("Credits Camera"), - )); - commands .spawn(( Credits, diff --git a/src/display2d.rs b/src/display2d.rs index 7cffe85..1ca4295 100644 --- a/src/display2d.rs +++ b/src/display2d.rs @@ -35,12 +35,22 @@ impl Plugin for Display2dPlugin { set_transform .after(game::update_board) .run_if(any_component_changed::), - set_piece_sprite.run_if(any_component_changed::).after(game::set_side), + set_piece_sprite + .run_if(any_component_changed::) + .after(game::set_side), set_tile_sprite.run_if(any_component_added::), ), ) .add_systems(OnEnter(DisplayState::Display2d), activate::) - .add_systems(OnExit(DisplayState::Display2d), deactivate::); + .add_systems(OnExit(DisplayState::Display2d), deactivate::) + .add_systems( + OnEnter(GameState::Play), + activate::.run_if(in_state(DisplayState::Display2d)), + ) + .add_systems( + OnExit(GameState::Play), + deactivate::.run_if(in_state(DisplayState::Display2d)), + ); } } @@ -61,6 +71,7 @@ struct BackgroundImage; fn initialize_camera(mut commands: Commands) { commands.spawn(( Display2d, + DisplayState::Display2d, Camera2dBundle { camera: Camera { is_active: false, diff --git a/src/display3d.rs b/src/display3d.rs index 760f3b1..440026d 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -35,7 +35,9 @@ impl Plugin for Display3dPlugin { set_board_model.run_if(any_component_added::), set_tile_hitbox.run_if(any_component_added::), set_piece_position.run_if(any_component_changed::), - set_piece_texture.run_if(any_component_changed::).after(game::set_side), + set_piece_texture + .run_if(any_component_changed::) + .after(game::set_side), select .run_if(in_state(GameState::Play)) .run_if(in_state(DisplayState::Display3d)) @@ -58,7 +60,15 @@ impl Plugin for Display3dPlugin { .run_if(in_state(DisplayState::Display3d)), ) .add_systems(OnEnter(DisplayState::Display3d), activate::) - .add_systems(OnExit(DisplayState::Display3d), deactivate::); + .add_systems(OnExit(DisplayState::Display3d), deactivate::) + .add_systems( + OnEnter(GameState::Play), + activate::.run_if(in_state(DisplayState::Display3d)), + ) + .add_systems( + OnExit(GameState::Play), + deactivate::.run_if(in_state(DisplayState::Display3d)), + ); } } @@ -173,6 +183,7 @@ fn hydrate_camera( info!("Initialize 3d camera"); commands.entity(entity).insert(( Display3d, + DisplayState::Display3d, Camera3dBundle { camera: Camera { is_active: false, diff --git a/src/game.rs b/src/game.rs index c3da7e7..65e95d7 100644 --- a/src/game.rs +++ b/src/game.rs @@ -17,7 +17,9 @@ impl Plugin for GamePlugin { cancel_place.run_if(|buttons: Res>| -> bool { buttons.just_pressed(MouseButton::Right) }), - select_sync.run_if(any_component_added::).after(deselect_sync), + select_sync + .run_if(any_component_added::) + .after(deselect_sync), deselect_sync.run_if(any_component_removed::()), move_piece.run_if(any_component_added::), capture_piece.run_if(any_component_added::), @@ -479,7 +481,10 @@ fn null_selections( ) { events.iter().for_each(|entity| { if selected_pieces.is_empty() { - info!("De-selecting piece that should not be selected {:?}", entity); + info!( + "De-selecting piece that should not be selected {:?}", + entity + ); commands.entity(entity).remove::(); writer.send(audio::AudioEvent::PutDown); } diff --git a/src/loading.rs b/src/loading.rs index 83d89f9..df1b205 100644 --- a/src/loading.rs +++ b/src/loading.rs @@ -5,7 +5,6 @@ pub(crate) struct LoadingPlugin; impl Plugin for LoadingPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, initialize) - .add_systems(PostUpdate, deactivate_cameras.run_if(any_component_added::)) .add_systems(OnEnter(GameState::Loading), activate::) .add_systems(OnExit(GameState::Loading), deactivate::); } @@ -15,19 +14,6 @@ impl Plugin for LoadingPlugin { struct Loading; fn initialize(mut commands: Commands) { - commands.spawn(( - Loading, - Camera2dBundle { - camera: Camera { - is_active: false, - ..default() - }, - ..default() - }, - UiCameraConfig { show_ui: true }, - Name::new("Loading Camera"), - )); - commands .spawn(( Loading, @@ -62,11 +48,3 @@ fn initialize(mut commands: Commands) { },)); }); } - -fn deactivate_cameras( - mut events: Query<&mut Camera, (Added, Without)> -) { - events.iter_mut().for_each(|mut camera| { - camera.is_active = false; - }); -} diff --git a/src/main.rs b/src/main.rs index 31b3c4b..0dd6d61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ #![feature(iter_array_chunks)] // used in ray.rs -#![feature(iter_intersperse)] // used in debug.rs +#![feature(iter_intersperse)] // used in debug.rs mod audio; mod credits; @@ -29,11 +29,22 @@ fn main() { let mut app = App::new(); app.add_state::(); app.add_state::(); - app.add_systems(Update, state.run_if(resource_changed::>())); + app.add_systems( + Update, + ( + debug_state::.run_if(resource_changed::>()), + debug_state::.run_if(resource_changed::>()), + ), + ); app.add_systems(Update, loading.run_if(in_state(GameState::Loading))); app.add_systems( Update, - toggle_display_mode.run_if(on_event::()), + ( + toggle_display_mode.run_if(on_event::()), + // TODO: Run this other times too so we ensure a camera is always active + toggle_display_camera.run_if(state_changed::()), + toggle_display_camera.run_if(state_changed::()), + ), ); app.add_plugins( DefaultPlugins @@ -116,9 +127,8 @@ fn loading( /// System for printing the current state /// /// Only runs when state is modified. -fn state(game_state: Res>, display_state: Res>) { - info!("Game State is {:?}", *game_state); - info!("Display State is {:?}", *display_state); +fn debug_state(state: Res>) { + info!("State change {:?}", *state); } fn toggle_display_mode( @@ -139,25 +149,26 @@ fn toggle_display_mode( }) } +fn toggle_display_camera( + state: Res>, + mut cameras: Query<(&mut Camera, &DisplayState)>, +) { + cameras.iter_mut().for_each(|(mut camera, display_state)| { + camera.is_active = display_state == state.get(); + }); +} + fn activate( - mut cameras: Query<&mut Camera, With>, mut entities: Query<&mut Visibility, (With, Without)>, ) { - cameras.iter_mut().for_each(|mut camera| { - camera.is_active = true; - }); entities.iter_mut().for_each(|mut visibility| { *visibility = Visibility::Visible; }); } fn deactivate( - mut cameras: Query<&mut Camera, With>, mut entities: Query<&mut Visibility, (With, Without)>, ) { - cameras.iter_mut().for_each(|mut camera| { - camera.is_active = false; - }); entities.iter_mut().for_each(|mut visibility| { *visibility = Visibility::Hidden; }); diff --git a/src/menu.rs b/src/menu.rs index 8bde1c6..e412086 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -33,18 +33,6 @@ struct Menu; struct Quit; fn init_menu_ui(mut commands: Commands) { - commands.spawn(( - Menu, - Camera2dBundle { - camera: Camera { - is_active: false, - ..default() - }, - ..default() - }, - UiCameraConfig { show_ui: true }, - Name::new("Menu Camera"), - )); commands .spawn(( Menu, @@ -67,7 +55,6 @@ fn init_menu_ui(mut commands: Commands) { parent .spawn(( GameState::Play, - DisplayState::Display3d, ButtonBundle { style: Style { padding: UiRect::all(Val::Px(5.0)), @@ -81,7 +68,6 @@ fn init_menu_ui(mut commands: Commands) { .with_children(|parent| { parent.spawn(( GameState::Play, - DisplayState::Display3d, TextBundle::from_section( "Start", TextStyle { @@ -150,14 +136,12 @@ fn init_menu_ui(mut commands: Commands) { } fn handle_menu_button( - events: Query<(&Interaction, &GameState, &DisplayState), (With