From dff54498f796a456bf15d07aa99b57892e43edbb Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 26 Jun 2022 16:28:16 -0500 Subject: [PATCH] add more stuff to PositionXYZ --- azalea-core/src/position.rs | 87 +++++++++++++++++++++++++------- azalea-entity/src/physics/mod.rs | 19 ++++++- 2 files changed, 88 insertions(+), 18 deletions(-) diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index a34f5ee9..879955a2 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -2,19 +2,54 @@ use crate::ResourceLocation; use azalea_buf::{McBufReadable, McBufWritable}; use std::{ io::{Read, Write}, - ops::Rem, + ops::{Add, Mul, Rem}, }; -pub trait PositionXYZ { - fn add_x(&self, n: T) -> Self; - fn add_y(&self, n: T) -> Self; - fn add_z(&self, n: T) -> Self; +pub trait PositionXYZ +where + T: Add + Mul, +{ + fn x(&self) -> T; + fn y(&self) -> T; + fn z(&self) -> T; + + fn set_x(&self, n: T) -> Self; + fn set_y(&self, n: T) -> Self; + fn set_z(&self, n: T) -> Self; + + // hopefully these get optimized + fn add_x(&self, n: T) -> Self + where + Self: Sized, + { + self.set_x(self.x() + n) + } + fn add_y(&self, n: T) -> Self + where + Self: Sized, + { + self.set_y(self.y() + n) + } + fn add_z(&self, n: T) -> Self + where + Self: Sized, + { + self.set_z(self.z() + n) + } + fn add(&self, x: T, y: T, z: T) -> Self where Self: Sized, { self.add_x(x).add_y(y).add_z(z) } + + fn length_sqr(&self) -> T + where + Self: Sized, + { + self.x() * self.x() + self.y() * self.y() + self.z() * self.z() + } } #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] @@ -43,25 +78,34 @@ impl Rem for BlockPos { } impl PositionXYZ for BlockPos { - fn add_x(&self, n: i32) -> Self { + fn x(&self) -> i32 { + self.x + } + fn y(&self) -> i32 { + self.y + } + fn z(&self) -> i32 { + self.z + } + fn set_x(&self, n: i32) -> Self { BlockPos { - x: self.x + n, + x: n, y: self.y, z: self.z, } } - fn add_y(&self, n: i32) -> Self { + fn set_y(&self, n: i32) -> Self { BlockPos { x: self.x, - y: self.y + n, + y: n, z: self.z, } } - fn add_z(&self, n: i32) -> Self { + fn set_z(&self, n: i32) -> Self { BlockPos { x: self.x, y: self.y, - z: self.z + n, + z: n, } } } @@ -138,25 +182,34 @@ pub struct Vec3 { } impl PositionXYZ for Vec3 { - fn add_x(&self, n: f64) -> Self { + fn x(&self) -> f64 { + self.x + } + fn y(&self) -> f64 { + self.y + } + fn z(&self) -> f64 { + self.z + } + fn set_x(&self, n: f64) -> Self { Vec3 { - x: self.x + n, + x: n, y: self.y, z: self.z, } } - fn add_y(&self, n: f64) -> Self { + fn set_y(&self, n: f64) -> Self { Vec3 { x: self.x, - y: self.y + n, + y: n, z: self.z, } } - fn add_z(&self, n: f64) -> Self { + fn set_z(&self, n: f64) -> Self { Vec3 { x: self.x, y: self.y, - z: self.z + n, + z: n, } } } diff --git a/azalea-entity/src/physics/mod.rs b/azalea-entity/src/physics/mod.rs index 01c497cd..7d62ee64 100644 --- a/azalea-entity/src/physics/mod.rs +++ b/azalea-entity/src/physics/mod.rs @@ -3,7 +3,7 @@ mod block_hit_result; use crate::Entity; pub use aabb::AABB; -use azalea_core::PositionDelta; +use azalea_core::{PositionDelta, PositionXYZ, Vec3}; pub use block_hit_result::BlockHitResult; pub enum MoverType { @@ -15,6 +15,7 @@ pub enum MoverType { } impl Entity { + /// Move an entity by a given delta, checking for collisions. pub fn move_entity(&mut self, mover_type: &MoverType, movement: &PositionDelta) { // if self.no_physics { // return; @@ -35,4 +36,20 @@ impl Entity { // TODO } + + // fn collide(movement: &Vec3, dimension: &Dimension) -> Vec3 { + // if movement.length_sqr() == 0.0 { + // *movement + // } else { + // // Self::collide_bounding_box( + // // Some(self), + // // movement, + // // entityBoundingBox, + // // this.level, + // // entityCollisions, + // // ) + // } + // } + + // fn collide_bounding_box(self: ) }