game states cleanup
This commit is contained in:
+2
-2
@@ -1,6 +1,6 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{camera::MainCamera, collision::Collider, player::Player};
|
||||
use crate::{camera::MainCamera, collision::Collider, game_state::GameState, player::Player};
|
||||
|
||||
fn constrain_player(
|
||||
mut player: Single<(&Collider, &mut Transform), With<Player>>,
|
||||
@@ -20,6 +20,6 @@ pub struct BoundsPlugin;
|
||||
|
||||
impl Plugin for BoundsPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, constrain_player);
|
||||
app.add_systems(Update, constrain_player.run_if(in_state(GameState::InGame)));
|
||||
}
|
||||
}
|
||||
|
||||
+14
-3
@@ -2,11 +2,15 @@ use bevy::prelude::*;
|
||||
|
||||
use crate::{camera::MainCamera, game_state::GameState, player::Player};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Debug;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct DebugCamera;
|
||||
|
||||
fn spawn_debug_camera(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Debug,
|
||||
DebugCamera,
|
||||
Camera2d,
|
||||
Camera {
|
||||
@@ -63,6 +67,7 @@ fn spawn_debug_frame(mut commands: Commands) {
|
||||
};
|
||||
|
||||
commands.spawn((
|
||||
Debug,
|
||||
FramePart(side),
|
||||
Sprite::from_color(Color::srgb(1.0, 0.0, 0.0), size),
|
||||
Visibility::Hidden,
|
||||
@@ -99,18 +104,24 @@ fn draw_view_frame(
|
||||
*visibility = visible;
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup_debug(mut commands: Commands, entities: Query<Entity, With<Debug>>) {
|
||||
for entity in entities {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DebugPlugin;
|
||||
|
||||
impl Plugin for DebugPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(
|
||||
Startup,
|
||||
OnEnter(GameState::InGame),
|
||||
(
|
||||
spawn_debug_camera,
|
||||
setup_debug_camera.after(spawn_debug_camera),
|
||||
spawn_debug_frame,
|
||||
)
|
||||
.run_if(in_state(GameState::InGame)),
|
||||
),
|
||||
)
|
||||
.add_systems(
|
||||
Update,
|
||||
|
||||
+12
-2
@@ -1,12 +1,13 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::game_state::GameState;
|
||||
use crate::{camera::MainCamera, game_state::GameState};
|
||||
|
||||
// To delete game over's components
|
||||
#[derive(Component)]
|
||||
pub struct GameOver;
|
||||
|
||||
fn game_over(mut commands: Commands) {
|
||||
fn game_over(mut commands: Commands, mut main_camera: Single<&mut Transform, With<MainCamera>>) {
|
||||
// Texts
|
||||
commands.spawn((
|
||||
GameOver,
|
||||
Node {
|
||||
@@ -35,6 +36,15 @@ fn game_over(mut commands: Commands) {
|
||||
)
|
||||
],
|
||||
));
|
||||
|
||||
main_camera.translation = Vec3::ZERO;
|
||||
|
||||
// Background
|
||||
commands.spawn((
|
||||
GameOver,
|
||||
Sprite::from_color(Color::BLACK, Vec2::new(1920.0, 1080.0)),
|
||||
Transform::from_xyz(0.0, 0.0, -1.0),
|
||||
));
|
||||
}
|
||||
|
||||
fn cleanup_game_over(mut commands: Commands, game_over: Query<Entity, With<GameOver>>) {
|
||||
|
||||
+16
-6
@@ -7,6 +7,7 @@ use crate::game_state::GameState;
|
||||
pub struct MainMenu;
|
||||
|
||||
fn main_menu(mut commands: Commands) {
|
||||
// Texts
|
||||
commands.spawn((
|
||||
MainMenu,
|
||||
Node {
|
||||
@@ -35,20 +36,29 @@ fn main_menu(mut commands: Commands) {
|
||||
)
|
||||
],
|
||||
));
|
||||
|
||||
// Background
|
||||
commands.spawn((
|
||||
MainMenu,
|
||||
Sprite::from_color(Color::BLACK, Vec2::new(1920.0, 1080.0)),
|
||||
Transform::from_xyz(0.0, 0.0, -1.0),
|
||||
));
|
||||
}
|
||||
|
||||
fn cleanup_menu(mut commands: Commands, menu: Query<Entity, With<MainMenu>>) {
|
||||
for entity in menu {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
// Wait player to press enter for starting the fall
|
||||
fn menu_input(input: Res<ButtonInput<KeyCode>>, mut next_state: ResMut<NextState<GameState>>) {
|
||||
if input.just_pressed(KeyCode::Enter) {
|
||||
next_state.set(GameState::InGame);
|
||||
}
|
||||
}
|
||||
|
||||
// Clean all menu's entities
|
||||
fn cleanup_menu(mut commands: Commands, menu: Query<Entity, With<MainMenu>>) {
|
||||
for entity in menu {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MainMenuPlugin;
|
||||
|
||||
impl Plugin for MainMenuPlugin {
|
||||
|
||||
+7
-2
@@ -111,11 +111,15 @@ pub fn reset_player(
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup_player(mut commands: Commands, player: Single<Entity, With<Player>>) {
|
||||
commands.entity(*player).despawn();
|
||||
}
|
||||
|
||||
pub struct PlayerPlugin;
|
||||
|
||||
impl Plugin for PlayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn_player)
|
||||
app.add_systems(OnEnter(GameState::InGame), spawn_player)
|
||||
.init_resource::<FallStarted>()
|
||||
.add_systems(
|
||||
Update,
|
||||
@@ -126,6 +130,7 @@ impl Plugin for PlayerPlugin {
|
||||
.after(check_collision_with_player),
|
||||
)
|
||||
.run_if(in_state(GameState::InGame)),
|
||||
);
|
||||
)
|
||||
.add_systems(OnExit(GameState::InGame), cleanup_player);
|
||||
}
|
||||
}
|
||||
|
||||
+14
-4
@@ -46,16 +46,26 @@ fn update_depth_text(
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup_score(mut commands: Commands, entities: Query<Entity, With<DepthText>>) {
|
||||
for entity in entities {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScorePlugin;
|
||||
|
||||
impl Plugin for ScorePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn_depth_text)
|
||||
.init_resource::<Depth>()
|
||||
app.init_resource::<Depth>()
|
||||
.add_systems(OnEnter(GameState::InGame), spawn_depth_text)
|
||||
.add_systems(
|
||||
Update,
|
||||
(update_depth_text, update_depth.after(move_player))
|
||||
(
|
||||
update_depth_text.after(spawn_depth_text),
|
||||
update_depth.after(move_player),
|
||||
)
|
||||
.run_if(in_state(GameState::InGame)),
|
||||
);
|
||||
)
|
||||
.add_systems(OnExit(GameState::InGame), cleanup_score);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-1
@@ -57,11 +57,19 @@ fn spawn_chunk(commands: &mut Commands, id: i64, run_seed: u64) {
|
||||
crate::background::spawn_background_chunk(commands, id);
|
||||
// crate::obstacle::spawn_obstacle_chunk(commands, id, run_seed);
|
||||
}
|
||||
|
||||
fn cleanup_chunk(mut commands: Commands, entities: Query<Entity, With<Chunk>>) {
|
||||
for entity in entities {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WorldPlugin;
|
||||
|
||||
impl Plugin for WorldPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(RunSeed(rand::random::<u64>()))
|
||||
.add_systems(Update, stream_world.run_if(in_state(GameState::InGame)));
|
||||
.add_systems(Update, stream_world.run_if(in_state(GameState::InGame)))
|
||||
.add_systems(OnExit(GameState::InGame), cleanup_chunk);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user