Skip to content

Commit

Permalink
Support card SFX
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 28, 2024
1 parent 7d3abaf commit 57dea6b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
35 changes: 21 additions & 14 deletions assets/config/card.ron
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
card_icon_map: {
"step": CardIcon(texture: "image/card/step.png"),
"double_beat": CardIcon(texture: "image/card/double_beat.png"),
"counter_point": CardIcon(texture: "image/card/counter_point.png"),
"major_chord": CardIcon(texture: "image/card/major_chord.png"),
"splits": CardIcon(texture: "image/card/splits.png"),
},
card_map: {
Expand All @@ -51,16 +51,32 @@
description: "A-one and a-two and a-step.",
background: "blue",
icon: "step",

// TODO: "Movement card" sfx.
play_sfx: "audio/sfx/Projectile Hits Enemy.ogg",
action: Step,
action_config: CardActionConfig(
remove_on_beat: 8,
),
),
"splits": Card(
name: "Splits",
description: "Split in two!",
background: "blue",
icon: "splits",

// TODO: "Movement card" sfx.
play_sfx: "audio/sfx/Projectile Hits Enemy.ogg",
action_config: CardActionConfig(
remove_on_beat: 16,
),
),
"double_beat": Card(
name: "Double Beat",
description: "Two beats, rapid fire!",
background: "pink",
icon: "double_beat",

action: DoubleBeat,
action_config: CardActionConfig(
remove_on_beat: 4,
Expand All @@ -71,21 +87,12 @@
),
),
),
"counter_point": Card(
name: "Counter Point",
"major_chord": Card(
name: "Major Chord",
description: "Notes that move apart synchronously",
background: "pink",
icon: "counter_point",
action_config: CardActionConfig(
remove_on_beat: 16,
),
),
"splits": Card(
name: "Splits",
description: "Split in two!",
background: "blue",
icon: "splits",
modifier: 1.0,
icon: "major_chord",

action_config: CardActionConfig(
remove_on_beat: 16,
),
Expand Down
File renamed without changes.
File renamed without changes
20 changes: 20 additions & 0 deletions src/game/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy::ecs::system::SystemState;
use bevy::prelude::*;
use bevy::sprite::Anchor;
use bevy::utils::HashMap;
use bevy_kira_audio::prelude::*;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -64,6 +65,9 @@ impl Config for CardConfig {

for card in self.card_map.values_mut() {
card.action = *c!(card_action_map.0.get(&card.action_key));
if !card.play_sfx_path.is_empty() {
card.play_sfx = Some(asset_server.load(&card.play_sfx_path));
}
}
}

Expand All @@ -75,6 +79,11 @@ impl Config for CardConfig {
.card_icon_map
.values()
.all(|x| asset_server.is_loaded_with_dependencies(&x.texture))
&& self.card_map.values().all(|x| {
!x.play_sfx
.as_ref()
.is_some_and(|x| !asset_server.is_loaded_with_dependencies(x))
})
}
}

Expand Down Expand Up @@ -161,13 +170,24 @@ pub struct Card {
pub background_key: String,
#[serde(rename = "icon")]
pub icon_key: String,

#[serde(rename = "play_sfx", default)]
play_sfx_path: String,
#[serde(skip)]
pub play_sfx: Option<Handle<AudioSource>>,
#[serde(default = "one")]
pub play_sfx_volume: f64,
#[serde(rename = "action", default)]
action_key: CardActionKey,
#[serde(skip)]
pub action: CardAction,
pub action_config: CardActionConfig, // TODO: Naming
}

fn one() -> f64 {
1.0
}

pub fn card(key: impl Into<String>, active: Option<bool>) -> impl EntityCommand {
let key = key.into();

Expand Down
18 changes: 13 additions & 5 deletions src/game/card/deck.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;
use serde::Deserialize;
use serde::Serialize;

use crate::core::UpdateSet;
use crate::game::actor::faction::Faction;
use crate::game::audio::music::on_full_beat;
use crate::game::card::card;
use crate::game::card::CardConfig;
Expand Down Expand Up @@ -84,15 +86,21 @@ impl Deck {
fn play_card_from_deck(
mut commands: Commands,
config: ConfigRef<CardConfig>,
mut deck_query: Query<(Entity, &mut Deck)>,
audio: Res<Audio>,
mut deck_query: Query<(Entity, &Faction, &mut Deck)>,
) {
let config = r!(config.get());

for (entity, mut deck) in &mut deck_query {
for (entity, &faction, mut deck) in &mut deck_query {
let card_key = c!(deck.advance(1));
let card_action = c!(config.card_map.get(card_key));
let action = card_action.action;
let action_config = card_action.action_config.clone();
let card = c!(config.card_map.get(card_key));

if let (Faction::Player, Some(play_sfx)) = (faction, card.play_sfx.clone()) {
audio.play(play_sfx).with_volume(card.play_sfx_volume);
}

let action = card.action;
let action_config = card.action_config.clone();
commands.run_system_with_input(action.0, (entity, action_config));
}
}
Expand Down

0 comments on commit 57dea6b

Please sign in to comment.