Renamed Draw to ColoredShape and merged shape clearing system to draw_shapes

feature/debug_draw
hheik 2025-02-25 15:13:07 +02:00
parent 9c76992905
commit dadea61352
2 changed files with 20 additions and 24 deletions

View File

@ -20,7 +20,6 @@ impl Plugin for DebugPlugin {
)); ));
app.register_type::<DebugCanvas>() app.register_type::<DebugCanvas>()
.add_systems(First, clear_shapes)
.add_systems(Update, debug_toggle) .add_systems(Update, debug_toggle)
// TODO: Check if this could be scheduled just before render instead // TODO: Check if this could be scheduled just before render instead
.add_systems(PostUpdate, draw_shapes); .add_systems(PostUpdate, draw_shapes);
@ -43,9 +42,8 @@ impl DebugMode {
} }
} }
/// TODO: Rename to something smarter
#[derive(Clone, Copy, Debug, Reflect)] #[derive(Clone, Copy, Debug, Reflect)]
pub struct Draw { pub struct ColoredShape {
pub shape: Shape, pub shape: Shape,
pub color: Srgba, pub color: Srgba,
} }
@ -71,12 +69,12 @@ pub struct DebugCanvas;
#[derive(Debug, Default, Resource, Reflect)] #[derive(Debug, Default, Resource, Reflect)]
#[reflect(Resource)] #[reflect(Resource)]
pub struct DebugDraw { pub struct DebugDraw {
pub draw_queue: Vec<Draw>, pub draw_queue: Vec<ColoredShape>,
} }
impl DebugDraw { impl DebugDraw {
pub fn shape(&mut self, draw: Draw) { pub fn colored_shape(&mut self, colored_shape: ColoredShape) {
self.draw_queue.push(draw); self.draw_queue.push(colored_shape);
} }
} }
@ -90,17 +88,15 @@ fn debug_toggle(input: Res<ButtonInput<KeyCode>>, mut debug_mode: ResMut<DebugMo
} }
} }
fn clear_shapes(mut commands: Commands, canvas_query: Query<Entity, With<DebugCanvas>>) {
for parent in canvas_query.iter() {
commands.entity(parent).despawn_descendants();
}
}
fn draw_shapes( fn draw_shapes(
mut commands: Commands, mut commands: Commands,
canvas: Option<Single<Entity, With<DebugCanvas>>>, canvas: Option<Single<Entity, With<DebugCanvas>>>,
mut debug_draw: ResMut<DebugDraw>, mut debug_draw: ResMut<DebugDraw>,
) { ) {
// TODO: Re-use old shapes
if let Some(canvas) = canvas.as_ref() {
commands.entity(**canvas).despawn_descendants();
}
let canvas = match canvas { let canvas = match canvas {
Some(canvas) => *canvas, Some(canvas) => *canvas,
None => commands None => commands
@ -108,9 +104,9 @@ fn draw_shapes(
.id(), .id(),
}; };
commands.entity(canvas).with_children(|builder| { commands.entity(canvas).with_children(|builder| {
for draw in debug_draw.draw_queue.drain(..) { for ColoredShape { shape, color } in debug_draw.draw_queue.drain(..) {
// TODO // TODO
let path = match draw.shape { let path = match shape {
Shape::Line { from, to } => GeometryBuilder::build_as(&shapes::Line(from, to)), Shape::Line { from, to } => GeometryBuilder::build_as(&shapes::Line(from, to)),
Shape::Polygon { Shape::Polygon {
center, center,
@ -126,7 +122,7 @@ fn draw_shapes(
builder.spawn(( builder.spawn((
ShapeBundle { path, ..default() }, ShapeBundle { path, ..default() },
Fill::color(Srgba::NONE), Fill::color(Srgba::NONE),
Stroke::color(draw.color), Stroke::color(color),
)); ));
} }
}); });

View File

@ -1,7 +1,7 @@
use bevy::{color::palettes::css, prelude::*}; use bevy::{color::palettes::css, prelude::*};
use crate::{ use crate::{
debug::{DebugDraw, Draw, Shape}, debug::{ColoredShape, DebugDraw, Shape},
game::{ game::{
item::{Inventory, ItemSource, Stockpile}, item::{Inventory, ItemSource, Stockpile},
work::{Task, WorkType, Worker}, work::{Task, WorkType, Worker},
@ -82,10 +82,10 @@ pub fn draw_job_targets(
) { ) {
for (worker_entity, worker) in worker_query.iter() { for (worker_entity, worker) in worker_query.iter() {
let worker_global = global_query.get(worker_entity).unwrap(); let worker_global = global_query.get(worker_entity).unwrap();
let draws = match worker.0 { let colored_shapes = match worker.0 {
Some(task) => match task.work_type { Some(task) => match task.work_type {
WorkType::Gather => vec![ WorkType::Gather => vec![
Draw { ColoredShape {
shape: Shape::Polygon { shape: Shape::Polygon {
center: worker_global.translation().xy(), center: worker_global.translation().xy(),
sides: 3, sides: 3,
@ -93,7 +93,7 @@ pub fn draw_job_targets(
}, },
color: css::GREEN, color: css::GREEN,
}, },
Draw { ColoredShape {
shape: Shape::Line { shape: Shape::Line {
from: worker_global.translation().xy(), from: worker_global.translation().xy(),
to: global_query.get(task.target).unwrap().translation().xy(), to: global_query.get(task.target).unwrap().translation().xy(),
@ -102,7 +102,7 @@ pub fn draw_job_targets(
}, },
], ],
WorkType::Store(_) => vec![ WorkType::Store(_) => vec![
Draw { ColoredShape {
shape: Shape::Polygon { shape: Shape::Polygon {
center: worker_global.translation().xy(), center: worker_global.translation().xy(),
sides: 3, sides: 3,
@ -110,7 +110,7 @@ pub fn draw_job_targets(
}, },
color: css::YELLOW, color: css::YELLOW,
}, },
Draw { ColoredShape {
shape: Shape::Line { shape: Shape::Line {
from: worker_global.translation().xy(), from: worker_global.translation().xy(),
to: global_query.get(task.target).unwrap().translation().xy(), to: global_query.get(task.target).unwrap().translation().xy(),
@ -119,7 +119,7 @@ pub fn draw_job_targets(
}, },
], ],
}, },
None => vec![Draw { None => vec![ColoredShape {
shape: Shape::Polygon { shape: Shape::Polygon {
center: worker_global.translation().xy(), center: worker_global.translation().xy(),
sides: 3, sides: 3,
@ -128,8 +128,8 @@ pub fn draw_job_targets(
color: css::RED, color: css::RED,
}], }],
}; };
for draw in draws { for colored_shape in colored_shapes {
debug_draw.shape(draw); debug_draw.colored_shape(colored_shape);
} }
} }
} }