Skip to main content

bevy_rapier_baseball_flight/
lib.rs

1mod ball_flight_state;
2mod common;
3mod components;
4mod errors;
5mod events;
6mod resources;
7mod systems;
8
9pub mod prelude {
10    pub use super::{
11        ball_flight_state::BaseballFlightState, components::*, constants::*, errors::*, events::*,
12        utils::*, BaseballFlightPlugin, GyroPole, Tilt,
13    };
14}
15
16use crate::systems::*;
17
18pub(crate) use crate::resources::*;
19pub(crate) use ball_flight_state::*;
20pub(crate) use bevy::{math::*, prelude::*}; // glam
21pub(crate) use bevy_rapier3d::prelude::*; // nalgebra
22pub(crate) use common::*;
23pub(crate) use constants::*;
24pub(crate) use errors::*;
25pub(crate) use events::*;
26pub(crate) use utils::*;
27
28pub struct BaseballFlightPlugin {
29    pub ssw_on: bool,
30    pub magnus_on: bool,
31    pub drag_on: bool,
32}
33
34impl Plugin for BaseballFlightPlugin {
35    fn build(&self, app: &mut App) {
36        app.add_event::<ActivateAerodynamicsEvent>()
37            .add_event::<PostActivateAerodynamicsEvent>()
38            .add_event::<DisableAerodynamicsEvent>();
39
40        // app.register_type::<BaseballFlightState>();
41
42        app.insert_resource(BaseballPluginConfig {
43            ssw_on: self.ssw_on,
44            magnus_on: self.magnus_on,
45            drag_on: self.drag_on,
46            ..default()
47        });
48
49        app.configure_sets(
50            Update,
51            (
52                AeroActivationSet::PreActivation,
53                AeroActivationSet::Activation,
54                AeroActivationSet::PostActivation,
55            )
56                .chain(),
57        )
58        .configure_sets(
59            Update,
60            (
61                UpdateBaseballFlightStateSet::PreUpdate,
62                UpdateBaseballFlightStateSet::Update,
63                UpdateBaseballFlightStateSet::PostUpdate,
64            )
65                .chain(),
66        )
67        .add_systems(
68            Update,
69            (activate_aerodynamics,)
70                .chain()
71                .in_set(AeroActivationSet::Activation),
72        );
73
74        app.configure_sets(
75            Update,
76            (
77                AeroDeactivationSet::PreDeactivation,
78                AeroDeactivationSet::Deactivation,
79                AeroDeactivationSet::PostDeactivation,
80            )
81                .chain(),
82        )
83        .add_systems(
84            Update,
85            (disable_aerodynamics,)
86                .chain()
87                .in_set(AeroDeactivationSet::Deactivation),
88        );
89
90        app.add_systems(
91            Update,
92            (
93                // _apply_physics_option_1,
94                // _apply_physics_option_2,
95                _apply_physics_option_3
96            )
97                .in_set(UpdateBaseballFlightStateSet::Update),
98        );
99    }
100}
101
102#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
103pub enum AeroActivationSet {
104    PreActivation,
105    Activation,
106    PostActivation,
107}
108
109#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
110pub enum AeroDeactivationSet {
111    PreDeactivation,
112    Deactivation,
113    PostDeactivation,
114}
115
116#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
117pub enum UpdateBaseballFlightStateSet {
118    PreUpdate,
119    Update,
120    PostUpdate,
121}
122
123#[derive(Debug, Clone, Copy, Reflect, PartialEq, Eq)]
124pub enum GyroPole {
125    Right,
126    Left,
127}
128
129impl Default for GyroPole {
130    fn default() -> Self {
131        Self::Right
132    }
133}
134
135#[derive(Debug, Reflect, Copy, Clone)]
136pub struct Tilt(f32);
137impl Tilt {
138    pub fn from_hour_mintes(h: i8, m: i8) -> Result<Self> {
139        if h > 12 || h <= 0 {
140            return Err(BaseballFlightError::InvalidInput(
141                "hr should be within the range of 1 and 12".into(),
142            ));
143        }
144        if m < 0 || m > 59 {
145            return Err(BaseballFlightError::InvalidInput(
146                "min should be within the range of 0 and 59".into(),
147            ));
148        }
149        let rad_hrs = (h - 3) as f32 * PI_32 / 6.;
150        let rad_mins = m as f32 * PI_32 / 360.;
151        Ok(Self(rad_hrs + rad_mins))
152    }
153
154    pub fn to_hour_minutes(&self) -> (i8, i8) {
155        let total_hours = (self.0 * 6.0 / PI_32) + 3.0;
156        let hrs = total_hours.floor() as i8;
157        let mins = ((total_hours.fract() * 60.0).round() as i8) % 60;
158        ((hrs - 1) % 12 + 1, mins)
159    }
160
161    pub fn get(&self) -> f32 {
162        self.0
163    }
164}