Skip to content

Commit

Permalink
add leniency pre/post (work on #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamby777 committed May 19, 2024
1 parent 6800a16 commit 0892c46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
25 changes: 22 additions & 3 deletions pets-lib/src/battle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ enum MenuSection {
Skill,
}

const LENIENCY_AFTER_BEAT: f64 = 0.02;
const LENIENCY_BEFORE_BEAT: f64 = 0.02;

#[derive(Default, PartialEq)]
enum BattleState {
/// Few seconds countdown before the music starts
Expand Down Expand Up @@ -151,31 +154,47 @@ impl BattleEngine {

self.rhythm_state = on.then_some(notetype);

if on {
if !on {
// if note off received, give X ms of leeway after the
// ending for them to still hit the note
let timer = &mut self.rhythm_timer;
timer.set_wait_time(0.04);
timer.set_wait_time(LENIENCY_AFTER_BEAT);
timer.start();
}
}

/// Called when the player successfully hits a note
fn on_successful_attack(&mut self) {
let pos = self.base().get_position() + Vector2::new(10.0, 0.0);
self.base_mut().set_position(pos);
}

fn on_flop_attack(&mut self) {
let pos = self.base().get_position() + Vector2::new(-10.0, 0.0);
self.base_mut().set_position(pos);
}

/// when player tries to attack on a beat
#[func]
pub fn on_player_note_hit(&mut self) {
let hit = self.try_attack();

if hit {
godot_print!("player hit the note!");
self.on_successful_attack();
} else {
godot_print!("player missed the note, trying again in 20ms");
let this_id = self.base().instance_id();
set_timeout(0.02, move || {
set_timeout(LENIENCY_BEFORE_BEAT, move || {
let mut this =
Gd::<Self>::try_from_instance_id(this_id).unwrap();
let hit = this.bind_mut().try_attack();
if hit {
godot_print!("counting the hit as early, but valid!");
this.bind_mut().on_successful_attack();
} else {
godot_print!("player missed the note again");
this.bind_mut().on_flop_attack();
}
});
}
Expand Down
10 changes: 9 additions & 1 deletion pets-lib/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ impl World {
#[signal]
fn battle_intro_done(eid: GString) {}

fn mute_audio_bus(mute_world: bool) {
let (muted, unmuted) = if mute_world { (1, 2) } else { (2, 1) };

let mut srv = AudioServer::singleton();
srv.set_bus_mute(muted, true);
srv.set_bus_mute(unmuted, false);
}

pub fn start_battle(eid: GString) {
PlayerCB::singleton().bind_mut().in_battle = true;
let world = current_scene();
Expand Down Expand Up @@ -121,7 +129,7 @@ impl World {
layer.add_child(scene.clone().upcast());
scene.bind_mut().animate_in();

AudioServer::singleton().set_bus_mute(1, true);
Self::mute_audio_bus(true);

// it's a performance thing
// PlayerCB::singleton().set_process(false);
Expand Down

0 comments on commit 0892c46

Please sign in to comment.