-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathauto_navmesh_aabb.rs
211 lines (197 loc) · 6.83 KB
/
auto_navmesh_aabb.rs
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
use std::f32::consts::PI;
use bevy::{
color::palettes,
math::vec2,
prelude::*,
render::primitives::Aabb,
window::{PrimaryWindow, WindowResized},
};
use polyanya::Triangulation;
use rand::Rng;
use vleue_navigator::prelude::*;
#[path = "helpers/agent2d.rs"]
mod agent;
#[path = "helpers/ui.rs"]
mod ui;
const MESH_WIDTH: u32 = 15;
const MESH_HEIGHT: u32 = 10;
#[derive(Component, Debug)]
struct Obstacle;
const FACTOR: f32 = 70.0;
fn main() {
App::new()
.insert_resource(ClearColor(palettes::css::BLACK.into()))
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Navmesh with Polyanya".to_string(),
fit_canvas_to_parent: true,
..default()
}),
..default()
}),
VleueNavigatorPlugin,
// Auto update the navmesh.
// Obstacles will be entities with the `Obstacle` marker component,
// and use the `Aabb` component as the obstacle data source.
NavmeshUpdaterPlugin::<Aabb, Obstacle>::default(),
))
.add_systems(
Startup,
(
setup,
ui::setup_stats::<true>,
ui::setup_settings::<false>,
agent::setup_agent::<10>,
),
)
.add_systems(
Update,
(
display_mesh,
spawn_obstacle_on_click.after(ui::update_settings::<50>),
ui::update_stats::<Obstacle>,
remove_obstacles,
ui::display_settings,
ui::update_settings::<50>,
agent::give_target_to_navigator::<10, MESH_WIDTH, MESH_HEIGHT>,
agent::move_navigator,
agent::display_navigator_path,
agent::refresh_path::<10, MESH_WIDTH, MESH_HEIGHT>,
),
)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
// Spawn a new navmesh that will be automatically updated.
commands.spawn((
NavMeshSettings {
// Define the outer borders of the navmesh.
fixed: Triangulation::from_outer_edges(&[
vec2(0.0, 0.0),
vec2(MESH_WIDTH as f32, 0.0),
vec2(MESH_WIDTH as f32, MESH_HEIGHT as f32),
vec2(0.0, MESH_HEIGHT as f32),
]),
..default()
},
// Mark it for update as soon as obstacles are changed.
// Other modes can be debounced or manually triggered.
NavMeshUpdateMode::Direct,
Transform::from_translation(Vec3::new(
-(MESH_WIDTH as f32) / 2.0 * FACTOR,
-(MESH_HEIGHT as f32) / 2.0 * FACTOR,
0.0,
))
.with_scale(Vec3::splat(FACTOR)),
));
// Spawn a few obstacles to start with.
// They need
// - the `Obstacle` marker component
// - the `Aabb` component to define the obstacle's shape
// - the `Transform` component to define the obstacle's position
// - the `GlobalTransform` so that it's correctly handled by Bevy
let mut rng = rand::thread_rng();
for _ in 0..100 {
commands.spawn((
Obstacle,
Aabb::from_min_max(
Vec3::ZERO,
Vec3::new(rng.gen_range(10.0..50.0), rng.gen_range(10.0..50.0), 0.0),
),
Transform::from_translation(
Vec3::new(
rng.gen_range((-(MESH_WIDTH as f32) / 2.0)..(MESH_WIDTH as f32 / 2.0)),
rng.gen_range((-(MESH_HEIGHT as f32) / 2.0)..(MESH_HEIGHT as f32 / 2.0)),
0.0,
) * FACTOR,
)
.with_rotation(Quat::from_rotation_z(rng.gen_range(0.0..PI))),
));
}
}
fn display_mesh(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
navmeshes: Res<Assets<NavMesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut current_mesh_entity: Local<Option<Entity>>,
window_resized: EventReader<WindowResized>,
navmesh: Query<(&ManagedNavMesh, Ref<NavMeshStatus>)>,
) {
let (navmesh_handle, status) = navmesh.single();
if (!status.is_changed() || *status != NavMeshStatus::Built) && window_resized.is_empty() {
return;
}
let Some(navmesh) = navmeshes.get(navmesh_handle) else {
return;
};
if let Some(entity) = *current_mesh_entity {
commands.entity(entity).despawn_recursive();
}
*current_mesh_entity = Some(
commands
.spawn((
Mesh2d(meshes.add(navmesh.to_mesh())),
MeshMaterial2d(materials.add(ColorMaterial::from(Color::Srgba(
palettes::tailwind::BLUE_800,
)))),
))
.with_children(|main_mesh| {
main_mesh.spawn((
Mesh2d(meshes.add(navmesh.to_wireframe_mesh())),
Transform::from_translation(Vec3::new(0.0, 0.0, 0.1)),
MeshMaterial2d(materials.add(ColorMaterial::from(Color::Srgba(
palettes::tailwind::TEAL_300,
)))),
));
})
.id(),
);
}
fn spawn_obstacle_on_click(
mouse_button_input: Res<ButtonInput<MouseButton>>,
primary_window: Query<&Window, With<PrimaryWindow>>,
camera_q: Query<(&Camera, &GlobalTransform)>,
mut commands: Commands,
settings: Query<Ref<NavMeshSettings>>,
) {
// Click was on a UI button that triggered a settings change, ignore it.
if settings.single().is_changed() {
return;
}
if mouse_button_input.just_pressed(MouseButton::Left) {
let (camera, camera_transform) = camera_q.single();
let window = primary_window.single();
if let Some(position) = window
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor).ok())
.map(|ray| ray.origin.truncate())
{
let mut rng = rand::thread_rng();
commands.spawn((
Obstacle,
Aabb::from_min_max(
Vec3::ZERO,
Vec3::new(rng.gen_range(10.0..50.), rng.gen_range(10.0..50.0), 0.0),
),
Transform::from_translation(position.extend(0.0))
.with_rotation(Quat::from_rotation_z(rng.gen_range(0.0..PI))),
GlobalTransform::default(),
));
info!("spawning an obstacle at {}", position);
}
}
}
fn remove_obstacles(
obstacles: Query<Entity, With<Obstacle>>,
mut commands: Commands,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
for entity in obstacles.iter() {
commands.entity(entity).despawn();
}
}
}