Skip to content

Commit

Permalink
Fix various bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 28, 2024
1 parent 7d36ffc commit 7f29752
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 37 deletions.
4 changes: 1 addition & 3 deletions src/game/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::game::actor::movement::MovementController;
use crate::game::actor::movement::OldMovementController;
use crate::game::audio::music::Beat;
use crate::game::card::deck::Deck;
use crate::game::combat::death::DespawnOnDeath;
use crate::game::combat::hit::Hurtbox;
use crate::game::sprite::SpriteAnimation;
use crate::util::prelude::*;
Expand Down Expand Up @@ -142,8 +141,7 @@ impl EntityCommand for Actor {
AttackController::default(),
self.health,
Hurtbox,
// TODO: Death animation instead, despawn when it's finished.
DespawnOnDeath,
// TODO: Death animation.
),
// Inventory:
(Level::default(), Xp::default(), self.xp_reward, self.deck),
Expand Down
3 changes: 3 additions & 0 deletions src/game/actor/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy::prelude::*;
use crate::game::actor::facing::FacePlayer;
use crate::game::actor::faction::Faction;
use crate::game::actor::ActorConfig;
use crate::game::combat::death::DespawnOnDeath;
use crate::game::GameLayer;
use crate::game::GameRoot;
use crate::util::prelude::*;
Expand Down Expand Up @@ -41,6 +42,8 @@ pub fn enemy(key: impl Into<String>) -> impl EntityCommand<World> {
Faction::Enemy,
CollisionLayers::new(GameLayer::Enemy, LayerMask::ALL),
FacePlayer,
// TODO: Despawn when death animation is finished, instead.
DespawnOnDeath,
))
.set_parent(parent);
}
Expand Down
20 changes: 13 additions & 7 deletions src/game/audio/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pyri_state::prelude::*;
use crate::core::pause::Pause;
use crate::core::UpdateSet;
use crate::game::audio::AudioConfig;
use crate::screen::playing::PlayingAssets;
use crate::screen::Screen;
use crate::util::prelude::*;

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

pub fn start_music(
music_handle: Res<MusicHandle>,
mut audio_instances: ResMut<Assets<AudioInstance>>,
config: ConfigRef<AudioConfig>,
audio: Res<Audio>,
assets: Res<PlayingAssets>,
mut music_handle: ResMut<MusicHandle>,
) {
let music = r!(audio_instances.get_mut(&music_handle.0));
music.seek_to(0.0);
music.resume(AudioTween::default());
let config = r!(config.get());
music_handle.0 = audio
.play(assets.music.clone())
.with_volume(config.music_volume)
.loop_from(config.music_loop_start)
.loop_until(config.music_loop_end)
.handle();
}

pub fn pause_music(
Expand Down
11 changes: 9 additions & 2 deletions src/game/combat/death.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ impl Configure for DespawnOnDeath {
}
}

fn despawn_on_death(trigger: Trigger<OnDeath>, mut despawn: ResMut<LateDespawn>) {
despawn.recursive(r!(trigger.get_entity()));
fn despawn_on_death(
trigger: Trigger<OnDeath>,
despawn_query: Query<(), With<DespawnOnDeath>>,
mut despawn: ResMut<LateDespawn>,
) {
let entity = r!(trigger.get_entity());
if despawn_query.contains(entity) {
despawn.recursive(entity);
}
}

#[derive(Component, Reflect)]
Expand Down
21 changes: 12 additions & 9 deletions src/screen/playing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,18 @@ impl Configure for PlayingAction {
app.add_plugins(InputManagerPlugin::<Self>::default());
app.add_systems(
StateFlush,
PlayingMenu::Pause
.toggle()
.in_set(ResolveStateSet::<PlayingMenu>::Compute)
.run_if(
Screen::Playing
.will_exit()
.and_then(not(PlayingMenu::LevelUp.will_exit()))
.and_then(action_just_pressed(Self::TogglePause)),
),
(
Screen::Playing.on_exit(PlayingMenu::disable),
PlayingMenu::Pause
.toggle()
.in_set(ResolveStateSet::<PlayingMenu>::Compute)
.run_if(
PlayingMenu::is_disabled
.or_else(PlayingMenu::Pause.will_exit())
.and_then(Screen::Playing.will_enter())
.and_then(action_just_pressed(Self::TogglePause)),
),
),
);
}
}
Expand Down
31 changes: 18 additions & 13 deletions src/screen/playing/defeat_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use pyri_state::extra::entity_scope::StateScope;
use pyri_state::prelude::*;

use crate::core::pause::Pause;
use crate::core::UpdateSet;
use crate::game::actor::player::IsPlayer;
use crate::game::combat::death::OnDeath;
use crate::screen::fade_out;
use crate::screen::playing::PlayingAssets;
use crate::screen::playing::PlayingMenu;
Expand All @@ -15,29 +15,32 @@ use crate::ui::prelude::*;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.observe(detect_defeat);

app.add_systems(
StateFlush,
PlayingMenu::Defeat.on_edge(Pause::disable, (Pause::enable_default, open_defeat_menu)),
);
}

app.add_systems(
Update,
PlayingMenu::Defeat
.enter()
.in_set(UpdateSet::SyncLate)
.run_if(detect_defeat),
);
fn detect_defeat(
trigger: Trigger<OnDeath>,
player_query: Query<(), With<IsPlayer>>,
mut playing_menu: NextMut<PlayingMenu>,
) {
let entity = r!(trigger.get_entity());
if !player_query.contains(entity) {
return;
}

playing_menu.enter(PlayingMenu::Defeat);
}

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

pub fn detect_defeat(player_query: Query<Entity, With<IsPlayer>>) -> bool {
player_query.is_empty()
}

fn defeat_overlay(mut entity: EntityWorldMut) {
entity.add(widget::blocking_overlay).insert((
Name::new("DefeatOverlay"),
Expand Down Expand Up @@ -126,7 +129,9 @@ fn restart_button(mut entity: EntityWorldMut) {

fn quit_to_title_button(mut entity: EntityWorldMut) {
entity.add(widget::menu_button("Quit to title")).insert((
On::<Pointer<Click>>::run(Screen::Title.enter()),
On::<Pointer<Click>>::run(|mut commands: Commands| {
commands.spawn_with(fade_out(Screen::Title));
}),
Style {
height: Vw(9.0),
width: Vw(38.0),
Expand Down
6 changes: 4 additions & 2 deletions src/screen/playing/pause_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn pause_menu(mut entity: EntityWorldMut) {
});
}

const HEADER: &str = "Paused :)";
const HEADER: &str = "Paused :|";

fn header(mut entity: EntityWorldMut) {
entity.insert((
Expand Down Expand Up @@ -133,7 +133,9 @@ fn restart_button(mut entity: EntityWorldMut) {

fn quit_to_title_button(mut entity: EntityWorldMut) {
entity.add(widget::menu_button("Quit to title")).insert((
On::<Pointer<Click>>::run(Screen::Title.enter()),
On::<Pointer<Click>>::run(|mut commands: Commands| {
commands.spawn_with(fade_out(Screen::Title));
}),
Style {
height: Vw(9.0),
width: Vw(38.0),
Expand Down
4 changes: 3 additions & 1 deletion src/screen/playing/victory_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ fn restart_button(mut entity: EntityWorldMut) {

fn quit_to_title_button(mut entity: EntityWorldMut) {
entity.add(widget::menu_button("Quit to title")).insert((
On::<Pointer<Click>>::run(Screen::Title.enter()),
On::<Pointer<Click>>::run(|mut commands: Commands| {
commands.spawn_with(fade_out(Screen::Title));
}),
Style {
height: Vw(9.0),
width: Vw(38.0),
Expand Down

0 comments on commit 7f29752

Please sign in to comment.