fly_b/simul/
hero.rs

1//! Module encapsulating the logic related to the in-game hero, who is controled by a player.
2
3/// Hero's components.
4pub mod compos {
5    use bevy::prelude::*;
6
7    /// Provides the hero's unique core.
8    ///
9    /// This marker allow to easily discern hero from other entities.
10    #[derive(Component, Reflect, Default)]
11    #[reflect(Component)]
12    pub struct HeroCore {
13        _future_priv_fields: (),
14    }
15}
16
17pub mod events {
18    pub mod death {
19        use bevy::prelude::*;
20
21        #[derive(Event, Debug, Default)]
22        pub struct HeroDeath {
23            _cause: (),
24        }
25    }
26    pub mod hop {
27        use bevy::prelude::*;
28        use derive_more::Constructor;
29
30        #[derive(Event, Constructor, Debug)]
31        pub struct HeroHop {
32            pub strength: f32,
33            /// Up teleport offset – the y offset used for calculating position after hop-induced teleportation.
34            pub up_tp_offset: f32,
35        }
36        impl Default for HeroHop {
37            fn default() -> Self {
38                Self::new(150., 15.)
39            }
40        }
41    }
42}
43
44/// Bundles.
45pub mod bundles;
46
47// Resources
48pub mod res;
49
50/// Systems controling hero and their effect on the surrounding environment.
51pub mod sys;
52
53use bevy::prelude::*;
54
55use crate::SimulState;
56
57// Constants
58
59/// Initial hero velocity.
60///
61/// The velocity value with witch the hero entity will start
62pub const INIT_VELOCITY: Vec2 = Vec2::new(100., -200.);
63pub const HOP_UP_HEIGHT: f32 = 60.;
64pub const HEAD_UP_ANGLE: f32 = 0.3;
65
66/// Provides hero with his behaviour and effect on the environment.
67#[derive(Default)]
68pub struct HeroPlugin {
69    _future_priv_fields: (),
70}
71
72// CRUD-C: Fabrication methods
73
74impl Plugin for HeroPlugin {
75    fn build(&self, app: &mut App) {
76        app.register_type::<compos::HeroCore>();
77        app.init_resource::<crate::simul::HeroColor>()
78            .add_event::<crate::simul::HeroDeath>()
79            .add_event::<crate::simul::HeroHop>();
80
81        use self::sys::collide;
82        app
83            // spawn systems
84            .add_systems(OnEnter(SimulState::Startup), sys::spawn)
85            // despawn sytems
86            .add_systems(OnEnter(SimulState::Cleanup), sys::despawn_if_present)
87            // movement systems
88            .add_systems(Update, (sys::hop, sys::up_implies_downs))
89            .add_systems(
90                OnEnter(SimulState::RunningWithGravity),
91                (sys::gravity_causes_downs_uncond,),
92            )
93            // collision systems
94            .add_systems(
95                Update,
96                (
97                    collide::with_ceiling,
98                    collide::with_floor,
99                    collide::with_lower_pole,
100                    collide::with_upper_pole,
101                )
102                    .run_if(SimulState::is_running_cond()),
103            )
104            // Misc.
105            .add_systems(Update, (sys::update_displayed_color,));
106    }
107}
108
109// CRUD-R: Properties
110
111fn upper_bound_y(hero_transform: &Transform) -> f32 {
112    hero_transform.translation.y + hero_transform.scale.y / 2.
113}
114fn lower_bound_y(hero_transform: &Transform) -> f32 {
115    hero_transform.translation.y - hero_transform.scale.y / 2.
116}
117fn left_bound_x(hero_transform: &Transform) -> f32 {
118    hero_transform.translation.x - hero_transform.scale.x / 2.
119}
120fn right_bound_x(hero_transform: &Transform) -> f32 {
121    hero_transform.translation.x + hero_transform.scale.x / 2.
122}