|
1 | 1 | use bevy::prelude::*; |
2 | 2 |
|
3 | | -use crate::gameplay::hud::tome::{TomeTab, UITomeLeftPageRoot, widgets}; |
| 3 | +use crate::gameplay::{ |
| 4 | + hud::tome::{TomeTab, UIEntryList, UITomeLeftPageRoot, widgets}, |
| 5 | + people::Person, |
| 6 | + player::Player, |
| 7 | + storage::{Storage, StoredBy}, |
| 8 | +}; |
4 | 9 |
|
5 | 10 | pub(super) fn plugin(app: &mut App) { |
6 | 11 | app.add_systems(OnEnter(TomeTab::People), spawn_people_grid); |
| 12 | + |
| 13 | + app.add_systems( |
| 14 | + Update, |
| 15 | + (backfill_people_grid, refresh_person_badges).run_if(in_state(TomeTab::People)), |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Component, Reflect, Debug)] |
| 20 | +#[reflect(Component)] |
| 21 | +pub struct PersonBadge(pub Entity); |
| 22 | + |
| 23 | +#[derive(Component, Reflect, Debug)] |
| 24 | +#[reflect(Component)] |
| 25 | +pub struct PersonName; |
| 26 | + |
| 27 | +fn person_badge(person: Entity) -> impl Bundle { |
| 28 | + ( |
| 29 | + PersonBadge(person), |
| 30 | + Node::default(), |
| 31 | + children![(Text::default(), PersonName,)], |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +fn refresh_person_badges( |
| 36 | + badges: Query<(Entity, &PersonBadge)>, |
| 37 | + children: Query<&Children>, |
| 38 | + people: Query<&Name, With<Person>>, |
| 39 | + mut components: ParamSet<(Query<&mut Text, With<PersonName>>,)>, |
| 40 | +) { |
| 41 | + for (badge, PersonBadge(person)) in badges { |
| 42 | + let Ok(name) = people.get(*person) else { |
| 43 | + continue; |
| 44 | + }; |
| 45 | + |
| 46 | + for child in children.iter_descendants(badge) { |
| 47 | + if let Ok(mut text) = components.p0().get_mut(child) { |
| 48 | + text.0 = name.to_string(); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn spawn_people_grid( |
| 55 | + mut commands: Commands, |
| 56 | + left_page: Single<Entity, With<UITomeLeftPageRoot>>, |
| 57 | + player: Single<Entity, With<Player>>, |
| 58 | + storage: Query<&Storage>, |
| 59 | + people: Query<Entity, With<Person>>, |
| 60 | +) { |
| 61 | + let people_grid = commands |
| 62 | + .spawn(( |
| 63 | + widgets::list_page(), |
| 64 | + ChildOf(*left_page), |
| 65 | + DespawnOnExit(TomeTab::People), |
| 66 | + )) |
| 67 | + .id(); |
| 68 | + |
| 69 | + for person in storage |
| 70 | + .iter_descendants(*player) |
| 71 | + .filter(|s| people.contains(*s)) |
| 72 | + { |
| 73 | + commands.spawn((person_badge(person), ChildOf(people_grid))); |
| 74 | + } |
7 | 75 | } |
8 | 76 |
|
9 | | -fn spawn_people_grid(mut commands: Commands, left_page: Single<Entity, With<UITomeLeftPageRoot>>) { |
10 | | - commands.spawn(( |
11 | | - widgets::list_page(), |
12 | | - ChildOf(*left_page), |
13 | | - DespawnOnExit(TomeTab::People), |
14 | | - children![Text::new("People")], |
15 | | - )); |
| 77 | +fn backfill_people_grid( |
| 78 | + mut commands: Commands, |
| 79 | + people_grid: Single<Entity, With<UIEntryList>>, |
| 80 | + player: Single<Entity, With<Player>>, |
| 81 | + new_people: Query<(Entity, &StoredBy), (With<Person>, Added<StoredBy>)>, |
| 82 | +) { |
| 83 | + for (person, stored_by) in new_people { |
| 84 | + if stored_by.0 != *player { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + commands.spawn((person_badge(person), ChildOf(*people_grid))); |
| 89 | + } |
16 | 90 | } |
0 commit comments