Crates.io | bevy_stat_bars |
lib.rs | bevy_stat_bars |
version | 0.3.1 |
source | src |
created_at | 2022-06-09 19:24:00.257947 |
updated_at | 2022-09-28 11:26:09.059783 |
description | plugin for drawing floating stat bars |
homepage | |
repository | https://github.com/ickshonpe/bevy_stat_bars |
max_upload_size | |
id | 602840 |
size | 359,723 |
Bevy crate for drawing floating statbars like health bars above enemy sprites etc.
bevy_inspector_egui
now.Add the dependency to your Cargo.toml file with
[dependencies.bevy_stat_bars]
version = "0.3"
Then register any components you want to observe with a statbar with your Bevy App:
use bevy_stat_bars::*;
App::new()
.add_plugins(DefaultPlugins)
.add_statbar_bar_component_observer::<HitPoints>()
// ..etc, rest of app
.run();
You also need to implement the StatbarObservable
trait on those components:
impl StatbarObservable for HitPoints {
fn get_statbar_value(&self) -> f32 {
self.value / self.max
}
}
And now you can add a Statbar::<HitPoints>
component to an entity to visualize its HitPoints component
commands.entity(enemy_id)
.insert_bundle((
Statbar::<HitPoints> {
empty_color: Color::NAVY,
length: 10.,
thickness: 2.,
displacement: 8. * Vec2::Y,
..Default::default()
},
StatbarBorder::<HitPoints>::all(Color::WHITE, 1.),
));
There are six examples you can look at that cover most of the features and use cases, run them with
cargo run --example minimal_standalone
cargo run --example basic_interactive
cargo run --example observe_resource
cargo run --example demo
cargo run --example stress --release
cargo run --example stress2 --release
The demo
example is the probably the most useful to look at.
The stress2
example uses macros to add hundreds of marker types and can take a few minutes to compile.
Only supports 2D.
When I was writing the examples I made a mistake where instead of
.add_statbar_component_observer::<Stat<Health>>()
I used
.add_statbar_component_observer::<Health>()
which is quite easy to miss. The crate fails silently and just won't render anything in this case, leaving the user with a frustrating bug hunt.
Likewise also when a statbar is set to observe its parent or another Entity that doesn't exist, it will render a statbar that doesn't update.
Statbars are drawn using Sprites with a z depth of 990, and if you translate the camera down more than 10 units they won't draw.
You can change the depth with the StatbarDepth
resource.
So with
commands.insert_resource(StatbarDepth(500.));
all Statbars will now render with a z depth of 500. There currently isn't any way to control the ordering in which the individual statbars are drawn.
Still uses sprites for rendering which isn't ideal but performance seems fine. You can run the stress
example to see what its like under a heavy load. I get about 100fps on my rx580.
add_statbar_component_observer
adds six systems to your Bevy app per component observed. Again not ideal but doesn't seem to be a problem. I get ~100fps with the stress2
example which spawns 100 entities with 200 Statbars each.
Replace the sprite based rendering with a custom renderer. I have some fragment shaders already written, and should be better performance with some nice effects like rounded corners and color gradients.
Pie-o-meters
Labels and numeric indicators
Some sort of, posibly feature gated or debug-only, falure detection that gives an error when you insert unregistered statbars, or when a statbar can't find the component it is meant to be observing.
Derive macro for StatbarObservable.
Auto arrangement/stacking of groups of statbars. I thought this would be more difficult but I dreamt up an easyish way to do it last night.