game states cleanup

This commit is contained in:
2026-09-19 11:43:07 +02:00
parent 3910eb4261
commit 452d39dd45
6 changed files with 29 additions and 16 deletions
+12 -6
View File
@@ -22,15 +22,21 @@ fn follow_player(
// + (player.translation.y - camera.translation.y) * 3.0 * time.delta_secs(); // with delay // + (player.translation.y - camera.translation.y) * 3.0 * time.delta_secs(); // with delay
} }
fn reset_camera(mut main_camera: Single<&mut Camera, With<MainCamera>>) {
main_camera.is_active = true;
}
pub struct CameraPlugin; pub struct CameraPlugin;
impl Plugin for CameraPlugin { impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, spawn_main_camera).add_systems( app.add_systems(Startup, spawn_main_camera)
Update, .add_systems(OnEnter(GameState::InGame), reset_camera)
follow_player .add_systems(
.run_if(in_state(GameState::InGame)) Update,
.after(move_player), follow_player
); .run_if(in_state(GameState::InGame))
.after(move_player),
);
} }
} }
+1 -1
View File
@@ -30,7 +30,7 @@ fn game_over(mut commands: Commands) {
}, },
), ),
( (
Text::new("| N new map | R restart | T main menu | "), Text::new("N new map | R restart | T main menu"),
TextFont { TextFont {
font_size: FontSize::Px(40.0), font_size: FontSize::Px(40.0),
..default() ..default()
+5 -2
View File
@@ -1,6 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::game_state::GameState; use crate::{camera::MainCamera, game_state::GameState};
// To delete menu's components // To delete menu's components
#[derive(Component)] #[derive(Component)]
@@ -53,12 +53,15 @@ fn cleanup_menu(mut commands: Commands, menu: Query<Entity, With<MainMenu>>) {
commands.entity(entity).despawn(); commands.entity(entity).despawn();
} }
} }
fn reset_camera(mut main_camera: Single<&mut Camera, With<MainCamera>>) {
main_camera.is_active = true;
}
pub struct MainMenuPlugin; pub struct MainMenuPlugin;
impl Plugin for MainMenuPlugin { impl Plugin for MainMenuPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::MainMenu), main_menu) app.add_systems(OnEnter(GameState::MainMenu), (main_menu, reset_camera))
.add_systems(Update, menu_input.run_if(in_state(GameState::MainMenu))) .add_systems(Update, menu_input.run_if(in_state(GameState::MainMenu)))
.add_systems(OnExit(GameState::MainMenu), cleanup_menu); .add_systems(OnExit(GameState::MainMenu), cleanup_menu);
} }
+1 -1
View File
@@ -1,4 +1,4 @@
use crate::{collision::Collider, player::reset_player}; use crate::{collision::Collider, player::debug_game_over};
use bevy::prelude::*; use bevy::prelude::*;
use rand::RngExt; use rand::RngExt;
use rand::SeedableRng; use rand::SeedableRng;
+3 -3
View File
@@ -84,8 +84,8 @@ pub(crate) fn move_player(
transform.translation += velocity.0.extend(0.0) * time.delta_secs(); transform.translation += velocity.0.extend(0.0) * time.delta_secs();
} }
// Reset // Debug
pub fn reset_player( pub fn debug_game_over(
// player: Single<(&mut Transform, &mut Velocity, &mut Sprite), With<Player>>, // player: Single<(&mut Transform, &mut Velocity, &mut Sprite), With<Player>>,
input: Res<ButtonInput<KeyCode>>, input: Res<ButtonInput<KeyCode>>,
mut fall_started: ResMut<FallStarted>, mut fall_started: ResMut<FallStarted>,
@@ -124,7 +124,7 @@ impl Plugin for PlayerPlugin {
Update, Update,
( (
move_player, move_player,
reset_player debug_game_over
.after(move_player) .after(move_player)
.after(check_collision_with_player), .after(check_collision_with_player),
) )
+7 -3
View File
@@ -35,10 +35,14 @@ fn spawn_depth_text(mut commands: Commands) {
)); ));
} }
// Update depth score texts // Update depth score text
fn update_depth_text(depth: Res<Depth>, texts: Query<&mut Text, With<DepthText>>) { fn update_depth_text(
mut commands: Commands,
depth: Res<Depth>,
texts: Query<&mut Text, With<DepthText>>,
) {
for mut text in texts { for mut text in texts {
text.0 = format!("Score: {}", (depth.0 / 100.0).floor()); text.0 = format!("Score: {}", depth.0);
} }
} }