add some player vars and display them in gauges

This commit is contained in:
yuni 2024-03-17 21:29:27 +01:00
parent bb0e08fc35
commit c80fec0dd4
3 changed files with 74 additions and 5 deletions

View file

@ -1,4 +1,4 @@
use crate::settings; use crate::{settings, player};
use bevy::prelude::*; use bevy::prelude::*;
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
@ -14,7 +14,7 @@ impl Plugin for OutFlyHudPlugin {
const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf"; const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf";
#[derive(Component)] #[derive(Component)]
struct FpsText; struct GaugesText;
#[derive(Resource)] #[derive(Resource)]
struct FPSUpdateTimer(Timer); struct FPSUpdateTimer(Timer);
@ -48,19 +48,56 @@ fn setup(
..default() ..default()
} }
), ),
TextSection::new(
"\n OXYGEN ",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
},
),
TextSection::new(
"",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
}
),
TextSection::new(
"\n Adren水平 ",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
},
),
TextSection::new(
"",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
}
),
]); ]);
bundle_fps.visibility = visibility; bundle_fps.visibility = visibility;
commands.spawn(( commands.spawn((
bundle_fps, bundle_fps,
FpsText, GaugesText,
)); ));
} }
fn update( fn update(
diagnostics: Res<DiagnosticsStore>, diagnostics: Res<DiagnosticsStore>,
time:Res<Time>, time:Res<Time>,
playervars:Res<player::PlayerVars>,
mut timer: ResMut<FPSUpdateTimer>, mut timer: ResMut<FPSUpdateTimer>,
mut query: Query<&mut Text, With<FpsText>>, mut query: Query<&mut Text, With<GaugesText>>,
) { ) {
if timer.0.tick(time.delta()).just_finished() { if timer.0.tick(time.delta()).just_finished() {
for mut text in &mut query { for mut text in &mut query {
@ -70,6 +107,10 @@ fn update(
text.sections[1].value = format!("{value:.0}"); text.sections[1].value = format!("{value:.0}");
} }
} }
let oxygen = playervars.oxygen * 100.0;
text.sections[3].value = format!("{oxygen:.1}%");
let adrenaline = playervars.adrenaline * 430.0;
text.sections[5].value = format!("{adrenaline:.1}ppm");
} }
} }
} }
@ -77,7 +118,7 @@ fn update(
fn handle_input( fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
settings: Res<settings::Settings>, settings: Res<settings::Settings>,
mut query: Query<&mut Visibility, With<FpsText>>, mut query: Query<&mut Visibility, With<GaugesText>>,
) { ) {
if keyboard_input.just_pressed(settings.key_togglehud) { if keyboard_input.just_pressed(settings.key_togglehud) {
for mut vis in &mut query { for mut vis in &mut query {

View file

@ -23,6 +23,9 @@ fn main() {
audio::toggle_bgm, audio::toggle_bgm,
world::asset_loaded.after(world::load_cubemap_asset), world::asset_loaded.after(world::load_cubemap_asset),
)) ))
.add_systems(FixedUpdate, (
player::drain_resources,
))
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.add_plugins(( .add_plugins((
camera::CameraControllerPlugin, camera::CameraControllerPlugin,
@ -33,6 +36,7 @@ fn main() {
LogDiagnosticsPlugin::default(), LogDiagnosticsPlugin::default(),
)) ))
.insert_resource(settings::Settings::default()) .insert_resource(settings::Settings::default())
.insert_resource(player::PlayerVars::default())
.run(); .run();
} }

View file

@ -18,6 +18,23 @@ impl Default for Player {
} }
} }
#[derive(Resource)]
pub struct PlayerVars {
pub oxygen: f32,
pub adrenaline: f32,
pub adrenaline_jolt: f32,
}
impl Default for PlayerVars {
fn default() -> Self {
Self {
oxygen: 1.0,
adrenaline: 0.1,
adrenaline_jolt: 0.1,
}
}
}
pub fn setup(mut commands: Commands) { pub fn setup(mut commands: Commands) {
commands.spawn(Player::default()); commands.spawn(Player::default());
} }
@ -39,3 +56,10 @@ pub fn handle_input(
} }
} }
} }
pub fn drain_resources(
mut playervars:ResMut<PlayerVars>,
) {
playervars.adrenaline = (playervars.adrenaline - 0.0001).clamp(0.0, 1.0);
playervars.oxygen = (playervars.oxygen - 0.000001).clamp(0.0, 1.0);
}