Skip to content

Commit

Permalink
Add eighth-beat resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 26, 2024
1 parent 9472ec2 commit 0570763
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 17 deletions.
8 changes: 4 additions & 4 deletions assets/config/card.ron
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
icon: "step",
action: Step,
action_config: CardActionConfig(
remove_on_beat: 2,
remove_on_beat: 8,
),
),
"double_beat": Card(
Expand All @@ -63,7 +63,7 @@
icon: "double_beat",
action: DoubleBeat,
action_config: CardActionConfig(
remove_on_beat: 2,
remove_on_beat: 4,
attack: Attack (
power: 2.0,
force: 4.0,
Expand All @@ -77,7 +77,7 @@
background: "pink",
icon: "counter_point",
action_config: CardActionConfig(
remove_on_beat: 2,
remove_on_beat: 16,
),
),
"splits": Card(
Expand All @@ -87,7 +87,7 @@
icon: "splits",
modifier: 1.0,
action_config: CardActionConfig(
remove_on_beat: 2,
remove_on_beat: 16,
),
),
}
Expand Down
2 changes: 2 additions & 0 deletions src/game/card/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ impl Default for CardAction {
#[derive(Default, Reflect, Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct CardActionConfig {
/// Remove component after this many eighth-beats.
#[serde(default)]
remove_on_beat: usize,
/// Remove component when this timer finishes.
#[serde(default)]
remove_on_timer: Timer,
#[serde(default)]
Expand Down
2 changes: 1 addition & 1 deletion src/game/card/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Configure for DoubleBeat {
Update,
double_beat
.in_set(UpdateSet::RecordInput)
.run_if(on_beat(1)),
.run_if(on_beat(4)),
);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/game/card/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::core::UpdateSet;
use crate::game::actor::player::IsPlayer;
use crate::game::card::card;
use crate::game::card::AddCardEvent;
use crate::game::music::beat::on_beat;
use crate::game::music::beat::on_full_beat;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
Expand All @@ -30,7 +30,9 @@ impl Configure for Deck {
Update,
(
add_cards_to_deck.in_set(UpdateSet::SyncLate),
advance_deck.in_set(UpdateSet::PlayCards).run_if(on_beat(2)),
advance_deck
.in_set(UpdateSet::PlayCards)
.run_if(on_full_beat(2)),
),
);
}
Expand Down
11 changes: 7 additions & 4 deletions src/game/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ fn apply_despawn_on_beat(
mut despawn_query: Query<(Entity, &mut DespawnOnBeat)>,
) {
for (entity, mut beat) in &mut despawn_query {
if beat.0 <= 1 {
if beat.0 > 0 {
beat.0 -= 1;
} else {
despawn.recursive(entity);
}
beat.0 = beat.0.saturating_sub(1);
}
}

Expand Down Expand Up @@ -222,6 +223,7 @@ fn apply_remove_on_timer<C: Component + TypePath>(
}
}

/// Remove a component after a certain number of eighth-beats.
#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct RemoveOnBeat<C: Component + TypePath> {
Expand Down Expand Up @@ -260,9 +262,10 @@ fn apply_remove_on_beat<C: Component + TypePath>(
mut remove_query: Query<(Entity, &mut RemoveOnBeat<C>)>,
) {
for (entity, mut remove) in &mut remove_query {
if remove.beat <= 1 {
if remove.beat > 0 {
remove.beat -= 1;
} else {
commands.entity(entity).remove::<(C, RemoveOnBeat<C>)>();
}
remove.beat = remove.beat.saturating_sub(1);
}
}
2 changes: 1 addition & 1 deletion src/game/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ impl Config for MusicConfig {
world
.resource_mut::<BeatTimer>()
.0
.set_duration(Duration::from_secs_f32(60.0 / self.bpm));
.set_duration(Duration::from_secs_f32(60.0 / 8.0 / self.bpm));
}
}
9 changes: 8 additions & 1 deletion src/game/music/beat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ fn pause_beat_timer(mut beat_timer: ResMut<BeatTimer>) {
#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct Beat {
/// The total number of eighth-beats counted.
pub total: usize,
/// The number of eighth-beats finished this tick (usually 0 or 1).
pub this_tick: usize,
}

Expand All @@ -68,11 +70,16 @@ fn update_beat(beat_timer: Res<BeatTimer>, mut beat: ResMut<Beat>) {
beat.total += beat.this_tick;
}

/// A run condition to run a system every `n` beats.
/// A run condition to run a system every `n` eighth-beats.
pub fn on_beat(n: usize) -> impl Fn(Res<Beat>) -> bool {
move |beat| {
let hi = beat.total;
let lo = hi - beat.this_tick;
hi / n > lo / n
}
}

/// A run condition to run a system every `n` beats.
pub fn on_full_beat(n: usize) -> impl Fn(Res<Beat>) -> bool {
on_beat(8 * n)
}
2 changes: 1 addition & 1 deletion src/game/spotlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Configure for IsSpotlightLampSpawner {
Update,
spawn_spotlight_lamps
.in_set(UpdateSet::Update)
.run_if(not(any_with_component::<Spotlight>).or_else(on_beat(1))),
.run_if(not(any_with_component::<Spotlight>).or_else(on_beat(4))),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Configure for SpriteAnimation {
Update,
update_sprite_animation
.in_set(UpdateSet::Update)
.run_if(on_beat(1)),
.run_if(on_beat(4)),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/game/wave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::core::camera::CameraRoot;
use crate::core::UpdateSet;
use crate::game::actor::enemy::enemy;
use crate::game::actor::level::Level;
use crate::game::music::beat::on_beat;
use crate::game::music::beat::on_full_beat;
use crate::screen::Screen;
use crate::util::prelude::*;

Expand Down Expand Up @@ -63,7 +63,7 @@ impl Configure for Wave {
Screen::Playing.on_update(
spawn_wave_enemies
.in_set(UpdateSet::Update)
.run_if(on_beat(2)),
.run_if(on_full_beat(1)),
),
);
}
Expand Down

0 comments on commit 0570763

Please sign in to comment.