Skip to content

Commit

Permalink
merge: sync with main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
LiteHell committed Jun 7, 2024
2 parents 8495f29 + f9a28dc commit 088fdfe
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 84 deletions.
73 changes: 41 additions & 32 deletions controller/controller.ino
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
#include <Keyboard.h>
#define OUTPUT_PIN_1 4 // 궁채
#define OUTPUT_PIN_2 5 // 열채
#define INPUT_PIN_1 8 // 궁편
#define INPUT_PIN_2 9 // 열편
#define RELAY_DELAY 50
#define PROCESS_KEYBOARD_INPUT(shifts, key) \
if (prevKeyState & (1 << (shifts)) != newKeyState & (1 << (shifts))) { \
if (newKeyState & (1 << (shifts)) != 0) { \
Keyboard.press(key); \
} else { \
Keyboard.release(key); \
} \
} 1+1
#define BILL_PIN 2 // 지폐기
#define COIN_PIN 3 // 코인기
#define INPUT_PIN_2 4 // 열편
#define OUTPUT_PIN_2 5 // 열채
#define INPUT_PIN_1 6 // 궁편
#define OUTPUT_PIN_1 7 // 궁채
#define RELAY_DELAY 50 // 딜레이 50ms

int step;
int pin1ConnectedTo, pin2ConnectedTo;
unsigned int lastTimestamp;
uint8_t prevKeyState;
unsigned int coin_cnt;

void setup()
{
step = 0;
prevKeyState = 0;
coin_cnt = 0;
pinMode(OUTPUT_PIN_1, OUTPUT);
pinMode(OUTPUT_PIN_2, OUTPUT);
pinMode(INPUT_PIN_1, INPUT_PULLUP);
pinMode(INPUT_PIN_2, INPUT_PULLUP);

// 열채, 궁채
digitalWrite(OUTPUT_PIN_1, LOW);
digitalWrite(OUTPUT_PIN_2, LOW);

Keyboard.begin();
// 지폐, 코인기 인터럽트
attachInterrupt(digitalPinToInterrupt(BILL_PIN), bill, FALLING);
attachInterrupt(digitalPinToInterrupt(COIN_PIN), coin, FALLING);

// 시리얼 셋
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
}

void bill()
{
coin_cnt++;
}

void coin()
{
coin_cnt++;
}

void loop()
Expand All @@ -45,7 +58,6 @@ void loop()
digitalWrite(OUTPUT_PIN_1, LOW);
digitalWrite(OUTPUT_PIN_2, HIGH);
delayMicroseconds(RELAY_DELAY);
//delay(RELAY_DELAY);
if(digitalRead(INPUT_PIN_1) == LOW) {
pin2ConnectedTo = INPUT_PIN_1;
} else if (digitalRead(INPUT_PIN_2) == LOW) {
Expand All @@ -58,7 +70,6 @@ void loop()
digitalWrite(OUTPUT_PIN_2, LOW);
digitalWrite(OUTPUT_PIN_1, HIGH);
delayMicroseconds(RELAY_DELAY);
//delay(RELAY_DELAY);
if(digitalRead(INPUT_PIN_1) == LOW) {
pin1ConnectedTo = INPUT_PIN_1;
} else if (digitalRead(INPUT_PIN_2) == LOW) {
Expand All @@ -71,22 +82,20 @@ void loop()
digitalWrite(OUTPUT_PIN_1, LOW);
digitalWrite(OUTPUT_PIN_2, LOW);
delayMicroseconds(RELAY_DELAY);
uint8_t newKeyState = 0;
uint8_t bits = 0;
if (pin1ConnectedTo == INPUT_PIN_1)
newKeyState |= (uint8_t)1;
bits |= (uint8_t)1;
if (pin1ConnectedTo == INPUT_PIN_2)
newKeyState |= (uint8_t)2;
bits |= (uint8_t)2;
if (pin2ConnectedTo == INPUT_PIN_1)
newKeyState |= (uint8_t)4;
bits |= (uint8_t)4;
if (pin2ConnectedTo == INPUT_PIN_2)
newKeyState |= (uint8_t)8;

PROCESS_KEYBOARD_INPUT(0, 'd');
PROCESS_KEYBOARD_INPUT(1, 'f');
PROCESS_KEYBOARD_INPUT(2, 'j');
PROCESS_KEYBOARD_INPUT(3, 'k');

prevKeyState = newKeyState;
bits |= (uint8_t)8;
if (coin_cnt>0){
bits |= (uint8_t)16;
coin_cnt--;
}
Serial.write(bits);
break;
}
}
}
104 changes: 69 additions & 35 deletions game/src/game/select_song.rs
Original file line number Diff line number Diff line change
@@ -1,12 +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, 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 @@ -38,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 @@ -106,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) {
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
39 changes: 22 additions & 17 deletions game/src/game/title.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::time::Instant;
use std::{path::Path, time::Duration};

use num_rational::Rational64;
use sdl2::{
event::Event, image::LoadTexture, keyboard::Keycode, pixels::Color, rect::Rect, render::Canvas,
video::Window,
};
use sdl2::{image::LoadTexture, pixels::Color, rect::Rect, render::Canvas, video::Window};

use crate::constants::DEFAULT_FONT_COLOR;
use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;
Expand All @@ -13,6 +11,8 @@ use crate::constants::DEFAULT_VIDEO_PATH as VIDEO_PATH;

use crate::create_streaming_iyuv_texture;

use super::game_player::janggu_state_with_tick::JangguStateWithTick;

use super::{
common::{event_loop_common, render_common},
game_common_context::GameCommonContext,
Expand Down Expand Up @@ -130,24 +130,29 @@ pub(crate) fn render_title(common_context: &mut GameCommonContext) -> TitleResul
background_video_size.1
)
.expect("Failed to create texture for title background video");
let mut janggu_state = JangguStateWithTick::new();
let title_started_at = Instant::now();
janggu_state.update(
common_context.read_janggu_state(),
title_started_at.elapsed().as_millis() as i128,
);
loop {
for event in common_context.event_pump.poll_iter() {
if event_loop_common(&event) {
return TitleResult::Exit;
}
match event {
Event::KeyDown {
keycode: Some(Keycode::Return),
..
} => {
if common_context.coin_and_janggu.get_coins() >= common_context.price {
common_context
.coin_and_janggu
.consume_coins(common_context.price);
return TitleResult::StartGame;
}
}
_ => {}
}

janggu_state.update(
common_context.read_janggu_state(),
title_started_at.elapsed().as_millis() as i128,
);
if janggu_state.궁채.is_keydown_now || janggu_state.열채.is_keydown_now {
if common_context.coin_and_janggu.get_coins() >= common_context.price {
common_context
.coin_and_janggu
.consume_coins(common_context.price);
return TitleResult::StartGame;
}
}

Expand Down

0 comments on commit 088fdfe

Please sign in to comment.