Crates.io | bevy_mod_simplest_healthbar |
lib.rs | bevy_mod_simplest_healthbar |
version | 0.1.0 |
source | src |
created_at | 2023-04-07 15:13:04.825431 |
updated_at | 2023-04-07 15:13:04.825431 |
description | Not really healthbars, but at least it can be used to get some kind of healt indication |
homepage | |
repository | https://github.com/TotalKrill/bevy_mod_simplest_healthbar.git |
max_upload_size | |
id | 832965 |
size | 2,842,884 |
Helps spawn and position small counter "health bars" that take a trait for max and current health, and then just shows them as text like this: "current/max" at the location of the specified camera component, make sure that the camera component specified only exists once, or this will panic
use bevy::prelude::*;
use bevy_mod_simplest_healthbar::{HealthBar, HealthBarPlugin, HealthTrait};
#[derive(Component)]
struct Health {
current: u32,
max: u32,
}
impl HealthTrait for Health {
fn current(&self) -> u32 {
self.current
}
fn max(&self) -> u32 {
self.max
}
}
#[derive(Component)]
struct BarCamera;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// custom
.add_plugin(
// Need to define which camera we are going to be spawning the stuff in relation to, as well as what is the "health" component
HealthBarPlugin::<Health, BarCamera>::new("fonts/FiraMono-Medium.ttf")
// to automatically spawn bars on stuff with Health and a Transform
.automatic_bar_creation(true),
)
.add_startup_system(setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_translation(Vec3::new(40., 40., 40.))
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
BarCamera,
));
for i in 0..5 {
commands.spawn((
PbrBundle {
mesh: meshes.add(shape::Cube::default().into()),
material: materials.add(Color::BLUE.into()),
transform: Transform::from_translation(Vec3::new(0.0, 1.0, (i * 5) as _)),
..default()
},
Health { current: i, max: 6 },
));
}
commands.spawn((
PbrBundle {
mesh: meshes.add(shape::Cube::default().into()),
material: materials.add(Color::BLUE.into()),
transform: Transform::from_translation(Vec3::new(0.0, 1.0, -5.)),
..default()
},
Health { current: 4, max: 6 },
// Create custom size and color and offset for the "bar"
HealthBar {
offset: Vec2::new(-4., 10.),
size: 100.,
color: Color::GREEN,
},
));
}