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

bevy_health_bar3d

Bevy实现的作为海报着色器的生命条

17个稳定版本

3.3.0 2024年7月7日
3.2.1 2024年5月2日
3.2.0 2024年3月21日
3.1.0 2023年11月16日
1.3.0 2023年5月10日

#244 in 游戏开发

Download history 239/week @ 2024-04-29 69/week @ 2024-05-06 50/week @ 2024-05-13 20/week @ 2024-05-20 15/week @ 2024-06-03 40/week @ 2024-06-10 9/week @ 2024-06-17 22/week @ 2024-06-24 92/week @ 2024-07-01 51/week @ 2024-07-08 6/week @ 2024-07-15 15/week @ 2024-07-22 73/week @ 2024-07-29 35/week @ 2024-08-05 19/week @ 2024-08-12

142 每月下载量

MIT/Apache

42KB
426

Checks Release

bevy_health_bar3d

Bevy 3D的生命条插件。尽管其名称如此,此插件具有通用性。它可以用于渲染任何可以表示为百分比的值的条。可以自由调整大小,支持水平和垂直方向,自定义前景和背景颜色,以及可选的边框,可配置厚度和颜色。与分屏或分层相机无缝配合。

Bevy兼容性

Bevy版本 包版本
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

使用方法

为您要跟踪的组件实现Percentage特质,并在实例化插件时传递组件的类型。

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();
}

创建一个网格、要跟踪的组件和一个BarSettings组件来配置条的外观和感觉。

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()
        },
    ));
}

注意BarBundle的泛型参数。它用于将配置与跟踪的组件关联起来,并且对于每个实体支持多个条是必要的。

就这样!组件值的更新将自动传播到条上。

示例

示例可以在这里找到。要运行Web上的示例,首先安装cargo-make (cargo install cargo-make),然后调用cargo make web <示例名称,例如cargo make web dinosaurs

依赖项

~37–74MB
~1.5M SLoC