Skip to content

Commit

Permalink
Fix missing checks
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Aug 19, 2024
1 parent a936461 commit 9ceb68f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn setup(
..default()
},
AvianPickupActor::default(),
RigidBody::Kinematic,
));

commands.spawn((
Expand Down
2 changes: 0 additions & 2 deletions src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ pub enum AvianPickupActorState {
Pulling(Entity),
/// The actor is holding an object.
Holding(Entity),
/// The actor is throwing an object.
Throwing(Entity),
}

impl Default for AvianPickupActor {
Expand Down
27 changes: 19 additions & 8 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use avian3d::prelude::*;
use bevy::{prelude::*, utils::HashSet};

use crate::{
Expand Down Expand Up @@ -55,18 +56,20 @@ fn set_verbs_according_to_input(
(
Entity,
Option<&AvianPickupActorState>,
Has<Cooldown>,
Option<&Cooldown>,
Has<GlobalTransform>,
Has<RigidBody>,
),
With<AvianPickupActor>,
>,
) {
let mut unhandled_actors: HashSet<_> = q_actor.iter().map(|(entity, ..)| entity).collect();
for &event in r_input.read() {
'outer: for &event in r_input.read() {
let kind = event.kind;
let actor = event.actor;
unhandled_actors.remove(&actor);
let Ok((_entity, state, has_cooldown, has_transform)) = q_actor.get(actor) else {
let Ok((_entity, state, cooldown, has_transform, has_rigid_body)) = q_actor.get(actor)
else {
error!(
"`AvianPickupEvent` was triggered on an entity without `AvianPickupActor`. Ignoring."
);
Expand All @@ -75,15 +78,15 @@ fn set_verbs_according_to_input(

// Doing these checks now so that later systems can just call `unwrap`
let checks = [
(has_cooldown, "Cooldown"),
(has_transform, "GlobalTransform"),
(has_rigid_body, "RigidBody"),
];
for (has_component, component_name) in checks.iter() {
if !has_component {
error!(
"`AvianPickupEvent` was triggered on an entity without `{component_name}`. Ignoring."
);
continue;
continue 'outer;
}
}
let Some(&state) = state else {
Expand All @@ -93,18 +96,26 @@ fn set_verbs_according_to_input(
continue;
};

let Some(cooldown) = cooldown else {
error!("`AvianPickupEvent` was triggered on an entity without `Cooldown`. Ignoring.");
continue;
};

let verb = match kind {
AvianPickupInputKind::JustPressedL => Some(Verb::Throw),
AvianPickupInputKind::JustPressedL if cooldown.left.finished() => Some(Verb::Throw),
AvianPickupInputKind::JustPressedL => None,
AvianPickupInputKind::JustPressedR
if matches!(state, AvianPickupActorState::Holding(..)) =>
if matches!(state, AvianPickupActorState::Holding(..))
&& cooldown.right.finished() =>
{
Some(Verb::Drop)
}
AvianPickupInputKind::JustPressedR | AvianPickupInputKind::PressedR => {
if matches!(
state,
AvianPickupActorState::Idle | AvianPickupActorState::Pulling(..)
) {
) && cooldown.right.finished()
{
Some(Verb::Pull)
} else {
None
Expand Down
3 changes: 0 additions & 3 deletions src/interaction/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ pub(super) fn plugin(app: &mut App) {

fn drop(mut q_state: Query<(&mut AvianPickupActorState, &mut Cooldown), With<Dropping>>) {
for (mut state, mut cooldown) in q_state.iter_mut() {
if !cooldown.right.finished() {
continue;
}
*state = AvianPickupActorState::Idle;
info!("Drop!");
cooldown.drop();
Expand Down
13 changes: 12 additions & 1 deletion src/interaction/hold/update.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use avian3d::math::Vector;
use bevy::prelude::*;

use super::{GrabParams, ShadowParams};
Expand Down Expand Up @@ -56,11 +57,14 @@ pub(super) fn update_object(
let max_error = 0.3048; // 12 inches in the source engine
for (actor, config, grab, _shadow, holding, actor_rotation) in q_actor.iter_mut() {
if grab.error > max_error {
info!("error: {}, max_error: {}", grab.error, max_error);
commands.entity(actor).add(SetVerb::new(Verb::Drop));
continue;
}
let actor_pitch = actor_rotation.to_euler(EulerRot::YXZ).1;
let actor_to_prop_pitch = actor_pitch.clamp(config.min_pitch, config.max_pitch);
let _actor_to_prop_pitch = actor_pitch.clamp(config.min_pitch, config.max_pitch);
let forward = Transform::from_rotation(actor_rotation.0).forward();
collide_get_extent(forward);

let prop = holding.0;
let (preferred_rotation, preferred_distance, _pickup_mass, _position, rotation) =
Expand All @@ -71,3 +75,10 @@ pub(super) fn update_object(
let _target_distance = preferred_distance.copied().unwrap_or_default().0;
}
}

fn collide_get_extent(dir: Dir3) {
let collider = Collider::cuboid(1.0, 1.0, 1.0);
let support_map = collider.shape().as_support_map().unwrap();
let extent = support_map.support_point(&default(), &Vector::from(-dir).into());
info!("extent: {extent:?}");
}
16 changes: 7 additions & 9 deletions src/interaction/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ fn find_object(
.entity(actor)
.add(SetVerb::new(Verb::Hold(rigid_body_entity)));
} else {
if cooldown.right.finished() {
let object_transform = object_transform.compute_transform();
let direction =
(origin.translation - object_transform.translation).normalize_or_zero();
let mass_adjustment = adjust_impulse_for_mass(mass);
let pull_impulse = direction * config.pull_force * mass_adjustment;
cooldown.pull();
impulse.apply_impulse(pull_impulse);
}
let object_transform = object_transform.compute_transform();
let direction = (origin.translation - object_transform.translation).normalize_or_zero();
let mass_adjustment = adjust_impulse_for_mass(mass);
let pull_impulse = direction * config.pull_force * mass_adjustment;
cooldown.pull();
impulse.apply_impulse(pull_impulse);

*state = AvianPickupActorState::Pulling(rigid_body_entity);
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/interaction/throw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ pub(super) fn plugin(app: &mut App) {
}

fn throw(mut q_actor: Query<&mut Cooldown, With<Throwing>>) {
for cooldown in q_actor.iter_mut() {
if !cooldown.left.finished() {
continue;
}
for _cooldown in q_actor.iter_mut() {
// Todo: cooldown.throw();
info!("Throw!");
}
Expand Down

0 comments on commit 9ceb68f

Please sign in to comment.