Skip to content

Commit

Permalink
improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
suprohub committed Feb 2, 2025
1 parent 1729f58 commit 28fd372
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 60 deletions.
23 changes: 23 additions & 0 deletions pumpkin-util/src/math/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ where
}
}

impl<T: Math + Copy> Vector3<T>
where
T: Into<f32>,
{
pub fn to_f32(&self) -> Vector3<f32> {
Vector3 {
x: self.x.into(),
y: self.y.into(),
z: self.z.into(),
}
}
}

impl Vector3<f64> {
pub fn as_f32(&self) -> Vector3<f32> {
Vector3 {
x: self.x as f32,
y: self.y as f32,
z: self.z as f32,
}
}
}

pub trait Math:
Mul<Output = Self>
//+ Neg<Output = Self>
Expand Down
29 changes: 17 additions & 12 deletions pumpkin/src/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -172,21 +169,29 @@ impl Entity {
}
}
}

/// Returns entity rotation as vector
pub fn rotation(&self) -> Vector3<f32> {
// 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<f32> {
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<f32>) {
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<f64>) {
let position = self.pos.load();
Expand Down
61 changes: 19 additions & 42 deletions pumpkin/src/entity/projectile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::f32::{self, consts::PI};

use pumpkin_util::math::vector3::Vector3;

use super::{living::LivingEntity, Entity, EntityBase};
Expand All @@ -8,40 +6,20 @@ 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();
owner_pos.y = (owner_pos.y + f64::from(owner.standing_eye_height)) - 0.1;
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<f32>, divergence: f32) {
self.set_velocity_unstable(velocity.to_f64(), f64::from(divergence));
let shooter_vel = shooter.velocity.load();
self.entity
.velocity
Expand All @@ -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<f64>, uncertainty: f64) {
fn next_triangular(mode: f64, deviation: f64) -> f64 {
mode + deviation * (rand::random::<f64>() - rand::random::<f64>())
}
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<f64>) {
self.entity.velocity.store(velocity);
self.entity.set_rotation_vec(velocity.as_f32());
}
}

Expand Down
4 changes: 1 addition & 3 deletions pumpkin/src/item/items/egg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 1 addition & 3 deletions pumpkin/src/item/items/snowball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 28fd372

Please # to comment.