Skip to content

Commit 96c900e

Browse files
committed
feat: Add time played to win screen
1 parent 09c6696 commit 96c900e

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

CREDITS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ https://pimen.itch.io/fire-spell-effect-02
5858

5959
https://pimen.itch.io/smoke-vfx-1
6060

61+
### Portal
62+
63+
https://elthen.itch.io/2d-pixel-art-portal-sprites
64+
6165
---
6266

6367
## UI

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "magus-parvus"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/player/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod input;
2+
pub mod speed_timer;
23
pub mod state;
34
pub mod stats;
45

@@ -32,6 +33,7 @@ impl Plugin for PlayerPlugin {
3233
collision::PlayerCollisionPlugin,
3334
movement::PlayerMovementPlugin,
3435
spawn::PlayerSpawnPlugin,
36+
speed_timer::SpeedTimerPlugin,
3537
));
3638
}
3739
}

src/player/speed_timer.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use bevy::prelude::*;
2+
3+
use crate::GameState;
4+
5+
#[derive(Resource, Default)]
6+
pub struct SpeedTimer {
7+
pub elapsed: f32,
8+
}
9+
10+
fn tick(time: Res<Time>, mut speed_timer: ResMut<SpeedTimer>) {
11+
speed_timer.elapsed += time.delta_seconds();
12+
}
13+
14+
pub struct SpeedTimerPlugin;
15+
16+
impl Plugin for SpeedTimerPlugin {
17+
fn build(&self, app: &mut App) {
18+
app.init_resource::<SpeedTimer>()
19+
.add_systems(Update, (tick,).run_if(in_state(GameState::Gaming)));
20+
}
21+
}

src/ui/win_ui.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use bevy::prelude::*;
22

33
use crate::{
44
audio::{GameAudio, PlaySound},
5+
player::speed_timer::SpeedTimer,
56
GameAssets, GameState,
67
};
78

@@ -41,19 +42,45 @@ fn spawn_title(commands: &mut Commands, font: Handle<Font>) -> Entity {
4142
color: Color::WHITE,
4243
};
4344
let text_bundle =
44-
TextBundle::from_sections([TextSection::new("WIN".to_string(), text_style.clone())]);
45+
TextBundle::from_sections([TextSection::new("YOU WIN".to_string(), text_style.clone())]);
4546
commands.spawn((GameOverScreen, text_bundle)).id()
4647
}
4748

48-
fn spawn_text(commands: &mut Commands, font: Handle<Font>) {
49+
fn spawn_thank_you(commands: &mut Commands, font: Handle<Font>) -> Entity {
50+
let text_style = TextStyle {
51+
font,
52+
font_size: 40.0,
53+
color: Color::WHITE,
54+
};
55+
let text_bundle = TextBundle::from_sections([TextSection::new(
56+
"THANKS FOR PLAYING".to_string(),
57+
text_style.clone(),
58+
)]);
59+
commands.spawn((GameOverScreen, text_bundle)).id()
60+
}
61+
62+
fn spawn_time(commands: &mut Commands, font: Handle<Font>, time: f32) -> Entity {
63+
let text = format!("TIME: {:.2} seconds", time);
64+
let text_style = TextStyle {
65+
font,
66+
font_size: 60.0,
67+
color: Color::WHITE,
68+
};
69+
let text_bundle = TextBundle::from_sections([TextSection::new(text, text_style.clone())]);
70+
commands.spawn((GameOverScreen, text_bundle)).id()
71+
}
72+
73+
fn spawn_text(commands: &mut Commands, font: Handle<Font>, time: f32) {
4974
let title_text = spawn_title(commands, font.clone());
75+
let thank_you_text = spawn_thank_you(commands, font.clone());
76+
let time_text = spawn_time(commands, font.clone(), time);
5077

5178
commands
5279
.spawn((
5380
GameOverScreen,
5481
NodeBundle {
5582
style: Style {
56-
top: Val::Percent(35.0),
83+
top: Val::Percent(20.0),
5784
width: Val::Percent(100.0),
5885
flex_direction: FlexDirection::Column,
5986
row_gap: Val::Vh(10.0),
@@ -65,16 +92,16 @@ fn spawn_text(commands: &mut Commands, font: Handle<Font>) {
6592
..default()
6693
},
6794
))
68-
.push_children(&[title_text]);
95+
.push_children(&[title_text, thank_you_text, time_text]);
6996
}
7097

7198
fn spawn_audio_silence_timer(commands: &mut Commands) {
7299
commands.spawn(AudioSilenceTimer(Timer::from_seconds(0.1, TimerMode::Once)));
73100
}
74101

75-
fn spawn_win_screen(mut commands: Commands, assets: Res<GameAssets>) {
102+
fn spawn_win_screen(mut commands: Commands, assets: Res<GameAssets>, speed_timer: Res<SpeedTimer>) {
76103
spawn_background(&mut commands, assets.white_pixel.clone());
77-
spawn_text(&mut commands, assets.font.clone());
104+
spawn_text(&mut commands, assets.font.clone(), speed_timer.elapsed);
78105
spawn_audio_silence_timer(&mut commands);
79106
}
80107

0 commit comments

Comments
 (0)