| Name | Message | Date |
|---|---|---|
| 📄 bunny.rs | 11 hours ago | |
| 📄 locator.rs | 11 hours ago | |
| 📄 mod.rs | 11 hours ago |
📄
src/bunnies/bunny.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
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
use std::{ops::DerefMut, time::Duration}; use bevy::{ app::{App, Startup, Update}, asset::{AssetServer, Assets}, ecs::{ component::Component, entity::Entity, query::{With, Without}, system::{Commands, Query, Res, ResMut, Single}, }, gltf::GltfAssetLabel, math::{Dir3, Vec3, primitives::Cuboid}, mesh::{Mesh, Mesh3d, MeshBuilder, Meshable}, scene::SceneRoot, time::Time, transform::components::Transform, }; use rand::RngExt; use crate::{Rng, bunnies::locator::Locator, dog::Dog, obstacles::Obstacles}; pub(super) fn add_bunny_systems(app: &mut App) -> &mut App { app.add_systems(Startup, setup) .add_systems(Update, (calculate, jump)) } #[derive(Component)] pub(super) struct Bunny; #[derive(Component)] enum JumpState { Jumping { from: Vec3, to: Vec3 }, Cooldown { ready: Duration }, } const BUNNY_COUNT: usize = 5; const DETECTION_DISTANCE: f32 = 5.0; const SHORT_JUMP_DISTANCE: f32 = 1.0; const LONG_JUMP_DISTANCE: f32 = 3.0; const LONE_JUMPS_PER_SECOND: f64 = 1.0; const COLLECTIVE_JUMPS_PER_SECOND: f64 = 1.0; const JUMP_HEIGHT: f32 = 1.0; const JUMP_COOLDOWN: f32 = 0.05; const DESIRED_SPEED: f32 = 10.0; const SPEED: f32 = LONG_JUMP_DISTANCE / (LONG_JUMP_DISTANCE / DESIRED_SPEED - JUMP_COOLDOWN); fn setup( mut commands: Commands, asset_server: Res<AssetServer>, mut meshes: ResMut<Assets<Mesh>>, mut rng: ResMut<Rng>, ) { let model = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/cube-pets/animal-bunny.glb")); let mesh = meshes.add(Cuboid::from_length(2.0).mesh().build()); for _ in 0..BUNNY_COUNT { commands.spawn(( SceneRoot(model.clone()), Transform::from_xyz( rng.random_range(-15.0..=15.0), 0.0, rng.random_range(-15.0..=15.0), ), Mesh3d(mesh.clone()), Bunny, )); } } type IdleBunniesQuery<'w, 's, 't> = Query<'w, 's, (Entity, &'t mut Transform), (With<Bunny>, Without<JumpState>)>; fn calculate( mut commands: Commands, time: Res<Time>, locator: Res<Locator>, dog: Single<&Transform, (With<Dog>, Without<Bunny>)>, mut bunnies: IdleBunniesQuery, mut obstacles: Obstacles, mut rng: ResMut<Rng>, ) { bunnies.iter_mut().for_each(|(bunny, mut bunny_transform)| { calculate_next_move( &mut commands, &time, &locator, &dog, &mut bunny_transform, bunny, &mut obstacles, &mut rng, ); }); } fn calculate_next_move( commands: &mut Commands, time: &Time, locator: &Locator, dog: &Transform, bunny_transform: &mut Transform, bunny: Entity, obstacles: &mut Obstacles, rng: &mut Rng, ) { if let Some(direction) = is_dog_nearby(dog, bunny_transform) { jump_away_from_dog(commands, bunny_transform, bunny, obstacles, direction); } else if let Some(nearby_bunnies) = is_near_others(locator, bunny, bunny_transform) { jump_with_others( commands, time, obstacles, bunny, bunny_transform, nearby_bunnies, rng, ); } else { jump_alone(commands, time, obstacles, bunny, bunny_transform, rng); } } fn is_dog_nearby(dog: &Transform, bunny_transform: &mut Transform) -> Option<Vec3> { let direction = bunny_transform.translation - dog.translation; if direction.length_squared() > DETECTION_DISTANCE * DETECTION_DISTANCE { return None; } Some(direction) } fn jump_away_from_dog( commands: &mut Commands, bunny_transform: &mut Transform, bunny: Entity, obstacles: &mut Obstacles, direction: Vec3, ) { let dog_direction = direction.with_y(0.0).normalize(); let Some(to) = obstacles.avoid( bunny_transform.translation, bunny_transform.translation + dog_direction * LONG_JUMP_DISTANCE, ) else { return; }; start_jump(commands, bunny, bunny_transform, to); } fn is_near_others( locator: &Locator, bunny: Entity, bunny_transform: &Transform, ) -> Option<Vec<(Entity, (Vec3, Dir3))>> { let nearby_bunnies = locator.get_nearby(bunny, bunny_transform.translation, DETECTION_DISTANCE); if nearby_bunnies.is_empty() { None } else { Some(nearby_bunnies) } } fn jump_with_others( commands: &mut Commands, time: &Time, obstacles: &mut Obstacles, bunny: Entity, bunny_transform: &mut Transform, nearby_bunnies: Vec<(Entity, (Vec3, Dir3))>, rng: &mut Rng, ) { if !rng.random_bool((COLLECTIVE_JUMPS_PER_SECOND * time.delta_secs_f64()).clamp(0.0, 1.0)) { return; } let average_direction = calculate_average_direction(nearby_bunnies); let Some(to) = obstacles.avoid( bunny_transform.translation, bunny_transform.translation + average_direction * SHORT_JUMP_DISTANCE, ) else { return; }; start_jump(commands, bunny, bunny_transform, to); fn calculate_average_direction(nearby_bunnies: Vec<(Entity, (Vec3, Dir3))>) -> Vec3 { let (sin, cos) = nearby_bunnies .iter() .skip(1) .fold((0.0, 0.0), |a, (_, (_, d))| { let angle = d.angle_between(Vec3::Z); (a.0 + angle.sin(), a.1 + angle.cos()) }); let average_angle = sin.atan2(cos); let average_direction = Dir3::Z.rotate_y(average_angle); average_direction } } fn jump_alone( commands: &mut Commands, time: &Time, obstacles: &mut Obstacles, bunny: Entity, bunny_transform: &mut Transform, rng: &mut Rng, ) { if !rng.random_bool((LONE_JUMPS_PER_SECOND * time.delta_secs_f64()).clamp(0.0, 1.0)) { return; } let angle = rng.random_range(-std::f32::consts::PI..=std::f32::consts::PI); let direction = Dir3::Z.rotate_y(angle); let Some(to) = obstacles.avoid( bunny_transform.translation, bunny_transform.translation + direction * LONG_JUMP_DISTANCE, ) else { return; }; start_jump(commands, bunny, bunny_transform, to); } fn start_jump(commands: &mut Commands, bunny: Entity, bunny_transform: &mut Transform, to: Vec3) { commands.entity(bunny).insert(JumpState::Jumping { from: bunny_transform.translation, to, }); bunny_transform.look_to(-(to - bunny_transform.translation), Vec3::Y); } fn jump( mut commands: Commands, time: Res<Time>, mut bunnies: Query<(Entity, &mut Transform, &mut JumpState), With<Bunny>>, ) { bunnies.iter_mut().for_each( |(bunny, mut bunny_transform, ref mut jump_state)| match jump_state.deref_mut() { JumpState::Jumping { from, to } => { let from = *from; let to = *to; animate_jumping( &time, &mut bunny_transform, jump_state.deref_mut(), from, to, ) } JumpState::Cooldown { ready } => { let ready = *ready; cooldown(&mut commands, &time, bunny, ready); } }, ); } fn animate_jumping( time: &Time, bunny_transform: &mut Transform, jump_state: &mut JumpState, from: Vec3, to: Vec3, ) { let current = bunny_transform.translation.with_y(to.y); let direction = to - current; let delta = SPEED * time.delta_secs(); let (direction, length) = direction.normalize_and_length(); if length <= delta { bunny_transform.translation = to; *jump_state = JumpState::Cooldown { ready: time.elapsed() + Duration::from_secs_f32(JUMP_COOLDOWN), }; return; } let next = current + direction.normalize() * delta; let total_distance = from.distance(to); bunny_transform.translation = next + Vec3::Y * JUMP_HEIGHT * (std::f32::consts::PI * from.distance(next) / total_distance).sin(); } fn cooldown(commands: &mut Commands, time: &Time, bunny: Entity, ready: Duration) { if time.elapsed() >= ready { commands.entity(bunny).remove::<JumpState>(); } }