From 406b307e5f298dc1182d51547abec248d82f8277 Mon Sep 17 00:00:00 2001 From: Trevor Settles Date: Sat, 28 Oct 2023 16:46:45 -0600 Subject: [PATCH] feat: showed stat of selected unit --- game/src/in_game.rs | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/game/src/in_game.rs b/game/src/in_game.rs index 90f3c72..5562947 100644 --- a/game/src/in_game.rs +++ b/game/src/in_game.rs @@ -66,6 +66,10 @@ fn setup( }, PickableBundle::default(), RaycastPickTarget::default(), + Health { + max: 5.0, + current: (i + 2) as f32, + }, )); } } @@ -90,19 +94,40 @@ fn draw_hud(mut commands: Commands, asset_server: Res) { should_emit_events: false, }, ), - |_| {}, + |p| { + texti("", (), (), HealthVis, p); + }, ); } #[derive(Component)] struct Hud; -fn apply_seelected(q: Query<(Entity, &PickingInteraction)>) { - for (ent, p) in &q { - match p { - PickingInteraction::Pressed => println!("{ent:?}"), - PickingInteraction::Hovered => {} - PickingInteraction::None => {} +#[derive(Component)] +struct HealthVis; + +#[derive(Component)] +struct Health { + max: f32, + current: f32, +} + +fn apply_seelected( + q: Query<(&Health, &PickingInteraction), Changed>, + mut texts: Query<&mut Text, With>, +) { + for (health, interaction) in &q { + for mut text in &mut texts { + match interaction { + PickingInteraction::Pressed => { + text.sections = vec![TextSection { + value: format!("{} of {}", health.current, health.max), + style: TextStyle::default(), + }] + } + PickingInteraction::Hovered => {} + PickingInteraction::None => text.sections = vec![], + } } } }