Add repeating down button

This commit is contained in:
Mitchell Marino 2024-08-10 22:04:31 -05:00
parent 1632504099
commit a9d9d747fb
2 changed files with 118 additions and 40 deletions

View File

@ -266,11 +266,11 @@ void step1(void) {
while (get_flipped_switch(nullptr)); while (get_flipped_switch(nullptr));
init_step(); init_step();
while (!play_part(30*1000)); while (!play_part(40*1000));
part = 1; part = 1;
while (!play_part(25*1000)); while (!play_part(35*1000));
part = 2; part = 2;
while (!play_part(22*1000)); while (!play_part(30*1000));
clean_up_step(); clean_up_step();
// TODO: flash lights // TODO: flash lights

View File

@ -54,13 +54,14 @@ lv_obj_t* piece_imgs[4] = {};
TaskHandle_t music_handle; TaskHandle_t music_handle;
std::random_device rd; static std::random_device rd;
std::mt19937 gen(rd()); static std::mt19937 gen(rd());
std::uniform_int_distribution<> piece_dist(2, 7); static std::uniform_int_distribution<> piece_dist(2, 7);
static void generate_block(void); static void generate_block(void);
static void show_board(void); static void show_board(void);
static void get_node_locations(void); static void get_node_locations(void);
static bool check_overlap(void);
static void line_clear(int hi); static void line_clear(int hi);
static void check_line_clears(void); static void check_line_clears(void);
@ -73,6 +74,7 @@ static void rotate_block(void);
static void update_score(void); static void update_score(void);
static void clear_board(void); static void clear_board(void);
static int bbcc(int i) { // [0,1,2,3] -> [ 0, 0,+1,+1] static int bbcc(int i) { // [0,1,2,3] -> [ 0, 0,+1,+1]
const int map[] = {0, 0, 1, 1}; const int map[] = {0, 0, 1, 1};
return map[i]; return map[i];
@ -168,7 +170,7 @@ static void init_screen(void) {
xSemaphoreGive(xGuiSemaphore); xSemaphoreGive(xGuiSemaphore);
xTaskCreate(music_task, "music", 4096, NULL, 5, &music_handle); // xTaskCreate(music_task, "music", 4096, NULL, 5, &music_handle);
} }
static void deinit_screen(void) { static void deinit_screen(void) {
@ -177,10 +179,11 @@ static void deinit_screen(void) {
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
xSemaphoreGive(xGuiSemaphore); xSemaphoreGive(xGuiSemaphore);
vTaskDelete(music_handle); // vTaskDelete(music_handle);
} }
bool play_game(int time, int required_score) { bool play_game(int time, int required_score) {
game = true;
target_score = required_score; target_score = required_score;
score = 0; score = 0;
update_score(); update_score();
@ -195,6 +198,11 @@ bool play_game(int time, int required_score) {
while (get_pressed_button(&button)); while (get_pressed_button(&button));
// SwitchKey switch_; // SwitchKey switch_;
const TickType_t first_repeat_time = pdMS_TO_TICKS(700);
const TickType_t repeat_time = pdMS_TO_TICKS(100);
TickType_t down_held = 0;
TickType_t last_repeat = 0;
while(game) { while(game) {
if (get_pressed_button(&button)) { if (get_pressed_button(&button)) {
switch (button) { switch (button) {
@ -205,6 +213,8 @@ bool play_game(int time, int required_score) {
move_right(); move_right();
break; break;
case ButtonKey::b3: case ButtonKey::b3:
down_held = xTaskGetTickCount();
last_repeat = 0;
drop(); drop();
break; break;
case ButtonKey::b4: case ButtonKey::b4:
@ -219,6 +229,29 @@ bool play_game(int time, int required_score) {
return true; return true;
} }
} }
if (get_released_button(&button)) {
if (button == ButtonKey::b3) {
down_held = 0;
}
}
if (down_held != 0) {
// check repeat
TickType_t now = xTaskGetTickCount();
if (now - down_held > first_repeat_time) {
// repeat!
if (now - last_repeat > repeat_time) {
last_repeat = now;
drop();
show_board();
if (score >= required_score) {
stop_module_timer();
return true;
}
}
}
}
if (get_module_time() <= 0) { if (get_module_time() <= 0) {
stop_module_timer(); stop_module_timer();
@ -374,44 +407,89 @@ static void rotate_block(void) {
if (piece_rotation > 3) { if (piece_rotation > 3) {
piece_rotation = 0; piece_rotation = 0;
} }
get_node_locations(); get_node_locations();
bool overlap = false;
// Check overlap without moving
if (check_overlap()) {
// Check overlap after moving up 1
piece_location[0]--;
get_node_locations();
if (check_overlap()) {
// Check overlap after moving down 1
piece_location[0]++;
piece_location[0]++;
get_node_locations();
if (check_overlap()) {
// Check overlap after moving left 1
piece_location[0]--;
piece_location[1]--;
get_node_locations();
if (check_overlap()) {
// Check overlap after moving right 1
piece_location[1]++;
piece_location[1]++;
get_node_locations();
if (check_overlap()) {
piece_location[1]--;
// This is Original Position
// If Line Piece, check 2 left, 2 right, 2 up and 2 down
// Check 2 left
if (piece == 1 && piece_rotation == 0) {
piece_location[1]-=2;
get_node_locations();
if (check_overlap()) {
piece_location[1]+=2;
get_node_locations();
}
// Check 2 up
} else if (piece == 1 && piece_rotation == 1) {
piece_location[0]+=2;
get_node_locations();
if (check_overlap()) {
piece_location[0]-=2;
get_node_locations();
}
// Check 2 right
} else if (piece == 1 && piece_rotation == 2) {
piece_location[1]+=2;
get_node_locations();
if (check_overlap()) {
piece_location[1]-=2;
get_node_locations();
}
// Check 2 down
} else if (piece == 1 && piece_rotation == 3) {
piece_location[0]-=2;
get_node_locations();
if (check_overlap()) {
piece_location[0]+=2;
get_node_locations();
}
}
}
}
}
}
}
}
// Check if nodes are outside of map or interposed onto placed nodes
static bool check_overlap(void) {
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) { for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
int* p = piece_nodes[i]; int* p = piece_nodes[i];
if (p[0] < 0) { if (p[0] < 0 || p[1] < 0 || p[1] >= width) {
overlap = true; return true;
break;
} else if (board[p[0]][p[1]] != 0 && board[p[0]][p[1]] != 9) { } else if (board[p[0]][p[1]] != 0 && board[p[0]][p[1]] != 9) {
overlap = true; return true;
break;
}
}
if (overlap) {
piece_location[0]++;
get_node_locations();
bool overlap2 = false;
for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
int* p = piece_nodes[i];
if (p[0] < 0) {
overlap2 = true;
break;
} else if (board[p[0]][p[1]] != 0 && board[p[0]][p[1]] != 9) {
overlap2 = true;
break;
}
}
if (overlap2) {
piece_rotation--;
if (piece_rotation < 0) {
piece_rotation += 4;
}
piece_location[0]--;
get_node_locations();
} }
} }
return false;
} }
static void move_left(void) { static void move_left(void) {