#[cfg(not(target_arch = "wasm32"))]
use bevy::{
input::common_conditions::input_just_pressed,
sprite_render::{Wireframe2dConfig, Wireframe2dPlugin},
};
use bevy::{input::common_conditions::input_toggle_active, prelude::*};
fn main() {
let mut app = App::new();
app.add_plugins((
DefaultPlugins,
#[cfg(not(target_arch = "wasm32"))]
Wireframe2dPlugin::default(),
))
.add_systems(Startup, setup);
#[cfg(not(target_arch = "wasm32"))]
app.add_systems(
Update,
toggle_wireframe.run_if(input_just_pressed(KeyCode::Space)),
);
app.add_systems(
Update,
rotate.run_if(input_toggle_active(false, KeyCode::KeyR)),
);
app.run();
}
const X_EXTENT: f32 = 1000.;
const Y_EXTENT: f32 = 150.;
const THICKNESS: f32 = 5.0;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(Camera2d);
let shapes = [
meshes.add(Circle::new(50.0)),
meshes.add(CircularSector::new(50.0, 1.0)),
meshes.add(CircularSegment::new(50.0, 1.25)),
meshes.add(Ellipse::new(25.0, 50.0)),
meshes.add(Annulus::new(25.0, 50.0)),
meshes.add(Capsule2d::new(25.0, 50.0)),
meshes.add(Rhombus::new(75.0, 100.0)),
meshes.add(Rectangle::new(50.0, 100.0)),
meshes.add(RegularPolygon::new(50.0, 6)),
meshes.add(Triangle2d::new(
Vec2::Y * 50.0,
Vec2::new(-50.0, -50.0),
Vec2::new(50.0, -50.0),
)),
meshes.add(Segment2d::new(
Vec2::new(-50.0, 50.0),
Vec2::new(50.0, -50.0),
)),
meshes.add(Polyline2d::new(vec![
Vec2::new(-50.0, 50.0),
Vec2::new(0.0, -50.0),
Vec2::new(50.0, 50.0),
])),
];
let num_shapes = shapes.len();
for (i, shape) in shapes.into_iter().enumerate() {
let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
commands.spawn((
Mesh2d(shape),
MeshMaterial2d(materials.add(color)),
Transform::from_xyz(
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
Y_EXTENT / 2.,
0.0,
),
));
}
let rings = [
meshes.add(Circle::new(50.0).to_ring(THICKNESS)),
meshes.add(Ring::new(
CircularSector::new(50.0, 1.0),
CircularSector::new(45.0, 1.0),
)),
meshes.add(CircularSegment::new(50.0, 1.25).to_ring(THICKNESS)),
meshes.add({
let outer = Ellipse::new(25.0, 50.0);
let mut inner = outer;
inner.half_size -= Vec2::splat(THICKNESS);
Ring::new(outer, inner)
}),
meshes.add(Ring::new(Circle::new(50.0), Circle::new(25.0))),
meshes.add(Capsule2d::new(25.0, 50.0).to_ring(THICKNESS)),
meshes.add(Rhombus::new(75.0, 100.0).to_ring(THICKNESS)),
meshes.add(Rectangle::new(50.0, 100.0).to_ring(THICKNESS)),
meshes.add(RegularPolygon::new(50.0, 6).to_ring(THICKNESS)),
meshes.add(
Triangle2d::new(
Vec2::Y * 50.0,
Vec2::new(-50.0, -50.0),
Vec2::new(50.0, -50.0),
)
.to_ring(THICKNESS),
),
];
let num_rings = rings.len() + 2;
for (i, shape) in rings.into_iter().enumerate() {
let color = Color::hsl(360. * i as f32 / num_rings as f32, 0.95, 0.7);
commands.spawn((
Mesh2d(shape),
MeshMaterial2d(materials.add(color)),
Transform::from_xyz(
-X_EXTENT / 2. + i as f32 / (num_rings - 1) as f32 * X_EXTENT,
-Y_EXTENT / 2.,
0.0,
),
));
}
let mut text = "Press 'R' to pause/resume rotation".to_string();
#[cfg(not(target_arch = "wasm32"))]
text.push_str("\nPress 'Space' to toggle wireframes");
commands.spawn((
Text::new(text),
Node {
position_type: PositionType::Absolute,
top: px(12),
left: px(12),
..default()
},
));
}
#[cfg(not(target_arch = "wasm32"))]
fn toggle_wireframe(mut wireframe_config: ResMut<Wireframe2dConfig>) {
wireframe_config.global = !wireframe_config.global;
}
fn rotate(mut query: Query<&mut Transform, With<Mesh2d>>, time: Res<Time>) {
for mut transform in &mut query {
transform.rotate_z(time.delta_secs() / 2.0);
}
}