Skip to content

Commit

Permalink
Add entity_view module with top bar
Browse files Browse the repository at this point in the history
  • Loading branch information
necrashter committed Dec 3, 2023
1 parent cc7de50 commit 72c65ad
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
17 changes: 13 additions & 4 deletions src/state/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::ui::BOLD_FONT_HANDLE;
use crate::AppRoot;

mod code_view;
use code_view::typing_system;
use code_view::update_code_view_bar;

mod entity_view;

pub struct GameStatePlugin;

Expand All @@ -20,7 +20,12 @@ impl Plugin for GameStatePlugin {
.add_systems(OnExit(Game), exit_game)
.add_systems(
Update,
(typing_system, update_code_view_bar).run_if(in_state(Game)),
(
code_view::typing_system,
code_view::update_bar,
entity_view::update_bar,
)
.run_if(in_state(Game)),
);
}
}
Expand All @@ -34,13 +39,17 @@ const TOP_BAR_TEXT_STYLE: TextStyle = TextStyle {
const TOP_BAR_FONT_SIZE: f32 = 8.0;
const TOP_BAR_BACKGROUND_COLOR: Color = Color::rgb(0.165, 0.18, 0.184);

const TOP_BAR_SEPARATOR_COLOR: Color = Color::rgb(0.510, 0.612, 0.769);
const TOP_BAR_SEPARATOR_WIDTH: f32 = 1.5;

#[derive(AssetCollection, Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct GameAssets {}

fn enter_game(mut commands: Commands, root: Res<AppRoot>, config: Res<Config>) {
commands.insert_resource(ClearColor(config.bg_color));
code_view::init(commands, root);
code_view::init(&mut commands, &root);
entity_view::init(&mut commands, &root);
}

fn exit_game(root: Res<AppRoot>, mut transform_query: Query<&mut Transform>) {
Expand Down
7 changes: 2 additions & 5 deletions src/state/game/code_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Default for CodeModel {
}
}

pub fn init(mut commands: Commands, root: Res<AppRoot>) {
pub fn init(commands: &mut Commands, root: &Res<AppRoot>) {
commands.insert_resource(CodeModel::default());

let code_view = commands
Expand Down Expand Up @@ -130,10 +130,7 @@ pub fn init(mut commands: Commands, root: Res<AppRoot>) {
.set_parent(text_area_container);
}

pub fn update_code_view_bar(
code_model: Res<CodeModel>,
mut query: Query<&mut Text, With<LinesText>>,
) {
pub fn update_bar(code_model: Res<CodeModel>, mut query: Query<&mut Text, With<LinesText>>) {
let mut text = query.single_mut();
text.sections[0].value = format!("Lines: {}", code_model.loc);
}
Expand Down
62 changes: 62 additions & 0 deletions src/state/game/entity_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use super::*;
use crate::ui::vh;
use crate::ui::FontSize;

#[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 init(commands: &mut Commands, root: &Res<AppRoot>) {
commands.insert_resource(EntityModel::default());

// Top bar part of the code view.
let header_container = commands
.spawn((
Name::new("EntityHeaderContainer"),
NodeBundle {
style: Style {
width: Val::Percent(40.0),
height: vh(20.0),
padding: UiRect::axes(Val::VMin(3.5), Val::VMin(3.5)),
border: UiRect::left(vh(TOP_BAR_SEPARATOR_WIDTH)),
..default()
},
background_color: TOP_BAR_BACKGROUND_COLOR.into(),
border_color: TOP_BAR_SEPARATOR_COLOR.into(),
..default()
},
))
.set_parent(root.ui)
.id();

commands
.spawn((
Name::new("HeaderText"),
TextBundle::from_section("Entities: 0", TOP_BAR_TEXT_STYLE)
.with_text_alignment(TextAlignment::Left),
FontSize::new(vh(TOP_BAR_FONT_SIZE)),
EntitiesText,
))
.set_parent(header_container);
}

pub fn update_bar(
mut entity_model: ResMut<EntityModel>,
mut query: Query<&mut Text, With<EntitiesText>>,
) {
let mut text = query.single_mut();
entity_model.count += 1.0;
text.sections[0].value = format!("Entities: {}", entity_model.count);
}

0 comments on commit 72c65ad

Please sign in to comment.