Skip to content

Commit

Permalink
💎 use bundle for healthBar
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Nov 1, 2023
1 parent 193da4a commit 796f352
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use bevy::prelude::*;
pub const GAME_MAX_WIDTH: f32 = 2000.;
pub const GAME_MAX_HEIGHT: f32 = 2000.;

pub const DEFAULT_HEALTH_COLOR: Color = Color::rgb(0.2, 0.8, 0.2);

#[derive(Component)]
pub struct Target {
pub position: Vec3,
Expand Down
28 changes: 28 additions & 0 deletions src/health_bar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;

pub const DEFAULT_HEALTH_COLOR: Color = Color::rgb(0.2, 0.8, 0.2);

#[derive(Component)]
pub struct Health {
pub value: f32,
Expand All @@ -19,6 +21,32 @@ pub struct HealthBar {
pub size: Vec2,
}

#[derive(Bundle)]
pub struct HealthBarBundle {
pub sprite: SpriteBundle,
pub health_bar: HealthBar,
}

impl HealthBarBundle {
pub fn new(entity: Entity, translation: Vec3, size: Vec2) -> Self {
Self {
sprite: SpriteBundle {
sprite: Sprite {
color: DEFAULT_HEALTH_COLOR,
custom_size: Some(size),
..default()
},
..default()
},
health_bar: HealthBar {
entity,
translation,
size,
},
}
}
}

pub struct HealthBarPlugin;

impl Plugin for HealthBarPlugin {
Expand Down
21 changes: 5 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy::{
};
use bevy_rapier2d::{prelude::*, render::RapierDebugRenderPlugin};
use common::*;
use health_bar::{Health, HealthBar, HealthBarPlugin};
use health_bar::{Health, HealthBarBundle, HealthBarPlugin};
use minions::MinionsPlugin;
use racks::RacksPlugin;

Expand Down Expand Up @@ -110,21 +110,10 @@ fn setup(mut commands: Commands) {
})
.id();

commands.spawn((
// TODO: do a health bundle and move it to heath_bar crate
SpriteBundle {
sprite: Sprite {
color: DEFAULT_HEALTH_COLOR,
custom_size: Some(Vec2::new(50.0, 5.0)),
..default()
},
..default()
},
HealthBar {
entity,
translation: Vec3::new(0.0, 40.0, 0.1),
size: Vec2::new(50.0, 5.0), // TODO: once a bundle make sure this initialise the sprite
},
commands.spawn(HealthBarBundle::new(
entity,
Vec3::new(0.0, 40.0, 0.1),
Vec2::new(50.0, 5.0),
));
}

Expand Down
22 changes: 5 additions & 17 deletions src/minions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
common::*,
health_bar::{Health, HealthBar},
health_bar::{Health, HealthBarBundle},
};
use bevy::{
prelude::*,
Expand All @@ -27,7 +27,6 @@ impl Plugin for MinionsPlugin {
}
}

// TODO: Use global tranform?
pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team) {
let mut rng = rand::thread_rng();

Expand Down Expand Up @@ -64,21 +63,10 @@ pub fn spawn_minion(commands: &mut Commands, transform: &Transform, team: Team)
))
.id();

commands.spawn((
// TODO: do a health bundle and move it to heath_bar crate
SpriteBundle {
sprite: Sprite {
color: DEFAULT_HEALTH_COLOR,
custom_size: Some(Vec2::new(10.0, 5.0)),
..default()
},
..default()
},
HealthBar {
entity,
translation: Vec3::new(0.0, 15.0, 0.1),
size: Vec2::new(10.0, 5.0), // TODO: once a bundle make sure this initialise the sprite
},
commands.spawn(HealthBarBundle::new(
entity,
Vec3::new(0.0, 15.0, 0.1),
Vec2::new(10.0, 5.0),
));

trace!("Spawning Minion: {:?}", entity);
Expand Down

0 comments on commit 796f352

Please sign in to comment.