From 38d3333635d03be7d8919a814cc059fb63c6803f Mon Sep 17 00:00:00 2001 From: Alex Page Date: Thu, 10 Feb 2022 17:40:00 -0500 Subject: [PATCH] Implement whipping blocks and trees --- src/graphics/sidebar.rs | 12 ++++++-- src/resources/sound_effects.rs | 29 +++++++++++-------- src/systems/whip.rs | 52 +++++++++++++++++++++++++--------- 3 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/graphics/sidebar.rs b/src/graphics/sidebar.rs index abb7634..765a64c 100644 --- a/src/graphics/sidebar.rs +++ b/src/graphics/sidebar.rs @@ -1,4 +1,4 @@ -use crate::constants::{SIDEBAR_POS_X, SIDEBAR_POS_Y}; +use crate::constants::*; use crate::graphics::vga_color as vga; use crate::resources::Resources; use bracket_lib::prelude::*; @@ -48,7 +48,15 @@ pub fn draw(resources: &Resources, bterm: &mut BTerm) { let stats = &resources.stats; bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 1, stats.score * 10); bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 7, stats.gems); - bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 10, stats.whips); + bterm.print_centered_at( + SIDEBAR_POS_X + 6, + SIDEBAR_POS_Y + 10, + if stats.whip_power <= BASE_WHIP_POWER { + stats.whips.to_string() + } else { + format!("{}+{}", stats.whips, stats.whip_power - BASE_WHIP_POWER) + }, + ); bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 13, stats.teleports); bterm.print_centered_at(SIDEBAR_POS_X + 6, SIDEBAR_POS_Y + 16, stats.keys); diff --git a/src/resources/sound_effects.rs b/src/resources/sound_effects.rs index bcbae52..b0d7638 100644 --- a/src/resources/sound_effects.rs +++ b/src/resources/sound_effects.rs @@ -34,7 +34,8 @@ pub struct SoundEffects { pub blocked: SoundSamples, pub whipping: SoundSamples, pub whipping_hit: SoundSamples, - pub whipping_hit_end: SoundSamples, + pub whipping_hit_enemy: SoundSamples, + pub whipping_hit_block: SoundSamples, pub slow_hit: SoundSamples, pub medium_hit: SoundSamples, pub fast_hit: SoundSamples, @@ -121,23 +122,27 @@ impl SoundEffects { }], }), whipping_hit: sound_output.render_sound_effect(&SoundEffect { - sounds: vec![ - Sound { - sound_type: SoundType::Tone(400), - duration: Duration::from_millis(20), - }, - Sound { - sound_type: SoundType::Tone(90), - duration: Duration::from_secs(3), - }, - ], + sounds: vec![Sound { + sound_type: SoundType::Tone(90), + duration: Duration::from_secs(3), + }], }), - whipping_hit_end: sound_output.render_sound_effect(&SoundEffect { + whipping_hit_enemy: sound_output.render_sound_effect(&SoundEffect { sounds: vec![Sound { sound_type: SoundType::Tone(400), duration: Duration::from_millis(20), }], }), + whipping_hit_block: sound_output.render_sound_effect(&SoundEffect { + sounds: (20..=5700) + .rev() + .step_by(100) + .map(|x| Sound { + sound_type: SoundType::Noise(0, x), + duration: Duration::from_millis(18), + }) + .collect(), + }), slow_hit: sound_output.render_sound_effect(&SoundEffect { sounds: vec![Sound { sound_type: SoundType::Tone(400), diff --git a/src/systems/whip.rs b/src/systems/whip.rs index 7a4fc83..d9f3db1 100644 --- a/src/systems/whip.rs +++ b/src/systems/whip.rs @@ -7,6 +7,7 @@ use hecs::{Entity, World}; use crate::{ components::{monster::damage_for_kind, Monster, Position, WantsToWhip}, resources::Resources, + tile_data::TileType, }; pub fn run(world: &mut World, resources: &mut Resources) { @@ -73,23 +74,15 @@ pub fn run(world: &mut World, resources: &mut Resources) { resources.stats.add_score(damage_for_kind(monster.kind)); to_kill.push(e); resources.map.clear_tile_content_at(dest); - if let Some(sound) = &mut wants_to_whip.sound { - sound.control::, _>().stop(); - } - if wants_to_whip.frame == 7 { - wants_to_whip.sound = None; - if let (Some(sound_effects), Some(sound_output)) = - (&mut resources.sound_effects, &mut resources.sound_output) - { - sound_output.play_sound(sound_effects.whipping_hit_end.clone()); - } - } else if let (Some(sound_effects), Some(sound_output)) = + if let (Some(sound_effects), Some(sound_output)) = (&mut resources.sound_effects, &mut resources.sound_output) { - wants_to_whip.sound = - Some(sound_output.play_sound(sound_effects.whipping_hit.clone())); + sound_output.play_sound(sound_effects.whipping_hit_enemy.clone()); } + switch_to_hit_sound(resources, wants_to_whip); } + } else { + hit_tile(resources, wants_to_whip, dest); } } @@ -115,3 +108,36 @@ pub fn run(world: &mut World, resources: &mut Resources) { let _ = world.remove_one::(e); } } + +fn hit_tile(resources: &mut Resources, wants_to_whip: &mut WantsToWhip, location: Point) { + let tile = resources.map.get_tile_at(location); + match tile { + TileType::Block | TileType::Tree => { + let mut rng = RandomNumberGenerator::new(); + + if (rng.range(0, 7) as u32) < resources.stats.whip_power { + resources.map.set_tile_at(location, TileType::Floor); + if let (Some(sound_effects), Some(sound_output)) = + (&mut resources.sound_effects, &mut resources.sound_output) + { + sound_output.play_sound(sound_effects.whipping_hit_block.clone()); + } + switch_to_hit_sound(resources, wants_to_whip); + } + } + TileType::Forest => todo!(), + _ => (), + } +} + +fn switch_to_hit_sound(resources: &mut Resources, wants_to_whip: &mut WantsToWhip) { + if let Some(sound) = &mut wants_to_whip.sound { + sound.control::, _>().stop(); + } + + if let (Some(sound_effects), Some(sound_output)) = + (&mut resources.sound_effects, &mut resources.sound_output) + { + wants_to_whip.sound = Some(sound_output.play_sound(sound_effects.whipping_hit.clone())); + } +}