Skip to main content

wde_terrain_navigation/
lib.rs

1use bevy::prelude::*;
2
3mod navigation;
4mod navmap;
5
6pub mod prelude {
7    pub use super::TerrainNavigator;
8}
9
10pub struct TerrainNavigationPlugin;
11impl Plugin for TerrainNavigationPlugin {
12    fn build(&self, app: &mut App) {
13        app.add_plugins(navigation::NavigationPlugin)
14            .init_resource::<TerrainNavigator>()
15            .add_systems(Update, handle_changes);
16    }
17}
18
19#[derive(Resource, Default)]
20pub struct TerrainNavigator {
21    new_entities: Vec<(Entity, Vec3)>,
22    removed_entities: Vec<Entity>
23}
24impl TerrainNavigator {
25    pub fn add(&mut self, entity: Entity, position: Vec3) {
26        self.new_entities.push((entity, position));
27    }
28
29    pub fn remove(&mut self, entity: Entity) {
30        self.removed_entities.push(entity);
31    }
32}
33
34#[derive(Component)]
35pub(crate) struct TargetLocation(Vec3);
36
37fn handle_changes(mut commands: Commands, mut navigator: ResMut<TerrainNavigator>) {
38    for (entity, position) in navigator.new_entities.drain(..) {
39        commands.entity(entity).insert(TargetLocation(position));
40    }
41
42    for entity in navigator.removed_entities.drain(..) {
43        commands.entity(entity).remove::<TargetLocation>();
44    }
45}