moonlit/src/debug.rs

113 lines
2.9 KiB
Rust

use bevy::prelude::*;
use crate::game::darkness::LightAabb;
pub struct DebugPlugin;
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub struct DebugSet;
impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) {
app.configure_set(Last, DebugSet.run_if(is_debug_enabled));
app.insert_resource(DebugMode::off())
.add_plugins(bevy_prototype_debug_lines::DebugLinesPlugin::default())
.add_plugins((
bevy_inspector_egui::quick::WorldInspectorPlugin::new().run_if(is_debug_enabled),
bevy_rapier2d::prelude::RapierDebugRenderPlugin::default(),
))
.add_systems(Update, debug_toggle)
.add_systems(Last, (light_boundaries).in_set(DebugSet));
}
}
#[derive(Reflect, Resource, Default)]
#[reflect(Resource)]
pub struct DebugMode {
pub enabled: bool,
}
impl DebugMode {
pub fn on() -> Self {
Self { enabled: true }
}
pub fn off() -> Self {
Self { enabled: false }
}
}
fn is_debug_enabled(debug_mode: Res<DebugMode>) -> bool {
debug_mode.enabled
}
fn debug_toggle(input: Res<Input<KeyCode>>, mut debug_mode: ResMut<DebugMode>) {
if input.just_pressed(KeyCode::P) {
debug_mode.enabled = !debug_mode.enabled
}
}
fn light_boundaries(
mut debug_draw: ResMut<bevy_prototype_debug_lines::DebugLines>,
point_query: Query<(&GlobalTransform, &crate::game::darkness::PointLight2D)>,
spot_query: Query<(&GlobalTransform, &crate::game::darkness::SpotLight2D)>,
) {
for (tranform, light) in &point_query {
let rect = light.aabb();
draw_rect(
Rect::from_center_size(
rect.center() + tranform.translation().truncate(),
rect.size(),
),
0.0,
Color::RED,
&mut debug_draw,
)
}
for (tranform, light) in &spot_query {
let rect = light.aabb();
draw_rect(
Rect::from_center_size(
rect.center() + tranform.translation().truncate(),
rect.size(),
),
0.0,
Color::RED,
&mut debug_draw,
)
}
}
fn draw_rect(
rect: Rect,
duration: f32,
color: Color,
debug_draw: &mut ResMut<bevy_prototype_debug_lines::DebugLines>,
) {
debug_draw.line_colored(
Vec3::new(rect.min.x, rect.min.y, 0.0),
Vec3::new(rect.max.x, rect.min.y, 0.0),
duration,
color,
);
debug_draw.line_colored(
Vec3::new(rect.max.x, rect.min.y, 0.0),
Vec3::new(rect.max.x, rect.max.y, 0.0),
duration,
color,
);
debug_draw.line_colored(
Vec3::new(rect.max.x, rect.max.y, 0.0),
Vec3::new(rect.min.x, rect.max.y, 0.0),
duration,
color,
);
debug_draw.line_colored(
Vec3::new(rect.min.x, rect.max.y, 0.0),
Vec3::new(rect.min.x, rect.min.y, 0.0),
duration,
color,
);
}