Skip to content

Commit 078b91d

Browse files
committed
Support rotatable hit boxes in check_for_collision_with_list
1 parent 6a4f953 commit 078b91d

1 file changed

Lines changed: 54 additions & 9 deletions

File tree

src/sprite_list.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,67 @@ pub fn check_for_collision_with_list(
1010
) -> Vec<PyObject> {
1111
let mut final_sprites: Vec<PyObject> = Vec::new();
1212

13+
let mut hitbox1: Option<HitBox> = None;
14+
let mut hitbox2: Option<PyRef<'_, RotatableHitBox>> = None;
15+
16+
let cls: &str = sprite
17+
.getattr("_hit_box")
18+
.unwrap()
19+
.get_type()
20+
.name()
21+
.unwrap();
22+
23+
match cls {
24+
"HitBox" => {
25+
hitbox1 = sprite.getattr("_hit_box").unwrap().extract().unwrap();
26+
}
27+
"RotatableHitBox" => {
28+
hitbox2 = sprite.getattr("_hit_box").unwrap().extract().unwrap();
29+
}
30+
_ => panic!(),
31+
}
32+
33+
let main_points: Vec<(f32, f32)>;
34+
35+
if hitbox1.is_some() {
36+
main_points = hitbox1.unwrap().get_adjusted_points_native();
37+
} else {
38+
main_points = RotatableHitBox::get_adjusted_points(hitbox2.unwrap());
39+
}
40+
1341
let sprite_list_list = sprite_list.getattr("sprite_list").unwrap();
1442
let sprites_to_check: Vec<PyObject> = sprite_list_list.extract().unwrap();
1543

1644
for sprite2 in sprites_to_check.iter() {
17-
let hitbox1: HitBox = sprite.getattr("_hit_box").unwrap().extract().unwrap();
18-
let hitbox2: HitBox = sprite2
19-
.getattr(py, "_hit_box")
45+
let other_sprite: &PyAny = sprite2.as_ref(py);
46+
let mut other_hitbox1: Option<HitBox> = None;
47+
let mut other_hitbox2: Option<PyRef<'_, RotatableHitBox>> = None;
48+
let other_cls: &str = other_sprite
49+
.getattr("_hit_box")
2050
.unwrap()
21-
.extract(py)
51+
.get_type()
52+
.name()
2253
.unwrap();
2354

24-
let check_2 = are_polygons_intersecting(
25-
hitbox1.get_adjusted_points_native().to_vec(),
26-
//HitBox::get_adjusted_points_native(hitbox1).to_vec(),
27-
HitBox::get_adjusted_points_native(hitbox2).to_vec(),
28-
);
55+
match other_cls {
56+
"HitBox" => {
57+
other_hitbox1 = other_sprite.getattr("_hit_box").unwrap().extract().unwrap();
58+
}
59+
"RotatableHitBox" => {
60+
other_hitbox2 = other_sprite.getattr("_hit_box").unwrap().extract().unwrap();
61+
}
62+
_ => panic!(),
63+
}
64+
65+
let other_points: Vec<(f32, f32)>;
66+
67+
if other_hitbox1.is_some() {
68+
other_points = other_hitbox1.unwrap().get_adjusted_points_native();
69+
} else {
70+
other_points = RotatableHitBox::get_adjusted_points(other_hitbox2.unwrap());
71+
}
72+
73+
let check_2 = are_polygons_intersecting(main_points.to_vec(), other_points);
2974

3075
if check_2 {
3176
final_sprites.push(sprite2.to_object(py));

0 commit comments

Comments
 (0)