Skip to content

Commit

Permalink
Remove unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Aug 26, 2024
1 parent bcfd79e commit 325be3d
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/cooldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Default for Cooldown {

impl Cooldown {
fn get(&self, action: &AvianPickupAction) -> &Timer {
// Safety: all actions are always present in the map as we initialize them in `default`.
self.0.get(action).unwrap()
}

Expand Down
2 changes: 1 addition & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn set_verbs_according_to_input(
continue;
};

// Doing these checks now so that later systems can just call `unwrap`
// Doing these checks now so that we can report issues early.
let checks = [
(has_global_transform, "GlobalTransform"),
(has_shadow, "ShadowParams"),
Expand Down
5 changes: 4 additions & 1 deletion src/interaction/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ fn drop(
});
// Safety: the prop is a dynamic rigid body and thus is guaranteed to have a
// linvel and angvel.
let (mut velocity, mut angvel) = q_prop.get_mut(prop).unwrap();
let Ok((mut velocity, mut angvel)) = q_prop.get_mut(prop) else {
error!("Prop entity was deleted or in an invalid state. Ignoring.");
continue;
};
// HL2 uses 190 inches per second, which is 4.826 meters per second.
// let's round that to 5 m/s.
const HL2_NORM_SPEED: Scalar = 5.0;
Expand Down
15 changes: 10 additions & 5 deletions src/interaction/hold/on_add_holding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ pub fn on_add_holding(
)>,
) {
let actor = trigger.entity();
let (config, mut state, mut hold_error, holding) = q_actor.get_mut(actor).unwrap();
let Ok((config, mut state, mut hold_error, holding)) = q_actor.get_mut(actor) else {
error!("Actor entity was deleted or in an invalid state. Ignoring.");
return;
};
let actor_transform = q_actor_transform.get_best_global_transform(actor);
let prop = holding.0;
*state = AvianPickupActorState::Holding(prop);
commands.entity(prop).insert(HeldProp);
// Safety: All props are rigid bodies, so they are guaranteed to have a
// `Rotation` and `Mass`.
let (rotation, mut mass, pickup_mass, non_pickup_mass, pre_pickup_rotation) =
q_prop.get_mut(prop).unwrap();
let Ok((rotation, mut mass, pickup_mass, non_pickup_mass, pre_pickup_rotation)) =
q_prop.get_mut(prop)
else {
error!("Prop entity was deleted or in an invalid state. Ignoring.");
return;
};
let new_mass = pickup_mass
.map(|m| m.0)
.unwrap_or(config.hold.temporary_prop_mass);
Expand Down
8 changes: 6 additions & 2 deletions src/interaction/hold/on_remove_holding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ fn on_remove_holding(
q_actor: Query<&Holding>,
mut q_prop: Query<(&mut Mass, Option<&NonPickupMass>, Has<HeldProp>)>,
) {
// Safety: We are removing a `Holding` component, so we know that the entity has
// one.
let holding = q_actor.get(trigger.entity()).unwrap();
let prop = holding.0;
// Safety: All props are rigid bodies, so they are guaranteed to have a `Mass`.
let (mut mass, non_pickup_mass, has_held_marker) = q_prop.get_mut(prop).unwrap();
let Ok((mut mass, non_pickup_mass, has_held_marker)) = q_prop.get_mut(prop) else {
error!("Prop entity was deleted or in an invalid state. Ignoring.");
return;
};
if !has_held_marker {
error!(
"A held prop that is no longer being held was not actually marked as held. This is supremely weird. Ignoring."
Expand Down
7 changes: 4 additions & 3 deletions src/interaction/hold/set_velocities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ fn set_velocities(
let inv_dt = dt.recip();
for (shadow, holding, actor) in q_actor.iter_mut() {
let prop = holding.0;
// Safety: All props are rigid bodies, so they are guaranteed to have a
// `Position`, `Rotation`, `LinearVelocity`, and `AngularVelocity`.
let (mut velocity, mut angvel, position, rotation) = q_prop.get_mut(prop).unwrap();
let Ok((mut velocity, mut angvel, position, rotation)) = q_prop.get_mut(prop) else {
error!("Prop entity was deleted or in an invalid state. Ignoring.");
continue;
};

let delta_position = shadow.target_position - position.0;

Expand Down
7 changes: 4 additions & 3 deletions src/interaction/hold/update_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ pub fn update_error(
if hold_error.error_time <= 0.0 {
continue;
}
// Safety: All props are rigid bodies, so they are guaranteed to have a
// `Position`.
let position = q_prop.get(prop).unwrap();
let Ok(position) = q_prop.get(prop) else {
error!("Prop entity was deleted or in an invalid state. Ignoring.");
continue;
};
let mut error = (position.0 - shadow.target_position).length();
if hold_error.error_time > 1.0 {
hold_error.error_time = 1.0;
Expand Down
9 changes: 5 additions & 4 deletions src/interaction/hold/update_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ fn set_targets(
}
let actor_transform = q_actor_transform.get_best_global_transform(actor);

// Safety: All props are rigid bodies, so they are guaranteed to have a
// `Rotation`.
let (
let Ok((
prop_rotation,
pre_pickup_rotation,
preferred_rotation,
preferred_distance,
clamp_pitch,
) = q_prop.get_mut(prop).unwrap();
)) = q_prop.get_mut(prop)else {
error!("Prop entity was deleted or in an invalid state. Ignoring.");
continue;
};
let pitch_range = clamp_pitch
.map(|c| &c.0)
.unwrap_or(&config.hold.pitch_range);
Expand Down
3 changes: 1 addition & 2 deletions src/interaction/pull/find_in_cone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ pub(super) fn find_prop_in_cone(
let mut canditate = None;

for collider in colliders {
// Safety: this collection only contains entities that are contained in
// `q_collider`
// Safety: Pretty sure a `shape_intersection` will never return an entity without a `Position`.
let object_translation = q_collider.get(collider).unwrap().0;

// Closer than other objects
Expand Down
7 changes: 5 additions & 2 deletions src/interaction/pull/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ fn find_object(
continue;
};

// Safety: all colliders have a `ColliderParent`
let rigid_body_entity = q_collider_parent.get(prop.entity).unwrap().get();
let Ok(rigid_body_entity) = q_collider_parent.get(prop.entity) else {
error!("Collider entity was deleted or in an invalid state. Ignoring.");
continue;
};
let rigid_body_entity = rigid_body_entity.get();

let Ok((&rigid_body, &mass, mut impulse, prop_position, is_already_being_held)) =
q_rigid_body.get_mut(rigid_body_entity)
Expand Down
8 changes: 6 additions & 2 deletions src/interaction/throw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ fn throw(
let actor_transform = q_actor_transform.get_best_global_transform(actor);
// Safety: All props are rigid bodies, which are guaranteed to have a
// `LinearVelocity`, `AngularVelocity`, and `Mass`.
let (mut velocity, mut angvel, mass, lin_speed_override, ang_speed_override) =
q_prop.get_mut(prop).unwrap();
let Ok((mut velocity, mut angvel, mass, lin_speed_override, ang_speed_override)) =
q_prop.get_mut(prop)
else {
error!("Prop entity was deleted or in an invalid state. Ignoring.");
continue;
};
// The 2013 code now does a `continue` on
// `prop_dist_sq > config.interaction_distance * config.interaction_distance`
// but eh, that's fine. Better to respect players' input in such edge cases.
Expand Down
2 changes: 1 addition & 1 deletion src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl GetBestGlobalTransform
for Query<'_, '_, (&GlobalTransform, Option<&Position>, Option<&Rotation>)>
{
fn get_best_global_transform(&self, entity: Entity) -> Transform {
let (global_transform, position, rotation) = self.get(entity).unwrap();
let (global_transform, position, rotation) = self.get(entity).expect("Got an entity without `GlobalTransform`");
if let Some(position) = position {
if let Some(rotation) = rotation {
return Transform::from_translation(position.0).with_rotation(rotation.0);
Expand Down
6 changes: 4 additions & 2 deletions src/verb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ fn set_verb(
mut commands: Commands,
q_actor: Query<(Has<Throwing>, Has<Dropping>, Has<Pulling>, Has<Holding>)>,
) {
// Safety: we are only querying optional components.
let (throwing, dropping, pulling, holding) = q_actor.get(actor).unwrap();
let Ok((throwing, dropping, pulling, holding)) = q_actor.get(actor) else {
error!("Actor entity was deleted or in an invalid state. Ignoring.");
return;
};
let mut commands = commands.entity(actor);
match verb {
Some(Verb::Throw(prop)) => {
Expand Down

0 comments on commit 325be3d

Please sign in to comment.