fix cash ui

This commit is contained in:
2025-12-21 17:52:24 -05:00
parent 89e6102b06
commit 375b8a5b46
3 changed files with 51 additions and 43 deletions

View File

@@ -0,0 +1,47 @@
use crate::{
GameState, HEDZ_GREEN, cash::CashInventory, loading_assets::UIAssets, player::LocalPlayer,
};
use bevy::prelude::*;
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct CashText;
pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), setup);
app.add_systems(Update, update_ui.run_if(in_state(GameState::Playing)));
}
fn update_ui(
cash: Single<&CashInventory, (Changed<CashInventory>, With<LocalPlayer>)>,
text: Query<Entity, With<CashText>>,
mut writer: TextUiWriter,
) {
let Some(text) = text.iter().next() else {
return;
};
*writer.text(text, 0) = cash.cash.to_string();
}
fn setup(mut commands: Commands, assets: Res<UIAssets>) {
commands.spawn((
Name::new("cash-ui"),
Text::new("0"),
TextShadow::default(),
CashText,
TextFont {
font: assets.font.clone(),
font_size: 34.0,
..default()
},
TextColor(HEDZ_GREEN.into()),
TextLayout::new_with_justify(Justify::Center),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(40.0),
left: Val::Px(100.0),
..default()
},
));
}