From fcb13eed312c866abfa2df3d693c552461ace623 Mon Sep 17 00:00:00 2001 From: extrawurst Date: Sun, 21 Dec 2025 12:36:51 -0500 Subject: [PATCH] UiActiveHeads can be a local player resource only --- crates/hedz_reloaded/src/heads/heads_ui.rs | 35 ++++++++++++---------- crates/hedz_reloaded/src/player.rs | 3 +- crates/hedz_reloaded/src/protocol/mod.rs | 3 +- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/crates/hedz_reloaded/src/heads/heads_ui.rs b/crates/hedz_reloaded/src/heads/heads_ui.rs index e4bf22c..870296b 100644 --- a/crates/hedz_reloaded/src/heads/heads_ui.rs +++ b/crates/hedz_reloaded/src/heads/heads_ui.rs @@ -21,9 +21,9 @@ struct HeadImage(pub usize); #[reflect(Component)] struct HeadDamage(pub usize); -#[derive(Component, Default, Reflect, Serialize, Deserialize, PartialEq)] -#[reflect(Component)] -pub struct UiActiveHeads { +#[derive(Resource, Default, Reflect, Serialize, Deserialize, PartialEq)] +#[reflect(Resource)] +struct UiActiveHeads { heads: [Option; 5], selected_slot: usize, } @@ -32,11 +32,10 @@ pub fn plugin(app: &mut App) { app.register_type::(); app.register_type::(); + app.init_resource::(); + app.add_systems(OnEnter(GameState::Playing), setup); - app.add_systems( - FixedUpdate, - sync.run_if(in_state(GameState::Playing).and(is_server)), - ); + app.add_systems(FixedUpdate, sync.run_if(in_state(GameState::Playing))); #[cfg(feature = "client")] app.add_systems( FixedUpdate, @@ -177,7 +176,7 @@ fn spawn_head_ui( #[cfg(feature = "client")] fn update( - res: Single<&UiActiveHeads>, + res: Res, heads_images: Res, mut head_image: Query<(&HeadImage, &mut Visibility, &mut ImageNode), Without>, mut head_selector: Query<(&HeadSelector, &mut Visibility), Without>, @@ -201,10 +200,14 @@ fn update( #[cfg(feature = "client")] fn update_ammo( - res: Single<&UiActiveHeads, Changed>, + res: Res, heads: Query<&HeadImage>, mut gradients: Query<(&mut BackgroundGradient, &ChildOf)>, ) { + if !res.is_changed() { + return; + } + for (mut gradient, child_of) in gradients.iter_mut() { let Ok(HeadImage(head)) = heads.get(child_of.parent()) else { continue; @@ -230,18 +233,18 @@ fn update_ammo( } #[cfg(feature = "client")] -fn update_health( - res: Single<&UiActiveHeads, Changed>, - mut query: Query<(&mut Node, &HeadDamage)>, -) { - for (mut node, HeadDamage(head)) in query.iter_mut() { - node.height = Val::Percent(res.heads[*head].map(|head| head.damage()).unwrap_or(0.) * 100.); +fn update_health(res: Res, mut query: Query<(&mut Node, &HeadDamage)>) { + if res.is_changed() { + for (mut node, HeadDamage(head)) in query.iter_mut() { + node.height = + Val::Percent(res.heads[*head].map(|head| head.damage()).unwrap_or(0.) * 100.); + } } } fn sync( active_heads: Single, With>, - mut state: Single<&mut UiActiveHeads>, + mut state: ResMut, time: Res