Skip to content

Commit a70ca5b

Browse files
committed
Fix various bugs
1 parent 7d36ffc commit a70ca5b

File tree

8 files changed

+91
-57
lines changed

8 files changed

+91
-57
lines changed

src/game/actor.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use crate::game::actor::movement::MovementController;
3030
use crate::game::actor::movement::OldMovementController;
3131
use crate::game::audio::music::Beat;
3232
use crate::game::card::deck::Deck;
33-
use crate::game::combat::death::DespawnOnDeath;
3433
use crate::game::combat::hit::Hurtbox;
3534
use crate::game::sprite::SpriteAnimation;
3635
use crate::util::prelude::*;
@@ -142,8 +141,7 @@ impl EntityCommand for Actor {
142141
AttackController::default(),
143142
self.health,
144143
Hurtbox,
145-
// TODO: Death animation instead, despawn when it's finished.
146-
DespawnOnDeath,
144+
// TODO: Death animation.
147145
),
148146
// Inventory:
149147
(Level::default(), Xp::default(), self.xp_reward, self.deck),

src/game/actor/enemy.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use bevy::prelude::*;
55
use crate::game::actor::facing::FacePlayer;
66
use crate::game::actor::faction::Faction;
77
use crate::game::actor::ActorConfig;
8+
use crate::game::combat::death::DespawnOnDeath;
89
use crate::game::GameLayer;
910
use crate::game::GameRoot;
1011
use crate::util::prelude::*;
@@ -41,6 +42,8 @@ pub fn enemy(key: impl Into<String>) -> impl EntityCommand<World> {
4142
Faction::Enemy,
4243
CollisionLayers::new(GameLayer::Enemy, LayerMask::ALL),
4344
FacePlayer,
45+
// TODO: Despawn when death animation is finished, instead.
46+
DespawnOnDeath,
4447
))
4548
.set_parent(parent);
4649
}

src/game/audio/music.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use pyri_state::prelude::*;
55
use crate::core::pause::Pause;
66
use crate::core::UpdateSet;
77
use crate::game::audio::AudioConfig;
8+
use crate::screen::playing::PlayingAssets;
89
use crate::screen::Screen;
910
use crate::util::prelude::*;
1011

@@ -36,17 +37,22 @@ pub fn stop_music(
3637
mut audio_instances: ResMut<Assets<AudioInstance>>,
3738
) {
3839
let music = r!(audio_instances.get_mut(&music_handle.0));
39-
music.seek_to(0.0);
40-
music.pause(AudioTween::default());
40+
music.stop(AudioTween::default());
4141
}
4242

4343
pub fn start_music(
44-
music_handle: Res<MusicHandle>,
45-
mut audio_instances: ResMut<Assets<AudioInstance>>,
44+
config: ConfigRef<AudioConfig>,
45+
audio: Res<Audio>,
46+
assets: Res<PlayingAssets>,
47+
mut music_handle: ResMut<MusicHandle>,
4648
) {
47-
let music = r!(audio_instances.get_mut(&music_handle.0));
48-
music.seek_to(0.0);
49-
music.resume(AudioTween::default());
49+
let config = r!(config.get());
50+
music_handle.0 = audio
51+
.play(assets.music.clone())
52+
.with_volume(config.music_volume)
53+
.loop_from(config.music_loop_start)
54+
.loop_until(config.music_loop_end)
55+
.handle();
5056
}
5157

5258
pub fn pause_music(

src/game/combat/death.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ impl Configure for DespawnOnDeath {
4040
}
4141
}
4242

43-
fn despawn_on_death(trigger: Trigger<OnDeath>, mut despawn: ResMut<LateDespawn>) {
44-
despawn.recursive(r!(trigger.get_entity()));
43+
fn despawn_on_death(
44+
trigger: Trigger<OnDeath>,
45+
despawn_query: Query<(), With<DespawnOnDeath>>,
46+
mut despawn: ResMut<LateDespawn>,
47+
) {
48+
let entity = r!(trigger.get_entity());
49+
if despawn_query.contains(entity) {
50+
despawn.recursive(entity);
51+
}
4552
}
4653

4754
#[derive(Component, Reflect)]

src/screen/playing.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ use crate::game::wave::wave;
2222
use crate::game::GameRoot;
2323
use crate::screen::fade_in;
2424
use crate::screen::playing::hud::playing_hud;
25-
use crate::screen::playing::victory_menu::reset_endless_mode;
2625
use crate::screen::Screen;
2726
use crate::ui::prelude::*;
2827
use crate::util::prelude::*;
2928

3029
pub(super) fn plugin(app: &mut App) {
3130
app.add_systems(
3231
StateFlush,
33-
Screen::Playing.on_edge(stop_music, (enter_playing, start_music, reset_endless_mode)),
32+
Screen::Playing.on_edge(stop_music, (enter_playing, start_music)),
3433
);
3534

3635
app.configure::<(PlayingAssets, PlayingAction, PlayingMenu)>();
@@ -127,15 +126,18 @@ impl Configure for PlayingAction {
127126
app.add_plugins(InputManagerPlugin::<Self>::default());
128127
app.add_systems(
129128
StateFlush,
130-
PlayingMenu::Pause
131-
.toggle()
132-
.in_set(ResolveStateSet::<PlayingMenu>::Compute)
133-
.run_if(
134-
Screen::Playing
135-
.will_exit()
136-
.and_then(not(PlayingMenu::LevelUp.will_exit()))
137-
.and_then(action_just_pressed(Self::TogglePause)),
138-
),
129+
(
130+
Screen::Playing.on_exit(PlayingMenu::disable),
131+
PlayingMenu::Pause
132+
.toggle()
133+
.in_set(ResolveStateSet::<PlayingMenu>::Compute)
134+
.run_if(
135+
PlayingMenu::is_disabled
136+
.or_else(PlayingMenu::Pause.will_exit())
137+
.and_then(Screen::Playing.will_enter())
138+
.and_then(action_just_pressed(Self::TogglePause)),
139+
),
140+
),
139141
);
140142
}
141143
}

src/screen/playing/defeat_menu.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use pyri_state::extra::entity_scope::StateScope;
55
use pyri_state::prelude::*;
66

77
use crate::core::pause::Pause;
8-
use crate::core::UpdateSet;
98
use crate::game::actor::player::IsPlayer;
9+
use crate::game::combat::death::OnDeath;
1010
use crate::screen::fade_out;
1111
use crate::screen::playing::PlayingAssets;
1212
use crate::screen::playing::PlayingMenu;
@@ -15,29 +15,32 @@ use crate::ui::prelude::*;
1515
use crate::util::prelude::*;
1616

1717
pub(super) fn plugin(app: &mut App) {
18+
app.observe(detect_defeat);
19+
1820
app.add_systems(
1921
StateFlush,
2022
PlayingMenu::Defeat.on_edge(Pause::disable, (Pause::enable_default, open_defeat_menu)),
2123
);
24+
}
2225

23-
app.add_systems(
24-
Update,
25-
PlayingMenu::Defeat
26-
.enter()
27-
.in_set(UpdateSet::SyncLate)
28-
.run_if(detect_defeat),
29-
);
26+
fn detect_defeat(
27+
trigger: Trigger<OnDeath>,
28+
player_query: Query<(), With<IsPlayer>>,
29+
mut playing_menu: NextMut<PlayingMenu>,
30+
) {
31+
let entity = r!(trigger.get_entity());
32+
if !player_query.contains(entity) {
33+
return;
34+
}
35+
36+
playing_menu.enter(PlayingMenu::Defeat);
3037
}
3138

3239
fn open_defeat_menu(mut commands: Commands, ui_root: Res<UiRoot>) {
3340
commands.spawn_with(defeat_overlay).set_parent(ui_root.body);
3441
commands.spawn_with(defeat_menu).set_parent(ui_root.body);
3542
}
3643

37-
pub fn detect_defeat(player_query: Query<Entity, With<IsPlayer>>) -> bool {
38-
player_query.is_empty()
39-
}
40-
4144
fn defeat_overlay(mut entity: EntityWorldMut) {
4245
entity.add(widget::blocking_overlay).insert((
4346
Name::new("DefeatOverlay"),
@@ -126,7 +129,9 @@ fn restart_button(mut entity: EntityWorldMut) {
126129

127130
fn quit_to_title_button(mut entity: EntityWorldMut) {
128131
entity.add(widget::menu_button("Quit to title")).insert((
129-
On::<Pointer<Click>>::run(Screen::Title.enter()),
132+
On::<Pointer<Click>>::run(|mut commands: Commands| {
133+
commands.spawn_with(fade_out(Screen::Title));
134+
}),
130135
Style {
131136
height: Vw(9.0),
132137
width: Vw(38.0),

src/screen/playing/pause_menu.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn pause_menu(mut entity: EntityWorldMut) {
5151
});
5252
}
5353

54-
const HEADER: &str = "Paused :)";
54+
const HEADER: &str = "Paused :|";
5555

5656
fn header(mut entity: EntityWorldMut) {
5757
entity.insert((
@@ -133,7 +133,9 @@ fn restart_button(mut entity: EntityWorldMut) {
133133

134134
fn quit_to_title_button(mut entity: EntityWorldMut) {
135135
entity.add(widget::menu_button("Quit to title")).insert((
136-
On::<Pointer<Click>>::run(Screen::Title.enter()),
136+
On::<Pointer<Click>>::run(|mut commands: Commands| {
137+
commands.spawn_with(fade_out(Screen::Title));
138+
}),
137139
Style {
138140
height: Vw(9.0),
139141
width: Vw(38.0),

src/screen/playing/victory_menu.rs

+30-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use pyri_state::prelude::*;
77
use crate::core::pause::Pause;
88
use crate::core::UpdateSet;
99
use crate::game::actor::level::up::LevelUp;
10+
use crate::game::actor::level::IsLevelDisplay;
1011
use crate::game::actor::level::Level;
1112
use crate::screen::fade_out;
1213
use crate::screen::playing::PlayingAssets;
@@ -21,7 +22,7 @@ pub(super) fn plugin(app: &mut App) {
2122
PlayingMenu::Victory.on_edge(Pause::disable, (Pause::enable_default, open_victory_menu)),
2223
);
2324

24-
app.init_resource::<EndlessMode>();
25+
app.configure::<EndlessMode>();
2526

2627
app.add_systems(
2728
Update,
@@ -32,28 +33,40 @@ pub(super) fn plugin(app: &mut App) {
3233
);
3334
}
3435

35-
#[derive(Default, Resource)]
36+
pub fn detect_victory(
37+
level_display_query: Query<&Selection, With<IsLevelDisplay>>,
38+
level_query: Query<&Level>,
39+
endless_mode: Res<EndlessMode>,
40+
) -> bool {
41+
let selection = r!(false, level_display_query.get_single());
42+
let level = r!(false, level_query.get(selection.0));
43+
44+
!endless_mode.0 && level.current >= 10
45+
}
46+
47+
#[derive(Resource, Reflect, Default)]
48+
#[reflect(Resource)]
3649
pub struct EndlessMode(bool);
3750

51+
impl Configure for EndlessMode {
52+
fn configure(app: &mut App) {
53+
app.register_type::<Self>();
54+
app.init_resource::<Self>();
55+
app.add_systems(StateFlush, Screen::Playing.on_enter(reset_endless_mode));
56+
}
57+
}
58+
59+
fn reset_endless_mode(mut endless_mode: ResMut<EndlessMode>) {
60+
endless_mode.0 = false;
61+
}
62+
3863
fn open_victory_menu(mut commands: Commands, ui_root: Res<UiRoot>) {
3964
commands
4065
.spawn_with(victory_overlay)
4166
.set_parent(ui_root.body);
4267
commands.spawn_with(victory_menu).set_parent(ui_root.body);
4368
}
4469

45-
pub fn detect_victory(level_query: Query<&Level>, endless_mode: Res<EndlessMode>) -> bool {
46-
if endless_mode.0 {
47-
return false;
48-
}
49-
50-
if let Some(level) = level_query.iter().last() {
51-
return level.current >= 10;
52-
}
53-
54-
false
55-
}
56-
5770
fn victory_overlay(mut entity: EntityWorldMut) {
5871
entity.add(widget::blocking_overlay).insert((
5972
Name::new("VictoryOverlay"),
@@ -123,10 +136,6 @@ fn button_container(mut entity: EntityWorldMut) {
123136
});
124137
}
125138

126-
pub fn reset_endless_mode(mut endless_mode: ResMut<EndlessMode>) {
127-
endless_mode.0 = false;
128-
}
129-
130139
fn keep_playing_button(mut entity: EntityWorldMut) {
131140
entity.add(widget::menu_button("Keep Playing")).insert((
132141
On::<Pointer<Click>>::run(
@@ -165,7 +174,9 @@ fn restart_button(mut entity: EntityWorldMut) {
165174

166175
fn quit_to_title_button(mut entity: EntityWorldMut) {
167176
entity.add(widget::menu_button("Quit to title")).insert((
168-
On::<Pointer<Click>>::run(Screen::Title.enter()),
177+
On::<Pointer<Click>>::run(|mut commands: Commands| {
178+
commands.spawn_with(fade_out(Screen::Title));
179+
}),
169180
Style {
170181
height: Vw(9.0),
171182
width: Vw(38.0),

0 commit comments

Comments
 (0)