diff --git a/src/lib.rs b/src/lib.rs index 75058bc..4d585c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ mod config; #[cfg(feature = "dev")] mod debug; mod physics; +mod simulation; mod state; mod ui; @@ -34,7 +35,12 @@ impl Plugin for AppPlugin { )); // Other plugins - app.add_plugins((camera::CameraPlugin, physics::PhysicsPlugin, ui::UiPlugin)); + app.add_plugins(( + camera::CameraPlugin, + physics::PhysicsPlugin, + simulation::SimulationPlugin, + ui::UiPlugin, + )); #[cfg(feature = "dev")] app.add_plugins(debug::DebugPlugin { diff --git a/src/simulation.rs b/src/simulation.rs new file mode 100644 index 0000000..8043ce9 --- /dev/null +++ b/src/simulation.rs @@ -0,0 +1,18 @@ +use bevy::prelude::*; + +pub struct SimulationPlugin; + +impl Plugin for SimulationPlugin { + fn build(&self, app: &mut App) { + app.register_type::() + .init_resource::(); + } +} + +#[derive(Resource, Default, Reflect)] +#[reflect(Resource)] +pub struct Simulation { + pub plugins: f64, + pub lines: f64, + pub entities: f64, +} diff --git a/src/state/editor_screen.rs b/src/state/editor_screen.rs index 9bbfc6b..e94abf5 100644 --- a/src/state/editor_screen.rs +++ b/src/state/editor_screen.rs @@ -9,6 +9,7 @@ use serde::Deserialize; use serde::Serialize; use crate::config::Config; +use crate::simulation::Simulation; use crate::state::AppState::*; use crate::ui::CodeTyper; use crate::ui::FontSize; @@ -26,7 +27,8 @@ impl Plugin for EditorScreenStatePlugin { app.register_type::() .init_collection::() .add_systems(OnEnter(EditorScreen), enter_editor_screen) - .add_systems(OnExit(EditorScreen), exit_editor_screen); + .add_systems(OnExit(EditorScreen), exit_editor_screen) + .add_systems(Update, update_info_bar_text); } } @@ -100,6 +102,8 @@ fn enter_editor_screen(mut commands: Commands, root: Res, config: Res, config: Res, + mut info_bar_query: Query<&mut Text, With>, +) { + // TODO: E.g. Format large numbers like 2,346,834 and then 8.435e22 + let plugins = simulation.plugins; + let lines = simulation.lines; + let entities = simulation.entities; + + // TODO: Remove "s" if number is equal to 1 + let info = format!("{plugins} plugins, {lines} lines, {entities} entities"); + + for mut text in &mut info_bar_query { + text.sections[0].value = info.clone(); + } +} diff --git a/src/state/editor_screen/entity_view.rs b/src/state/editor_screen/entity_view.rs deleted file mode 100644 index 4425296..0000000 --- a/src/state/editor_screen/entity_view.rs +++ /dev/null @@ -1,88 +0,0 @@ -use bevy::prelude::*; - -use crate::state::editor_screen::EditorScreenConfig; -use crate::ui::FontSize; -use crate::ui::BOLD_FONT_HANDLE; - -#[derive(Resource)] -pub struct EntityModel { - /// Number of entities. - count: f64, -} - -impl Default for EntityModel { - fn default() -> Self { - Self { count: 0.0 } - } -} - -/// Component for the text that displays "Entities: X" -#[derive(Component)] -pub struct EntitiesText; - -pub fn spawn(commands: &mut Commands, config: &EditorScreenConfig) -> Entity { - let top_bar_text_style = TextStyle { - font: BOLD_FONT_HANDLE, - color: config.top_bar_text_color, - ..default() - }; - - commands.init_resource::(); - - let entity_view = commands - .spawn(( - Name::new("EntityView"), - NodeBundle { - style: Style { - width: config.entity_view_width, - height: Val::Percent(100.0), - flex_grow: 1.0, - ..default() - }, - ..default() - }, - )) - .id(); - - // Top bar part of the code view. - let header_container = commands - .spawn(( - Name::new("EntityHeaderContainer"), - NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: config.top_bar_height, - padding: UiRect::left(Val::VMin(3.5)), - border: UiRect::left(config.top_bar_separator_width), - align_items: AlignItems::Center, - ..default() - }, - background_color: config.top_bar_background_color.into(), - border_color: config.top_bar_separator_color.into(), - ..default() - }, - )) - .set_parent(entity_view) - .id(); - - commands - .spawn(( - Name::new("HeaderText"), - TextBundle::from_section("0 entities", top_bar_text_style) - .with_text_alignment(TextAlignment::Left), - FontSize::new(config.top_bar_font_size), - EntitiesText, - )) - .set_parent(header_container); - - entity_view -} - -pub fn update_bar( - mut entity_model: ResMut, - mut query: Query<&mut Text, With>, -) { - let mut text = query.single_mut(); - entity_model.count += 1.0; - text.sections[0].value = format!("{} entities", entity_model.count); -} diff --git a/src/ui/code_typer.rs b/src/ui/code_typer.rs index 01e2dba..38835d6 100644 --- a/src/ui/code_typer.rs +++ b/src/ui/code_typer.rs @@ -3,6 +3,8 @@ use std::str::Chars; use bevy::prelude::*; +use crate::simulation::Simulation; + pub struct CodeTyperPlugin; impl Plugin for CodeTyperPlugin { @@ -31,8 +33,6 @@ pub struct CodeTyper { pub lines_count: usize, /// The maximum number of lines to display before old lines start getting deleted. pub lines_max: usize, - /// The total number of \n typed over the full lifetime. - pub lines_typed: usize, /// An infinite iterator that yields the next character that will be added. #[reflect(ignore)] pub code: CodeGenerator, @@ -44,7 +44,6 @@ impl Default for CodeTyper { chars_per_key: 1, lines_count: 1, lines_max: 1, - lines_typed: 0, code: default(), } } @@ -53,7 +52,8 @@ impl Default for CodeTyper { pub fn type_code( mut char_events: EventReader, keyboard_input: Res>, - mut query: Query<(&mut CodeTyper, &mut Text)>, + mut simulation: ResMut, + mut typer_query: Query<(&mut CodeTyper, &mut Text)>, ) { let count = char_events .read() @@ -63,7 +63,7 @@ pub fn type_code( return; } - for (mut typer, mut text) in &mut query { + for (mut typer, mut text) in &mut typer_query { let text = &mut text.sections[0].value; for _ in 0..count * typer.chars_per_key { loop { @@ -73,7 +73,7 @@ pub fn type_code( // If it was a newline, update typer's lines if c == '\n' { - typer.lines_typed += 1; + simulation.lines += 1.0; typer.lines_count += 1; if typer.lines_count > typer.lines_max { typer.lines_count -= 1;