@@ -4,61 +4,107 @@ use pyo3::prelude::*;
44pub struct HitBox {
55 #[ pyo3( get, set) ]
66 points : Vec < ( f32 , f32 ) > ,
7+ #[ pyo3( get, set) ]
8+ position : ( f32 , f32 ) ,
9+ #[ pyo3( get, set) ]
10+ scale : ( f32 , f32 ) ,
711}
812
913#[ pymethods]
1014impl HitBox {
1115 #[ new]
12- fn new ( points : Vec < ( f32 , f32 ) > ) -> HitBox {
13- HitBox { points : points }
16+ fn new (
17+ points : Vec < ( f32 , f32 ) > ,
18+ position : Option < ( f32 , f32 ) > ,
19+ scale : Option < ( f32 , f32 ) > ,
20+ ) -> HitBox {
21+ let final_position = position. unwrap_or ( ( 0.0 , 0.0 ) ) ;
22+ let final_scale = scale. unwrap_or ( ( 1.0 , 1.0 ) ) ;
23+ HitBox {
24+ points,
25+ position : final_position,
26+ scale : final_scale,
27+ }
1428 }
1529
16- fn create_adjustable (
30+ fn create_rotatable (
1731 self_ : PyRef < ' _ , Self > ,
1832 py : Python < ' _ > ,
19- position : ( f32 , f32 ) ,
20- angle : f32 ,
21- scale : ( f32 , f32 ) ,
22- ) -> PyResult < Py < AdjustableHitBox > > {
23- let adjustable: Py < AdjustableHitBox > = Py :: new (
33+ angle : Option < f32 > ,
34+ ) -> PyResult < Py < RotatableHitBox > > {
35+ let adjustable: Py < RotatableHitBox > = Py :: new (
2436 py,
25- AdjustableHitBox :: new ( self_. points . to_vec ( ) , position, angle, scale) ,
37+ RotatableHitBox :: new (
38+ self_. points . to_vec ( ) ,
39+ Some ( self_. position ) ,
40+ Some ( self_. scale ) ,
41+ angle,
42+ ) ,
2643 )
2744 . unwrap ( ) ;
2845 Ok ( adjustable)
2946 }
3047
31- fn get_adjusted_points ( & self ) -> Vec < ( f32 , f32 ) > {
32- self . points . to_vec ( )
48+ fn get_adjusted_points ( self_ : PyRef < ' _ , Self > ) -> Vec < ( f32 , f32 ) > {
49+ let mut new_points: Vec < ( f32 , f32 ) > = Vec :: with_capacity ( self_. points . len ( ) ) ;
50+
51+ for point in self_. points . iter ( ) {
52+ let x = ( point. 0 * self_. scale . 0 ) + self_. position . 0 ;
53+ let y = ( point. 1 * self_. scale . 1 ) + self_. position . 1 ;
54+ new_points. push ( ( x, y) ) ;
55+ }
56+
57+ new_points
58+ }
59+
60+ #[ getter]
61+ fn left ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
62+ let mut converted = HitBox :: get_adjusted_points ( self_) ;
63+ converted. sort_by ( |a, b| a. 0 . partial_cmp ( & b. 0 ) . unwrap ( ) ) ;
64+ Ok ( converted[ 0 ] . 0 )
65+ }
66+
67+ #[ getter]
68+ fn right ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
69+ let mut converted: Vec < ( f32 , f32 ) > = HitBox :: get_adjusted_points ( self_) ;
70+ converted. sort_by ( |a, b| b. 0 . partial_cmp ( & a. 0 ) . unwrap ( ) ) ;
71+ Ok ( converted[ 0 ] . 0 )
72+ }
73+
74+ #[ getter]
75+ fn bottom ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
76+ let mut converted: Vec < ( f32 , f32 ) > = HitBox :: get_adjusted_points ( self_) ;
77+ converted. sort_by ( |a, b| a. 1 . partial_cmp ( & b. 1 ) . unwrap ( ) ) ;
78+ Ok ( converted[ 0 ] . 1 )
79+ }
80+
81+ #[ getter]
82+ fn top ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
83+ let mut converted: Vec < ( f32 , f32 ) > = HitBox :: get_adjusted_points ( self_) ;
84+ converted. sort_by ( |a, b| b. 1 . partial_cmp ( & a. 1 ) . unwrap ( ) ) ;
85+ Ok ( converted[ 0 ] . 1 )
3386 }
3487}
3588
3689#[ pyclass( extends=HitBox , module = "arcade.hitbox.base" ) ]
37- pub struct AdjustableHitBox {
38- #[ pyo3( get, set) ]
39- position : ( f32 , f32 ) ,
90+ pub struct RotatableHitBox {
4091 #[ pyo3( get, set) ]
4192 angle : f32 ,
42- #[ pyo3( get, set) ]
43- scale : ( f32 , f32 ) ,
4493}
4594
4695#[ pymethods]
47- impl AdjustableHitBox {
96+ impl RotatableHitBox {
4897 #[ new]
4998 fn new (
5099 points : Vec < ( f32 , f32 ) > ,
51- position : ( f32 , f32 ) ,
52- angle : f32 ,
53- scale : ( f32 , f32 ) ,
100+ position : Option < ( f32 , f32 ) > ,
101+ scale : Option < ( f32 , f32 ) > ,
102+ angle : Option < f32 > ,
54103 ) -> ( Self , HitBox ) {
104+ let final_angle = angle. unwrap_or ( 0.0 ) ;
55105 (
56- AdjustableHitBox {
57- position,
58- angle,
59- scale,
60- } ,
61- HitBox :: new ( points) ,
106+ RotatableHitBox { angle : final_angle } ,
107+ HitBox :: new ( points, position, scale) ,
62108 )
63109 }
64110
@@ -70,43 +116,38 @@ impl AdjustableHitBox {
70116 let rad_cos = rad. cos ( ) ;
71117 let rad_sin = rad. sin ( ) ;
72118 for point in super_. points . iter ( ) {
73- new_points. push ( self_. adjust_point ( * point, rad_cos, rad_sin) ) ;
119+ let x = ( ( point. 0 * rad_cos - point. 1 * rad_sin) * super_. scale . 0 ) + super_. position . 0 ;
120+ let y = ( ( point. 0 * rad_sin + point. 1 * rad_cos) * super_. scale . 1 ) + super_. position . 1 ;
121+ new_points. push ( ( x, y) ) ;
74122 }
75123
76124 new_points
77125 }
78126
79- fn adjust_point ( & self , point : ( f32 , f32 ) , cos : f32 , sin : f32 ) -> ( f32 , f32 ) {
80- let ( mut x, mut y) = point;
81- x = ( ( x * cos - y * sin) * self . scale . 0 ) + self . position . 0 ;
82- y = ( ( x * sin + y * cos) * self . scale . 1 ) + self . position . 1 ;
83- ( x, y)
84- }
85-
86127 #[ getter]
87128 fn left ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
88- let mut converted: Vec < ( f32 , f32 ) > = AdjustableHitBox :: get_adjusted_points ( self_) ;
129+ let mut converted: Vec < ( f32 , f32 ) > = RotatableHitBox :: get_adjusted_points ( self_) ;
89130 converted. sort_by ( |a, b| a. 0 . partial_cmp ( & b. 0 ) . unwrap ( ) ) ;
90131 Ok ( converted[ 0 ] . 0 )
91132 }
92133
93134 #[ getter]
94135 fn right ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
95- let mut converted: Vec < ( f32 , f32 ) > = AdjustableHitBox :: get_adjusted_points ( self_) ;
136+ let mut converted: Vec < ( f32 , f32 ) > = RotatableHitBox :: get_adjusted_points ( self_) ;
96137 converted. sort_by ( |a, b| b. 0 . partial_cmp ( & a. 0 ) . unwrap ( ) ) ;
97138 Ok ( converted[ 0 ] . 0 )
98139 }
99140
100141 #[ getter]
101142 fn bottom ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
102- let mut converted: Vec < ( f32 , f32 ) > = AdjustableHitBox :: get_adjusted_points ( self_) ;
143+ let mut converted: Vec < ( f32 , f32 ) > = RotatableHitBox :: get_adjusted_points ( self_) ;
103144 converted. sort_by ( |a, b| a. 1 . partial_cmp ( & b. 1 ) . unwrap ( ) ) ;
104145 Ok ( converted[ 0 ] . 1 )
105146 }
106147
107148 #[ getter]
108149 fn top ( self_ : PyRef < ' _ , Self > ) -> PyResult < f32 > {
109- let mut converted: Vec < ( f32 , f32 ) > = AdjustableHitBox :: get_adjusted_points ( self_) ;
150+ let mut converted: Vec < ( f32 , f32 ) > = RotatableHitBox :: get_adjusted_points ( self_) ;
110151 converted. sort_by ( |a, b| b. 1 . partial_cmp ( & a. 1 ) . unwrap ( ) ) ;
111152 Ok ( converted[ 0 ] . 1 )
112153 }
0 commit comments