Skip to content

Commit

Permalink
Optimize scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed Aug 17, 2023
1 parent 7af7fb9 commit 2e3d9ff
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ pub struct Editor {
music: geng::SoundEffect,
/// Stop the music after the timer runs out.
music_timer: Time,
/// Whether the last frame was scrolled through time.
was_scrolling: bool,
/// Whether currently scrolling through time.
/// Used as a hack to not replay the music every frame.
scrolling: bool,
grid_size: Coord,
show_grid: bool,
snap_to_grid: bool,
Expand Down Expand Up @@ -94,6 +99,8 @@ impl Editor {
state: State::Place,
music: assets.music.effect(),
music_timer: Time::ZERO,
was_scrolling: false,
scrolling: false,
grid_size: Coord::new(model.camera.fov) / config.grid.height,
show_grid: true,
snap_to_grid: true,
Expand All @@ -107,13 +114,7 @@ impl Editor {

fn scroll_time(&mut self, delta: Time) {
self.current_beat = (self.current_beat + delta).max(Time::ZERO);
// Play a quarter beat of music
self.music.stop();
self.music = self.assets.music.effect();
self.music.play_from(time::Duration::from_secs_f64(
(self.current_beat * self.level.beat_time()).as_f32() as f64,
));
self.music_timer = self.level.beat_time() * self.config.playback_duration;
self.scrolling = true;
}

fn snap_pos_grid(&self, pos: vec2<Coord>) -> vec2<Coord> {
Expand Down Expand Up @@ -188,6 +189,19 @@ impl geng::State for Editor {
}
}

if self.scrolling {
self.was_scrolling = true;
} else if self.was_scrolling {
// Stopped scrolling
// Play some music
self.music.stop();
self.music = self.assets.music.effect();
self.music.play_from(time::Duration::from_secs_f64(
(self.current_beat * self.level.beat_time()).as_f32() as f64,
));
self.music_timer = self.level.beat_time() * self.config.playback_duration;
}

if let State::Playing { .. } = self.state {
self.current_beat = self.time / self.level.beat_time();
}
Expand Down

0 comments on commit 2e3d9ff

Please sign in to comment.