Skip to content

Commit 744b8e2

Browse files
committed
add game over screen
1 parent 5bac9d6 commit 744b8e2

File tree

8 files changed

+255
-20
lines changed

8 files changed

+255
-20
lines changed

src/game/audio/sfx.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub(super) fn plugin(app: &mut App) {
1414
app.insert_resource(SfxPlaying { states: vec![] });
1515
app.observe(play_looping_sfx);
1616
app.observe(stop_looping_sfx);
17+
app.observe(stop_all_looping_sfx);
1718
}
1819

1920
#[derive(Resource)]
@@ -112,6 +113,30 @@ fn stop_looping_sfx(
112113
}
113114
}
114115

116+
fn stop_all_looping_sfx(
117+
_trigger: Trigger<StopAllLoopingSfx>,
118+
mut commands: Commands,
119+
audio: Query<(Entity, &LoopingSfx)>,
120+
mut sfx_playing: ResMut<SfxPlaying>,
121+
) {
122+
let mut s = sfx_playing.states.clone();
123+
let states = sfx_playing.states.iter_mut();
124+
125+
for (sfx_key, playing) in states {
126+
*playing = false;
127+
128+
for (entity, sfx) in audio.iter() {
129+
if sfx == &LoopingSfx(*sfx_key) {
130+
s.retain(|(key, _)| *key != *sfx_key);
131+
let e = commands.get_entity(entity);
132+
if let Some(e) = e {
133+
e.despawn_recursive();
134+
}
135+
}
136+
}
137+
}
138+
}
139+
115140
fn play_sfx(
116141
trigger: Trigger<PlaySfx>,
117142
mut commands: Commands,
@@ -147,6 +172,9 @@ pub enum StopLoopingSfx {
147172
Key(SfxKey),
148173
}
149174

175+
#[derive(Event)]
176+
pub struct StopAllLoopingSfx;
177+
150178
fn random_step() -> SfxKey {
151179
[SfxKey::Step1, SfxKey::Step2, SfxKey::Step3, SfxKey::Step4]
152180
.choose(&mut rand::thread_rng())

src/game/movement.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use super::{
1010
audio::sfx::PlaySfx,
1111
spawn::clock::{Clock, ClockController, Interactable, Positions},
1212
};
13-
use crate::{screen::Screen, AppSet};
13+
use crate::{
14+
screen::{PlayingState, Screen},
15+
AppSet,
16+
};
1417

1518
pub(super) fn plugin(app: &mut App) {
1619
// Record directional input as movement controls.
@@ -19,7 +22,8 @@ pub(super) fn plugin(app: &mut App) {
1922
Update,
2023
movement
2124
.in_set(AppSet::RecordInput)
22-
.run_if(in_state(Screen::Playing)),
25+
.run_if(in_state(Screen::Playing))
26+
.run_if(in_state(PlayingState::Playing)),
2327
);
2428
}
2529

src/game/spawn/clock.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use crate::{
55
assets::{HandleMap, ImageKey, SfxKey},
66
audio::sfx::{PlayLoopingSfx, PlaySfx, StopLoopingSfx},
77
},
8-
screen::Screen,
8+
screen::{PlayingState, Screen},
99
AppSet,
1010
};
1111

12-
use super::level::Score;
12+
use super::level::{Score, Scoresource};
1313

1414
pub(super) fn plugin(app: &mut App) {
1515
app.observe(spawn_interact_clock);
@@ -19,19 +19,22 @@ pub(super) fn plugin(app: &mut App) {
1919
(tick_clocks, score_clocks)
2020
.chain()
2121
.in_set(AppSet::FixedUpdate)
22-
.run_if(in_state(Screen::Playing)),
22+
.run_if(in_state(Screen::Playing))
23+
.run_if(in_state(PlayingState::Playing)),
2324
);
2425
app.add_systems(
2526
Update,
2627
record_clock_controller
2728
.in_set(AppSet::RecordInput)
28-
.run_if(in_state(Screen::Playing)),
29+
.run_if(in_state(Screen::Playing))
30+
.run_if(in_state(PlayingState::Playing)),
2931
);
3032
app.add_systems(
3133
FixedUpdate,
3234
apply_clock_control
3335
.in_set(AppSet::Update)
34-
.run_if(in_state(Screen::Playing)),
36+
.run_if(in_state(Screen::Playing))
37+
.run_if(in_state(PlayingState::Playing)),
3538
);
3639
app.insert_resource(Positions {
3740
clock_spawn: Vec2::new(-550.0, -185.0),
@@ -299,6 +302,7 @@ fn score_clocks(
299302
mut score: Query<(&mut Score, &mut Text)>,
300303
clocks: Query<(&Clock, &Children)>,
301304
clock_children: Query<(&Transform, &ClockHandType)>,
305+
mut scoresource: ResMut<Scoresource>,
302306
) {
303307
let main = clocks.iter().find(|(clock, _)| clock.is_main).unwrap();
304308
let main_rotations = get_clock_rotations(main.1, &clock_children);
@@ -352,6 +356,7 @@ fn score_clocks(
352356
_ => {}
353357
}
354358
text.sections[0].value = format!("{:.0}", score.0);
359+
scoresource.0 = score.0;
355360
}
356361

357362
struct ClockRotations {
@@ -511,7 +516,7 @@ fn spawn_interact_clock(
511516
SpriteBundle {
512517
texture: image_handles[&ImageKey::ClockMinute].clone_weak(),
513518
transform: Transform {
514-
translation: Vec3::new(0.0, 0.0, 400.0),
519+
translation: Vec3::new(0.0, 0.0, 350.0),
515520
..default()
516521
},
517522
sprite: Sprite {

src/game/spawn/level.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy::prelude::*;
44

55
use crate::{
66
game::assets::{HandleMap, ImageKey},
7-
screen::Screen,
7+
screen::{PlayingState, Screen},
88
};
99

1010
use super::clock::{Positions, SpawnClock, SpawnMainClock};
@@ -18,6 +18,8 @@ pub(super) fn plugin(app: &mut App) {
1818
app.observe(spawn_clock_table);
1919
app.observe(spawn_oil_table);
2020
app.observe(spawn_background);
21+
22+
app.insert_resource(Scoresource(0.0));
2123
}
2224

2325
#[derive(Event, Debug)]
@@ -41,6 +43,9 @@ pub struct SpawnOilTable;
4143
#[derive(Component)]
4244
pub struct Score(pub f32);
4345

46+
#[derive(Resource)]
47+
pub struct Scoresource(pub f32);
48+
4449
#[derive(Event, Debug)]
4550
pub struct SpawnBackground;
4651

@@ -63,7 +68,11 @@ fn spawn_background(
6368
});
6469
}
6570

66-
fn spawn_level(_trigger: Trigger<SpawnLevel>, mut commands: Commands) {
71+
fn spawn_level(
72+
_trigger: Trigger<SpawnLevel>,
73+
mut commands: Commands,
74+
mut scoresource: ResMut<Scoresource>,
75+
) {
6776
commands.trigger(SpawnBackground);
6877
commands.trigger(SpawnPlayer);
6978
commands.trigger(SpawnTable);
@@ -73,6 +82,7 @@ fn spawn_level(_trigger: Trigger<SpawnLevel>, mut commands: Commands) {
7382
commands.trigger(SpawnScore);
7483
commands.trigger(SpawnClockTable);
7584
commands.trigger(SpawnOilTable);
85+
scoresource.0 = 0.0;
7686
}
7787

7888
fn spawn_table(

src/game/spawn/player.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@ use crate::{
66
audio::sfx::{PlayLoopingSfx, StopLoopingSfx},
77
movement::MovementController,
88
},
9-
screen::Screen,
9+
screen::{PlayingState, Screen},
1010
};
1111

1212
use super::clock::ClockController;
1313

1414
pub(super) fn plugin(app: &mut App) {
1515
app.observe(spawn_player);
1616
app.register_type::<Player>();
17-
app.add_systems(Update, oil_leak.run_if(in_state(Screen::Playing)));
18-
app.add_systems(Update, oil_drink.run_if(in_state(Screen::Playing)));
17+
app.add_systems(
18+
Update,
19+
oil_leak
20+
.run_if(in_state(Screen::Playing))
21+
.run_if(in_state(PlayingState::Playing)),
22+
);
23+
app.add_systems(
24+
Update,
25+
oil_drink
26+
.run_if(in_state(Screen::Playing))
27+
.run_if(in_state(PlayingState::Playing)),
28+
);
1929
}
2030

2131
#[derive(Event, Debug)]
@@ -53,11 +63,13 @@ fn oil_leak(
5363
mut query: Query<(&mut Handle<Image>, &mut Sprite), With<OilMeter>>,
5464
time: Res<Time>,
5565
images: Res<HandleMap<ImageKey>>,
66+
mut next_state: ResMut<NextState<PlayingState>>,
5667
) {
5768
let mut controller = controller.single_mut();
5869
controller.oil_level -= time.delta_seconds() * controller.oil_leak;
5970
if controller.oil_level <= 0.0 {
6071
println!("Game over!");
72+
next_state.set(PlayingState::GameOver);
6173
return;
6274
}
6375
controller.oil_leak += time.delta_seconds() * 0.01;

src/screen/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ pub enum Screen {
3131
Credits,
3232
Playing,
3333
}
34+
35+
#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
36+
pub enum PlayingState {
37+
Playing,
38+
GameOver,
39+
Disabled,
40+
}

0 commit comments

Comments
 (0)