Skip to content

Commit da76e8d

Browse files
committed
big refactoring of plugin management
1 parent 43cf943 commit da76e8d

18 files changed

Lines changed: 760 additions & 662 deletions

File tree

src/camera/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ use bevy::prelude::*;
1616
use crate::camera::config::CameraConfig;
1717
use crate::camera::systems::{
1818
camera_movement, debug_draw_card_positions, handle_window_resize,
19-
manage_game_camera_visibility, set_initial_zoom, setup_camera,
19+
manage_game_camera_visibility, set_initial_zoom,
2020
};
21+
// Import the player debug system
22+
use crate::player::systems::debug::debug_draw_player_positions;
2123
#[cfg(feature = "snapshot")]
2224
use crate::snapshot::SnapshotPlugin;
2325

@@ -30,7 +32,8 @@ impl Plugin for CameraPlugin {
3032
#[cfg(feature = "snapshot")]
3133
app.add_plugins(SnapshotPlugin::new());
3234

33-
app.add_systems(Startup, setup_camera)
35+
// app.add_systems(Startup, setup_camera) // Moved to setup_game in RummagePlugin
36+
app // Removed setup_camera from Startup
3437
.add_systems(PostStartup, set_initial_zoom)
3538
.add_systems(
3639
Update,
@@ -39,6 +42,8 @@ impl Plugin for CameraPlugin {
3942
camera_movement,
4043
manage_game_camera_visibility,
4144
debug_draw_card_positions,
45+
// Add player debug drawing system here
46+
debug_draw_player_positions,
4247
),
4348
);
4449
}

src/menu/cleanup/main_menu.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
use crate::menu::components::MenuRoot;
2+
use crate::menu::main_menu::components::{MainMenuBackground, MainMenuMusic};
23
use bevy::prelude::*;
34

4-
/// Cleans up main menu entities by despawning the root node
5-
pub fn cleanup_main_menu(mut commands: Commands, menu_root_query: Query<Entity, With<MenuRoot>>) {
5+
/// Cleans up main menu entities including root, background, and music
6+
pub fn cleanup_main_menu(
7+
mut commands: Commands,
8+
menu_root_query: Query<Entity, With<MenuRoot>>,
9+
background_query: Query<Entity, With<MainMenuBackground>>,
10+
music_query: Query<Entity, With<MainMenuMusic>>,
11+
) {
612
// Despawn the root entity recursively
713
if let Ok(root_entity) = menu_root_query.get_single() {
814
info!("Despawning main menu root entity: {:?}", root_entity);
@@ -20,6 +26,18 @@ pub fn cleanup_main_menu(mut commands: Commands, menu_root_query: Query<Entity,
2026
info!("No MenuRoot entity found to clean up.");
2127
}
2228

29+
// Despawn background entities
30+
for entity in background_query.iter() {
31+
info!("Despawning main menu background entity: {:?}", entity);
32+
commands.entity(entity).despawn_recursive();
33+
}
34+
35+
// Despawn music entities
36+
for entity in music_query.iter() {
37+
info!("Despawning main menu music entity: {:?}", entity);
38+
commands.entity(entity).despawn_recursive();
39+
}
40+
2341
// Log the completion of cleanup
2442
info!("Main menu cleanup complete");
2543
}

src/menu/logo/plugin.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::menu::components::{MenuItem, ZLayers};
44
use crate::menu::decorations::MenuDecorativeElement;
55
use crate::menu::logo::text::{create_english_text, create_hebrew_text};
66
use crate::menu::star_of_david::create_star_of_david;
7-
use crate::menu::state::{GameMenuState, StateTransitionContext};
7+
use crate::menu::state::{AppState, GameMenuState, StateTransitionContext};
88
use bevy::prelude::*;
99

1010
/// Resource to track logo initialization attempts
@@ -47,7 +47,9 @@ impl Plugin for LogoPlugin {
4747
.add_systems(
4848
OnExit(GameMenuState::PauseMenu),
4949
cleanup_non_persistent_logo,
50-
);
50+
)
51+
// Add general cleanup when exiting the overall Menu AppState
52+
.add_systems(OnExit(AppState::Menu), cleanup_all_logos);
5153

5254
debug!("Logo plugin registered - combines Star of David with text");
5355
}
@@ -302,6 +304,21 @@ fn cleanup_non_persistent_logo(
302304
}
303305
}
304306

307+
/// Cleans up ALL logo entities, regardless of persistence
308+
fn cleanup_all_logos(mut commands: Commands, logos: Query<Entity, With<MenuDecorativeElement>>) {
309+
let mut count = 0;
310+
for entity in logos.iter() {
311+
commands.entity(entity).despawn_recursive();
312+
count += 1;
313+
}
314+
if count > 0 {
315+
info!(
316+
"Cleaned up {} logo entities on exiting AppState::Menu",
317+
count
318+
);
319+
}
320+
}
321+
305322
/// Sets up the pause menu logo
306323
fn setup_pause_logo(
307324
commands: Commands,

src/menu/pause.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy::prelude::*;
77

88
use super::systems::pause_menu::{
99
// Correct path for systems
10-
input_handler::esc_key_system,
10+
input_handler::{esc_key_system, handle_pause_trigger},
1111
interactions::pause_menu_action,
1212
setup::setup_pause_menu,
1313
};
@@ -29,6 +29,8 @@ impl Plugin for PauseMenuPlugin {
2929
)
3030
.run_if(in_state(GameMenuState::PauseMenu).and(in_state(AppState::Paused))),
3131
)
32+
// System to *trigger* the pause menu from the game
33+
.add_systems(Update, handle_pause_trigger.run_if(in_state(AppState::InGame)))
3234
// Exit pause menu cleanup - Commented out
3335
// .add_systems(
3436
// OnExit(GameMenuState::PauseMenu),

src/menu/save_load/systems/interactions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn handle_save_load_buttons(
2222
mut interaction_query: SaveLoadButtonInteractionQuery,
2323
mut save_load_state: ResMut<NextState<SaveLoadUiState>>,
2424
mut game_state: ResMut<NextState<GameMenuState>>,
25-
mut app_state: ResMut<NextState<AppState>>,
25+
mut _app_state: ResMut<NextState<AppState>>,
2626
mut save_events: EventWriter<SaveGameEvent>,
2727
mut load_events: EventWriter<LoadGameEvent>,
2828
context: ResMut<SaveLoadUiContext>,

src/menu/visibility/plugin.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::systems::{
66
force_main_menu_items_visibility, force_startup_visibility, update_menu_background,
77
};
88
use crate::menu::components::MenuVisibilityState;
9+
use crate::menu::state::AppState;
910
use crate::menu::ui::update_menu_visibility_state;
1011

1112
/// Plugin for managing menu item visibility and UI hierarchy
@@ -24,7 +25,8 @@ impl Plugin for MenuVisibilityPlugin {
2425
detect_ui_hierarchy_issues,
2526
update_menu_visibility_state,
2627
debug_menu_visibility,
27-
),
28+
)
29+
.run_if(in_state(AppState::Menu)),
2830
)
2931
// Main update systems - handle visibility changes
3032
.add_systems(
@@ -34,10 +36,14 @@ impl Plugin for MenuVisibilityPlugin {
3436
fix_visibility_for_changed_items,
3537
fix_changed_main_menu_visibility,
3638
force_main_menu_items_visibility,
37-
),
39+
)
40+
.run_if(in_state(AppState::Menu)),
3841
)
3942
// Background update system runs last
40-
.add_systems(Last, update_menu_background);
43+
.add_systems(
44+
Last,
45+
update_menu_background.run_if(in_state(AppState::Menu)),
46+
);
4147

4248
debug!("Visibility plugin registered");
4349
}

src/player/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ pub mod systems;
88

99
use bevy::prelude::*;
1010

11-
// Re-export the core components and systems
11+
// Import and re-export common player components and systems
1212
pub use components::Player;
1313
pub use playmat::PlayerPlaymatPlugin;
1414
pub use resources::PlayerConfig;
15-
pub use systems::{PlayerPositionTracker, debug_draw_player_positions};
15+
pub use systems::debug::{PlayerPositionTracker, debug_draw_player_positions};
1616

1717
/// Plugin for player-related functionality
1818
pub struct PlayerPlugin;

src/player/systems/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
mod debug;
1+
//! Player systems module - aggregates all player related logic
2+
3+
// Keep spawn module public as it's used by game setup
24
pub mod spawn;
35

4-
pub use debug::{PlayerPositionTracker, debug_draw_player_positions};
6+
// Make debug module public for external use (e.g., CameraPlugin)
7+
pub mod debug;
8+
9+
// Other player systems can remain private for now
10+
// mod interactions; // Example: handle player clicks, etc.
11+
// mod movement; // Example: if players could move independently
12+
// ... add other player system modules here

src/player/systems/spawn/cards.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::table::TableLayout;
2-
use crate::camera::components::{AppLayer, GameCamera};
2+
use crate::camera::components::AppLayer;
33
use crate::cards::components::card_entity::CardZone;
44
use crate::cards::drag::Draggable;
55
use crate::cards::text::card_text::spawn_card_text_components;
@@ -10,7 +10,6 @@ use bevy::prelude::*;
1010
/// Helper function to spawn visual card entities
1111
pub fn spawn_visual_cards(
1212
commands: &mut Commands,
13-
game_cameras: &Query<Entity, With<GameCamera>>,
1413
card_size: &Vec2,
1514
spacing_multiplier: f32,
1615
player_position: Vec3,
@@ -76,8 +75,8 @@ pub fn spawn_visual_cards(
7675
let card_clone = card.clone(); // Clone card to use later
7776

7877
// Calculate z-index based on position to ensure proper layering
79-
// Use a higher base z-value (30.0) to ensure cards are always above playmats (which use around 10.0)
80-
let z = 30.0 + (i as f32 * 1.0); // Increased z-index base to stay above playmats
78+
// Use a smaller base z-value to ensure cards are closer to the camera
79+
let z = 1.0 + (i as f32 * 0.1); // Drastically reduced z-index base
8180

8281
// Calculate the position for this card
8382
let position = Vec3::new(
@@ -161,7 +160,8 @@ pub fn spawn_visual_cards(
161160
// The RenderLayers component should handle visibility via the camera's layers.
162161
// Parenting to the camera makes the card's transform relative to the camera,
163162
// which is likely incorrect given the world-space position calculation above.
164-
// for camera in game_cameras.iter() {
163+
// We are relying on RenderLayers now.
164+
// for camera in game_cameras.iter() { // This loop is now invalid as game_cameras is removed
165165
// debug!(
166166
// "Attaching card for player {} to game camera {:?}",
167167
// player_index, camera

src/player/systems/spawn/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use bevy::prelude::*;
2525
pub fn spawn_players<'w, 's>(
2626
commands: &mut Commands<'w, 's>,
2727
asset_server: Res<AssetServer>,
28-
game_cameras: Query<'w, 's, Entity, With<crate::camera::components::GameCamera>>,
2928
player_config: Option<Res<PlayerConfig>>,
3029
) {
3130
// Use default config if none exists
@@ -133,14 +132,13 @@ pub fn spawn_players<'w, 's>(
133132
// Remove context creation, call spawn_visual_cards directly
134133
cards::spawn_visual_cards(
135134
commands, // Pass mutable commands directly
136-
&game_cameras,
137135
&config.card_size,
138136
config.card_spacing_multiplier,
139137
card_position,
140138
player_index,
141139
player_entity,
142140
&table,
143-
Some(&asset_server),
141+
Some(&asset_server).map(|v| &**v), // Convert Option<&Res<AssetServer>> to Option<&AssetServer>
144142
display_cards,
145143
);
146144
} else {

0 commit comments

Comments
 (0)