Skip to content

Commit

Permalink
Implement throw cd
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Aug 21, 2024
1 parent f0c8f64 commit 9557942
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy::{
};

use crate::{
interaction::{GrabParams, ShadowParams},
interaction::{HoldError, ShadowParams},
prelude::Cooldown,
};

Expand Down Expand Up @@ -146,7 +146,7 @@ impl Component for AvianPickupActor {
commands.entity(targeted_entity).insert((
AvianPickupActorState::default(),
Cooldown::default(),
GrabParams::default(),
HoldError::default(),
ShadowParams::default(),
));
});
Expand Down
8 changes: 8 additions & 0 deletions src/cooldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ pub(crate) struct Cooldown {
}

impl Cooldown {
pub(crate) fn throw(&mut self) {
// Happens to be the same as `drop`, but that's a coincidence
// Also, the CD does not differentiate between throwing a held object
// and throwing an object in front of us.
self.left = Timer::from_seconds(0.5, TimerMode::Once);
self.right = Timer::from_seconds(0.5, TimerMode::Once);
}

pub(crate) fn drop(&mut self) {
self.left = Timer::from_seconds(0.5, TimerMode::Once);
self.right = Timer::from_seconds(0.5, TimerMode::Once);
Expand Down
8 changes: 5 additions & 3 deletions src/interaction/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(super) fn plugin(app: &mut App) {
}

pub(super) mod prelude {
pub(crate) use super::{GrabParams, ShadowParams};
pub(crate) use super::{HoldError, ShadowParams};
}

#[derive(Debug, Copy, Clone, Component)]
Expand All @@ -46,15 +46,17 @@ impl Default for ShadowParams {
}
}

/// Cache for accumulating errors when holding an object.
/// When this reaches a critical value, the object will be dropped.
#[derive(Debug, Copy, Clone, Component)]
pub(crate) struct GrabParams {
pub(crate) struct HoldError {
/// Time until error starts accumulating
error_time: f32,
/// The distance between the object and the target position
error: f32,
}

impl Default for GrabParams {
impl Default for HoldError {
fn default() -> Self {
Self {
error_time: 0.0,
Expand Down
4 changes: 2 additions & 2 deletions src/interaction/hold/on_hold.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::*;

use super::GrabParams;
use super::HoldError;
use crate::{
prelude::*,
prop::{PickupMass, PrePickupRotation},
Expand All @@ -13,7 +13,7 @@ pub(super) fn on_hold(
mut commands: Commands,
mut q_actor: Query<(
&mut AvianPickupActorState,
&mut GrabParams,
&mut HoldError,
&Holding,
&Position,
&Rotation,
Expand Down
6 changes: 3 additions & 3 deletions src/interaction/hold/update.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use avian3d::sync::ancestor_marker::AncestorMarker;
use bevy::prelude::*;

use super::{GrabParams, ShadowParams};
use super::{HoldError, ShadowParams};
use crate::{
math::rigid_body_compound_collider,
prelude::*,
Expand All @@ -13,7 +13,7 @@ use crate::{
pub(super) fn update_error(
time: Res<Time>,
q_prop: Query<&Position>,
mut q_actor: Query<(&mut GrabParams, &ShadowParams, &Holding)>,
mut q_actor: Query<(&mut HoldError, &ShadowParams, &Holding)>,
) {
let dt = time.delta_seconds();
for (mut grab, shadow, holding) in q_actor.iter_mut() {
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(super) fn update_object(
mut q_actor: Query<(
Entity,
&AvianPickupActor,
&GrabParams,
&HoldError,
&mut ShadowParams,
&Holding,
&Position,
Expand Down
9 changes: 6 additions & 3 deletions src/interaction/throw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ pub(super) fn plugin(app: &mut App) {

/// DetachObject
fn throw(mut commands: Commands, mut q_actor: Query<(Entity, &mut Cooldown, &Throwing)>) {
for (actor, _cooldown, throw) in q_actor.iter_mut() {
for (actor, mut cooldown, throw) in q_actor.iter_mut() {
let _prop = throw.0;
// Todo: cooldown.throw();
info!("Throw!");
commands.entity(actor).remove::<Throwing>();
// TODO: Yeet object. This is also handled in DetachObject
// TODO: Yeet object. This is also handled in DetachObject through
// PrimaryAttack

// TODO: only CD when we actually threw something
cooldown.throw();

// TODO: let the user know this prop was dropped through an event or
// observer. Do events sent in a fixed timestep get propagated
Expand Down

0 comments on commit 9557942

Please sign in to comment.