// Builds a big list every frame; the arena is reset between calls, so the
// memory doesn't add up.
trait Pick {
fn pick(&self, a: f32, b: f32) -> f32;
}
struct Larger;
impl Pick for Larger {
fn pick(&self, a: f32, b: f32) -> f32 {
a.max(b)
}
}
fn tally(mut score: ResMut<Score>, q: Query<&Health>) {
let mut healths = Vec::new();
for hp in &q {
healths.push(hp.0);
}
let mut filler: Vec<u32> = Vec::new();
let mut i = 0;
while i < 100000 {
filler.push(i);
i += 1;
}
// Through a `Box<dyn Trait>`, and `f32::max`.
let chooser: Box<dyn Pick> = Box::new(Larger);
let mut best = 0.0;
for h in &healths {
best = chooser.pick(best, *h);
}
score.changed = best as u32 + filler.len() as u32;
// Frames run: a mod disabled by running out of memory would stop here.
score.added += 1;
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, tally);
}