JoJoJet / bevy-trait-query

adds trait queries to the bevy game engine
Apache License 2.0
65 stars 11 forks source link

feat: Implement `WithoutOne` #58

Open RobWalt opened 1 month ago

RobWalt commented 1 month ago

This PR:

  1. implements WithoutAny, a QueryFilter which is supposed to be the opposite of WithOne
  2. cleans up some parts of WithOne to make it QueryFilter only

Note that 2. is a breaking change since we can't use it in the Data position anymore. However, I think this is fine since there is One which already fills this gap. So this is really a fix since it clears up the separation of concerns of the two structs.


This should also solve https://github.com/JoJoJet/bevy-trait-query/issues/50. My use case is something similar as described there. I want to query for things that have / don't have components of a certain class. Small pseudo code example:

struct Food;

trait Fruit {}
struct Banana;
impl Fruit for Banana {}
struct Apple;
impl Fruit for Apple {}

struct Sweets;
struct Cake;

fn eat_unhealthy(
  mut commands: Commands,
  q_non_fruits: Query<Entity, (With<Food>, WithoutAny<&dyn Fruit>)>
) {
  q_non_fruits.iter().for_each(|food| {
    // only sweets and cakes without fruits
    commands.eat(food); 
  });
}  

(adding all the missing things to make this actually run is left as an exercise to the reader)