#bevy #gamedev #healthbar #background-color #billboard

bevy_health_bar3d

Health bar for bevy implemented as a billboard shader

17 stable releases

3.3.0 Jul 7, 2024
3.2.1 May 2, 2024
3.2.0 Mar 21, 2024
3.1.0 Nov 16, 2023
1.3.0 May 10, 2023

#240 in Game dev

Download history 20/week @ 2024-07-24 74/week @ 2024-07-31 30/week @ 2024-08-07 22/week @ 2024-08-14 12/week @ 2024-08-21 2/week @ 2024-08-28 12/week @ 2024-09-04 24/week @ 2024-09-11 44/week @ 2024-09-18 66/week @ 2024-09-25 20/week @ 2024-10-02 22/week @ 2024-10-09 15/week @ 2024-10-16 6/week @ 2024-10-23 18/week @ 2024-10-30 18/week @ 2024-11-06

61 downloads per month

MIT/Apache

42KB
426 lines

Checks Release

bevy_health_bar3d

Health Bar plugin for Bevy 3D. Despite its name, this plugin is universally applicable. It can be used to render a bar for any value that can be represented as percentage. Can be freely sized, supports horizontal or vertical orientation, custom fore- and background colors, and an optional border with configurable thickness and color. Works with split-screens or layered cameras out of the box.

Bevy Compatibility

Bevy Version Crate Version
0.14 >= 3.3.0
0.13 3.2.0
0.12 2.0.0
0.11 1.2.0
0.10 1.1.0
0.9 1.0.0

Usage

Implement the Percentage trait for the component you want to track and pass the type of your component to the plugin on instantiation:

use bevy_health_bar3d::prelude::{HealthBarPlugin, Percentage};

#[derive(Component, Reflect)]
struct Health {
    max: f32,
    current: f32,
}

impl Percentage for Health {
    fn value(&self) -> f32 {
        self.current / self.max
    }
}

fn main() {
    App::new()
        // add multiple times to track further component types
        .add_plugins((HealthBarPlugin::<Health>::default(), HealthBarPlugin::<Mana>::default()))
        // set a different color for the Mana bar
        .insert_resource(ColorScheme::<Mana>::new().foreground_color(ForegroundColor::Static(Color::BLUE)))
        .run();
}

Spawn a mesh, the component to be tracked, and a BarSettings component to configure the look & feel of your bar.

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    commands.spawn((
        PbrBundle {
            mesh: meshes.add(Sphere { radius }),
            // ...
        },
        Health {
            max: 10.,
            current: 2.,
        },
        BarSettings::<Health> {
            width: 5.,
            offset: 2.,
            orientation: BarOrientation::Vertical, // default is horizontal
            ..default()
        },
    ));
}

Note the generic parameter of BarBundle. It is used to associate the configuration with the component it is tracking and necessary to support multiple bars per entity.

That's it! Updates to the values of your component will be automatically propagated through to the bar.

Examples

Examples can be found here. To run an example for web, first install cargo-make (cargo install cargo-make) and then call cargo make web <name-of-the-example, such as cargo make web dinosaurs

Dependencies

~38–75MB
~1.5M SLoC