Replies: 1 comment
-
Your architecture is very close to what I would come up with, the general idea is sound. However, The key piece you are missing is Think of Regarding the text, right now, dynamic text in bevy is kind of super awful to work with, and you already are updating the text the best way it can be done… With this, you could tweak slightly your algo: Now, instead of iterating over all tiles, you only iterate through the tiles mentioned in the event. In short, the algorithm would now look as follow: struct RegionDiscoveredEvent {
entity: Entity,
}
fn on_discovered(
mut ev: EventReader<RegionDiscoveredEvent>,
mut tiles_query: Query<(&mut BackgroundColor, &Children, &TileMarker)>,
mut text: Query<&mut Text>, // Used to `get` the text I have as children of my `ButtonBundle`
) {
for ev in ev.read() {
let Ok((mut bg, children, tile)) = tiles_query.get_mut(ev.entity) else {
continue;
};
let Some(mut text) = children.iter().find_map(|e| text.get_mut(e).ok()) else {
continue;
};
*text = …;
*bg = …;
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been teaching myself Bevy by writing a small Minesweeper. I have the basic features working, but I feel that I'm doing something wrong.
If we limit to the grid (aka the Model), I have
struct Grid
that derivesResource
, containing aVec<Tile>
;struct Tile
that contains the precomputed number of neighbouring bombs and a few booleans to determine whether there is a bomb, the tile has been visited, etc.;ButtonBundle
withTextBundle
as children (to be replaced with images once I have the time to do so);struct TileMarker
that derivesComponent
, which I spawn along with theButtonBundle
to be able to determine the index inGrid
of the tile I've just clicked.This works. However, for instance, when I'm discovering an entire region after clicking on a tile with 0 neighbouring bombs, the algorithm is a bit odd:
on_interact
System, compute the set of tiles that need to become visible;What am I doing wrong? Should I somehow get rid of
Grid
as a data structure and make it one Entity with plenty of Component tiles? If so, how can I organize the logic so that I can easily access the neighbours of a tile?I feel that the question is more generic than Minesweeper so perhaps I'm simply missing a source "how to think in ECS" :)
Beta Was this translation helpful? Give feedback.
All reactions