Skip to content

Commit

Permalink
[game] Fix gameplay song leadin oddities
Browse files Browse the repository at this point in the history
  • Loading branch information
Drewol committed Oct 26, 2024
1 parent a26f2dc commit 1ea240a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
8 changes: 4 additions & 4 deletions game/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,11 +1133,11 @@ impl Game {
}

fn with_offset(&self, time_ms: f64) -> f64 {
time_ms - self.chart.audio.bgm.offset as f64
time_ms - self.chart.audio.bgm.offset as f64 - self.playback.leadin().as_secs_f64() * 1000.0
}

fn without_offset(&self, time_ms: f64) -> f64 {
time_ms + self.chart.audio.bgm.offset as f64
time_ms + self.chart.audio.bgm.offset as f64 + self.playback.leadin().as_secs_f64() * 1000.0
}

fn fail_song(&mut self) -> anyhow::Result<()> {
Expand Down Expand Up @@ -1678,7 +1678,7 @@ impl Scene for Game {
.update(vec2(viewport.width as f32, viewport.height as f32));
if self.intro_done && !self.playback.is_playing() {
info!("Starting playback");
self.zero_time = SystemTime::now() + LEADIN;
self.zero_time = SystemTime::now();
if !self.playback.play() {
log::error!("Could not play audio");
self.closed = true;
Expand Down Expand Up @@ -1752,7 +1752,7 @@ impl Scene for Game {
.map(|x| x.roll_at(self.current_tick as f32))
.sum::<f32>();

self.view.cursor = self.with_offset(time.as_secs_f64() * 1000.0) + leadin_ms;
self.view.cursor = self.with_offset(time.as_secs_f64() * 1000.0);

self.current_tick = self.chart.ms_to_tick(self.view.cursor);
self.camera.kson_radius = self
Expand Down
4 changes: 2 additions & 2 deletions game/src/game/chart_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ impl ChartView {
let chip_h = chip_h.copysign(-1.0);
let view_time = self.cursor;
let view_offset = if view_time < 0.0 {
chart.ms_to_tick(view_time.abs()) as i64 //will be weird with early bpm changes
(view_time / chart.tick_to_ms(1)) as i64
} else {
0
};

td.set_depth_test(three_d::DepthTest::Never);

let _glow_state = if (0.0_f32 * 8.0).fract() > 0.5 { 2 } else { 3 };
let view_tick = chart.ms_to_tick(view_time) as i64 - view_offset;
let view_tick = chart.ms_to_tick(view_time) as i64 + view_offset;
let view_distance = (KSON_RESOLUTION as f32 * 8.0) / self.hispeed;
let last_view_tick = view_distance.ceil() as i64 + view_tick;
let first_view_tick = view_tick - view_distance as i64;
Expand Down
11 changes: 9 additions & 2 deletions kson-music-playback/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub struct AudioPlayback {
file: Option<AudioFile>,
last_file: String,
effects: Vec<((u64, u64), Box<EffectBuilder>)>,
leadin: Duration,
}

impl AudioPlayback {
Expand All @@ -183,6 +184,7 @@ impl AudioPlayback {
file: None,
last_file: String::new(),
effects: vec![],
leadin: Duration::ZERO,
}
}

Expand All @@ -195,10 +197,15 @@ impl AudioPlayback {

pub fn set_leadin(&mut self, duration: Duration) {
if let Some(file) = &self.file {
file.set_leadin(duration)
file.set_leadin(duration);
self.leadin = duration;
}
}

pub fn leadin(&self) -> Duration {
self.leadin
}

pub fn build_effects(&mut self, chart: &Chart) {
let offset = Duration::from_millis(chart.audio.bgm.offset.max(0) as _);
let neg_offset = Duration::from_millis(chart.audio.bgm.offset.min(0).unsigned_abs() as _);
Expand Down Expand Up @@ -371,7 +378,7 @@ impl AudioPlayback {

pub fn get_ms(&self) -> f64 {
if let Some(file) = &self.file {
file.get_ms()
file.get_ms() + (self.leadin.as_secs_f64() * 1000.0)
} else {
0.0
}
Expand Down

0 comments on commit 1ea240a

Please sign in to comment.