Make Vector3 use a generic

This commit is contained in:
lukas0008 2024-08-17 20:40:06 +02:00
parent 391d5cbcfc
commit 94748859c6
6 changed files with 127 additions and 92 deletions

View File

@ -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<i32>);
impl Serialize for WorldPosition {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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)

View File

@ -7,4 +7,5 @@ pub mod block;
mod global_registry;
pub mod item;
pub mod radial_chunk_iterator;
pub mod vector3;
mod world;

View File

@ -0,0 +1,113 @@
use std::ops::{Add, Div, Mul, Neg, Sub};
use num_traits::Float;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vector3<T> {
pub x: T,
pub y: T,
pub z: T,
}
impl<T: Math + Copy> Vector3<T> {
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<T>) -> Self {
Vector3 {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
pub fn sub(&self, other: &Vector3<T>) -> 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<T: Math + Copy + Float> Vector3<T> {
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<T: Math + Copy> Mul<T> for Vector3<T> {
type Output = Self;
fn mul(self, scalar: T) -> Self {
Self {
x: self.x * scalar,
y: self.y * scalar,
z: self.z * scalar,
}
}
}
impl<T: Math + Copy> Add for Vector3<T> {
type Output = Vector3<T>;
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl<T: Math + Copy> Neg for Vector3<T> {
type Output = Self;
fn neg(self) -> Self {
Vector3 {
x: -self.x,
y: -self.y,
z: -self.z,
}
}
}
impl<T> From<(T, T, T)> for Vector3<T> {
fn from((x, y, z): (T, T, T)) -> Self {
Vector3 { x, y, z }
}
}
pub trait Math:
Mul<Output = Self>
+ Neg<Output = Self>
+ Add<Output = Self>
+ Div<Output = Self>
+ Sub<Output = Self>
+ Sized
{
}
impl Math for f64 {}
impl Math for f32 {}
impl Math for i32 {}
impl Math for i64 {}

View File

@ -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<f64>,
// Current awaiting teleport id, None if did not teleport
pub awaiting_teleport: Option<VarInt>,
@ -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::<f64>() - rand::random::<f64>()) * 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)

View File

@ -1,2 +1 @@
pub mod math;
pub mod vec3;

View File

@ -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<f64> 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,
}
}
}