font and cash ui

This commit is contained in:
2025-03-06 16:35:41 +01:00
parent 0e2b575620
commit a2fe9a4a5a
3 changed files with 67 additions and 4 deletions

BIN
assets/font.ttf Normal file

Binary file not shown.

View File

@@ -5,8 +5,34 @@ use bevy::prelude::*;
#[require(Transform)]
pub struct Cash;
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct CashText;
#[derive(Resource, Reflect, Default)]
pub struct CashResource {
pub cash: i32,
}
#[derive(Event)]
pub struct CashCollectEvent;
pub fn plugin(app: &mut App) {
app.add_systems(Update, rotate);
app.init_resource::<CashResource>();
app.add_systems(Startup, setup);
app.add_systems(Update, (rotate, update_ui));
app.add_observer(on_collect);
}
fn on_collect(
_trigger: Trigger<CashCollectEvent>,
mut commands: Commands,
mut cash: ResMut<CashResource>,
asset_server: Res<AssetServer>,
) {
commands.spawn(AudioPlayer::new(asset_server.load("sfx/effects/cash.ogg")));
cash.cash += 100;
}
fn rotate(time: Res<Time>, mut query: Query<&mut Transform, With<Cash>>) {
@@ -14,3 +40,37 @@ fn rotate(time: Res<Time>, mut query: Query<&mut Transform, With<Cash>>) {
transform.rotate(Quat::from_rotation_y(time.delta_secs()));
}
}
fn update_ui(
cash: Res<CashResource>,
text: Query<Entity, With<CashText>>,
mut writer: TextUiWriter,
) {
if cash.is_changed() {
let Some(text) = text.iter().next() else {
return;
};
*writer.text(text, 0) = cash.cash.to_string();
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
Text::new("0"),
CashText,
TextFont {
font: asset_server.load("font.ttf"),
font_size: 34.0,
..default()
},
TextColor(Color::Srgba(Srgba::rgb(0., 1., 0.))),
TextLayout::new_with_justify(JustifyText::Center),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(40.0),
left: Val::Px(100.0),
..default()
},
));
}

View File

@@ -1,4 +1,8 @@
use crate::{camera::GameCameraRig, cash::Cash, tb_entities::SpawnPoint};
use crate::{
camera::GameCameraRig,
cash::{Cash, CashCollectEvent},
tb_entities::SpawnPoint,
};
use avian3d::prelude::*;
use bevy::{
input::mouse::MouseMotion,
@@ -170,7 +174,6 @@ fn collect_cash(
mut collision_event_reader: EventReader<CollisionStarted>,
query_player: Query<&Player>,
query_cash: Query<&Cash>,
asset_server: Res<AssetServer>,
) {
for CollisionStarted(e1, e2) in collision_event_reader.read() {
let collect = if query_player.contains(*e1) && query_cash.contains(*e2) {
@@ -182,7 +185,7 @@ fn collect_cash(
};
if let Some(cash) = collect {
commands.spawn(AudioPlayer::new(asset_server.load("sfx/effects/cash.ogg")));
commands.trigger(CashCollectEvent);
commands.entity(cash).despawn();
}
}