is it possible to create complex queries that directly filter by the value of a field in a component?
For example, say - I have a 2D board game (say chess or something), and I want to figure out if there is a piece on a specific coordinate. My component will look as following:
#[derive(Component)]
struct BoardPosition {
x: usize,
y: usize,
}
And I'm looking to find, say the piece in position (4,5). Then, my query will look as following - iterating through the entire board:
fn some_system(pieces: Query<&BoardPosition>) {
for piece in pieces.iter() {
if piece.x == 4 && piece.y == 5 {
// This is my piece, now I can do whatever I want with it
}
}
}
But clearly, this takes O(n) to find just one simple piece. If I would have stored the board as a double-list (i.e, [[T; N]; N]
), then finding this piece would have taken O(1) by simply doing board[4][5]
, but since bevy owns the components, I can't simply access the entity (or its other components) in this way.
Is there a smarter way to do this, which will allow a fast query and allow me to get (or change) other components of the queried entity?