use bevy::prelude::{Component, Entity, Resource};
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct PatternTrigger {
pub id: String,
pub channel: usize,
pub min_amplitude: f32,
pub frequency_hz: Option<f32>,
pub frequency_tolerance_hz: f32,
pub cooldown_frames: u64,
}
impl PatternTrigger {
pub fn new(id: impl Into<String>, channel: usize) -> Self {
Self {
id: id.into(),
channel,
min_amplitude: 0.25,
frequency_hz: None,
frequency_tolerance_hz: 12.0,
cooldown_frames: 0,
}
}
pub fn with_min_amplitude(mut self, threshold: f32) -> Self {
self.min_amplitude = threshold.max(0.0);
self
}
pub fn with_frequency(mut self, freq_hz: f32, tolerance_hz: f32) -> Self {
self.frequency_hz = Some(freq_hz.max(0.0));
self.frequency_tolerance_hz = tolerance_hz.abs();
self
}
pub fn with_cooldown(mut self, frames: u64) -> Self {
self.cooldown_frames = frames;
self
}
}
#[derive(Component, Clone, Debug, Default)]
pub struct PatternTriggerSet {
pub patterns: Vec<PatternTrigger>,
}
impl PatternTriggerSet {
pub fn new() -> Self {
Self {
patterns: Vec::new(),
}
}
pub fn from_patterns(patterns: Vec<PatternTrigger>) -> Self {
Self { patterns }
}
pub fn push(&mut self, trigger: PatternTrigger) {
self.patterns.push(trigger);
}
pub fn with_pattern(mut self, trigger: PatternTrigger) -> Self {
self.patterns.push(trigger);
self
}
pub fn is_empty(&self) -> bool {
self.patterns.is_empty()
}
}
#[derive(Resource, Default)]
pub struct PatternTriggerRuntime(pub HashMap<Entity, Vec<u64>>);