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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
//! Tools to control the [`ActionState`] of other entities automatically from other entities.
#[allow(unused_imports)] // For the documentation
use crate::action_state::ActionState;
use bevy::utils::hashbrown::hash_set::Iter;
use std::iter::Once;
use bevy::{
ecs::{component::Component, entity::Entity},
utils::HashSet,
};
use crate::Actionlike;
/// A component that allows the attached entity to drive the [`ActionState`] of the associated entity
///
/// # Examples
///
/// By default, [`update_action_state_from_interaction`](crate::systems::update_action_state_from_interaction) uses this component
/// in order to connect `bevy::ui` buttons to the corresponding `ActionState`.
///
/// ```rust
/// use bevy::prelude::*;
/// use leafwing_input_manager::prelude::*;
///
/// #[derive(Actionlike, PartialEq, Eq, Hash, Clone, Copy, Reflect)]
/// enum DanceDance {
/// Left,
/// Right,
/// Up,
/// Down,
/// }
///
/// // Spawn entity to track dance inputs
/// let mut world = World::new();
/// let dance_tracker = world
/// .spawn(ActionState::<DanceDance>::default())
/// .id();
///
/// // Spawn a button, which is wired up to the dance tracker
/// // When used with InputManagerPlugin<DanceDance>, this button will press the DanceDance::Left action when it is pressed.
/// world
/// .spawn(ButtonBundle::default())
/// // This component links the button to the entity with the `ActionState` component
/// .insert(ActionStateDriver {
/// action: DanceDance::Left,
/// targets: dance_tracker.into(),
/// });
///```
///
/// Writing your own systems that use the [`ActionStateDriver`] component is easy,
/// although this should be reserved for cases where the entity whose value you want to check
/// is distinct from the entity whose [`ActionState`] you want to set.
/// Check the source code of [`update_action_state_from_interaction`](crate::systems::update_action_state_from_interaction) for an example of how this is done.
#[derive(Debug, Component, Clone, PartialEq, Eq)]
pub struct ActionStateDriver<A: Actionlike> {
/// The action triggered by this entity
pub action: A,
/// The entity whose action state should be updated
pub targets: ActionStateDriverTarget,
}
/// Represents the entities that an ``ActionStateDriver`` targets.
#[derive(Debug, Component, Clone, PartialEq, Eq)]
pub enum ActionStateDriverTarget {
/// No targets
None,
/// Single target
Single(Entity),
/// Multiple targets
Multi(HashSet<Entity>),
}
impl ActionStateDriverTarget {
/// Get an iterator for the entities targeted.
#[inline(always)]
pub fn iter(&self) -> impl Iterator<Item = &Entity> {
match self {
Self::None => ActionStateDriverTargetIterator::None,
Self::Single(entity) => {
ActionStateDriverTargetIterator::Single(std::iter::once(entity))
}
Self::Multi(entities) => ActionStateDriverTargetIterator::Multi(entities.iter()),
}
}
/// Insert an entity as a target.
#[inline(always)]
pub fn insert(&mut self, entity: Entity) {
// Don't want to copy a bunch of logic, switch out the ref, then replace it
// rust doesn't like in place replacement
*self = std::mem::replace(self, Self::None).with(entity);
}
/// Remove an entity as a target if it's in the target set.
#[inline(always)]
pub fn remove(&mut self, entity: Entity) {
// see insert
*self = std::mem::replace(self, Self::None).without(entity);
}
/// Add an entity as a target.
#[inline(always)]
pub fn add(&mut self, entities: impl Iterator<Item = Entity>) {
for entity in entities {
self.insert(entity)
}
}
/// Get the number of targets.
#[inline(always)]
pub fn len(&self) -> usize {
match self {
Self::None => 0,
Self::Single(_) => 1,
Self::Multi(targets) => targets.len(),
}
}
/// Returns true if there are no targets.
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Add an entity as a target using a builder style pattern.
#[inline(always)]
pub fn with(mut self, entity: Entity) -> Self {
match self {
Self::None => Self::Single(entity),
Self::Single(og) => Self::Multi(HashSet::from([og, entity])),
Self::Multi(ref mut targets) => {
targets.insert(entity);
self
}
}
}
/// Remove an entity as a target if it's in the set using a builder style pattern.
pub fn without(self, entity: Entity) -> Self {
match self {
Self::None => Self::None,
Self::Single(_) => Self::None,
Self::Multi(mut targets) => {
targets.remove(&entity);
Self::from_iter(targets)
}
}
}
}
impl From<Entity> for ActionStateDriverTarget {
fn from(value: Entity) -> Self {
Self::Single(value)
}
}
impl From<()> for ActionStateDriverTarget {
fn from(_value: ()) -> Self {
Self::None
}
}
impl FromIterator<Entity> for ActionStateDriverTarget {
fn from_iter<T: IntoIterator<Item = Entity>>(iter: T) -> Self {
let entities = HashSet::from_iter(iter);
match entities.len() {
0 => Self::None,
1 => Self::Single(entities.into_iter().next().unwrap()),
_ => Self::Multi(entities),
}
}
}
impl<'a> FromIterator<&'a Entity> for ActionStateDriverTarget {
fn from_iter<T: IntoIterator<Item = &'a Entity>>(iter: T) -> Self {
let entities = HashSet::from_iter(iter.into_iter().cloned());
match entities.len() {
0 => Self::None,
1 => Self::Single(entities.into_iter().next().unwrap()),
_ => Self::Multi(entities),
}
}
}
enum ActionStateDriverTargetIterator<'a> {
None,
Single(Once<&'a Entity>),
Multi(Iter<'a, Entity>),
}
impl<'a> Iterator for ActionStateDriverTargetIterator<'a> {
type Item = &'a Entity;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::None => None,
Self::Single(iter) => iter.next(),
Self::Multi(iter) => iter.next(),
}
}
}
#[cfg(test)]
mod tests {
use super::ActionStateDriverTarget;
use bevy::prelude::Entity;
#[test]
fn action_state_driver_targets() {
let mut target = ActionStateDriverTarget::from(());
assert_eq!(0, target.len());
target.insert(Entity::from_raw(0));
assert_eq!(1, target.len());
target.insert(Entity::from_raw(1));
assert_eq!(2, target.len());
target.remove(Entity::from_raw(0));
assert_eq!(1, target.len());
target.remove(Entity::from_raw(1));
assert_eq!(0, target.len());
target = target.with(Entity::from_raw(0));
assert_eq!(1, target.len());
target = target.without(Entity::from_raw(0));
assert_eq!(0, target.len());
target.add(
[
Entity::from_raw(0),
Entity::from_raw(1),
Entity::from_raw(2),
]
.iter()
.cloned(),
);
assert_eq!(3, target.len());
let mut sum = 0;
for entity in target.iter() {
sum += entity.index();
}
assert_eq!(3, sum);
}
}