Skip to content

Commit e0dcb27

Browse files
committed
Add total star count
1 parent fe9a45d commit e0dcb27

1 file changed

Lines changed: 70 additions & 2 deletions

File tree

src/ui/level_select.rs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,14 @@ fn level_select_button_system(
7373
}
7474
}
7575

76-
fn level_select_enter(mut commands: Commands, best_scores: Res<BestScores>, handles: Res<Handles>) {
76+
fn level_select_enter(
77+
mut commands: Commands,
78+
best_scores: Res<BestScores>,
79+
handles: Res<Handles>,
80+
levels: Res<Assets<Level>>,
81+
) {
7782
let total_score: u32 = best_scores.0.iter().map(|(_, v)| v).sum();
83+
let num_stars = num_stars(&best_scores, &handles, &levels);
7884

7985
let root = commands
8086
.spawn((
@@ -131,6 +137,43 @@ fn level_select_enter(mut commands: Commands, best_scores: Res<BestScores>, hand
131137
..default()
132138
})
133139
.with_children(|parent| {
140+
parent.spawn((
141+
Node {
142+
align_self: AlignSelf::Center,
143+
..default()
144+
},
145+
Text::new("★"),
146+
TextFont {
147+
font: handles.fonts[0].clone(),
148+
font_size: 25.0,
149+
..default()
150+
},
151+
TextColor(theme::UI_LABEL.into()),
152+
Children::spawn((
153+
Spawn((
154+
TextSpan::new(format!("{}/", num_stars.0)),
155+
TextFont {
156+
font: handles.fonts[0].clone(),
157+
font_size: 25.0,
158+
..default()
159+
},
160+
TextColor(if num_stars.0 == num_stars.1 {
161+
theme::UI_LABEL.into()
162+
} else {
163+
theme::UI_LABEL_MUTED.into()
164+
}),
165+
)),
166+
Spawn((
167+
TextSpan::new(format!("{}", num_stars.1)),
168+
TextFont {
169+
font: handles.fonts[0].clone(),
170+
font_size: 25.0,
171+
..default()
172+
},
173+
TextColor(theme::UI_LABEL.into()),
174+
)),
175+
)),
176+
));
134177
parent.spawn((
135178
Node {
136179
align_self: AlignSelf::Center,
@@ -144,7 +187,6 @@ fn level_select_enter(mut commands: Commands, best_scores: Res<BestScores>, hand
144187
},
145188
TextColor(theme::FINISHED_ROAD[1].into()),
146189
));
147-
// TODO add total star count
148190
// TODO clock for flavor?
149191
});
150192
})
@@ -443,3 +485,29 @@ fn music_volume_text_system(
443485
text.0 = format!("{}%", volume.0);
444486
}
445487
}
488+
489+
/// Returns a tuple containing the number of stars the player has
490+
/// earned and the total number of stars available to earn.
491+
fn num_stars(
492+
best_scores: &BestScores,
493+
handles: &Handles,
494+
levels: &Assets<Level>,
495+
) -> (usize, usize) {
496+
(1..=NUM_LEVELS)
497+
.flat_map(|i| {
498+
let handle = handles.levels.get(i as usize - 1)?;
499+
let level = levels.get(handle)?;
500+
let score = best_scores.0.get(&i)?;
501+
502+
let stars = level
503+
.star_thresholds
504+
.iter()
505+
.filter(|t| **t <= *score)
506+
.count();
507+
508+
let total = level.star_thresholds.len();
509+
510+
Some((stars, total))
511+
})
512+
.fold((0, 0), |acc, e| (acc.0 + e.0, acc.1 + e.1))
513+
}

0 commit comments

Comments
 (0)