aboutsummaryrefslogtreecommitdiff
path: root/plugins/movement/src/lib.rs
blob: d279d3b0e52c386b922647a5b2d42e951a62a938 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! CH Modules - Movement
//!
//! CH Movement provides a set of systems for driving Entity movement.
//!
//! # Included
//!
//! - **Velocity**: Represents the entity's current momentum, used by systems to calculate the next frame's translation
//! - **Movement**: A pure marker, used to flag this entity as movable
//! - **VelocityReducer**: Controls how Velocity is reduced
//!
//! Movement is applied to the entity's [`Transform::translation`].

#![deny(missing_docs)]

use bevy::{
    app::{FixedUpdate, Plugin},
    ecs::{query::With, system::Query},
    transform::components::Transform,
};
use ch_macro_rules::import;

// Basic data
import!(velocity, movement);

// Modifiers
import!(reducer);

/// Cigarette Hacker movement plugin
///
/// Used to describe the movement of characters in the game
pub struct CHMovementPlugins;

impl Plugin for CHMovementPlugins {
    fn build(&self, app: &mut bevy::app::App) {
        app.add_systems(FixedUpdate, update_movement);
    }
}

fn update_movement(
    query: Query<(&mut Transform, &mut Velocity, Option<&VelocityReducer>), With<Movement>>,
) {
    for (mut transform, mut velo, reducer) in query {
        // Add Velocity to the entity's translation
        transform.translation += **velo;

        // Apply velocity reduction (default to Lerp(0.25) if no reducer present)
        match reducer {
            Some(reducer) => reducer.calculate(&mut velo),
            None => VelocityReducer::Lerp(0.25).calculate(&mut velo),
        }
    }
}