diff --git a/pumpkin-protocol/src/position.rs b/pumpkin-protocol/src/position.rs index 63180f215..b9eed35e4 100644 --- a/pumpkin-protocol/src/position.rs +++ b/pumpkin-protocol/src/position.rs @@ -1,19 +1,16 @@ +use pumpkin_world::vector3::Vector3; use serde::{Deserialize, Serialize}; -pub struct WorldPosition { - pub x: i32, - pub y: i32, - pub z: i32, -} +pub struct WorldPosition(pub Vector3); impl Serialize for WorldPosition { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { - let long = ((self.x as i64 & 0x3FFFFFF) << 38) - | ((self.z as i64 & 0x3FFFFFF) << 12) - | (self.y as i64 & 0xFFF); + let long = ((self.0.x as i64 & 0x3FFFFFF) << 38) + | ((self.0.z as i64 & 0x3FFFFFF) << 12) + | (self.0.y as i64 & 0xFFF); serializer.serialize_i64(long) } } @@ -33,11 +30,11 @@ impl<'de> Deserialize<'de> for WorldPosition { where E: serde::de::Error, { - Ok(WorldPosition { + Ok(WorldPosition(Vector3 { x: (v >> 38) as i32, y: (v << 52 >> 52) as i32, z: (v << 26 >> 38) as i32, - }) + })) } } deserializer.deserialize_i64(Visitor) diff --git a/pumpkin-world/src/lib.rs b/pumpkin-world/src/lib.rs index 2af7326ca..209451eb2 100644 --- a/pumpkin-world/src/lib.rs +++ b/pumpkin-world/src/lib.rs @@ -7,4 +7,5 @@ pub mod block; mod global_registry; pub mod item; pub mod radial_chunk_iterator; +pub mod vector3; mod world; diff --git a/pumpkin-world/src/vector3.rs b/pumpkin-world/src/vector3.rs new file mode 100644 index 000000000..4e91883af --- /dev/null +++ b/pumpkin-world/src/vector3.rs @@ -0,0 +1,113 @@ +use std::ops::{Add, Div, Mul, Neg, Sub}; + +use num_traits::Float; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Vector3 { + pub x: T, + pub y: T, + pub z: T, +} + +impl Vector3 { + pub fn new(x: T, y: T, z: T) -> Self { + Vector3 { x, y, z } + } + + pub fn length_sqared(&self) -> T { + self.x * self.x + self.y * self.y + self.z * self.z + } + + pub fn add(&self, other: &Vector3) -> Self { + Vector3 { + x: self.x + other.x, + y: self.y + other.y, + z: self.z + other.z, + } + } + + pub fn sub(&self, other: &Vector3) -> Self { + Vector3 { + x: self.x - other.x, + y: self.y - other.y, + z: self.z - other.z, + } + } + + pub fn multiply(self, x: T, y: T, z: T) -> Self { + Self { + x: self.x * x, + y: self.y * y, + z: self.z * z, + } + } +} + +impl Vector3 { + pub fn length(&self) -> T { + self.length_sqared().sqrt() + } + pub fn normalize(&self) -> Self { + let length = self.length(); + Vector3 { + x: self.x / length, + y: self.y / length, + z: self.z / length, + } + } +} + +impl Mul for Vector3 { + type Output = Self; + + fn mul(self, scalar: T) -> Self { + Self { + x: self.x * scalar, + y: self.y * scalar, + z: self.z * scalar, + } + } +} + +impl Add for Vector3 { + type Output = Vector3; + fn add(self, rhs: Self) -> Self::Output { + Self { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl Neg for Vector3 { + type Output = Self; + + fn neg(self) -> Self { + Vector3 { + x: -self.x, + y: -self.y, + z: -self.z, + } + } +} + +impl From<(T, T, T)> for Vector3 { + fn from((x, y, z): (T, T, T)) -> Self { + Vector3 { x, y, z } + } +} + +pub trait Math: + Mul + + Neg + + Add + + Div + + Sub + + Sized +{ +} +impl Math for f64 {} +impl Math for f32 {} +impl Math for i32 {} +impl Math for i64 {} diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 5f3c28e25..80f1e3438 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -1,13 +1,13 @@ use std::str::FromStr; use num_derive::{FromPrimitive, ToPrimitive}; +use num_traits::Float; use pumpkin_entity::{entity_type::EntityType, Entity, EntityId}; use pumpkin_inventory::player::PlayerInventory; use pumpkin_protocol::VarInt; +use pumpkin_world::vector3::Vector3; use serde::{Deserialize, Serialize}; -use crate::util::vec3::Vec3; - pub struct Player { pub entity: Entity, // current gamemode @@ -25,7 +25,7 @@ pub struct Player { pub sprinting: bool, // TODO: prbly should put this into an Living Entitiy or something - pub velocity: Vec3, + pub velocity: Vector3, // Current awaiting teleport id, None if did not teleport pub awaiting_teleport: Option, @@ -43,7 +43,7 @@ impl Player { health: 20.0, food: 20, food_saturation: 20.0, - velocity: Vec3::new(0.0, 0.0, 0.0), + velocity: Vector3::new(0.0, 0.0, 0.0), inventory: PlayerInventory::new(), gamemode, } @@ -62,9 +62,9 @@ impl Player { z = (rand::random::() - rand::random::()) * 0.01; } - let var8 = Vec3::new(x, 0.0, z).normalize() * y; + let var8 = Vector3::new(x, 0.0, z).normalize() * y; let var7 = self.velocity; - self.velocity = Vec3::new( + self.velocity = Vector3::new( var7.x / 2.0 - var8.x, if self.on_ground { (var7.y / 2.0 + x).min(0.4) diff --git a/pumpkin/src/util/mod.rs b/pumpkin/src/util/mod.rs index f538bb225..b8135c3c3 100644 --- a/pumpkin/src/util/mod.rs +++ b/pumpkin/src/util/mod.rs @@ -1,2 +1 @@ pub mod math; -pub mod vec3; diff --git a/pumpkin/src/util/vec3.rs b/pumpkin/src/util/vec3.rs deleted file mode 100644 index 96fd970c0..000000000 --- a/pumpkin/src/util/vec3.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::ops::{Mul, Neg}; - -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct Vec3 { - pub x: f64, - pub y: f64, - pub z: f64, -} - -impl Vec3 { - pub fn new(x: f64, y: f64, z: f64) -> Self { - Vec3 { x, y, z } - } - - pub fn length(&self) -> f64 { - (self.x * self.x + self.y * self.y + self.z * self.z).sqrt() - } - - pub fn normalize(&self) -> Self { - let length = self.length(); - Vec3 { - x: self.x / length, - y: self.y / length, - z: self.z / length, - } - } - - pub fn add(&self, other: &Vec3) -> Self { - Vec3 { - x: self.x + other.x, - y: self.y + other.y, - z: self.z + other.z, - } - } - - pub fn sub(&self, other: &Vec3) -> Self { - Vec3 { - x: self.x - other.x, - y: self.y - other.y, - z: self.z - other.z, - } - } - - pub fn multiply(self, x: f64, y: f64, z: f64) -> Self { - Self { - x: self.x * x, - y: self.y * y, - z: self.z * z, - } - } -} - -impl Mul for Vec3 { - type Output = Self; - - fn mul(self, scalar: f64) -> Self { - Self { - x: self.x * scalar, - y: self.y * scalar, - z: self.z * scalar, - } - } -} - -impl Neg for Vec3 { - type Output = Self; - - fn neg(self) -> Self { - Vec3 { - x: -self.x, - y: -self.y, - z: -self.z, - } - } -}