48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
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()
|
|
},
|
|
));
|
|
}
|