From 28fd372a65de2773b88bdf0a8c151cb8ae94fc14 Mon Sep 17 00:00:00 2001 From: suprohub Date: Sun, 2 Feb 2025 10:08:26 +0400 Subject: [PATCH] improve code quality --- pumpkin-util/src/math/vector3.rs | 23 +++++++++++ pumpkin/src/entity/mod.rs | 29 +++++++------ pumpkin/src/entity/projectile/mod.rs | 61 +++++++++------------------- pumpkin/src/item/items/egg.rs | 4 +- pumpkin/src/item/items/snowball.rs | 4 +- 5 files changed, 61 insertions(+), 60 deletions(-) diff --git a/pumpkin-util/src/math/vector3.rs b/pumpkin-util/src/math/vector3.rs index 1a3d81ba2..0a74ffda7 100644 --- a/pumpkin-util/src/math/vector3.rs +++ b/pumpkin-util/src/math/vector3.rs @@ -158,6 +158,29 @@ where } } +impl Vector3 +where + T: Into, +{ + pub fn to_f32(&self) -> Vector3 { + Vector3 { + x: self.x.into(), + y: self.y.into(), + z: self.z.into(), + } + } +} + +impl Vector3 { + pub fn as_f32(&self) -> Vector3 { + Vector3 { + x: self.x as f32, + y: self.y as f32, + z: self.z as f32, + } + } +} + pub trait Math: Mul //+ Neg diff --git a/pumpkin/src/entity/mod.rs b/pumpkin/src/entity/mod.rs index 8037f6891..7f05b91bb 100644 --- a/pumpkin/src/entity/mod.rs +++ b/pumpkin/src/entity/mod.rs @@ -1,8 +1,5 @@ use core::f32; -use std::{ - f32::consts::PI, - sync::{atomic::AtomicBool, Arc}, -}; +use std::sync::{atomic::AtomicBool, Arc}; use async_trait::async_trait; use crossbeam::atomic::AtomicCell; @@ -172,21 +169,29 @@ impl Entity { } } } - /// Returns entity rotation as vector - pub fn rotation(&self) -> Vector3 { - // Convert degrees to radians if necessary - let yaw_rad = self.yaw.load() * (PI / 180.0); - let pitch_rad = self.pitch.load() * (PI / 180.0); + pub fn rotation_vec(&self) -> Vector3 { + let yaw_rad = self.yaw.load().to_radians(); + let pitch_rad = self.pitch.load().to_radians(); + + let cos_pitch = pitch_rad.cos(); Vector3::new( - yaw_rad.cos() * pitch_rad.cos(), - pitch_rad.sin(), - yaw_rad.sin() * pitch_rad.cos(), + -yaw_rad.sin() * cos_pitch, + -pitch_rad.sin(), + yaw_rad.cos() * cos_pitch, ) .normalize() } + /// Sets rotation by vector + pub fn set_rotation_vec(&self, rotation: Vector3) { + self.set_rotation( + rotation.x.atan2(rotation.z).to_degrees(), + rotation.y.atan2(rotation.horizontal_length()).to_degrees(), + ); + } + /// Changes this entity's pitch and yaw to look at target pub async fn look_at(&self, target: Vector3) { let position = self.pos.load(); diff --git a/pumpkin/src/entity/projectile/mod.rs b/pumpkin/src/entity/projectile/mod.rs index 400d72580..9a550275e 100644 --- a/pumpkin/src/entity/projectile/mod.rs +++ b/pumpkin/src/entity/projectile/mod.rs @@ -1,5 +1,3 @@ -use std::f32::{self, consts::PI}; - use pumpkin_util::math::vector3::Vector3; use super::{living::LivingEntity, Entity, EntityBase}; @@ -8,8 +6,6 @@ pub struct ThrownItem { entity: Entity, } -const DEG_PER_RAD_F32: f32 = 180.0 / PI; - impl ThrownItem { pub fn new(entity: Entity, owner: &Entity) -> Self { let mut owner_pos = owner.pos.load(); @@ -17,31 +13,13 @@ impl ThrownItem { entity.pos.store(owner_pos); Self { entity } } - pub fn set_velocity_from( - &self, - shooter: &Entity, - pitch: f32, - yaw: f32, - roll: f32, - speed: f32, - divergence: f32, - ) { - let to_radians = |degrees: f32| degrees * PI / 180.0; - let yaw_rad = to_radians(yaw); - let pitch_rad = to_radians(pitch); - let roll_rad = to_radians(pitch + roll); + pub fn set_velocity_shooter_rot(&self, shooter: &Entity, power: f32, divergence: f32) { + self.set_velocity_shooter(shooter, shooter.rotation_vec() * power, divergence); + } - let x = -yaw_rad.sin() * pitch_rad.cos(); - let y = -roll_rad.sin(); - let z = yaw_rad.cos() * pitch_rad.cos(); - self.set_velocity( - f64::from(x), - f64::from(y), - f64::from(z), - f64::from(speed), - f64::from(divergence), - ); + pub fn set_velocity_shooter(&self, shooter: &Entity, velocity: Vector3, divergence: f32) { + self.set_velocity_unstable(velocity.to_f64(), f64::from(divergence)); let shooter_vel = shooter.velocity.load(); self.entity .velocity @@ -55,25 +33,24 @@ impl ThrownItem { shooter_vel.z, )); } - /// The velocity and rotation will be set to the same direction. - pub fn set_velocity(&self, x: f64, y: f64, z: f64, power: f64, uncertainty: f64) { + + /// Velocity will be set a bit randomly + pub fn set_velocity_unstable(&self, velocity: Vector3, uncertainty: f64) { fn next_triangular(mode: f64, deviation: f64) -> f64 { mode + deviation * (rand::random::() - rand::random::()) } - let velocity = Vector3::new(x, y, z) - .normalize() - .add_raw( - next_triangular(0.0, 0.017_227_5 * uncertainty), - next_triangular(0.0, 0.017_227_5 * uncertainty), - next_triangular(0.0, 0.017_227_5 * uncertainty), - ) - .multiply(power, power, power); - self.entity.velocity.store(velocity); - let len = velocity.horizontal_length(); - self.entity.set_rotation( - velocity.x.atan2(velocity.z) as f32 * DEG_PER_RAD_F32, - velocity.y.atan2(len) as f32 * DEG_PER_RAD_F32, + let velocity = velocity.add_raw( + next_triangular(0.0, 0.017_227_5 * uncertainty), + next_triangular(0.0, 0.017_227_5 * uncertainty), + next_triangular(0.0, 0.017_227_5 * uncertainty), ); + self.set_velocity(velocity); + } + + /// Velocity will be set normally + pub fn set_velocity(&self, velocity: Vector3) { + self.entity.velocity.store(velocity); + self.entity.set_rotation_vec(velocity.as_f32()); } } diff --git a/pumpkin/src/item/items/egg.rs b/pumpkin/src/item/items/egg.rs index 106b46ee4..9577c3b29 100644 --- a/pumpkin/src/item/items/egg.rs +++ b/pumpkin/src/item/items/egg.rs @@ -29,9 +29,7 @@ impl PumpkinItem for EggItem { // TODO: Implement eggs the right way, so there is a chance of spawning chickens let entity = server.add_entity(position, EntityType::Egg, world); let snowball = ThrownItem::new(entity, &player.living_entity.entity); - let yaw = player.living_entity.entity.yaw.load(); - let pitch = player.living_entity.entity.pitch.load(); - snowball.set_velocity_from(&player.living_entity.entity, pitch, yaw, 0.0, POWER, 1.0); + snowball.set_velocity_shooter_rot(&player.living_entity.entity, POWER, 1.0); world.spawn_entity(Arc::new(snowball)).await; } } diff --git a/pumpkin/src/item/items/snowball.rs b/pumpkin/src/item/items/snowball.rs index 12ea9d10c..8726ac591 100644 --- a/pumpkin/src/item/items/snowball.rs +++ b/pumpkin/src/item/items/snowball.rs @@ -28,9 +28,7 @@ impl PumpkinItem for SnowBallItem { .await; let entity = server.add_entity(position, EntityType::Snowball, world); let snowball = ThrownItem::new(entity, &player.living_entity.entity); - let yaw = player.living_entity.entity.yaw.load(); - let pitch = player.living_entity.entity.pitch.load(); - snowball.set_velocity_from(&player.living_entity.entity, pitch, yaw, 0.0, POWER, 1.0); + snowball.set_velocity_shooter_rot(&player.living_entity.entity, POWER, 1.0); world.spawn_entity(Arc::new(snowball)).await; } }