@@ -88,22 +88,24 @@ impl ICharacterBody3D for Player {
88
88
// smooth out the character's motion. So we have to loop over all collisions that may have
89
89
// happened.
90
90
// If there are no "slides" this frame, the loop below won't run.
91
- for index in 0 ..self . base ( ) . get_slide_collision_count ( ) {
91
+ for index in 0 ..self . base ( ) . get_slide_collision_count ( ) {
92
92
let collision = self . base_mut ( ) . get_slide_collision ( index) . unwrap ( ) ;
93
- if let Some ( collider) = collision. get_collider ( ) {
94
- if let Ok ( node) = collider. try_cast :: < Node3D > ( ) {
95
- if node. is_in_group ( "mob" ) {
96
- let mut mob = collision. get_collider ( ) . unwrap ( ) . cast :: < Mob > ( ) ;
97
- if Vector3 :: UP . dot ( collision. get_normal ( ) ) > 0.1 {
98
- mob. bind_mut ( ) . squash ( ) ;
99
-
100
- self . target_velocity . y = self . bounce_impulse ;
101
- // Prevent this block from running more than once,
102
- // which would award the player more than 1 point for squashing a single mob.
103
- break ;
104
- }
105
- }
106
- }
93
+ // Skip given collider if they are not a Mob.
94
+ // Mob is an instance of Mob class in the "mob" group.
95
+ let Some ( Ok ( mut mob) ) = collision
96
+ . get_collider ( )
97
+ . map ( Gd :: cast :: < Node3D > )
98
+ . filter ( |n| n. is_in_group ( "mob" ) )
99
+ . map ( |n| n. try_cast :: < Mob > ( ) )
100
+ else {
101
+ continue ;
102
+ } ;
103
+ if Vector3 :: UP . dot ( collision. get_normal ( ) ) > 0.1 {
104
+ mob. bind_mut ( ) . squash ( ) ;
105
+ self . target_velocity . y = self . bounce_impulse ;
106
+ // Prevent this block from running more than once,
107
+ // which would award the player more than 1 point for squashing a single mob.
108
+ break ;
107
109
}
108
110
}
109
111
// This makes the character follow a nice arc when jumping.
0 commit comments