11use azalea_buf:: AzBuf ;
22
3- use crate :: position:: Vec3 ;
3+ use crate :: position:: { BlockPos , Vec3 } ;
44
55#[ derive( Clone , Copy , Debug , AzBuf , Default , Eq , PartialEq ) ]
66#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
@@ -15,6 +15,14 @@ pub enum Direction {
1515}
1616
1717impl Direction {
18+ pub const HORIZONTAL : [ Direction ; 4 ] = [
19+ Direction :: North ,
20+ Direction :: South ,
21+ Direction :: West ,
22+ Direction :: East ,
23+ ] ;
24+ pub const VERTICAL : [ Direction ; 2 ] = [ Direction :: Down , Direction :: Up ] ;
25+
1826 pub fn nearest ( vec : Vec3 ) -> Direction {
1927 let mut best_direction = Direction :: North ;
2028 let mut best_direction_amount = 0.0 ;
@@ -29,7 +37,7 @@ impl Direction {
2937 ]
3038 . iter ( )
3139 {
32- let amount = dir. normal ( ) . dot ( vec) ;
40+ let amount = dir. normal_vec3 ( ) . dot ( vec) ;
3341 if amount > best_direction_amount {
3442 best_direction = * dir;
3543 best_direction_amount = amount;
@@ -39,17 +47,23 @@ impl Direction {
3947 best_direction
4048 }
4149
42- pub fn normal ( self ) -> Vec3 {
50+ #[ inline]
51+ pub fn normal ( self ) -> BlockPos {
4352 match self {
44- Direction :: Down => Vec3 :: new ( 0.0 , -1.0 , 0. 0) ,
45- Direction :: Up => Vec3 :: new ( 0.0 , 1.0 , 0. 0) ,
46- Direction :: North => Vec3 :: new ( 0.0 , 0.0 , -1.0 ) ,
47- Direction :: South => Vec3 :: new ( 0.0 , 0.0 , 1.0 ) ,
48- Direction :: West => Vec3 :: new ( -1.0 , 0.0 , 0. 0) ,
49- Direction :: East => Vec3 :: new ( 1.0 , 0.0 , 0. 0) ,
53+ Direction :: Down => BlockPos :: new ( 0 , -1 , 0 ) ,
54+ Direction :: Up => BlockPos :: new ( 0 , 1 , 0 ) ,
55+ Direction :: North => BlockPos :: new ( 0 , 0 , -1 ) ,
56+ Direction :: South => BlockPos :: new ( 0 , 0 , 1 ) ,
57+ Direction :: West => BlockPos :: new ( -1 , 0 , 0 ) ,
58+ Direction :: East => BlockPos :: new ( 1 , 0 , 0 ) ,
5059 }
5160 }
5261
62+ #[ inline]
63+ pub fn normal_vec3 ( self ) -> Vec3 {
64+ self . normal ( ) . to_vec3_floored ( )
65+ }
66+
5367 pub fn opposite ( self ) -> Direction {
5468 match self {
5569 Direction :: Down => Direction :: Up ,
@@ -60,6 +74,16 @@ impl Direction {
6074 Direction :: East => Direction :: West ,
6175 }
6276 }
77+
78+ pub fn x ( self ) -> i32 {
79+ self . normal ( ) . x
80+ }
81+ pub fn y ( self ) -> i32 {
82+ self . normal ( ) . y
83+ }
84+ pub fn z ( self ) -> i32 {
85+ self . normal ( ) . z
86+ }
6387}
6488
6589/// The four cardinal directions.
0 commit comments