Skip to main content

avian_pickup/
lib.rs

1#![allow(clippy::too_many_arguments, clippy::type_complexity)]
2#![warn(missing_docs)]
3#![doc = include_str!("../readme.md")]
4
5use avian3d::prelude::*;
6use bevy_app::prelude::*;
7use bevy_ecs::prelude::*;
8
9pub mod actor;
10mod cooldown;
11pub mod input;
12mod interaction;
13mod math;
14pub mod output;
15pub mod prop;
16mod rng;
17mod verb;
18pub use verb::Holding;
19
20pub use rng::*;
21/// Everything you need to get started with Avian Pickup.
22pub mod prelude {
23    pub(crate) use super::{
24        HandleVerbSystem,
25        cooldown::prelude::*,
26        prop::{NonPickupMass, prelude::*},
27    };
28    pub use crate::{
29        AvianPickupPlugin, AvianPickupSystem, actor::prelude::*, input::prelude::*,
30        output::prelude::*,
31    };
32    pub(crate) use avian3d::prelude::*;
33    pub(crate) use bevy_app::prelude::*;
34    pub(crate) use bevy_ecs::prelude::*;
35    pub(crate) use bevy_log::prelude::*;
36    pub(crate) use bevy_math::prelude::*;
37    pub(crate) use bevy_reflect::prelude::*;
38    pub(crate) use bevy_transform::prelude::*;
39    pub(crate) use bevy_utils::prelude::*;
40}
41
42/// The Avian Pickup plugin. Add this after the Avian Physics plugins to enable
43/// pickup functionality. Uses the same [`Schedule`]` as Avian.
44///
45/// # Example
46///
47/// ```no_run
48/// # use avian3d::prelude::*;
49/// # use avian_pickup::prelude::*;
50/// # use bevy::prelude::*;
51///
52/// App::new().add_plugins((
53///     DefaultPlugins,
54///     PhysicsPlugins::default(),
55///     AvianPickupPlugin::default(),
56/// ));
57/// ```
58#[derive(Default)]
59#[non_exhaustive]
60pub struct AvianPickupPlugin;
61
62impl Plugin for AvianPickupPlugin {
63    fn build(&self, app: &mut App) {
64        let physics_schedule = app.get_schedule(PhysicsSchedule);
65        if physics_schedule.is_none() {
66            panic!(
67                "Failed to build `AvianPickupPlugin`:\
68                Avian's `PhysicsSchedule` was not found. Make sure to add Avian's plugins *before* `AvianPickupPlugin`.\
69                This usually done by adding `PhysicsPlugins` to your `App`."
70            )
71        }
72
73        app.configure_sets(
74            PhysicsSchedule,
75            (
76                AvianPickupSystem::First,
77                AvianPickupSystem::HandleVerb,
78                AvianPickupSystem::ResetIdle,
79                AvianPickupSystem::TickTimers,
80                AvianPickupSystem::Last,
81            )
82                .chain()
83                .before(PhysicsStepSystems::First),
84        )
85        .configure_sets(
86            PhysicsSchedule,
87            (
88                HandleVerbSystem::Pull,
89                HandleVerbSystem::Hold,
90                HandleVerbSystem::Drop,
91                HandleVerbSystem::Throw,
92            )
93                .chain()
94                .in_set(AvianPickupSystem::HandleVerb),
95        );
96
97        app.add_plugins((
98            input::plugin,
99            output::plugin,
100            actor::plugin,
101            interaction::plugin,
102            cooldown::plugin,
103            prop::plugin,
104            verb::plugin,
105            rng::plugin,
106        ));
107    }
108}
109
110/// Set enum for the systems added by [`AvianPickupPlugin`].
111/// Use this to order your systems relative to the ones used by Avian Pickup.
112/// This is run in Avian's [`PhysicsStepSet::First`] and scheduled under [`PhysicsSchedule`].
113#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
114pub enum AvianPickupSystem {
115    /// Runs at the start of the [`AvianPickupSystem`]. Empty by default.
116    First,
117    /// Adds forces to an object held by
118    /// [`AvianPickupActorState::Holding`](crate::prelude::AvianPickupActorState::Holding)
119    /// in order to keep it in place in front of the
120    /// [`AvianPickupActor`](crate::prelude::AvianPickupActor).
121    HandleVerb,
122    /// Resets the
123    /// [`AvianPickupActorState`](crate::prelude::AvianPickupActorState) to
124    /// [`AvianPickupActorState::Idle`](crate::prelude::AvianPickupActorState::Idle)
125    /// if needed
126    ResetIdle,
127    /// Advances internal cooldown timers.
128    TickTimers,
129    /// Runs at the end of the [`AvianPickupSystem`]. Empty by default.
130    Last,
131}
132
133#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
134pub(crate) enum HandleVerbSystem {
135    Pull,
136    Hold,
137    Drop,
138    Throw,
139}