Skip to content

Commit 1d5388e

Browse files
rparrettBD103alice-i-cecileJondolf
authored
Un-hardcode positions and colors in 2d_shapes example (#11867)
# Objective We recently got some neat new 2d shapes and the shapes are no longer centered on the screen. The hardcoded positions and colors are a pain to deal with when a new shape is added. ## Solution Delete a bunch of code and position shapes evenly. Assign colors evenly too. ## Before <img width="1280" alt="Screenshot 2024-02-14 at 3 17 40 PM" src="https://github.com/bevyengine/bevy/assets/200550/cc9fd9a8-4019-4907-a50e-621cb656c20a"> ## After <img width="1280" alt="Screenshot 2024-02-14 at 3 17 24 PM" src="https://github.com/bevyengine/bevy/assets/200550/033a3f91-d3bc-4ec8-af59-42a221f8b8e7"> --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
1 parent dc9b486 commit 1d5388e

1 file changed

Lines changed: 36 additions & 54 deletions

File tree

examples/2d/2d_shapes.rs

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Shows how to render simple primitive shapes with a single color.
22
3-
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
3+
use bevy::{
4+
prelude::*,
5+
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
6+
};
47

58
fn main() {
69
App::new()
@@ -9,64 +12,43 @@ fn main() {
912
.run();
1013
}
1114

15+
const X_EXTENT: f32 = 600.;
16+
1217
fn setup(
1318
mut commands: Commands,
1419
mut meshes: ResMut<Assets<Mesh>>,
1520
mut materials: ResMut<Assets<ColorMaterial>>,
1621
) {
1722
commands.spawn(Camera2dBundle::default());
1823

19-
// Circle
20-
commands.spawn(MaterialMesh2dBundle {
21-
mesh: meshes.add(Circle { radius: 50.0 }).into(),
22-
material: materials.add(Color::VIOLET),
23-
transform: Transform::from_translation(Vec3::new(-275.0, 0.0, 0.0)),
24-
..default()
25-
});
26-
27-
// Ellipse
28-
commands.spawn(MaterialMesh2dBundle {
29-
mesh: meshes.add(Ellipse::new(25.0, 50.0)).into(),
30-
material: materials.add(Color::TURQUOISE),
31-
transform: Transform::from_translation(Vec3::new(-150.0, 0.0, 0.0)),
32-
..default()
33-
});
34-
35-
// Capsule
36-
commands.spawn(MaterialMesh2dBundle {
37-
mesh: meshes.add(Capsule2d::new(25.0, 50.0)).into(),
38-
material: materials.add(Color::LIME_GREEN),
39-
transform: Transform::from_translation(Vec3::new(-50.0, 0.0, 0.0)),
40-
..default()
41-
});
42-
43-
// Rectangle
44-
commands.spawn(MaterialMesh2dBundle {
45-
mesh: meshes.add(Rectangle::new(50.0, 100.0)).into(),
46-
material: materials.add(Color::YELLOW),
47-
transform: Transform::from_translation(Vec3::new(50.0, 0.0, 0.0)),
48-
..default()
49-
});
50-
51-
// Hexagon
52-
commands.spawn(MaterialMesh2dBundle {
53-
mesh: meshes.add(RegularPolygon::new(50.0, 6)).into(),
54-
material: materials.add(Color::ORANGE),
55-
transform: Transform::from_translation(Vec3::new(175.0, 0.0, 0.0)),
56-
..default()
57-
});
58-
59-
// Triangle
60-
commands.spawn(MaterialMesh2dBundle {
61-
mesh: meshes
62-
.add(Triangle2d::new(
63-
Vec2::Y * 50.0,
64-
Vec2::new(-50.0, -50.0),
65-
Vec2::new(50.0, -50.0),
66-
))
67-
.into(),
68-
material: materials.add(Color::ORANGE_RED),
69-
transform: Transform::from_translation(Vec3::new(300.0, 0.0, 0.0)),
70-
..default()
71-
});
24+
let shapes = [
25+
Mesh2dHandle(meshes.add(Circle { radius: 50.0 })),
26+
Mesh2dHandle(meshes.add(Ellipse::new(25.0, 50.0))),
27+
Mesh2dHandle(meshes.add(Capsule2d::new(25.0, 50.0))),
28+
Mesh2dHandle(meshes.add(Rectangle::new(50.0, 100.0))),
29+
Mesh2dHandle(meshes.add(RegularPolygon::new(50.0, 6))),
30+
Mesh2dHandle(meshes.add(Triangle2d::new(
31+
Vec2::Y * 50.0,
32+
Vec2::new(-50.0, -50.0),
33+
Vec2::new(50.0, -50.0),
34+
))),
35+
];
36+
let num_shapes = shapes.len();
37+
38+
for (i, shape) in shapes.into_iter().enumerate() {
39+
// Distribute colors evenly across the rainbow.
40+
let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
41+
42+
commands.spawn(MaterialMesh2dBundle {
43+
mesh: shape,
44+
material: materials.add(color),
45+
transform: Transform::from_xyz(
46+
// Distribute shapes from -X_EXTENT to +X_EXTENT.
47+
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
48+
0.0,
49+
0.0,
50+
),
51+
..default()
52+
});
53+
}
7254
}

0 commit comments

Comments
 (0)