Skip to content

Commit

Permalink
feat(sounds): added battle sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
pedryx committed Apr 15, 2024
1 parent fd3d0c5 commit 56e988f
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 16 deletions.
Binary file added assets/audio/battle/demon_dying.ogg
Binary file not shown.
Binary file added assets/audio/battle/demon_pain.ogg
Binary file not shown.
Binary file added assets/audio/battle/hurt_male.ogg
Binary file not shown.
Binary file added assets/audio/battle/man_dies.ogg
Binary file not shown.
Binary file added assets/audio/battle/monster_bite.ogg
Binary file not shown.
Binary file added assets/audio/battle/sword_sound.ogg
Binary file not shown.
8 changes: 7 additions & 1 deletion credits/CREDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
* Scary Of Horror by Baihaki (https://www.dafont.com/scary-of-horror.font)
* eerie_ambience by zimbot (https://freesound.org/s/122971/) [Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/)
* Spooky Halloween Soundtrack by MusicByMisterbates (https://freesound.org/s/677436/) [Creative Commons 0 License](https://creativecommons.org/publicdomain/zero/1.0/)
* Visible Sadness by theoctopus559 (https://freesound.org/s/720693/) [Attribution NonCommercial 4.0](https://creativecommons.org/licenses/by-nc/4.0/)
* Visible Sadness by theoctopus559 (https://freesound.org/s/720693/) [Attribution NonCommercial 4.0 License](https://creativecommons.org/licenses/by-nc/4.0/)
* monster bite by LucasDuff (https://freesound.org/s/467701/) [Creative Commons 0 License](https://creativecommons.org/publicdomain/zero/1.0/)
* Demon Pain by StormwaveAudio (https://freesound.org/s/614068/) [Attribution NonCommercial 4.0 License](https://creativecommons.org/licenses/by-nc/4.0/)
* Sword sound by Merrick079 (https://freesound.org/s/568169/) [Creative Commons 0 License](https://creativecommons.org/publicdomain/zero/1.0/)
* hurt male Christopherderp (https://freesound.org/s/342229/) [Creative Commons 0 License](https://creativecommons.org/publicdomain/zero/1.0/)
* demon dying by THE_bizniss (https://freesound.org/s/37823/) [Attribution 3.0 License](https://creativecommons.org/licenses/by/3.0/)
* Man dies by starkvind (https://freesound.org/s/559975/) [Creative Commons 0 License](https://creativecommons.org/publicdomain/zero/1.0/)
10 changes: 5 additions & 5 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::GameState;
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;

const GLOBAL_VOLUME: f64 = 1.0;
const SOUNDTRACK_GLOBAL_VOLUME: f64 = 1.6;

pub struct InternalAudioPlugin;

Expand Down Expand Up @@ -41,28 +41,28 @@ fn start_audio(
audio
.play(audio_assets.ambient.clone())
.looped()
.with_volume(GLOBAL_VOLUME * 0.01);
.with_volume(SOUNDTRACK_GLOBAL_VOLUME * 0.01);

// basic soundtrack
soundtrack.basic = audio
.play(audio_assets.soundtrack.clone())
.looped()
.with_volume(GLOBAL_VOLUME * 0.07)
.with_volume(SOUNDTRACK_GLOBAL_VOLUME * 0.07)
.handle();

// battle soundtrack
soundtrack.battle = audio
.play(audio_assets.battle_soundtrack.clone())
.looped()
.with_volume(GLOBAL_VOLUME * 0.07)
.with_volume(SOUNDTRACK_GLOBAL_VOLUME * 0.15)
.paused()
.handle();

// game over soundtrack
soundtrack.game_over = audio
.play(audio_assets.game_over_soundtrack.clone())
.looped()
.with_volume(GLOBAL_VOLUME * 0.07)
.with_volume(SOUNDTRACK_GLOBAL_VOLUME * 0.07)
.paused()
.handle();
}
20 changes: 16 additions & 4 deletions src/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ impl Plugin for BattlePlugin {
app.add_plugins(EffectsPlugin)
.add_event::<MinionAttackEvent>()
.add_event::<EnemyAttackEvent>()
.add_event::<MinionDiedEvent>()
.add_event::<EnemyDiedEvent>()
.add_systems(
OnEnter(GameScreen::Battle),
(prepare_battle, prepare_battle_screen),
Expand Down Expand Up @@ -68,6 +70,12 @@ pub struct EnemyAttackEvent {
target: Entity,
}

#[derive(Event)]
pub struct EnemyDiedEvent;

#[derive(Event)]
pub struct MinionDiedEvent;

fn prepare_battle(
mut commands: Commands,
mut minion_count: ResMut<MinionCount>,
Expand Down Expand Up @@ -114,11 +122,11 @@ pub fn update_battle(
if battle_participant.turn_accumulator >= 1. / stats.speed {
battle_participant.turn_accumulator -= 1. / stats.speed;

enemy_stats.current_hp -= stats.damage;
println!(
"minion attacking for {}, enemy has {} hp",
stats.damage, enemy_stats.current_hp
);
enemy_stats.current_hp -= stats.damage;
minion_attack_event.send(MinionAttackEvent {
attacker: entity,
target: enemy_entity,
Expand All @@ -135,11 +143,11 @@ pub fn update_battle(
.iter_mut()
.nth(battle_rng.0.gen_range(0..minion_count.0));
if let Some((entity, _, mut stats)) = target {
stats.current_hp -= enemy_stats.damage;
println!(
"enemy attacking for {}, minion has {} hp",
enemy_stats.damage, stats.current_hp
);
stats.current_hp -= enemy_stats.damage;
enemy_attack_event.send(EnemyAttackEvent {
attacker: enemy_entity,
target: entity,
Expand All @@ -151,6 +159,7 @@ pub fn update_battle(

fn handle_enemy_dead(
mut commands: Commands,
mut enemy_died_event: EventWriter<EnemyDiedEvent>,
mut next_screen: ResMut<NextState<GameScreen>>,
mut inventory_items: ResMut<InventoryItems>,
mut battle_count: ResMut<BattleCount>,
Expand All @@ -174,6 +183,8 @@ fn handle_enemy_dead(
}
}

enemy_died_event.send(EnemyDiedEvent);

// battle win
battle_count.0 += 1;
commands.entity(entity).despawn_recursive();
Expand All @@ -182,10 +193,10 @@ fn handle_enemy_dead(

fn handle_minion_dead(
mut commands: Commands,
mut minion_died_event: EventWriter<MinionDiedEvent>,
mut next_screen: ResMut<NextState<GameScreen>>,
mut next_state: ResMut<NextState<GameState>>,
mut minion_count: ResMut<MinionCount>,
mut inventory_items: ResMut<InventoryItems>,
minion_query: Query<(Entity, &Stats), With<Minion>>,
enemy_query: Query<Entity, With<Enemy>>,
) {
Expand All @@ -197,9 +208,10 @@ fn handle_minion_dead(
commands.entity(entity).despawn_recursive();
minion_count.0 -= 1;

minion_died_event.send(MinionDiedEvent);

// game over
if minion_count.0 == 0 {
inventory_items.0.clear();
if enemy_query.iter().next().is_some() {
commands.entity(enemy_query.single()).despawn_recursive();
}
Expand Down
72 changes: 69 additions & 3 deletions src/battle/effects.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::time::Duration;

use bevy::prelude::*;
use bevy_kira_audio::{Audio, AudioControl};
use bevy_tweening::{
lens::{SpriteColorLens, TransformPositionLens},
Animator, EaseFunction, RepeatCount, RepeatStrategy, Tween,
};

use crate::{GameScreen, GameState};
use crate::{loading::AudioAssets, GameScreen, GameState};

use super::{EnemyAttackEvent, MinionAttackEvent};
use super::{EnemyAttackEvent, EnemyDiedEvent, MinionAttackEvent, MinionDiedEvent};

const ATTACK_DURATION: f32 = 0.2;
const MINION_ATTACK_OFFSET: Vec2 = Vec2::new(30., 0.);
Expand All @@ -25,24 +26,63 @@ impl Plugin for EffectsPlugin {
Update,
(
handle_minion_attack_effect,
handle_minion_hurt_effect,
handle_enemy_attack_effect,
handle_enemy_hurt_effect,
handle_minion_hurt_effect,
)
.run_if(in_state(GameScreen::Battle).and_then(in_state(GameState::Playing))),
)
.add_systems(
Update,
(handle_minion_died_effect, handle_enemy_died_effect)
.run_if(in_state(GameState::Playing)),
);
}
}

fn handle_minion_died_effect(
mut minion_died_event: EventReader<MinionDiedEvent>,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
) {
for _ in minion_died_event.read() {
audio
.play(audio_assets.minion_died.clone())
.with_volume(1.0);
}
}

fn handle_enemy_died_effect(
mut enemy_died_event: EventReader<EnemyDiedEvent>,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
) {
for _ in enemy_died_event.read() {
audio
.play(audio_assets.enemy_died.clone())
.end_at(0.8)
.with_volume(1.0);
}
}

fn handle_minion_attack_effect(
mut commands: Commands,
mut minion_attack_event: EventReader<MinionAttackEvent>,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
query: Query<&Transform>,
) {
for event in minion_attack_event.read() {
let Ok(transform) = query.get(event.attacker) else {
continue;
};

//play sound
audio
.play(audio_assets.minion_attack.clone())
.with_volume(0.3);

// start tween
let position = transform.translation;

let tween = Tween::new(
Expand All @@ -63,6 +103,8 @@ fn handle_minion_attack_effect(
fn handle_enemy_attack_effect(
mut commands: Commands,
mut enemy_attack_event: EventReader<EnemyAttackEvent>,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
query: Query<&Transform>,
) {
for event in enemy_attack_event.read() {
Expand All @@ -71,6 +113,13 @@ fn handle_enemy_attack_effect(
};
let position = transform.translation;

// play sound
audio
.play(audio_assets.enemy_attack.clone())
.start_from(0.35)
.with_volume(0.3);

// start tween
let tween = Tween::new(
EaseFunction::QuadraticOut,
Duration::from_secs_f32(ATTACK_DURATION / 2.),
Expand All @@ -89,13 +138,22 @@ fn handle_enemy_attack_effect(
fn handle_minion_hurt_effect(
mut commands: Commands,
mut enemy_attack_event: EventReader<EnemyAttackEvent>,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
query: Query<()>,
) {
for event in enemy_attack_event.read() {
if query.get(event.target).is_err() {
continue;
}

// play sound
audio
.play(audio_assets.minion_hurt.clone())
.start_from(0.24)
.with_volume(0.9);

// start tween
let tween = Tween::new(
EaseFunction::QuadraticOut,
Duration::from_secs_f32(HURT_DURATION / 2.),
Expand All @@ -114,13 +172,21 @@ fn handle_minion_hurt_effect(
fn handle_enemy_hurt_effect(
mut commands: Commands,
mut minion_attack_event: EventReader<MinionAttackEvent>,
audio: Res<Audio>,
audio_assets: Res<AudioAssets>,
query: Query<()>,
) {
for event in minion_attack_event.read() {
if query.get(event.target).is_err() {
continue;
}

// play sound
audio
.play(audio_assets.enemy_hurt.clone())
.with_volume(0.15);

// start tween
let tween = Tween::new(
EaseFunction::QuadraticOut,
Duration::from_secs_f32(HURT_DURATION / 2.),
Expand Down
3 changes: 3 additions & 0 deletions src/game_over.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
loading::{FontAssets, TextureAssets},
mouse_control::Clickable,
statistics::Statistics,
summoning::InventoryItems,
BattleCount, GameState,
};

Expand Down Expand Up @@ -254,6 +255,7 @@ fn handle_menu_button(
mut next_state: ResMut<NextState<GameState>>,
mut battle_count: ResMut<BattleCount>,
mut statistics: ResMut<Statistics>,
mut inventory_items: ResMut<InventoryItems>,
query: Query<&Clickable, With<MenuButton>>,
) {
let clickable = query.single();
Expand All @@ -265,6 +267,7 @@ fn handle_menu_button(
battle_count.0 = 1;
statistics.elapsed_seconds = 0.;
statistics.summoned_minions = 0;
inventory_items.0.clear();

next_state.set(GameState::Menu);
}
14 changes: 14 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ pub struct FontAssets {

#[derive(AssetCollection, Resource)]
pub struct AudioAssets {
#[asset(path = "audio/battle/sword_sound.ogg")]
pub minion_attack: Handle<AudioSource>,
#[asset(path = "audio/battle/hurt_male.ogg")]
pub minion_hurt: Handle<AudioSource>,
#[asset(path = "audio/battle/man_dies.ogg")]
pub minion_died: Handle<AudioSource>,

#[asset(path = "audio/battle/monster_bite.ogg")]
pub enemy_attack: Handle<AudioSource>,
#[asset(path = "audio/battle/demon_pain.ogg")]
pub enemy_hurt: Handle<AudioSource>,
#[asset(path = "audio/battle/demon_dying.ogg")]
pub enemy_died: Handle<AudioSource>,

#[asset(path = "audio/eerie_ambience.ogg")]
pub ambient: Handle<AudioSource>,
#[asset(path = "audio/spooky_halloween_soundtrack.ogg")]
Expand Down
2 changes: 1 addition & 1 deletion src/planning_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn spawn_enemy_cards(
max_hp: hp_tier as f32 * 10.,
hp_regeneration: hp_regeneration_tier as f32 * 1.,
damage: damage_tier as f32 * 4.,
speed: speed_tier as f32 * 1.,
speed: speed_tier as f32 * 0.2,
..Default::default()
};

Expand Down
4 changes: 2 additions & 2 deletions src/summoning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ fn summon_minion(
let mut stats = Stats {
current_hp: 10.,
max_hp: 10.,
speed: 1.,
speed: 0.66666,
damage: 1.,
..Default::default()
};
Expand All @@ -437,7 +437,7 @@ fn summon_minion(
stats.current_hp += 10. * item.tier as f32;
}
SummoningItemType::HPRegeneration => stats.hp_regeneration += 0.5 * item.tier as f32,
SummoningItemType::Speed => stats.speed += 1. * item.tier as f32,
SummoningItemType::Speed => stats.speed += 0.1 * item.tier as f32,
SummoningItemType::Damage => stats.damage += 2. * item.tier as f32,
}
}
Expand Down

0 comments on commit 56e988f

Please sign in to comment.