bevy_input_bindings 0.0.2

High level, flexible and shareable input binding library for the Bevy game engine
Documentation
use std::time::Duration;

use bevy::reflect::Reflect;

#[derive(Clone, Debug, Reflect)]
pub struct Repeat {
    duration_before_first_repeat: Duration,
    duration_between_repeat: Duration,
    time_counter: Duration,
}

impl Repeat {
    pub(crate) fn new(
        duration_before_first_repeat: Duration,
        duration_between_repeat: Duration,
    ) -> Self {
        Self {
            duration_before_first_repeat,
            duration_between_repeat,
            time_counter: Duration::ZERO,
        }
    }

    pub(crate) fn reset(&mut self) {
        self.time_counter = Duration::ZERO;
    }

    pub(crate) fn is_triggered(&mut self, signal: bool, dt: &Duration) -> bool {
        // If no signal, reset the counter
        if !signal {
            self.time_counter = Duration::ZERO;
            return false;
        }
        // Increment time
        let previous_time = self.time_counter.clone();
        self.time_counter += *dt;
        // Check if it crossed the first repeat
        if previous_time < self.duration_before_first_repeat
            && self.time_counter >= self.duration_before_first_repeat
        {
            return true;
        }
        // Check if it crossed a subsequent repeat
        let mut next_repeat = self.duration_before_first_repeat + self.duration_between_repeat;
        while previous_time > next_repeat {
            next_repeat += self.duration_between_repeat;
        }
        if previous_time < next_repeat && self.time_counter >= next_repeat {
            return true;
        }
        return false;
    }
}