diff --git a/applications/plugins/game15/README.md b/applications/plugins/game15/README.md index ec6b08634..493983180 100644 --- a/applications/plugins/game15/README.md +++ b/applications/plugins/game15/README.md @@ -1,8 +1,12 @@ -#Game "15" for Flipper Zero + +# Game "15" for Flipper Zero Logic game [Wikipedia](https://en.wikipedia.org/wiki/15_puzzle) ![Game screen](images/Game15.png) -FAP file for firmware 0.69.1 +![Restore game](images/Game15Restore.png) +![Popoup](images/Game15Popup.png) + +FAP file for firmware 0.69.1 diff --git a/applications/plugins/game15/game15.c b/applications/plugins/game15/game15.c index 939b49e6e..7e1053b4c 100644 --- a/applications/plugins/game15/game15.c +++ b/applications/plugins/game15/game15.c @@ -11,8 +11,9 @@ #define CELL_HEIGHT 8 #define MOVE_TICKS 5 #define KEY_STACK_SIZE 16 -#define TOP_RECORD_DIRECTORY "/ext/apps/Games" -#define TOP_RECORD_FILENAME TOP_RECORD_DIRECTORY "/game15.top" +#define SAVING_DIRECTORY "/ext/apps/Games" +#define SAVING_FILENAME SAVING_DIRECTORY "/game15.save" +#define POPUP_MENU_ITEMS 2 typedef enum { DirectionNone, @@ -22,7 +23,7 @@ typedef enum { DirectionRight } direction_e; -typedef enum { ScenePlay, SceneWin } scene_e; +typedef enum { ScenePlay, SceneWin, ScenePopup } scene_e; typedef struct { uint8_t cell_index; @@ -31,13 +32,24 @@ typedef struct { uint8_t move_ticks; } moving_cell_t; -static scene_e scene; -static uint8_t board[16]; -static uint16_t top_record; -static uint16_t move_count; -static uint32_t tick_count; +typedef struct { + uint16_t top_record; + scene_e scene; + uint16_t move_count; + uint32_t tick_count; + uint8_t board[16]; +} game_state_t; + +static game_state_t game_state; static NotificationApp* notification; static moving_cell_t moving_cell; +static uint8_t loaded_saving_ticks; +static uint8_t popup_menu_selected_item; + +static const char* popup_menu_strings[] = { + "Continue", + "Reset" +}; static uint8_t keys[KEY_STACK_SIZE]; static uint8_t key_stack_head = 0; @@ -105,29 +117,32 @@ static int key_stack_push(uint8_t value) { return -1; } -static void storage_top_record_load() { +static bool storage_game_state_load() { Storage* storage = furi_record_open(RECORD_STORAGE); File* file = storage_file_alloc(storage); - if(storage_file_open(file, TOP_RECORD_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING)) - storage_file_read(file, &top_record, 2); + uint16_t bytes_readed = 0; + if(storage_file_open(file, SAVING_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING)) + bytes_readed = storage_file_read(file, &game_state, sizeof(game_state_t)); storage_file_close(file); storage_file_free(file); furi_record_close(RECORD_STORAGE); + return bytes_readed == sizeof(game_state_t); } -static void storage_top_record_save() { +static void storage_game_state_save() { Storage* storage = furi_record_open(RECORD_STORAGE); - if(storage_common_stat(storage, TOP_RECORD_DIRECTORY, NULL) == FSE_NOT_EXIST) { - if(!storage_simply_mkdir(storage, TOP_RECORD_DIRECTORY)) { + if(storage_common_stat(storage, SAVING_DIRECTORY, NULL) == FSE_NOT_EXIST) { + if(!storage_simply_mkdir(storage, SAVING_DIRECTORY)) { return; } } File* file = storage_file_alloc(storage); - if(storage_file_open(file, TOP_RECORD_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) - storage_file_write(file, &top_record, 2); + if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) { + storage_file_write(file, &game_state, sizeof(game_state_t)); + } storage_file_close(file); storage_file_free(file); furi_record_close(RECORD_STORAGE); @@ -138,7 +153,7 @@ static void set_moving_cell_by_direction(direction_e direction) { moving_cell.zero_index = 0xff; for(int i = 0; i < 16; i++) { - if(!board[i]) { + if(!game_state.board[i]) { moving_cell.zero_index = i; break; } @@ -168,51 +183,52 @@ static void set_moving_cell_by_direction(direction_e direction) { static bool is_board_has_solution() { uint8_t i, j, inv = 0; for(i = 0; i < 16; ++i) - if(board[i]) + if(game_state.board[i]) for(j = 0; j < i; ++j) - if(board[j] > board[i]) ++inv; + if(game_state.board[j] > game_state.board[i]) ++inv; for(i = 0; i < 16; ++i) - if(board[i] == 0) inv += 1 + i / 4; + if(game_state.board[i] == 0) inv += 1 + i / 4; return inv % 2 == 0; } static void board_init() { for(int i = 0; i < 16; i++) { - board[i] = (i + 1) % 16; + game_state.board[i] = (i + 1) % 16; } do { for(int i = 15; i >= 1; i--) { int j = rand() % (i + 1); - uint8_t tmp = board[j]; - board[j] = board[i]; - board[i] = tmp; + uint8_t tmp = game_state.board[j]; + game_state.board[j] = game_state.board[i]; + game_state.board[i] = tmp; } } while(!is_board_has_solution()); } static void game_init() { - scene = ScenePlay; - move_count = 0; - tick_count = 0; - storage_top_record_load(); + game_state.scene = ScenePlay; + game_state.move_count = 0; + game_state.tick_count = 0; moving_cell.move_direction = DirectionNone; board_init(); key_stack_init(); + popup_menu_selected_item = 0; } static bool is_board_solved() { for(int i = 0; i < 16; i++) - if(((i + 1) % 16) != board[i]) return false; + if(((i + 1) % 16) != game_state.board[i]) return false; return true; } static void game_tick() { - switch(scene) { + switch(game_state.scene) { case ScenePlay: - tick_count++; - + game_state.tick_count++; + if (loaded_saving_ticks) + loaded_saving_ticks--; if(moving_cell.move_direction == DirectionNone && !key_stack_is_empty()) { set_moving_cell_by_direction(key_stack_pop()); if(moving_cell.move_direction == DirectionNone) { @@ -224,18 +240,19 @@ static void game_tick() { if(moving_cell.move_direction != DirectionNone) { moving_cell.move_ticks++; if(moving_cell.move_ticks == MOVE_TICKS) { - board[moving_cell.zero_index] = board[moving_cell.cell_index]; - board[moving_cell.cell_index] = 0; + game_state.board[moving_cell.zero_index] = + game_state.board[moving_cell.cell_index]; + game_state.board[moving_cell.cell_index] = 0; moving_cell.move_direction = DirectionNone; - move_count++; + game_state.move_count++; } if(is_board_solved()) { notification_message(notification, &sequence_double_vibro); - if(move_count < top_record || top_record == 0) { - top_record = move_count; - storage_top_record_save(); + if(game_state.move_count < game_state.top_record || game_state.top_record == 0) { + game_state.top_record = game_state.move_count; + storage_game_state_save(); } - scene = SceneWin; + game_state.scene = SceneWin; } } break; @@ -243,6 +260,32 @@ static void game_tick() { case SceneWin: if(!key_stack_is_empty()) game_init(); break; + + case ScenePopup: + if (!key_stack_is_empty()) { + switch(key_stack_pop()) + { + case DirectionDown: + popup_menu_selected_item++; + popup_menu_selected_item = popup_menu_selected_item % POPUP_MENU_ITEMS; + break; + case DirectionUp: + popup_menu_selected_item--; + popup_menu_selected_item = popup_menu_selected_item % POPUP_MENU_ITEMS; + break; + case DirectionNone: + if (popup_menu_selected_item == 0) { + game_state.scene = ScenePlay; + notification_message(notification, &sequence_single_vibro); + } + else if (popup_menu_selected_item == 1) { + notification_message(notification, &sequence_single_vibro); + game_init(); + } + break; + } + } + break; } } @@ -255,9 +298,9 @@ static void draw_cell(Canvas* canvas, uint8_t x, uint8_t y, uint8_t cell_number) static void board_draw(Canvas* canvas) { for(int i = 0; i < 16; i++) { - if(board[i]) { + if(game_state.board[i]) { if(moving_cell.move_direction == DirectionNone || moving_cell.cell_index != i) - draw_cell(canvas, (i % 4) * 20 + 7, (i / 4) * 16 + 1, board[i]); + draw_cell(canvas, (i % 4) * 20 + 7, (i / 4) * 16 + 1, game_state.board[i]); if(moving_cell.move_direction != DirectionNone && moving_cell.cell_index == i) { uint8_t from_x = (moving_cell.cell_index % 4) * 20 + 7; uint8_t from_y = (moving_cell.cell_index / 4) * 16 + 1; @@ -265,7 +308,7 @@ static void board_draw(Canvas* canvas) { uint8_t to_y = (moving_cell.zero_index / 4) * 16 + 1; int now_x = from_x + (to_x - from_x) * moving_cell.move_ticks / MOVE_TICKS; int now_y = from_y + (to_y - from_y) * moving_cell.move_ticks / MOVE_TICKS; - draw_cell(canvas, now_x, now_y, board[i]); + draw_cell(canvas, now_x, now_y, game_state.board[i]); } } } @@ -296,32 +339,40 @@ static void plate_draw( } static void info_draw(Canvas* canvas) { - plate_draw(canvas, 1, pic_top, top_record, true); - plate_draw(canvas, 22, pic_move, move_count, false); - plate_draw(canvas, 43, pic_time, tick_count / FPS, false); + plate_draw(canvas, 1, pic_top, game_state.top_record, true); + plate_draw(canvas, 22, pic_move, game_state.move_count, false); + plate_draw(canvas, 43, pic_time, game_state.tick_count / FPS, false); +} + +static void gray_screen(Canvas* const canvas) { + canvas_set_color(canvas, ColorWhite); + for(int x = 0; x < 128; x += 2) { + for(int y = 0; y < 64; y++) { + canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y); + } + } } static void render_callback(Canvas* const canvas) { canvas_set_color(canvas, ColorWhite); canvas_draw_box(canvas, 0, 0, 128, 64); - if(scene == ScenePlay || scene == SceneWin) { + if(game_state.scene == ScenePlay || game_state.scene == SceneWin || game_state.scene == ScenePopup) { canvas_set_color(canvas, ColorBlack); board_draw(canvas); info_draw(canvas); - canvas_set_color(canvas, ColorWhite); - canvas_draw_box(canvas, 0, 0, 128, 64); - canvas_set_color(canvas, ColorBlack); - canvas_draw_str(canvas, 10, 10, "0123456789"); - } - if(scene == SceneWin) { - canvas_set_color(canvas, ColorWhite); - for(int x = 0; x < 128; x += 2) { - for(int y = 0; y < 64; y++) { - canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y); - } + if (loaded_saving_ticks && game_state.scene != ScenePopup) { + canvas_set_color(canvas, ColorWhite); + canvas_draw_rbox(canvas, 20, 24, 88, 16, 4); + canvas_set_color(canvas, ColorBlack); + canvas_draw_rframe(canvas, 20, 24, 88, 16, 4); + canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, "Restore game ..."); } + } + + if(game_state.scene == SceneWin) { + gray_screen(canvas); canvas_draw_box(canvas, 7, 20, 114, 24); canvas_set_color(canvas, ColorBlack); canvas_draw_box(canvas, 8, 21, 112, 22); @@ -330,6 +381,23 @@ static void render_callback(Canvas* const canvas) { canvas_set_color(canvas, ColorBlack); canvas_draw_xbm(canvas, 14, 27, 100, 10, pic_puzzled); } + else if (game_state.scene == ScenePopup) { + gray_screen(canvas); + canvas_set_color(canvas, ColorWhite); + canvas_draw_rbox(canvas, 28, 16, 72, 32, 4); + canvas_set_color(canvas, ColorBlack); + canvas_draw_rframe(canvas, 28, 16, 72, 32, 4); + + for(int i=0; i < POPUP_MENU_ITEMS; i++) { + if ( i == popup_menu_selected_item) { + canvas_set_color(canvas, ColorBlack); + canvas_draw_box(canvas, 34, 20 + 12 * i, 60, 12); + } + + canvas_set_color(canvas, i == popup_menu_selected_item ? ColorWhite : ColorBlack); + canvas_draw_str_aligned(canvas, 64, 26 + 12 * i, AlignCenter, AlignCenter, popup_menu_strings[i]); + } + } } static void game_event_handler(GameEvent const event) { @@ -349,10 +417,21 @@ static void game_event_handler(GameEvent const event) { key_stack_push(DirectionLeft); break; case InputKeyOk: - key_stack_push(DirectionNone); + if (game_state.scene == ScenePlay) { + game_state.scene = ScenePopup; + key_stack_init(); + } + else + key_stack_push(DirectionNone); break; case InputKeyBack: - sandbox_loop_exit(); + if (game_state.scene == ScenePopup) { + game_state.scene = ScenePlay; + } + else { + storage_game_state_save(); + sandbox_loop_exit(); + } break; } } @@ -376,6 +455,17 @@ static void game_free() { int32_t game15_app() { game_alloc(); game_init(); + + loaded_saving_ticks = 0; + if(storage_game_state_load()) { + if (game_state.scene != ScenePlay) + game_init(); + else + loaded_saving_ticks = FPS; + } + else + game_init(); + sandbox_init( FPS, (SandboxRenderCallback)render_callback, (SandboxEventHandler)game_event_handler); sandbox_loop(); diff --git a/applications/plugins/game15/game15.fap b/applications/plugins/game15/game15.fap deleted file mode 100644 index 2348c1fbb..000000000 Binary files a/applications/plugins/game15/game15.fap and /dev/null differ diff --git a/applications/plugins/game15/images/Game15Popup.png b/applications/plugins/game15/images/Game15Popup.png new file mode 100644 index 000000000..1df14729f Binary files /dev/null and b/applications/plugins/game15/images/Game15Popup.png differ diff --git a/applications/plugins/game15/images/Game15Restore.png b/applications/plugins/game15/images/Game15Restore.png new file mode 100644 index 000000000..05aac27f6 Binary files /dev/null and b/applications/plugins/game15/images/Game15Restore.png differ