فهرست منبع

fix furi_check failed and change game loading

jblanked 1 سال پیش
والد
کامیت
3a862041fd
6فایلهای تغییر یافته به همراه85 افزوده شده و 33 حذف شده
  1. 58 11
      callback/callback.c
  2. 4 6
      flipper_http/flipper_http.c
  3. 1 1
      game/enemy.c
  4. 1 0
      game/game.c
  5. 20 14
      game/player.c
  6. 1 1
      game/player.h

+ 58 - 11
callback/callback.c

@@ -688,8 +688,8 @@ static bool start_game_thread(void *context)
     furi_thread_start(thread);
     thread_id = furi_thread_get_id(thread);
     game_thread_running = true;
-    view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
-    view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventPlay);
+    // view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
+    // view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventPlay);
     return true;
 }
 static bool flip_world_fetch_world_list(DataLoaderModel *model)
@@ -698,13 +698,6 @@ static bool flip_world_fetch_world_list(DataLoaderModel *model)
 }
 static char *flip_world_parse_world_list(DataLoaderModel *model)
 {
-    // free game thread
-    if (game_thread_running)
-    {
-        game_thread_running = false;
-        furi_thread_flags_set(thread_id, WorkerEvtStop);
-        furi_thread_free(thread_id);
-    }
     FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
 
     if (!start_game_thread(app))
@@ -716,7 +709,7 @@ static char *flip_world_parse_world_list(DataLoaderModel *model)
     }
     return "Game starting... please wait :D";
 }
-static void flip_world_world_list_switch_to_view(FlipWorldApp *app)
+void flip_world_world_list_switch_to_view(FlipWorldApp *app)
 {
     return flip_world_generic_switch_to_view(app, "Fetching World List..", flip_world_fetch_world_list, flip_world_parse_world_list, 1, callback_to_submenu, FlipWorldViewLoader);
 }
@@ -1024,8 +1017,62 @@ void callback_submenu_choices(void *context, uint32_t index)
         // check if logged in
         if (is_logged_in() || is_logged_in_to_flip_social())
         {
+            FlipperHTTP *fhttp = flipper_http_alloc();
+            if (!fhttp)
+            {
+                FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
+                easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
+                return;
+            }
+            bool fetch_world_list_i()
+            {
+                return fetch_world_list(fhttp);
+            }
+            bool parse_world_list_i()
+            {
+                return fhttp->state != ISSUE;
+            }
+
+            Loading *loading;
+            int32_t loading_view_id = 987654321; // Random ID
+
+            loading = loading_alloc();
+            if (!loading)
+            {
+                FURI_LOG_E(HTTP_TAG, "Failed to allocate loading");
+                view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
+                flipper_http_free(fhttp);
+                return;
+            }
+
+            view_dispatcher_add_view(app->view_dispatcher, loading_view_id, loading_get_view(loading));
+
+            // Switch to the loading view
+            view_dispatcher_switch_to_view(app->view_dispatcher, loading_view_id);
+
+            // Make the request
+            if (!flipper_http_process_response_async(fhttp, fetch_world_list_i, parse_world_list_i))
+            {
+                FURI_LOG_E(HTTP_TAG, "Failed to make request");
+                view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
+                view_dispatcher_remove_view(app->view_dispatcher, loading_view_id);
+                loading_free(loading);
+                flipper_http_free(fhttp);
+            }
+            else
+            {
+                view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
+                view_dispatcher_remove_view(app->view_dispatcher, loading_view_id);
+                loading_free(loading);
+                flipper_http_free(fhttp);
 
-            flip_world_world_list_switch_to_view(app);
+                if (!start_game_thread(app))
+                {
+                    FURI_LOG_E(TAG, "Failed to start game thread");
+                    easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
+                    return;
+                }
+            }
         }
         else
         {

+ 4 - 6
flipper_http/flipper_http.c

@@ -421,7 +421,7 @@ FlipperHTTP *flipper_http_alloc()
 
     fhttp->state = IDLE;
 
-    FURI_LOG_I(HTTP_TAG, "UART initialized successfully.");
+    // FURI_LOG_I(HTTP_TAG, "UART initialized successfully.");
     return fhttp;
 }
 
@@ -480,7 +480,7 @@ void flipper_http_free(FlipperHTTP *fhttp)
     free(fhttp);
     fhttp = NULL;
 
-    FURI_LOG_I("FlipperHTTP", "UART deinitialized successfully.");
+    // FURI_LOG_I("FlipperHTTP", "UART deinitialized successfully.");
 }
 
 // Function to send data over UART with newline termination
@@ -530,7 +530,7 @@ bool flipper_http_send_data(FlipperHTTP *fhttp, const char *data)
     furi_hal_serial_tx(fhttp->serial_handle, (const uint8_t *)send_buffer, send_length);
 
     // Uncomment below line to log the data sent over UART
-    FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
+    // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
     fhttp->state = IDLE;
     return true;
 }
@@ -1290,8 +1290,6 @@ void flipper_http_rx_callback(const char *line, void *context)
         return;
     }
 
-    FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
-
     // Trim the received line to check if it's empty
     char *trimmed_line = trim(line);
     if (trimmed_line != NULL && trimmed_line[0] != '\0')
@@ -1313,7 +1311,7 @@ void flipper_http_rx_callback(const char *line, void *context)
     }
 
     // Uncomment below line to log the data received over UART
-    FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
+    // FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
 
     // Check if we've started receiving data from a GET request
     if (fhttp->started_receiving_get)

+ 1 - 1
game/enemy.c

@@ -212,7 +212,7 @@ static void send_attack_notification(GameContext *game_context, EnemyContext *en
 // Enemy collision function
 static void enemy_collision(Entity *self, Entity *other, GameManager *manager, void *context)
 {
-    if (!self || !other || !context)
+    if (!self || !other || !context || !manager)
     {
         FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
         return;

+ 1 - 0
game/game.c

@@ -14,6 +14,7 @@ static void game_start(GameManager *game_manager, void *ctx)
     game_context->fps = game_fps_choices_2[game_fps_index];
     game_context->player_context = NULL;
     game_context->current_level = 0;
+    game_context->ended_early = false;
     if (!allocate_level(game_manager, 0))
     {
         FURI_LOG_E("Game", "Failed to allocate level 0");

+ 20 - 14
game/player.c

@@ -133,9 +133,11 @@ static int player_y_from_roll(float roll)
     return 0;
 }
 
-// Modify player_update to track direction
 static void player_update(Entity *self, GameManager *manager, void *context)
 {
+    if (!self || !manager || !context)
+        return;
+
     PlayerContext *player = (PlayerContext *)context;
     InputState input = game_manager_input_get(manager);
     Vector pos = entity_pos_get(self);
@@ -154,43 +156,42 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         player->dx = player_x_from_pitch(-imu_pitch_get(game_context->imu));
         player->dy = player_y_from_roll(-imu_roll_get(game_context->imu));
 
-        switch (player_x_from_pitch(-imu_pitch_get(game_context->imu)))
+        switch (player->dx)
         {
         case -1:
             player->direction = PLAYER_LEFT;
-            player->dx = -1;
             pos.x -= 1;
             break;
         case 1:
             player->direction = PLAYER_RIGHT;
-            player->dx = 1;
             pos.x += 1;
             break;
         default:
             break;
-        };
-        switch (player_y_from_roll(-imu_roll_get(game_context->imu)))
+        }
+
+        switch (player->dy)
         {
         case -1:
             player->direction = PLAYER_UP;
-            player->dy = -1;
             pos.y -= 1;
             break;
         case 1:
             player->direction = PLAYER_DOWN;
-            player->dy = 1;
             pos.y += 1;
             break;
         default:
             break;
-        };
+        }
     }
 
-    // apply health regeneration
+    // Apply health regeneration
     player->elapsed_health_regen += 1.0f / game_context->fps;
     if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
     {
-        player->health += (player->health_regen + player->health > player->max_health) ? player->max_health - player->health : player->health_regen;
+        player->health += (player->health_regen + player->health > player->max_health)
+                              ? (player->max_health - player->health)
+                              : player->health_regen;
         player->elapsed_health_regen = 0;
     }
 
@@ -235,8 +236,9 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     entity_pos_set(self, pos);
 
     // switch levels if holding OK
-    if (input.held & GameKeyOk)
+    if (input.pressed & GameKeyOk)
     {
+        FURI_LOG_I(TAG, "Player is pressing OK");
         // if all enemies are dead, allow the "OK" button to switch levels
         // otherwise the "OK" button will be used to attack
         if (game_context->enemy_count == 0)
@@ -248,9 +250,11 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         }
         else
         {
+            FURI_LOG_I(TAG, "Player is attacking");
             game_context->user_input = GameKeyOk;
+            // furi_delay_ms(100);
         }
-        return;
+        FURI_LOG_I(TAG, "Player is done pressing OK");
     }
 
     // If the player is not moving, retain the last movement direction
@@ -275,8 +279,10 @@ static void player_update(Entity *self, GameManager *manager, void *context)
 
 static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
 {
-    // Get player context
     UNUSED(manager);
+    if (!self || !context || !canvas)
+        return;
+    // Get player context
     PlayerContext *player = context;
 
     // Get player position

+ 1 - 1
game/player.h

@@ -6,7 +6,7 @@
 
 // Maximum enemies
 #define MAX_ENEMIES 2
-#define MAX_LEVELS 5
+#define MAX_LEVELS 10
 
 typedef enum
 {