use bevy::ecs::component::Component;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub enum VariantValue {
None,
Bool(bool),
Axis1d(f32),
Axis2d(f32, f32),
}
#[derive(Component)]
pub struct ControllerValues<ValueKey>
where
ValueKey: std::hash::Hash + PartialEq + Eq + Sync + Send + 'static,
{
pub(crate) values: HashMap<ValueKey, VariantValue>,
}
impl<ValueKey> ControllerValues<ValueKey>
where
ValueKey: std::hash::Hash + PartialEq + Eq + Sync + Send + 'static,
{
pub(crate) fn new() -> Self {
Self {
values: HashMap::new(),
}
}
pub fn get<'a>(&'a self, key: &ValueKey) -> Option<&'a VariantValue> {
return self.values.get(key);
}
pub fn get_as_bool(&self, key: &ValueKey) -> Option<bool> {
match self.values.get(key) {
Some(VariantValue::Bool(value)) => Some(*value),
_ => None,
}
}
pub fn get_as_axis1d(&self, key: &ValueKey) -> Option<f32> {
match self.values.get(key) {
Some(VariantValue::Axis1d(value)) => Some(*value),
_ => None,
}
}
pub fn get_as_axis2d(&self, key: &ValueKey) -> Option<(f32, f32)> {
match self.values.get(key) {
Some(VariantValue::Axis2d(value0, value1)) => Some((*value0, *value1)),
_ => None,
}
}
}