refactor: use private plugins

This commit is contained in:
Kristofers Solo 2024-12-08 20:42:14 +02:00
parent 80bc027477
commit c694281e88
9 changed files with 598 additions and 318 deletions

789
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,32 @@
mod plugin;
mod ui;
pub use plugin::DevToolsPlugin;
use crate::screens::Screen;
use bevy::{
dev_tools::{
states::log_transitions,
ui_debug_overlay::{DebugUiPlugin, UiDebugOptions},
},
input::common_conditions::input_just_pressed,
prelude::*,
};
use bevy_egui::EguiPlugin;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use ui::maze_controls_ui;
pub(super) fn plugin(app: &mut App) {
app.add_systems(Update, log_transitions::<Screen>)
.add_plugins(EguiPlugin)
.add_plugins(WorldInspectorPlugin::new())
.add_plugins(DebugUiPlugin)
.add_systems(Update, maze_controls_ui)
.add_systems(
Update,
toggle_debug_ui.run_if(input_just_pressed(TOGGLE_KEY)),
);
}
const TOGGLE_KEY: KeyCode = KeyCode::Backquote;
fn toggle_debug_ui(mut options: ResMut<UiDebugOptions>) {
options.toggle();
}

View File

@ -1,36 +0,0 @@
use crate::screens::Screen;
use bevy::{
dev_tools::{
states::log_transitions,
ui_debug_overlay::{DebugUiPlugin, UiDebugOptions},
},
input::common_conditions::input_just_pressed,
prelude::*,
};
use bevy_egui::EguiPlugin;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use super::ui::maze_controls_ui;
#[derive(Debug)]
pub struct DevToolsPlugin;
impl Plugin for DevToolsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, log_transitions::<Screen>)
.add_plugins(EguiPlugin)
.add_plugins(WorldInspectorPlugin::new())
.add_plugins(DebugUiPlugin)
.add_systems(Update, maze_controls_ui)
.add_systems(
Update,
toggle_debug_ui.run_if(input_just_pressed(TOGGLE_KEY)),
);
}
}
const TOGGLE_KEY: KeyCode = KeyCode::Backquote;
fn toggle_debug_ui(mut options: ResMut<UiDebugOptions>) {
options.toggle();
}

View File

@ -3,6 +3,7 @@ pub mod audio;
#[cfg(feature = "dev")]
mod dev_tools;
mod maze;
mod player;
mod screens;
mod theme;
@ -57,14 +58,15 @@ impl Plugin for AppPlugin {
// Add other plugins.
app.add_plugins((
asset_tracking::plugin,
maze::plugin::MazePlugin,
screens::plugin,
theme::plugin,
maze::plugin,
player::plugin,
));
// Enable dev tools for dev builds.
#[cfg(feature = "dev")]
app.add_plugins(dev_tools::DevToolsPlugin);
app.add_plugins(dev_tools::plugin);
}
}

View File

@ -1,14 +1,21 @@
use bevy::{ecs::world::Command, prelude::*};
use plugin::MazePlugin;
use bevy::{ecs::system::RunSystemOnce, prelude::*};
use events::RecreateMazeEvent;
mod assets;
mod components;
pub mod events;
pub mod plugin;
mod resources;
mod systems;
pub use resources::{MazeConfig, MazePluginLoaded};
use systems::recreation::handle_maze_recreation_event;
pub(super) fn plugin(app: &mut App) {
app.init_resource::<MazeConfig>()
.add_event::<RecreateMazeEvent>()
.add_systems(Update, handle_maze_recreation_event);
}
pub fn spawn_maze(world: &mut World) {
MazePlugin.apply(world);
world.insert_resource(MazePluginLoaded);
world.run_system_once(systems::setup::setup);
}

View File

@ -1,8 +1,7 @@
use std::num::TryFromIntError;
use bevy::prelude::*;
use hexx::{Hex, HexLayout, HexOrientation};
use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
use std::num::TryFromIntError;
use thiserror::Error;
#[derive(Debug, Default, Reflect, Resource)]

5
src/player/components.rs Normal file
View File

@ -0,0 +1,5 @@
use bevy::prelude::*;
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Default, Reflect)]
#[reflect(Component)]
pub struct Player;

9
src/player/mod.rs Normal file
View File

@ -0,0 +1,9 @@
mod components;
mod systems;
use bevy::prelude::*;
use components::Player;
pub(super) fn plugin(app: &mut App) {
app.register_type::<Player>().add_plugins(());
}

21
src/player/systems.rs Normal file
View File

@ -0,0 +1,21 @@
use super::components::Player;
use crate::maze::{events::RecreateMazeEvent, MazeConfig};
use bevy::prelude::*;
pub fn spawn_player(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
maze_config: Res<MazeConfig>,
) {
}
pub fn respawn_player(
mut commands: Commands,
player_query: Query<Entity, With<Player>>,
mut recreation_events: EventReader<RecreateMazeEvent>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
maze_config: Res<MazeConfig>,
) {
}