Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Stop following when player left
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Jan 4, 2024
1 parent f04b5af commit 790e5ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions minecraft-server/src/entities/monsters/zombies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,19 @@ impl ZombieTask {
}

/// Returns the movement towards the target that can be applied without colliding with the world.
async fn get_movement(&self, h: &Handler<Zombie>, self_position: &Position, target_position: &Position) -> Translation {
async fn get_movement(&mut self, h: &Handler<Zombie>, self_position: &Position, target_position: &Position) -> Translation {
// Create a movement vector
let mut translation = Translation {
x: target_position.x - self_position.x,
y: target_position.y - self_position.y,
z: target_position.z - self_position.z,
};
if translation.norm() > ZOMBIE_BASE_MOVEMENT_SPEED {
let norm = translation.norm();
if norm > ZOMBIE_BASE_FOLLOW_RANGE {
self.target = None;
return Translation::zero();
}
if norm > ZOMBIE_BASE_MOVEMENT_SPEED {
translation.set_norm(ZOMBIE_BASE_MOVEMENT_SPEED);
}

Expand Down
6 changes: 5 additions & 1 deletion minecraft-server/src/world/collisions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,18 @@ impl<'a> Iterator for PointIter<'a> {
}

/// Vector describing a movement
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Translation {
pub x: f64,
pub y: f64,
pub z: f64,
}

impl Translation {
pub fn zero() -> Self {
Self::default()
}

pub fn yaw_pitch(&self) -> (f32, f32) {
let r = (self.x * self.x + self.y * self.y + self.z * self.z).sqrt();
let pitch = -(self.y / r).asin() / std::f64::consts::PI * 180.0;
Expand Down

0 comments on commit 790e5ab

Please sign in to comment.