From a9d9d747fbac238e40f73d52d53adc893944f2a3 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Sat, 10 Aug 2024 22:04:31 -0500 Subject: [PATCH] Add repeating down button --- main/steps/step1.cpp | 6 +- main/steps/step2.cpp | 152 ++++++++++++++++++++++++++++++++----------- 2 files changed, 118 insertions(+), 40 deletions(-) diff --git a/main/steps/step1.cpp b/main/steps/step1.cpp index 1116c0d..c4b5ae6 100644 --- a/main/steps/step1.cpp +++ b/main/steps/step1.cpp @@ -266,11 +266,11 @@ void step1(void) { while (get_flipped_switch(nullptr)); init_step(); - while (!play_part(30*1000)); + while (!play_part(40*1000)); part = 1; - while (!play_part(25*1000)); + while (!play_part(35*1000)); part = 2; - while (!play_part(22*1000)); + while (!play_part(30*1000)); clean_up_step(); // TODO: flash lights diff --git a/main/steps/step2.cpp b/main/steps/step2.cpp index efe7aab..4467802 100644 --- a/main/steps/step2.cpp +++ b/main/steps/step2.cpp @@ -54,13 +54,14 @@ lv_obj_t* piece_imgs[4] = {}; TaskHandle_t music_handle; -std::random_device rd; -std::mt19937 gen(rd()); -std::uniform_int_distribution<> piece_dist(2, 7); +static std::random_device rd; +static std::mt19937 gen(rd()); +static std::uniform_int_distribution<> piece_dist(2, 7); static void generate_block(void); static void show_board(void); static void get_node_locations(void); +static bool check_overlap(void); static void line_clear(int hi); static void check_line_clears(void); @@ -73,6 +74,7 @@ static void rotate_block(void); static void update_score(void); static void clear_board(void); + static int bbcc(int i) { // [0,1,2,3] -> [ 0, 0,+1,+1] const int map[] = {0, 0, 1, 1}; return map[i]; @@ -168,7 +170,7 @@ static void init_screen(void) { 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) { @@ -177,10 +179,11 @@ static void deinit_screen(void) { lv_obj_clean(lv_scr_act()); xSemaphoreGive(xGuiSemaphore); - vTaskDelete(music_handle); + // vTaskDelete(music_handle); } bool play_game(int time, int required_score) { + game = true; target_score = required_score; score = 0; update_score(); @@ -195,6 +198,11 @@ bool play_game(int time, int required_score) { while (get_pressed_button(&button)); // 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) { if (get_pressed_button(&button)) { switch (button) { @@ -205,6 +213,8 @@ bool play_game(int time, int required_score) { move_right(); break; case ButtonKey::b3: + down_held = xTaskGetTickCount(); + last_repeat = 0; drop(); break; case ButtonKey::b4: @@ -219,6 +229,29 @@ bool play_game(int time, int required_score) { 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) { stop_module_timer(); @@ -374,44 +407,89 @@ static void rotate_block(void) { if (piece_rotation > 3) { piece_rotation = 0; } - 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++) { int* p = piece_nodes[i]; - if (p[0] < 0) { - overlap = true; - break; + if (p[0] < 0 || p[1] < 0 || p[1] >= width) { + return true; } else if (board[p[0]][p[1]] != 0 && board[p[0]][p[1]] != 9) { - overlap = 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 true; } } + return false; } static void move_left(void) {