Skip to content

Commit

Permalink
Move the debugging code in the example to be part of `bevy_landmass::…
Browse files Browse the repository at this point in the history
…debug`.

Previously, all this debug drawing code was bespoke - every user needed to write their own version. Now it's part of bevy_landmass so every user can just toss it in immediately! Note we still provide `draw_archipelago_debug` as a low-level API in case users don't like my debug visualizations.
  • Loading branch information
andriyDev committed Jun 30, 2024
1 parent 2ed7577 commit 683fb12
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 79 deletions.
1 change: 1 addition & 0 deletions crates/bevy_landmass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type_complexity = "allow"
[dependencies]
bevy = { version = "0.13.0", default-features = false, features = [
"bevy_asset",
"bevy_gizmos",
] }
landmass = { path = "../landmass", version = "0.4.0" }

Expand Down
73 changes: 0 additions & 73 deletions crates/bevy_landmass/example/debug.rs

This file was deleted.

23 changes: 18 additions & 5 deletions crates/bevy_landmass/example/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use std::sync::Arc;

use bevy::{prelude::*, render::mesh::CylinderMeshBuilder};
use bevy_landmass::{nav_mesh::bevy_mesh_to_landmass_nav_mesh, prelude::*};

mod debug;
use bevy::{
input::common_conditions::input_just_pressed, prelude::*,
render::mesh::CylinderMeshBuilder,
};
use bevy_landmass::{
debug::{EnableLandmassDebug, LandmassDebugPlugin},
nav_mesh::bevy_mesh_to_landmass_nav_mesh,
prelude::*,
};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(LandmassPlugin)
.add_plugins(debug::DebugPlugin)
.add_plugins(LandmassDebugPlugin::default())
.add_systems(Startup, setup)
.add_systems(Update, (convert_mesh, handle_clicks))
.add_systems(Update, toggle_debug.run_if(input_just_pressed(KeyCode::F12)))
.add_systems(
Update,
(update_agent_velocity, move_agent_by_velocity).chain(),
Expand Down Expand Up @@ -199,9 +205,11 @@ fn move_agent_by_velocity(
}
}

/// Marker component for the target entity.
#[derive(Component)]
struct Target;

/// Handles clicks by spawning agents with LMB and moving the target with RMB.
fn handle_clicks(
buttons: Res<ButtonInput<MouseButton>>,
window_query: Query<&Window>,
Expand Down Expand Up @@ -240,3 +248,8 @@ fn handle_clicks(
.insert(Transform::from_translation(world_position));
}
}

/// System for toggling the `EnableLandmassDebug` resource.
fn toggle_debug(mut debug: ResMut<EnableLandmassDebug>) {
**debug = !**debug;
}
112 changes: 111 additions & 1 deletion crates/bevy_landmass/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
use crate::{util::landmass_vec3_to_bevy_vec3, Archipelago, LandmassSystemSet};
use bevy::{
app::Update,
gizmos::AppGizmoBuilder,
math::Quat,
prelude::{
Deref, DerefMut, GizmoConfig, GizmoConfigGroup, Gizmos, IntoSystemConfigs,
Plugin, Query, Res, Resource,
},
reflect::Reflect,
render::color::Color,
time::Time,
};

pub use landmass::debug::*;

/// Draws all parts of `archipelago` to `debug_drawer`.
/// Draws all parts of `archipelago` to `debug_drawer`. This is a lower level
/// API to allow custom debug drawing. For a pre-made implementation, use
/// [`LandmassDebugPlugin`].
pub fn draw_archipelago_debug(
archipelago: &crate::Archipelago,
debug_drawer: &mut impl DebugDrawer,
Expand All @@ -10,3 +26,97 @@ pub fn draw_archipelago_debug(
debug_drawer,
)
}

/// A plugin to draw landmass debug data with Bevy gizmos.
pub struct LandmassDebugPlugin {
/// Whether to begin drawing on startup.
pub draw_on_start: bool,
}

impl Default for LandmassDebugPlugin {
fn default() -> Self {
Self { draw_on_start: true }
}
}

impl Plugin for LandmassDebugPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app
.insert_resource(EnableLandmassDebug(self.draw_on_start))
.add_systems(
Update,
draw_archipelagos_default
.in_set(LandmassSystemSet::Output)
.run_if(|enable: Res<EnableLandmassDebug>| enable.0),
)
.insert_gizmo_group(
LandmassGizmoConfigGroup,
GizmoConfig { depth_bias: -1.0, ..Default::default() },
);
}
}

/// A resource controlling whether debug data is drawn.
#[derive(Resource, Default, Deref, DerefMut)]
pub struct EnableLandmassDebug(pub bool);

/// A config group for landmass debug gizmos.
#[derive(Default, Reflect, GizmoConfigGroup)]
pub struct LandmassGizmoConfigGroup;

/// A gizmo debug drawer.
struct GizmoDrawer<'w, 's, 'a>(
&'a mut Gizmos<'w, 's, LandmassGizmoConfigGroup>,
);

impl<'w, 's, 'a> DebugDrawer for GizmoDrawer<'w, 's, 'a> {
fn add_point(&mut self, point_type: PointType, point: landmass::Vec3) {
self.0.sphere(
landmass_vec3_to_bevy_vec3(point),
Quat::IDENTITY,
0.2,
match point_type {
PointType::AgentPosition(_) => Color::rgba(0.0, 1.0, 0.0, 0.6),
PointType::TargetPosition(_) => Color::rgba(1.0, 1.0, 0.0, 0.6),
PointType::Waypoint(_) => Color::rgba(0.6, 0.6, 0.6, 0.6),
},
);
}

fn add_line(&mut self, line_type: LineType, line: [landmass::Vec3; 2]) {
self.0.line(
landmass_vec3_to_bevy_vec3(line[0]),
landmass_vec3_to_bevy_vec3(line[1]),
match line_type {
LineType::BoundaryEdge => Color::rgba(0.0, 0.0, 1.0, 0.6),
LineType::ConnectivityEdge => Color::rgba(0.5, 0.5, 1.0, 0.6),
LineType::AgentCorridor(_) => Color::rgba(0.6, 0.0, 0.6, 0.6),
LineType::Target(_) => Color::rgba(1.0, 1.0, 0.0, 0.6),
LineType::Waypoint(_) => Color::rgba(0.6, 0.6, 0.6, 0.6),
},
);
}

fn add_triangle(
&mut self,
_triangle_type: TriangleType,
_triangle: [landmass::Vec3; 3],
) {
// Bevy doesn't have a way to draw triangles :'(
}
}

/// A system for drawing debug data.
fn draw_archipelagos_default(
time: Res<Time>,
archipelagos: Query<&Archipelago>,
mut gizmos: Gizmos<'_, '_, LandmassGizmoConfigGroup>,
) {
if time.delta_seconds() == 0.0 {
return;
}
let mut drawer = GizmoDrawer(&mut gizmos);
for archipelago in archipelagos.iter() {
draw_archipelago_debug(archipelago, &mut drawer);
}
}

0 comments on commit 683fb12

Please sign in to comment.