Make Vector3 use a generic
This commit is contained in:
1 parent
391d5cbcfc
commit
94748859c6
6 files changed
+127
-92
No files matched your search
@@ -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)
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
pub mod math;
|
||||
pub mod vec3;
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user