Skip to content

Commit

Permalink
feat(taiko): 3 secs preparation before game start
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Apr 29, 2024
1 parent 782e7e9 commit e2a17fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
25 changes: 22 additions & 3 deletions taiko-game/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct App {
auto_play: Option<Vec<TaikoNote>>,
auto_play_combo_sleep: u8,
guage_color_change: i32,
enter_countdown: i16,
}

impl App {
Expand Down Expand Up @@ -105,6 +106,7 @@ impl App {
auto_play: None,
auto_play_combo_sleep: 0,
guage_color_change: 0,
enter_countdown: 0,
})
}

Expand Down Expand Up @@ -167,6 +169,8 @@ impl App {
}

async fn enter_game(&mut self) -> Result<()> {
self.enter_countdown = self.args.tps as i16 * -3;

let selected = self.course_selector.selected().unwrap_or(0);
let mut course = self
.song
Expand Down Expand Up @@ -198,7 +202,7 @@ impl App {
self.taiko.replace(DefaultTaikoEngine::new(source));

self.player.stop_music().await;
self.player.play_music(self.music.as_ref().unwrap()).await;
self.player.load_music(self.music.as_ref().unwrap()).await;

Ok(())
}
Expand Down Expand Up @@ -406,7 +410,18 @@ impl App {
self.ticks.remove(0);
}

let player_time = self.player.get_music_time().await;
if self.enter_countdown < 0 {
self.enter_countdown += 1;
} else if self.enter_countdown == 0 {
self.player.play_loaded_music().await;
self.enter_countdown = 1;
}

let player_time = if self.enter_countdown <= 0 {
self.enter_countdown as f64 / self.args.tps as f64
} else {
self.player.get_music_time().await
};
if self.taiko.is_some() {
let taiko = self.taiko.as_mut().unwrap();

Expand Down Expand Up @@ -480,7 +495,11 @@ impl App {
} else {
0.0
};
let player_time = self.player.get_music_time().await;
let player_time = if self.enter_countdown <= 0 {
self.enter_countdown as f64 / self.args.tps as f64
} else {
self.player.get_music_time().await
};

let song_name: Option<String> = if self.song.is_none() {
None
Expand Down
20 changes: 17 additions & 3 deletions taiko-game/src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ pub(crate) trait SoundPlayer {
/// Plays a sound effect.
async fn play_effect(&mut self, effect: &SoundData);

/// Loads a background music.
async fn load_music(&mut self, music: &SoundData);

/// Plays a background music after loading it.
async fn play_loaded_music(&mut self);

/// Plays a background music.
async fn play_music(&mut self, music: &SoundData);

Expand Down Expand Up @@ -118,7 +124,7 @@ impl SoundPlayer for RodioSoundPlayer {
self.controller.add(source);
}

async fn play_music(&mut self, music: &SoundData) {
async fn load_music(&mut self, music: &SoundData) {
let sink = self.sink.lock().await;

let source = rodio::buffer::SamplesBuffer::new(
Expand All @@ -131,12 +137,20 @@ impl SoundPlayer for RodioSoundPlayer {
sink.append(mixer);
self.controller = controller;
self.controller.add(source);

sink.play();
sink.pause();
self.music_start = Some(Instant::now());
self.music_time = 0.0;
}

async fn play_loaded_music(&mut self) {
self.resume_music().await
}

async fn play_music(&mut self, music: &SoundData) {
self.load_music(music).await;
self.play_loaded_music().await;
}

async fn play_music_from(&mut self, music: &SoundData, time: f64) {
let sink = self.sink.lock().await;
let data = music
Expand Down

0 comments on commit e2a17fa

Please sign in to comment.