Skip to content

Commit 4599205

Browse files
committed
add 3th Yarwin suggestion:
"Use let statements: https://doc.rust-lang.org/book/ch19-01-all-the-places-for-patterns.html#let-statements. If let ... can get very messy in more complicated scenarios and overall make flow control more messy (it is hard to track all these indentations!)."
1 parent fa3608f commit 4599205

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

squash-the-creeps/rust/src/player.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,24 @@ impl ICharacterBody3D for Player {
8888
// smooth out the character's motion. So we have to loop over all collisions that may have
8989
// happened.
9090
// 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() {
9292
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;
107109
}
108110
}
109111
// This makes the character follow a nice arc when jumping.

0 commit comments

Comments
 (0)