forked from dimforge/bevy_rapier
-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom_system_setup3.rs
166 lines (144 loc) · 4.52 KB
/
custom_system_setup3.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
use bevy::{core::FrameCount, prelude::*};
use bevy_rapier3d::prelude::*;
struct SpecialStagingPlugin {
schedule: Schedule,
}
impl SpecialStagingPlugin {
pub fn new(schedule: Schedule) -> Self {
Self { schedule }
}
}
impl SpecialStagingPlugin {
fn build(self, app: &mut App) {
app.add_stage_after(
CoreStage::Update,
"special_staging_plugin_stage",
SpecialStage::new(self.schedule),
);
}
}
struct SpecialStage {
schedule: Schedule,
}
impl SpecialStage {
pub fn new(schedule: Schedule) -> Self {
Self { schedule }
}
}
impl Stage for SpecialStage {
fn run(&mut self, world: &mut World) {
self.schedule.run_once(world);
}
}
fn main() {
let mut app = App::new();
app.insert_resource(ClearColor(Color::rgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins(DefaultPlugins)
.add_plugin(RapierDebugRenderPlugin::default())
.add_startup_system(setup_graphics)
.add_startup_system(setup_physics);
// Do the stage setup however we want, maybe in a special plugin that has
// its very own schedule
SpecialStagingPlugin::new(
Schedule::default()
.with_stage(
PhysicsSet::SyncBackend,
SystemStage::parallel().with_system_set(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend),
),
)
.with_stage_after(
PhysicsSet::SyncBackend,
PhysicsSet::StepSimulation,
SystemStage::parallel()
.with_system(despawn_one_box) // We can add a special despawn to determine cleanup later
.with_system_set(RapierPhysicsPlugin::<NoUserData>::get_systems(
PhysicsSet::StepSimulation,
)),
)
.with_stage_after(
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
SystemStage::parallel().with_system_set(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::Writeback),
),
),
)
.build(&mut app);
app.add_plugin(RapierPhysicsPlugin::<NoUserData>::default().with_default_system_setup(false));
app.run();
}
fn despawn_one_box(
mut commands: Commands,
mut frame_count: ResMut<FrameCount>,
query: Query<Entity, (With<Collider>, With<RigidBody>)>,
) {
frame_count.0 += 1;
// Delete a box every 10 frames
if frame_count.0 % 10 == 0 && !query.is_empty() {
let count = query.iter().count();
if let Some(entity) = query
.iter()
.skip(frame_count.0 as usize % count) // Get a "random" box to make sim interesting
.take(1)
.next()
{
commands.entity(entity).despawn();
}
}
}
fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
..Default::default()
});
}
pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 200.1;
let ground_height = 0.1;
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height, ground_size),
));
/*
* Create the cubes
*/
let num = 8;
let rad = 1.0;
let shift = rad * 2.0 + rad;
let centerx = shift * (num / 2) as f32;
let centery = shift / 2.0;
let centerz = shift * (num / 2) as f32;
let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
let mut color = 0;
let colors = [
Color::hsl(220.0, 1.0, 0.3),
Color::hsl(180.0, 1.0, 0.3),
Color::hsl(260.0, 1.0, 0.7),
];
for j in 0usize..20 {
for i in 0..num {
for k in 0usize..num {
let x = i as f32 * shift - centerx + offset;
let y = j as f32 * shift + centery + 3.0;
let z = k as f32 * shift - centerz + offset;
color += 1;
commands.spawn((
TransformBundle::from(Transform::from_xyz(x, y, z)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad, rad),
ColliderDebugColor(colors[color % 3]),
));
}
}
offset -= 0.05 * rad * (num as f32 - 1.0);
}
}