Skip to content

Commit

Permalink
Slight perf improvements and tidy for contributors example (bevyengin…
Browse files Browse the repository at this point in the history
  • Loading branch information
SUPERCILEX authored and aevyrie committed Jun 7, 2022
1 parent c9f7a47 commit 2e1c9cf
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
.add_system(move_system)
.add_system(collision_system)
.add_system(select_system)
.insert_resource(SelectTimer(Timer::from_seconds(SHOWCASE_TIMER_SECS, true)))
.insert_resource(SelectionState::default())
.run();
}

Expand All @@ -27,8 +27,19 @@ struct ContributorSelection {
idx: usize,
}

#[derive(Deref, DerefMut)]
struct SelectTimer(Timer);
struct SelectionState {
timer: Timer,
has_triggered: bool,
}

impl Default for SelectionState {
fn default() -> Self {
Self {
timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, true),
has_triggered: false,
}
}
}

#[derive(Component)]
struct ContributorDisplay;
Expand Down Expand Up @@ -70,7 +81,7 @@ fn setup_contributor_selection(mut commands: Commands, asset_server: Res<AssetSe
let texture_handle = asset_server.load("branding/icon.png");

let mut contributor_selection = ContributorSelection {
order: vec![],
order: Vec::with_capacity(contribs.len()),
idx: 0,
};

Expand Down Expand Up @@ -156,37 +167,40 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

/// Finds the next contributor to display and selects the entity
fn select_system(
mut timer: ResMut<SelectTimer>,
mut timer: ResMut<SelectionState>,
mut contributor_selection: ResMut<ContributorSelection>,
mut text_query: Query<&mut Text, With<ContributorDisplay>>,
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
time: Res<Time>,
) {
if !timer.tick(time.delta()).just_finished() {
if !timer.timer.tick(time.delta()).just_finished() {
return;
}
if !timer.has_triggered {
let mut text = text_query.single_mut();
text.sections[0].value = "Contributor: ".to_string();

let prev = contributor_selection.idx;

if (contributor_selection.idx + 1) < contributor_selection.order.len() {
contributor_selection.idx += 1;
} else {
contributor_selection.idx = 0;
timer.has_triggered = true;
}

{
let (_, entity) = &contributor_selection.order[prev];
let (_, entity) = &contributor_selection.order[contributor_selection.idx];
if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(*entity) {
deselect(&mut sprite, contributor, &mut *transform);
}
}

if (contributor_selection.idx + 1) < contributor_selection.order.len() {
contributor_selection.idx += 1;
} else {
contributor_selection.idx = 0;
}

let (name, entity) = &contributor_selection.order[contributor_selection.idx];

if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(*entity) {
if let Some(mut text) = text_query.iter_mut().next() {
select(&mut sprite, contributor, &mut *transform, &mut *text, name);
}
let mut text = text_query.single_mut();
select(&mut sprite, contributor, &mut *transform, &mut *text, name);
}
}

Expand All @@ -197,7 +211,7 @@ fn select(
contributor: &Contributor,
transform: &mut Transform,
text: &mut Text,
name: &str,
name: &String,
) {
sprite.color = Color::hsla(
contributor.hue,
Expand All @@ -208,8 +222,7 @@ fn select(

transform.translation.z = 100.0;

text.sections[0].value = "Contributor: ".to_string();
text.sections[1].value = name.to_string();
text.sections[1].value.clone_from(name);
text.sections[1].style.color = sprite.color;
}

Expand Down

0 comments on commit 2e1c9cf

Please sign in to comment.