Skip to content

Commit

Permalink
Merge pull request #54 from cau-overchaos/BID-74-song-select-janggu-c…
Browse files Browse the repository at this point in the history
…ontrol

janggu input when selecting song
  • Loading branch information
boulce committed Jun 5, 2024
2 parents 052c1a9 + fae3e15 commit e369484
Showing 1 changed file with 69 additions and 37 deletions.
106 changes: 69 additions & 37 deletions game/src/game/select_song.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::time::Duration;
use std::{path::Path, time::Instant};

use bidrum_data_struct_lib::janggu::JangguFace;
use bidrum_data_struct_lib::song::GameSong;
use sdl2::{
event::Event, image::LoadTexture, keyboard::Keycode, pixels::Color, rect::Rect, render::Texture,
};
use sdl2::{image::LoadTexture, rect::Rect, render::Texture};

use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;
use crate::constants::DEFAULT_IMG_PATH as IMG_PATH;
use crate::constants::SELECT_SONG_FONT_COLOR;

use super::game_player::janggu_state_with_tick::JangguStateWithTick;
use super::util::create_outlined_font_texture::create_font_texture;
use super::{
common::{event_loop_common, render_common},
Expand Down Expand Up @@ -40,6 +41,13 @@ pub(crate) fn select_song(
songs: &Vec<GameSong>,
) -> SongSelectionResult {
let texture_creator = common_context.canvas.texture_creator();
let mut janggu_state = JangguStateWithTick::new();

let mut hit_left = false;
let mut hit_right = false;
let mut last_left_hit_time = Instant::now();
let mut last_right_hit_time = Instant::now();
let hit_both_side_time_delay = 100; // ms

// font information
let font_path = &(FONT_PATH.to_owned() + "/sans.ttf");
Expand Down Expand Up @@ -108,52 +116,76 @@ pub(crate) fn select_song(

let mut selected_song_item_moving_center_x = selected_song_item_center_x; // x position of center of moving selected song item

let selecting_song_started_at = Instant::now();
// enable alpha blending
common_context
.canvas
.set_blend_mode(sdl2::render::BlendMode::Blend);
'running: loop {
// waiting user input
let tick = selecting_song_started_at.elapsed().as_millis();

// waiting keyboard input
for event in common_context.event_pump.poll_iter() {
if event_loop_common(&event, &mut common_context.coins) {
break 'running;
}
}

match event {
Event::KeyDown {
// if user press right key, then song menu moves to right for specific distance
keycode: Some(Keycode::Right),
repeat: false,
..
} => {
if moving_direction == MovingDirection::Stop {
// to prevent changing direction when moving
moving_direction = MovingDirection::Right;
last_key_press_time = Instant::now();
}
}
Event::KeyDown {
// if user press right key, then song menu moves to left for specific distance
keycode: Some(Keycode::Left),
repeat: false,
..
} => {
if moving_direction == MovingDirection::Stop {
// to prevent changing direction when moving
moving_direction = MovingDirection::Left;
last_key_press_time = Instant::now();
}
// process janggu input
janggu_state.update(common_context.read_janggu_state(), tick as i128);
if (janggu_state.궁채.is_keydown_now
&& matches!(janggu_state.궁채.face, Some(JangguFace::궁편)))
&& (janggu_state.열채.is_keydown_now
&& matches!(janggu_state.열채.face, Some(JangguFace::열편)))
{
if moving_direction == MovingDirection::Stop {
break 'running;
}
} else if (janggu_state.궁채.is_keydown_now
&& matches!(janggu_state.궁채.face, Some(JangguFace::궁편)))
|| (janggu_state.열채.is_keydown_now
&& matches!(janggu_state.열채.face, Some(JangguFace::궁편)))
{
// to prevent changing direction when moving
if moving_direction == MovingDirection::Stop {
if hit_left == false {
// If hitting left side, don't react directly. Just assign false to hit_left
hit_left = true;
last_left_hit_time = Instant::now();
}
Event::KeyDown {
// if user press enter key, then song is selected
keycode: Some(Keycode::Return),
..
} => {
if moving_direction == MovingDirection::Stop {
break 'running;
}
}
} else if (janggu_state.궁채.is_keydown_now
&& matches!(janggu_state.궁채.face, Some(JangguFace::열편)))
|| (janggu_state.열채.is_keydown_now
&& matches!(janggu_state.열채.face, Some(JangguFace::열편)))
{
// to prevent changing direction when moving
if moving_direction == MovingDirection::Stop {
if hit_right == false {
// If hitting right side, don't react directly. Just assign false to hit_right
hit_right = true;
last_right_hit_time = Instant::now();
}
_ => {}
}
}

// to detect delay for hitting both side
if hit_left && hit_right {
// detect hitting both side
break 'running;
} else if hit_left {
if last_left_hit_time.elapsed() > Duration::from_millis(hit_both_side_time_delay) {
// after the limit, regard as going to left song
moving_direction = MovingDirection::Left;
last_key_press_time = Instant::now();
hit_left = false;
}
} else if hit_right {
if last_right_hit_time.elapsed() > Duration::from_millis(hit_both_side_time_delay) {
// after the limit, regard as going to right song
moving_direction = MovingDirection::Right;
last_key_press_time = Instant::now();
hit_right = false;
}
}

Expand Down

0 comments on commit e369484

Please sign in to comment.