#progress-bar #ui #gamedev #healthbar #bevy-plugin

bevy_health_bar

Bevy插件,用于轻松创建进度条、生命条等UI元素

1 个不稳定版本

0.1.0 2023年11月26日

#2155游戏开发

MIT/Apache

30KB
73

安装

cargo add bevy_health_bar
bevy_health_bar = "0.1.0"

示例

# Left Click | Right Click
cargo run --example basic

用法

use bevy::prelude::*;
use bevy_health_bar::{ProgressBar, ProgressBarBundle, ProgressBarPlugin};

fn main() {
    let mut app = App::new();
    // Add Plugin
    app.add_plugins((DefaultPlugins, ProgressBarPlugin));

    // Spawn A Progress Bar
    app.add_systems(Startup, spawn_health_bar);

    // Control Progress Bar
    app.add_systems(Update, update_health_bar);
    app.run();
}

fn spawn_health_bar(mut commands: Commands, asset_server: Res<AssetServer>) {
    // Need a camera to see scene
    let camera_bundle = Camera2dBundle::default();
    commands.spawn(camera_bundle);

    // Sets value (and max value) of health bar
    commands.spawn(ProgressBarBundle::new(
        100.0,
        asset_server.load("health_bar.png"),
    ));
}

fn update_health_bar(
    mut query: Query<&mut ProgressBar>,
    mouse_input: Res<Input<MouseButton>>,
    dt: Res<Time>,
) {
    if mouse_input.pressed(MouseButton::Left) {
        for mut health_bar in query.iter_mut() {
            health_bar.value -= 10.0 * dt.delta_seconds();
        }
    }
}

功能

  • 0.1 水平进度条
  • 0.2 步骤事件流
  • 0.3 垂直进度条
  • 0.4 背景图片
  • x.x 动画进度条

致谢

  • mz_eth 提供示例生命条艺术

依赖

~22MB
~404K SLoC