Skip to content

Commit

Permalink
added player death screen
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukinoko-kun committed Aug 13, 2024
1 parent 30568d8 commit fdc838a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
85 changes: 85 additions & 0 deletions src/death_screen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Mageanoid - A computer game
* Copyright (C) 2024 Frank Mayer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use crate::state::AppState;
use bevy::prelude::*;

#[derive(Debug, Component)]
pub struct DeathScreen {
pub remove_timer: Timer,
}

fn spawn_death_screen(mut commands: Commands) {
commands
.spawn((
DeathScreen {
remove_timer: Timer::from_seconds(3.0, TimerMode::Once),
},
NodeBundle {
style: Style {
width: Val::Vw(100.0),
height: Val::Vh(100.0),
position_type: PositionType::Absolute,
display: Display::Flex,
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
background_color: BackgroundColor(Color::BLACK),
..default()
},
))
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"You died!",
TextStyle {
font_size: 64.0,
color: Color::WHITE,
..default()
},
));
});
}

fn remove_death_screen(
mut commands: Commands,
mut next_state: ResMut<NextState<AppState>>,
time: Res<Time>,
mut query: Query<(Entity, &mut DeathScreen)>,
) {
for (entity, mut death_screen) in query.iter_mut() {
death_screen.remove_timer.tick(time.delta());
if death_screen.remove_timer.finished() {
commands.entity(entity).despawn_recursive();
next_state.set(AppState::MainMenu);
}
}
}

pub struct DeathScreenPlugin;

impl Plugin for DeathScreenPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::Death), spawn_death_screen)
.add_systems(
Update,
remove_death_screen.run_if(in_state(AppState::Death)),
);
}
}
2 changes: 1 addition & 1 deletion src/gameplay/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn enemy_attack(
EnemyState::ReadyBlade => {
enemy_attack_fx(&mut commands, &asset_server, mixer);
if player_health.damage(1.0) {
next_state.set(AppState::MainMenu);
next_state.set(AppState::Death);
}
EnemyState::SwingBlade
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use bevy_rand::prelude::EntropyPlugin;

mod cam;
mod controls;
mod death_screen;
mod ext;
mod gameplay;
mod ldtk;
Expand Down Expand Up @@ -54,6 +55,7 @@ fn main() {
.add_plugins(EntropyPlugin::<WyRand>::default())
.add_plugins(cam::CamPlugin)
.add_plugins(controls::ControlPlugin)
.add_plugins(death_screen::DeathScreenPlugin)
.add_plugins(gameplay::GameplayPlugin)
.add_plugins(ldtk::LdtkPlugin)
.add_plugins(mainmenu::MainMenuPlugin)
Expand Down
1 change: 1 addition & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum AppState {
MainMenu,
Paused,
InGame,
Death,
}

pub const ON_ENTER_GAMEPLAY: OnTransition<AppState> = OnTransition {
Expand Down

0 comments on commit fdc838a

Please sign in to comment.