Skip to content

Commit

Permalink
feat: game ui module + more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisSmuda committed Feb 18, 2024
1 parent b985234 commit 2ab672c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/game/game_ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::*;

///
/// Spawn UI Bundle
pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Score Text
commands.spawn(TextBundle {
style: Style {
position_type: PositionType::Absolute,
..default()
},
text: Text::from_section(
format!("score: {} ", 0.),
TextStyle {
font: asset_server.load("fonts/Efforts.ttf"),
font_size: 32.0,
color: Color::WHITE,
},
),
..Default::default()
});
}

///
/// Score system updates score on fixed interval
pub fn update(
time: Res<Time>,
mut timer: ResMut<ScoreTimer>,
mut game_state: ResMut<GameState>,
mut text_query: Query<&mut Text>,
) {
if timer.0.tick(time.delta()).just_finished() {
game_state.score += 10;
let mut text = text_query.single_mut();
text.sections[0].value = format!("score: {}", game_state.score);
println!("Score: {}", game_state.score);
}
}

pub fn teardown(mut commands: Commands, entities: Query<Entity, Without<Camera>>) {
for entity in entities.iter() {
commands.entity(entity).despawn_recursive();
}
}
7 changes: 6 additions & 1 deletion src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::*;

mod game_ui;

#[derive(Component)]
struct Player;

Expand All @@ -14,11 +16,14 @@ pub struct GamePlugin;
impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::InGame), setup_game)
.add_systems(OnEnter(AppState::InGame), game_ui::setup)
.add_systems(Update, game_ui::update.run_if(in_state(AppState::InGame)))
.add_systems(Update, spawn_enemies.run_if(in_state(AppState::InGame)))
.add_systems(Update, move_enemies.run_if(in_state(AppState::InGame)))
.add_systems(Update, player_movement.run_if(in_state(AppState::InGame)))
.add_systems(Update, check_collisions.run_if(in_state(AppState::InGame)))
.add_systems(OnExit(AppState::InGame), teardown_game_state);
.add_systems(OnExit(AppState::InGame), teardown_game_state)
.add_systems(OnExit(AppState::InGame), game_ui::teardown);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/gameover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ fn update_gameover(
}
}

fn teardown_gameover_state(mut commands: Commands, menu_data: Res<MenuData>) {
fn teardown_gameover_state(
mut commands: Commands,
menu_data: Res<MenuData>,
mut game_state: ResMut<GameState>,
) {
commands.entity(menu_data.button_entity).despawn_recursive();
// Reset Score!
game_state.score = 0;
}
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use game::GamePlugin;
#[derive(Resource)]
struct SpawnTimer(Timer);

#[derive(Resource)]
struct ScoreTimer(Timer);

const TIME_STEP: f32 = 5.0;

#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
Expand All @@ -23,11 +26,22 @@ pub enum AppState {
GameOver,
}

// Game State => resource to hold score information
#[derive(Resource)]
pub struct GameState {
pub score: u64,
}

fn main() {
App::new()
// Resources
.insert_resource(ClearColor(BG_COLOR))
.insert_resource(SpawnTimer(Timer::from_seconds(1.0, TimerMode::Repeating))) // Adjust spawn rate as needed
.insert_resource(SpawnTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
.insert_resource(ScoreTimer(Timer::from_seconds(1.0, TimerMode::Repeating)))
.insert_resource(GameState { score: 0 })
// State
.add_state::<AppState>()
// Plugins
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Run Bevy".to_string(),
Expand Down

0 comments on commit 2ab672c

Please sign in to comment.