Skip to content

Commit 796f352

Browse files
committed
💎 use bundle for healthBar
1 parent 193da4a commit 796f352

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

src/common.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use bevy::prelude::*;
33
pub const GAME_MAX_WIDTH: f32 = 2000.;
44
pub const GAME_MAX_HEIGHT: f32 = 2000.;
55

6-
pub const DEFAULT_HEALTH_COLOR: Color = Color::rgb(0.2, 0.8, 0.2);
7-
86
#[derive(Component)]
97
pub struct Target {
108
pub position: Vec3,

src/health_bar.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use bevy::prelude::*;
22

3+
pub const DEFAULT_HEALTH_COLOR: Color = Color::rgb(0.2, 0.8, 0.2);
4+
35
#[derive(Component)]
46
pub struct Health {
57
pub value: f32,
@@ -19,6 +21,32 @@ pub struct HealthBar {
1921
pub size: Vec2,
2022
}
2123

24+
#[derive(Bundle)]
25+
pub struct HealthBarBundle {
26+
pub sprite: SpriteBundle,
27+
pub health_bar: HealthBar,
28+
}
29+
30+
impl HealthBarBundle {
31+
pub fn new(entity: Entity, translation: Vec3, size: Vec2) -> Self {
32+
Self {
33+
sprite: SpriteBundle {
34+
sprite: Sprite {
35+
color: DEFAULT_HEALTH_COLOR,
36+
custom_size: Some(size),
37+
..default()
38+
},
39+
..default()
40+
},
41+
health_bar: HealthBar {
42+
entity,
43+
translation,
44+
size,
45+
},
46+
}
47+
}
48+
}
49+
2250
pub struct HealthBarPlugin;
2351

2452
impl Plugin for HealthBarPlugin {

src/main.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bevy::{
1313
};
1414
use bevy_rapier2d::{prelude::*, render::RapierDebugRenderPlugin};
1515
use common::*;
16-
use health_bar::{Health, HealthBar, HealthBarPlugin};
16+
use health_bar::{Health, HealthBarBundle, HealthBarPlugin};
1717
use minions::MinionsPlugin;
1818
use racks::RacksPlugin;
1919

@@ -110,21 +110,10 @@ fn setup(mut commands: Commands) {
110110
})
111111
.id();
112112

113-
commands.spawn((
114-
// TODO: do a health bundle and move it to heath_bar crate
115-
SpriteBundle {
116-
sprite: Sprite {
117-
color: DEFAULT_HEALTH_COLOR,
118-
custom_size: Some(Vec2::new(50.0, 5.0)),
119-
..default()
120-
},
121-
..default()
122-
},
123-
HealthBar {
124-
entity,
125-
translation: Vec3::new(0.0, 40.0, 0.1),
126-
size: Vec2::new(50.0, 5.0), // TODO: once a bundle make sure this initialise the sprite
127-
},
113+
commands.spawn(HealthBarBundle::new(
114+
entity,
115+
Vec3::new(0.0, 40.0, 0.1),
116+
Vec2::new(50.0, 5.0),
128117
));
129118
}
130119

src/minions.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
common::*,
3-
health_bar::{Health, HealthBar},
3+
health_bar::{Health, HealthBarBundle},
44
};
55
use bevy::{
66
prelude::*,
@@ -27,7 +27,6 @@ impl Plugin for MinionsPlugin {
2727
}
2828
}
2929

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

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

67-
commands.spawn((
68-
// TODO: do a health bundle and move it to heath_bar crate
69-
SpriteBundle {
70-
sprite: Sprite {
71-
color: DEFAULT_HEALTH_COLOR,
72-
custom_size: Some(Vec2::new(10.0, 5.0)),
73-
..default()
74-
},
75-
..default()
76-
},
77-
HealthBar {
78-
entity,
79-
translation: Vec3::new(0.0, 15.0, 0.1),
80-
size: Vec2::new(10.0, 5.0), // TODO: once a bundle make sure this initialise the sprite
81-
},
66+
commands.spawn(HealthBarBundle::new(
67+
entity,
68+
Vec3::new(0.0, 15.0, 0.1),
69+
Vec2::new(10.0, 5.0),
8270
));
8371

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

0 commit comments

Comments
 (0)