bevy_northstar 0.3.0

A Bevy plugin for Hierarchical Pathfinding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Plugin to add systems for drawing gizmos. For debugging pathfinding.
use bevy::{color::palettes::css, math::Vec2, prelude::*};

use crate::{
    components::{DebugGrid, DebugPath},
    grid::Grid,
    neighbor::Neighborhood,
    path::Path,
    prelude::{AgentOfGrid, DebugOffset},
};

/// Required to calculate how to draw the debug gizmos
#[derive(Reflect, Debug, Clone, Default)]
pub enum DebugTilemapType {
    #[default]
    Square,
    Isometric,
}

/// Debug plugin for the Northstar pathfinding library.
/// Add this plugin to your app to add the systems required to draw the debug helper gizmos.
///
/// # Example
///
/// ```rust,no_run
/// use bevy::prelude::*;
/// use bevy_northstar::prelude::*;
///
/// fn main() {
///    App::new()
///       .add_plugins(DefaultPlugins)
///       .add_plugins(NorthstarDebugPlugin::<CardinalNeighborhood>::default())
///       .add_systems(Startup, setup);
/// }
///
/// fn setup(mut commands: Commands) {
///   let debug_grid = DebugGridBuilder::new(32, 32)
///      .enable_chunks()
///      .enable_entrances()
///      .enable_cached_paths()
///      .build();
///
///   commands.spawn(debug_grid);
/// }
///
#[derive(Clone)]
pub struct NorthstarDebugPlugin<N: Neighborhood + 'static> {
    /// Debug gizmos configuration
    pub _marker: std::marker::PhantomData<N>,
}

impl<N: Neighborhood + 'static> Default for NorthstarDebugPlugin<N> {
    fn default() -> Self {
        NorthstarDebugPlugin {
            _marker: std::marker::PhantomData,
        }
    }
}

impl<N: Neighborhood + 'static> Plugin for NorthstarDebugPlugin<N> {
    fn build(&self, app: &mut bevy::prelude::App) {
        app.add_systems(Update, (draw_debug_map::<N>, draw_debug_paths::<N>))
            .register_type::<DebugGrid>()
            .register_type::<DebugTilemapType>();
    }
}

// / Draw the debug gizmos for the chunks, cells, entrances, and cached paths.
fn draw_debug_map<N: Neighborhood + 'static>(
    query: Query<(&DebugOffset, &DebugGrid)>,
    grid: Query<&Grid<N>>,
    mut gizmos: Gizmos,
) {
    let grid = if let Ok(grid) = grid.single() {
        grid
    } else {
        return;
    };

    for (debug_offset, debug_grid) in query.iter() {
        //let offset = transform.translation.truncate();
        let offset = debug_offset.0.truncate();

        let half_tile_width = debug_grid.tile_width as f32 * 0.5;
        let half_tile_height = debug_grid.tile_height as f32 * 0.5;

        if debug_grid.draw_chunks {
            // Draw chunk boundaries
            let chunk_size = grid.chunk_size();
            let chunk_width = grid.width() / chunk_size;
            let chunk_height = grid.height() / chunk_size;

            match debug_grid.map_type {
                DebugTilemapType::Square => {
                    for x in 0..chunk_width {
                        for y in 0..chunk_height {
                            let bottom_left = Vec2::new(
                                (x * chunk_size) as f32 * debug_grid.tile_width as f32,
                                (y * chunk_size) as f32 * debug_grid.tile_height as f32,
                            );
                            let bottom_right = Vec2::new(
                                ((x + 1) * chunk_size) as f32 * debug_grid.tile_width as f32,
                                (y * chunk_size) as f32 * debug_grid.tile_height as f32,
                            );
                            let top_left = Vec2::new(
                                (x * chunk_size) as f32 * debug_grid.tile_width as f32,
                                ((y + 1) * chunk_size) as f32 * debug_grid.tile_height as f32,
                            );
                            let top_right = Vec2::new(
                                ((x + 1) * chunk_size) as f32 * debug_grid.tile_width as f32,
                                ((y + 1) * chunk_size) as f32 * debug_grid.tile_height as f32,
                            );

                            let bottom_left = bottom_left + offset - half_tile_width;
                            let bottom_right = bottom_right + offset - half_tile_height;
                            let top_left = top_left + offset - half_tile_width;
                            let top_right = top_right + offset - half_tile_height;

                            gizmos.line_2d(bottom_left, bottom_right, css::WHITE);
                            gizmos.line_2d(bottom_right, top_right, css::WHITE);
                            gizmos.line_2d(top_right, top_left, css::WHITE);
                            gizmos.line_2d(top_left, bottom_left, css::WHITE);
                        }
                    }
                }
                DebugTilemapType::Isometric => {
                    for x in 1..chunk_width {
                        for y in 1..chunk_height {
                            let position = Vec2::new(
                                (y as f32 + x as f32) * half_tile_width * chunk_size as f32,
                                (y as f32 - x as f32) * half_tile_height * chunk_size as f32,
                            );

                            let top = position
                                + Vec2::new(0.0, debug_grid.tile_height as f32 * chunk_size as f32);
                            let right = position
                                + Vec2::new(debug_grid.tile_width as f32 * chunk_size as f32, 0.0);
                            let bottom = position
                                - Vec2::new(0.0, debug_grid.tile_height as f32 * chunk_size as f32);
                            let left = position
                                - Vec2::new(debug_grid.tile_width as f32 * chunk_size as f32, 0.0);

                            let offset = offset - Vec2::new(half_tile_width, half_tile_height);

                            gizmos.line_2d(top + offset, right + offset, css::WHITE);
                            gizmos.line_2d(right + offset, bottom + offset, css::WHITE);
                            gizmos.line_2d(bottom + offset, left + offset, css::WHITE);
                            gizmos.line_2d(left + offset, top + offset, css::WHITE);
                        }
                    }
                }
            }
        }

        if debug_grid.draw_cells {
            // Draw cell gizmos
            for x in 0..grid.width() {
                for y in 0..grid.height() {
                    let cell = grid.navcell(UVec3::new(x, y, 0));
                    let color = if cell.is_impassable() {
                        css::RED
                    } else {
                        css::WHITE
                    };

                    let position = match debug_grid.map_type {
                        DebugTilemapType::Square => Vec2::new(
                            (x * debug_grid.tile_width) as f32,
                            (y * debug_grid.tile_height) as f32,
                        ),
                        DebugTilemapType::Isometric => Vec2::new(
                            (y as f32 + x as f32) * half_tile_width,
                            (y as f32 - x as f32) * half_tile_height - half_tile_height,
                        ),
                    };

                    gizmos.circle_2d(position + offset, 2.0, color);
                }
            }
        }

        if debug_grid.draw_entrances {
            // Draw graph nodes
            for node in grid.graph().nodes() {
                let position = match debug_grid.map_type {
                    DebugTilemapType::Square => Vec2::new(
                        (node.pos.x * debug_grid.tile_width) as f32,
                        (node.pos.y * debug_grid.tile_height) as f32,
                    ),
                    DebugTilemapType::Isometric => Vec2::new(
                        (node.pos.y as f32 + node.pos.x as f32) * half_tile_width,
                        (node.pos.y as f32 - node.pos.x as f32) * half_tile_height
                            - half_tile_height,
                    ),
                };

                gizmos.circle_2d(position + offset, 2.0, css::MAGENTA);

                // Draw the node connection only to nodes in other chunks
                for edge in node.edges() {
                    let neighbor = grid.graph().node_at(edge);
                    if let Some(neighbor) = neighbor {
                        if neighbor.chunk != node.chunk {
                            let neighbor_position = match debug_grid.map_type {
                                DebugTilemapType::Square => Vec2::new(
                                    (neighbor.pos.x * debug_grid.tile_width) as f32,
                                    (neighbor.pos.y * debug_grid.tile_height) as f32,
                                ),
                                DebugTilemapType::Isometric => Vec2::new(
                                    (neighbor.pos.y as f32 + neighbor.pos.x as f32)
                                        * half_tile_width,
                                    (neighbor.pos.y as f32 - neighbor.pos.x as f32)
                                        * half_tile_height
                                        - half_tile_height,
                                ),
                            };
                            gizmos.line_2d(
                                position + offset,
                                neighbor_position + offset,
                                css::GREEN,
                            );
                        }
                    }
                }
            }
        }

        if debug_grid.draw_cached_paths {
            let path_colors = [
                css::RED,
                css::PURPLE,
                css::YELLOW,
                css::PINK,
                css::DARK_CYAN,
                css::MAGENTA,
                css::GREY,
                css::GREEN,
                css::ORANGE,
                css::NAVY,
            ];

            let mut color_index = 0;

            // Draw cached paths
            for path in grid.graph().all_paths() {
                if debug_grid.draw_entrances && !debug_grid.draw_cached_paths && path.len() > 2 {
                    continue;
                }

                // Iterate over path.path() drawing a line from one cell to the next cell until completed
                let mut iter = path.path().iter();
                let mut prev = iter.next().unwrap();

                for next in iter {
                    let prev_position = match debug_grid.map_type {
                        DebugTilemapType::Square => Vec2::new(
                            (prev.x * debug_grid.tile_width) as f32,
                            (prev.y * debug_grid.tile_height) as f32,
                        ),
                        DebugTilemapType::Isometric => Vec2::new(
                            (prev.y as f32 + prev.x as f32) * (debug_grid.tile_width as f32 * 0.5),
                            (prev.y as f32 - prev.x as f32) * (debug_grid.tile_height as f32 * 0.5),
                        ),
                    };

                    let next_position = match debug_grid.map_type {
                        DebugTilemapType::Square => Vec2::new(
                            (next.x * debug_grid.tile_width) as f32,
                            (next.y * debug_grid.tile_height) as f32,
                        ),
                        DebugTilemapType::Isometric => Vec2::new(
                            (next.y as f32 + next.x as f32) * (debug_grid.tile_width as f32 * 0.5),
                            (next.y as f32 - next.x as f32) * (debug_grid.tile_height as f32 * 0.5),
                        ),
                    };

                    let mut color = css::BLUE;

                    if debug_grid.draw_entrances && debug_grid.draw_cached_paths {
                        color = path_colors[color_index % path_colors.len()];
                    }

                    gizmos.line_2d(prev_position + offset, next_position + offset, color);

                    prev = next;
                }

                color_index += 1;
            }
        }
    }
}

fn draw_debug_paths<N: Neighborhood + 'static>(
    grid_children: Query<(Entity, &Children), With<Grid<N>>>,
    debug_grid: Query<(&DebugGrid, &DebugOffset)>,
    debug_paths: Query<(&DebugPath, &Path, &AgentOfGrid)>,
    mut gizmos: Gizmos,
) {
    for (grid_entity, child) in grid_children {
        // Find the DebugGrid component for the Grid entity
        let debug_grid_vec: Vec<_> = child
            .iter()
            .filter_map(|child_entity| debug_grid.get(child_entity).ok())
            .collect();

        if debug_grid_vec.is_empty() {
            continue;
        }

        if debug_grid_vec.len() > 1 {
            warn!(
                "Multiple DebugGrid components found for Grid entity: {:?}",
                grid_entity
            );
        }

        let (debug_grid, debug_offset) = debug_grid_vec[0];

        let center_offset = debug_offset.0.truncate();

        for (debug_path, path, parent_grid) in debug_paths {
            if parent_grid.0 != grid_entity {
                continue;
            }

            if path.is_empty() {
                continue;
            }

            let half_tile_width = debug_grid.tile_width as f32 * 0.5;
            let half_tile_height = debug_grid.tile_height as f32 * 0.5;

            // Iterate over path.path() drawing a line from one cell to the next cell until completed
            let mut iter = path.path().iter();
            let mut prev = iter.next().unwrap();

            for next in iter {
                let prev_position = match debug_grid.map_type {
                    DebugTilemapType::Square => Vec2::new(
                        (prev.x * debug_grid.tile_width) as f32,
                        (prev.y * debug_grid.tile_height) as f32,
                    ),
                    DebugTilemapType::Isometric => Vec2::new(
                        (prev.y as f32 + prev.x as f32) * half_tile_width,
                        (prev.y as f32 - prev.x as f32) * half_tile_height - half_tile_height,
                    ),
                };

                let next_position = match debug_grid.map_type {
                    DebugTilemapType::Square => Vec2::new(
                        (next.x * debug_grid.tile_width) as f32,
                        (next.y * debug_grid.tile_height) as f32,
                    ),
                    DebugTilemapType::Isometric => Vec2::new(
                        (next.y as f32 + next.x as f32) * half_tile_width,
                        (next.y as f32 - next.x as f32) * half_tile_height - half_tile_height,
                    ),
                };

                let x = prev_position + center_offset;
                let y = next_position + center_offset;

                gizmos.line_2d(x, y, debug_path.color);

                prev = next;
            }

            if debug_path.draw_unrefined {
                let mut iter = path.graph_path.iter();
                let mut prev = if let Some(p) = iter.next() {
                    p
                } else {
                    continue;
                };

                let inverted_color = debug_path.color.to_srgba();
                let inverted_color = Color::srgba(
                    1.0 - inverted_color.red,
                    1.0 - inverted_color.green,
                    1.0 - inverted_color.blue,
                    inverted_color.alpha,
                );

                for next in iter {
                    let prev_position = match debug_grid.map_type {
                        DebugTilemapType::Square => Vec2::new(
                            (prev.x * debug_grid.tile_width) as f32,
                            (prev.y * debug_grid.tile_height) as f32,
                        ),
                        DebugTilemapType::Isometric => Vec2::new(
                            (prev.y as f32 + prev.x as f32) * (debug_grid.tile_width as f32 * 0.5),
                            (prev.y as f32 - prev.x as f32) * (debug_grid.tile_height as f32 * 0.5),
                        ),
                    };

                    let next_position = match debug_grid.map_type {
                        DebugTilemapType::Square => Vec2::new(
                            (next.x * debug_grid.tile_width) as f32,
                            (next.y * debug_grid.tile_height) as f32,
                        ),
                        DebugTilemapType::Isometric => Vec2::new(
                            (next.y as f32 + next.x as f32) * (debug_grid.tile_width as f32 * 0.5),
                            (next.y as f32 - next.x as f32) * (debug_grid.tile_height as f32 * 0.5),
                        ),
                    };

                    let x = prev_position + center_offset;
                    let y = next_position + center_offset;

                    gizmos.line_2d(x, y, inverted_color);

                    prev = next;
                }
            }
        }
    }
}