| Name | Message | Date |
|---|---|---|
| 📄 bunny.rs | 1 month ago | |
| 📄 heart.rs | 1 month ago | |
| 📄 locator.rs | 1 month ago | |
| 📄 mod.rs | 1 month ago |
📄
src/bunnies/locator.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
use std::collections::HashMap; use bevy::{ app::{App, PreUpdate}, ecs::{ entity::Entity, query::With, resource::Resource, system::{Query, ResMut}, }, math::{Dir3, Vec3}, transform::components::Transform, }; use crate::bunnies::bunny::Bunny; pub fn add_locator(app: &mut App) -> &mut App { app.init_resource::<BunnyLocator>() .add_systems(PreUpdate, update_locator) } fn update_locator( mut locator: ResMut<BunnyLocator>, bunnies: Query<(Entity, &Transform), With<Bunny>>, ) { bunnies .iter() .for_each(|(bunny, transform)| locator.set(bunny, transform.translation, transform.back())); } #[derive(Resource, Default)] pub struct BunnyLocator { bunnies: HashMap<Entity, (Vec3, Dir3)>, } impl BunnyLocator { fn set(&mut self, bunny: Entity, position: Vec3, direction: Dir3) { self.bunnies.insert(bunny, (position, direction)); } pub fn get_nearby_count(&self, center: Vec3, radius: f32) -> usize { self.bunnies .iter() .filter(|(_, (pos, _))| center.distance(*pos) <= radius) .count() } pub(super) fn get_nearby_except_self( &self, this: Entity, center: Vec3, radius: f32, ) -> Vec<(Entity, (Vec3, Dir3))> { self.bunnies .iter() .filter(|(e, (pos, _))| **e != this && center.distance(*pos) <= radius) .map(|(b, (p, d))| (*b, (*p, *d))) .collect::<Vec<_>>() } }