Skip to content

Commit 48fb4aa

Browse files
MclilzeeRCoder01Jondolf
authored
Update breakout to use Required Components (bevyengine#16577)
# Objective This PR update breakout to use the new 0.15 Required Component feature instead of the Bundle. Add more information in the comment about where to find more info about Required Components. ## Solution Replace `#[derive(Bundle)]` with a new Wall component and `#[require()]` Macro to include the other components. ## Testing Tested with `cargo test` as well tested the game manually with `cargo run --example breakout` It looks to me that it works like it used to before the changes. Tested on Arch Linux, Wayland --------- Co-authored-by: Arnav Mummineni <[email protected]> Co-authored-by: Joona Aalto <[email protected]>
1 parent f5de3f0 commit 48fb4aa

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

examples/games/breakout.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ struct Ball;
8989
#[derive(Component, Deref, DerefMut)]
9090
struct Velocity(Vec2);
9191

92-
#[derive(Component)]
93-
struct Collider;
94-
9592
#[derive(Event, Default)]
9693
struct CollisionEvent;
9794

@@ -101,15 +98,14 @@ struct Brick;
10198
#[derive(Resource, Deref)]
10299
struct CollisionSound(Handle<AudioSource>);
103100

104-
// This bundle is a collection of the components that define a "wall" in our game
105-
#[derive(Bundle)]
106-
struct WallBundle {
107-
// You can nest bundles inside of other bundles like this
108-
// Allowing you to compose their functionality
109-
sprite: Sprite,
110-
transform: Transform,
111-
collider: Collider,
112-
}
101+
// Default must be implemented to define this as a required component for the Wall component below
102+
#[derive(Component, Default)]
103+
struct Collider;
104+
105+
// This is a collection of the components that define a "Wall" in our game
106+
#[derive(Component)]
107+
#[require(Sprite, Transform, Collider)]
108+
struct Wall;
113109

114110
/// Which side of the arena is this wall located on?
115111
enum WallLocation {
@@ -149,13 +145,15 @@ impl WallLocation {
149145
}
150146
}
151147

152-
impl WallBundle {
148+
impl Wall {
153149
// This "builder method" allows us to reuse logic across our wall entities,
154150
// making our code easier to read and less prone to bugs when we change the logic
155-
fn new(location: WallLocation) -> WallBundle {
156-
WallBundle {
157-
sprite: Sprite::from_color(WALL_COLOR, Vec2::ONE),
158-
transform: Transform {
151+
// Notice the use of Sprite and Transform alongside Wall, overwriting the default values defined for the required components
152+
fn new(location: WallLocation) -> (Wall, Sprite, Transform) {
153+
(
154+
Wall,
155+
Sprite::from_color(WALL_COLOR, Vec2::ONE),
156+
Transform {
159157
// We need to convert our Vec2 into a Vec3, by giving it a z-coordinate
160158
// This is used to determine the order of our sprites
161159
translation: location.position().extend(0.0),
@@ -165,8 +163,7 @@ impl WallBundle {
165163
scale: location.size().extend(1.0),
166164
..default()
167165
},
168-
collider: Collider,
169-
}
166+
)
170167
}
171168
}
172169

@@ -242,10 +239,10 @@ fn setup(
242239
));
243240

244241
// Walls
245-
commands.spawn(WallBundle::new(WallLocation::Left));
246-
commands.spawn(WallBundle::new(WallLocation::Right));
247-
commands.spawn(WallBundle::new(WallLocation::Bottom));
248-
commands.spawn(WallBundle::new(WallLocation::Top));
242+
commands.spawn(Wall::new(WallLocation::Left));
243+
commands.spawn(Wall::new(WallLocation::Right));
244+
commands.spawn(Wall::new(WallLocation::Bottom));
245+
commands.spawn(Wall::new(WallLocation::Top));
249246

250247
// Bricks
251248
let total_width_of_bricks = (RIGHT_WALL - LEFT_WALL) - 2. * GAP_BETWEEN_BRICKS_AND_SIDES;

0 commit comments

Comments
 (0)