|
1 | 1 | use bevy::prelude::*; |
2 | 2 |
|
3 | | -use crate::gameplay::hud::tome::{TomeTab, UITomeLeftPageRoot, widgets}; |
| 3 | +use crate::gameplay::{ |
| 4 | + hud::tome::{TomeTab, UITomeLeftPageRoot, widgets}, |
| 5 | + recipe::assets::RecipeDef, |
| 6 | +}; |
4 | 7 |
|
5 | 8 | pub(super) fn plugin(app: &mut App) { |
6 | 9 | app.add_systems(OnEnter(TomeTab::Recipes), spawn_recipe_list); |
| 10 | + |
| 11 | + app.add_systems( |
| 12 | + Update, |
| 13 | + refresh_recipe_plates.run_if(in_state(TomeTab::Recipes)), |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Component, Reflect, Debug)] |
| 18 | +#[reflect(Component)] |
| 19 | +pub struct RecipePlate(pub AssetId<RecipeDef>); |
| 20 | + |
| 21 | +fn recipe_plate(recipe: AssetId<RecipeDef>) -> impl Bundle { |
| 22 | + (RecipePlate(recipe), Text::default()) |
| 23 | +} |
| 24 | + |
| 25 | +fn spawn_recipe_list( |
| 26 | + mut commands: Commands, |
| 27 | + left_page: Single<Entity, With<UITomeLeftPageRoot>>, |
| 28 | + recipe_defs: Res<Assets<RecipeDef>>, |
| 29 | +) { |
| 30 | + let recipe_list = commands |
| 31 | + .spawn(( |
| 32 | + widgets::list_page(), |
| 33 | + ChildOf(*left_page), |
| 34 | + DespawnOnExit(TomeTab::Recipes), |
| 35 | + )) |
| 36 | + .id(); |
| 37 | + |
| 38 | + for (asset_id, _) in recipe_defs.iter() { |
| 39 | + commands.spawn((recipe_plate(asset_id), ChildOf(recipe_list))); |
| 40 | + } |
7 | 41 | } |
8 | 42 |
|
9 | | -fn spawn_recipe_list(mut commands: Commands, left_page: Single<Entity, With<UITomeLeftPageRoot>>) { |
10 | | - commands.spawn(( |
11 | | - widgets::list_page(), |
12 | | - ChildOf(*left_page), |
13 | | - DespawnOnExit(TomeTab::Recipes), |
14 | | - children![Text::new("Recipes")], |
15 | | - )); |
| 43 | +fn refresh_recipe_plates( |
| 44 | + plates: Query<(&RecipePlate, &mut Text)>, |
| 45 | + recipe_defs: Res<Assets<RecipeDef>>, |
| 46 | +) { |
| 47 | + for (plate, mut text) in plates { |
| 48 | + if let Some(recipe) = recipe_defs.get(plate.0) { |
| 49 | + text.0 = recipe.name.clone(); |
| 50 | + } |
| 51 | + } |
16 | 52 | } |
0 commit comments