Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiiita committed Dec 2, 2024
1 parent f57ac17 commit afae3d0
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,25 +723,38 @@ impl World {
.collect::<HashMap<uuid::Uuid, Arc<Player>>>()
}

pub async fn get_nearby_players(&self, pos: Vector3<f64>, radius: u16) -> HashMap<uuid::Uuid, Arc<Player>> {
let radius_squared = (radius as f64).powi(2);

/// Gets the nearby players around a given world position
/// It "creates" a sphere and checks if whether players are inside
/// and returns a hashmap where the uuid is the key and the player
/// object the value.
///
/// # Arguments
/// * `pos`: The middlepoint of the sphere
/// * `radius`: The radius of the sphere. The higher the radius
/// the more area will be checked, in every direction.
pub async fn get_nearby_players(
&self,
pos: Vector3<f64>,
radius: u16,
) -> HashMap<uuid::Uuid, Arc<Player>> {
let radius_squared = (f64::from(radius)).powi(2);

let mut found_players = HashMap::new();
for player in self.current_players.lock().await.iter() {
let player_pos = player.1.living_entity.entity.pos.load();

let diff = Vector3::new(
player_pos.x - pos.x,
player_pos.y - pos.y,
player_pos.z - pos.z
player_pos.z - pos.z,
);

let distance_squared = diff.x.powi(2) + diff.y.powi(2) + diff.z.powi(2);
if distance_squared <= radius_squared {
found_players.insert(player.0.clone(), player.1.clone());
found_players.insert(*player.0, player.1.clone());
}
}

found_players
}

Expand Down

0 comments on commit afae3d0

Please sign in to comment.