use bevy_app::prelude::*;
use bevy_ecs::entity::Entities;
use crate::{
Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic, DEFAULT_MAX_HISTORY_LENGTH,
};
pub struct EntityCountDiagnosticsPlugin {
pub max_history_length: usize,
}
impl Default for EntityCountDiagnosticsPlugin {
fn default() -> Self {
Self::new(DEFAULT_MAX_HISTORY_LENGTH)
}
}
impl EntityCountDiagnosticsPlugin {
pub fn new(max_history_length: usize) -> Self {
Self { max_history_length }
}
}
impl Plugin for EntityCountDiagnosticsPlugin {
fn build(&self, app: &mut App) {
app.register_diagnostic(
Diagnostic::new(Self::ENTITY_COUNT).with_max_history_length(self.max_history_length),
)
.add_systems(Update, Self::diagnostic_system);
}
}
impl EntityCountDiagnosticsPlugin {
pub const ENTITY_COUNT: DiagnosticPath = DiagnosticPath::const_new("entity_count");
pub fn diagnostic_system(mut diagnostics: Diagnostics, entities: &Entities) {
diagnostics.add_measurement(&Self::ENTITY_COUNT, || entities.count_spawned() as f64);
}
}