-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathparry3d.rs
155 lines (139 loc) · 4.72 KB
/
parry3d.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
//! A simple example showing how to use oxidized_navigation with a custom component using parry3d colliders.
//! Press M to draw nav-mesh.
//! Press X to spawn or despawn red cube.
use bevy::prelude::*;
use oxidized_navigation::{
colliders::OxidizedCollider,
debug_draw::{DrawNavMesh, OxidizedNavigationDebugDrawPlugin},
NavMeshAffector, NavMeshSettings, OxidizedNavigationPlugin,
};
use parry3d::{
bounding_volume::Aabb,
shape::{SharedShape, TypedShape},
};
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Oxidized Navigation: Parry3d".to_owned(),
..default()
}),
..default()
}),
OxidizedNavigationPlugin::<MyParryCollider>::new(
NavMeshSettings::from_agent_and_bounds(0.5, 1.9, 10.0, -1.0),
),
OxidizedNavigationDebugDrawPlugin,
))
.add_systems(Startup, setup)
.add_systems(
Update,
(toggle_nav_mesh_debug_draw, spawn_or_despawn_affector_system),
)
.run();
}
#[derive(Component)]
struct MyParryCollider {
collider: SharedShape,
}
impl OxidizedCollider for MyParryCollider {
fn oxidized_into_typed_shape(&self) -> TypedShape {
self.collider.as_typed_shape()
}
fn oxidized_compute_local_aabb(&self) -> Aabb {
self.collider.compute_local_aabb()
}
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
print_controls();
// Camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(4.0, 10.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y),
));
// Directional light
commands.spawn((
DirectionalLight {
shadows_enabled: true,
..default()
},
Transform::from_rotation(Quat::from_euler(EulerRot::XYZ, -1.0, -0.5, 0.0)),
));
// Ground plane
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(20.0, 20.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
Transform::IDENTITY,
MyParryCollider {
collider: SharedShape::cuboid(10.0, 0.1, 10.0),
},
NavMeshAffector, // Only entities with a NavMeshAffector component will contribute to the nav-mesh.
));
// Cube
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(2.0, 2.0, 2.0))),
MeshMaterial3d(materials.add(Color::srgb(0.4, 0.5, 0.9))),
Transform::from_xyz(-5.0, 0.8, -5.0),
MyParryCollider {
collider: SharedShape::cuboid(1.0, 1.0, 1.0),
},
NavMeshAffector, // Only entities with a NavMeshAffector component will contribute to the nav-mesh.
));
// Thin wall
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(0.1, 0.1, 0.1))),
MeshMaterial3d(materials.add(Color::srgb(0.4, 0.8, 0.9))),
Transform::from_xyz(-3.0, 0.6, 3.0).with_scale(Vec3::new(30.0, 12.0, 1.0)),
MyParryCollider {
collider: SharedShape::cuboid(1.0, 1.0, 1.0),
},
NavMeshAffector, // Only entities with a NavMeshAffector component will contribute to the nav-mesh.
));
}
fn toggle_nav_mesh_debug_draw(
keys: Res<ButtonInput<KeyCode>>,
mut show_navmesh: ResMut<DrawNavMesh>,
) {
if keys.just_pressed(KeyCode::KeyM) {
show_navmesh.0 = !show_navmesh.0;
}
}
fn spawn_or_despawn_affector_system(
keys: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut spawned_entity: Local<Option<Entity>>,
) {
if !keys.just_pressed(KeyCode::KeyX) {
return;
}
if let Some(entity) = *spawned_entity {
commands.entity(entity).despawn_recursive();
*spawned_entity = None;
} else {
let entity = commands
.spawn((
Mesh3d(meshes.add(Mesh::from(Cuboid::new(2.5, 2.5, 2.5)))),
MeshMaterial3d(materials.add(Color::srgb(1.0, 0.1, 0.5))),
Transform::from_xyz(5.0, 0.8, 5.0),
MyParryCollider {
collider: SharedShape::cuboid(1.25, 1.25, 1.25),
},
NavMeshAffector, // Only entities with a NavMeshAffector component will contribute to the nav-mesh.
))
.id();
*spawned_entity = Some(entity);
}
}
fn print_controls() {
info!("=========================================");
info!("| Press M to draw nav-mesh. |");
info!("| Press X to spawn or despawn red cube. |");
info!("=========================================");
}