Skip to content

Commit 7c7a1c9

Browse files
committed
feat(bevy_city): add toggle for traffic light
1 parent d505a0c commit 7c7a1c9

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

examples/large_scenes/bevy_city/src/generate_city.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ pub fn spawn_city(commands: &mut Commands, assets: &CityAssets, seed: u64, size:
7878
});
7979
}
8080

81+
#[derive(Component)]
82+
pub struct TrafficLight;
83+
8184
fn spawn_roads_and_cars<R: RngExt>(
8285
commands: &mut ChildSpawnerCommands,
8386
assets: &CityAssets,
@@ -220,6 +223,7 @@ fn spawn_roads_and_cars<R: RngExt>(
220223
WorldAssetRoot(assets.traffic_lights.clone()),
221224
Transform::from_translation(pos + offset)
222225
.with_rotation(Quat::from_axis_angle(Vec3::Y, rot)),
226+
TrafficLight,
223227
));
224228
}
225229
}

examples/large_scenes/bevy_city/src/settings.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ use bevy::{
1313
};
1414
use rand::RngExt;
1515

16-
use crate::generate_city::{spawn_city, CityRoot};
17-
use crate::{assets::CityAssets, CitySpawned};
16+
use crate::assets::CityAssets;
17+
use crate::generate_city::{spawn_city, CityRoot, TrafficLight};
18+
use crate::CitySpawned;
1819

1920
#[derive(Resource)]
2021
pub struct Settings {
2122
pub simulate_cars: bool,
23+
pub traffic_lights: bool,
2224
pub shadow_maps_enabled: bool,
2325
pub contact_shadows_enabled: bool,
2426
pub wireframe_enabled: bool,
@@ -29,6 +31,7 @@ impl Default for Settings {
2931
fn default() -> Self {
3032
Self {
3133
simulate_cars: true,
34+
traffic_lights: true,
3235
shadow_maps_enabled: true,
3336
contact_shadows_enabled: true,
3437
wireframe_enabled: false,
@@ -71,6 +74,20 @@ pub fn settings_ui() -> impl Scene {
7174
})
7275
Children [ (Text("Simulate Cars") ThemedText) ]
7376
),
77+
(
78+
checkbox()
79+
Checked
80+
on(checkbox_self_update)
81+
on(|change: On<ValueChange<bool>>, mut settings: ResMut<Settings>, mut traffic_light: Query<&mut Visibility, With<TrafficLight>>| {
82+
settings.traffic_lights = change.value;
83+
let vis = if change.value {Visibility::Inherited} else {Visibility::Hidden};
84+
for mut v in &mut traffic_light {
85+
*v = vis;
86+
}
87+
})
88+
Children [ (Text("Traffic lights") ThemedText) ]
89+
),
90+
7491
(
7592
checkbox()
7693
Checked

0 commit comments

Comments
 (0)