diff --git a/src/common.rs b/src/common.rs index 663d015..1bb01dc 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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, diff --git a/src/health_bar.rs b/src/health_bar.rs index bdf5a50..f1c5de6 100644 --- a/src/health_bar.rs +++ b/src/health_bar.rs @@ -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, @@ -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 { diff --git a/src/main.rs b/src/main.rs index 96cd8be..f5f55e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -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), )); } diff --git a/src/minions.rs b/src/minions.rs index 412fe98..dfa7c71 100644 --- a/src/minions.rs +++ b/src/minions.rs @@ -1,6 +1,6 @@ use crate::{ common::*, - health_bar::{Health, HealthBar}, + health_bar::{Health, HealthBarBundle}, }; use bevy::{ prelude::*, @@ -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(); @@ -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);