Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.devcontainer/
/target
Cargo.lock
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ repository = "https://github.com/ickshonpe/bevy_stat_bars"
description = "plugin for drawing floating stat bars"

[dependencies.bevy]
version = "0.8"
version = "0.15"
default-features = false
features = ["render"]
features = ["bevy_render", "bevy_color", "bevy_sprite"]

[dependencies.copyless]
version = "0.1.5"
version = "0.1"

[dev-dependencies.bevy]
version = "0.8"
version = "0.15"
default-features = true

[dev-dependencies.bevy-inspector-egui]
version = "0.13.0"
version = "0.28"

[dev-dependencies.seq-macro]
version = "0.3.1"
version = "0.3"
66 changes: 27 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Bevy crate for drawing floating statbars like health bars above enemy sprites et
* Multiple Statbar components on one entity implemented using PhantomData. This requires
![/media/example.png](/media/example.png)

#

## How to use

Add the dependency to your Cargo.toml file with
Expand All @@ -31,7 +29,7 @@ version = "0.3"

Then register any components you want to observe with a statbar with your Bevy App:

```rust
```rust
use bevy_stat_bars::*;

App::new()
Expand All @@ -43,7 +41,7 @@ App::new()

You also need to implement the ```StatbarObservable``` trait on those components:

```rust
```rust
impl StatbarObservable for HitPoints {
fn get_statbar_value(&self) -> f32 {
self.value / self.max
Expand All @@ -69,59 +67,60 @@ commands.entity(enemy_id)

![/media/example2.png](/media/example2.png)

#

## Examples

There are six examples you can look at that cover most of the features and use cases,
run them with
```

```bash
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.

#

## Notes

* Only supports 2D.

* When I was writing the examples I made a mistake where instead of

```rust
.add_statbar_component_observer::<Stat<Health>>()
```
I used
```rust
.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.
```rust
.add_statbar_component_observer::<Stat<Health>>()
```

I used :

```rust
.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.
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
You can change the depth with the ```StatbarDepth``` resource.

```rust
commands.insert_resource(StatbarDepth(500.));
```
So with:

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.
```rust
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.
#
* `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.

## Future Plans

* 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.
Expand All @@ -130,14 +129,3 @@ You can change the depth with the ```StatbarDepth``` resource.
* 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.











47 changes: 24 additions & 23 deletions examples/basic_interactive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::prelude::*;
use bevy::{
prelude::*,
render::view::{RenderVisibleEntities, VisibleEntities},
};
use bevy_stat_bars::*;

// Spawns a red and navy statbar with a white border in the middle of the window.
Expand All @@ -16,36 +19,35 @@ impl StatbarObservable for ObservedValue {
}

fn spawn_camera(mut commands: Commands) {
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2d);
}

fn spawn_statbar(mut commands: Commands) {
commands
.spawn_bundle((
Statbar::<ObservedValue> {
color: Color::RED,
empty_color: Color::NAVY,
length: 400.,
thickness: 40.,
..Default::default()
},
StatbarBorder::<ObservedValue>::all(Color::WHITE, 4.0),
ObservedValue(0.35),
))
.insert_bundle(SpatialBundle::default());
commands.spawn((
Statbar::<ObservedValue> {
color: Color::from(bevy::color::palettes::css::RED),
empty_color: Color::from(bevy::color::palettes::css::NAVY),
length: 400.,
thickness: 40.,
..Default::default()
},
StatbarBorder::<ObservedValue>::all(Color::WHITE, 4.0),
ObservedValue(0.35),
Visibility::Visible,
));
}

fn adjust_value(
time: Res<Time>,
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
mut observed_values: Query<&mut ObservedValue>,
) {
let delta = time.delta_seconds() * 0.25;
observed_values.for_each_mut(|mut value| {
if input.pressed(KeyCode::Left) {
let delta = time.delta_secs() * 0.25;
observed_values.iter_mut().for_each(|mut value| {
if input.pressed(KeyCode::ArrowLeft) {
value.0 -= delta;
}
if input.pressed(KeyCode::Right) {
if input.pressed(KeyCode::ArrowRight) {
value.0 += delta;
}
value.0 = value.0.clamp(0., 1.0);
Expand All @@ -56,8 +58,7 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_statbar_component_observer::<ObservedValue>()
.add_startup_system(spawn_camera)
.add_startup_system(spawn_statbar)
.add_system(adjust_value)
.add_systems(Startup, (spawn_camera, spawn_statbar))
.add_systems(Update, adjust_value)
.run();
}
Loading