jblanked пре 1 година
родитељ
комит
ede0f9c860
9 измењених фајлова са 89 додато и 63 уклоњено
  1. 0 1
      callback/callback.c
  2. 1 2
      flip_world.c
  3. 1 2
      flip_world.h
  4. 4 4
      game/enemy.c
  5. 6 1
      game/game.c
  6. 23 1
      game/level.c
  7. 43 48
      game/player.c
  8. 9 2
      game/world.c
  9. 2 2
      game/world.h

+ 0 - 1
callback/callback.c

@@ -583,7 +583,6 @@ void free_all_views(void *context, bool should_free_variable_item_list, bool sho
         game_thread_running = false;
         furi_thread_flags_set(thread_id, WorkerEvtStop);
         furi_thread_free(thread_id);
-        player_context_loaded = false;
     }
 
     if (should_free_submenu_settings)

+ 1 - 2
flip_world.c

@@ -4,5 +4,4 @@ const float game_fps_choices_2[] = {30.0, 60.0, 120.0, 240.0};
 int game_fps_index = 1;
 char *game_screen_always_on_choices[] = {"No", "Yes"};
 int game_screen_always_on_index = 1;
-FlipWorldApp *app_instance = NULL;
-bool player_context_loaded = false;
+FlipWorldApp *app_instance = NULL;

+ 1 - 2
flip_world.h

@@ -79,5 +79,4 @@ extern const float game_fps_choices_2[];
 extern int game_fps_index;
 extern char *game_screen_always_on_choices[];
 extern int game_screen_always_on_index;
-extern FlipWorldApp *app_instance;
-extern bool player_context_loaded;
+extern FlipWorldApp *app_instance;

+ 4 - 4
game/enemy.c

@@ -333,12 +333,12 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
             }
 
             // Move the player and enemy away from each other
-            player_pos.x -= direction_vector.x * (enemy_context->radius / 2);
-            player_pos.y -= direction_vector.y * (enemy_context->radius / 2);
+            player_pos.x -= direction_vector.x * 3;
+            player_pos.y -= direction_vector.y * 3;
             entity_pos_set(other, player_pos);
 
-            enemy_pos.x += direction_vector.x * (enemy_context->radius / 2);
-            enemy_pos.y += direction_vector.y * (enemy_context->radius / 2);
+            enemy_pos.x += direction_vector.x * 3;
+            enemy_pos.y += direction_vector.y * 3;
             entity_pos_set(self, enemy_pos);
 
             // Reset player's movement direction to prevent immediate re-collision

+ 6 - 1
game/game.c

@@ -56,7 +56,12 @@ static void game_stop(void *ctx)
     }
 
     GameContext *game_context = ctx;
-    if (game_context && game_context->player_context)
+    if (!game_context)
+    {
+        FURI_LOG_E("Game", "Game context is NULL");
+        return;
+    }
+    if (game_context->player_context)
     {
         easy_flipper_dialog("Game Over", "Thanks for playing Flip World!\nHit BACK then wait for\nthe game to save.");
         FURI_LOG_I("Game", "Saving player context");

+ 23 - 1
game/level.c

@@ -8,6 +8,7 @@ void set_world(Level *level, GameManager *manager, char *id)
              STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
              id, id);
 
+    FURI_LOG_I("Game", "Loading world data from %s", file_path);
     FuriString *json_data_str = flipper_http_load_from_file(file_path);
     if (!json_data_str || furi_string_empty(json_data_str))
     {
@@ -15,7 +16,13 @@ void set_world(Level *level, GameManager *manager, char *id)
         draw_town_world(level);
         return;
     }
+    size_t heap_size = memmgr_get_free_heap();
+    size_t total_heap_size = memmgr_get_total_heap();
 
+    FURI_LOG_I(TAG, "Heap size: %d", heap_size);
+    FURI_LOG_I(TAG, "Total heap size: %d", total_heap_size);
+
+    FURI_LOG_I("Game", "Drawing world");
     if (!draw_json_world_furi(level, json_data_str))
     {
         FURI_LOG_E("Game", "Failed to draw world");
@@ -24,11 +31,12 @@ void set_world(Level *level, GameManager *manager, char *id)
     }
     else
     {
+        FURI_LOG_I("Game", "Drawing enemies");
         furi_string_free(json_data_str);
         snprintf(file_path, sizeof(file_path),
                  STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
                  id, id);
-
+        FURI_LOG_I("Game", "Loading enemy data from %s", file_path);
         FuriString *enemy_data_str = flipper_http_load_from_file(file_path);
         if (!enemy_data_str || furi_string_empty(enemy_data_str))
         {
@@ -36,6 +44,7 @@ void set_world(Level *level, GameManager *manager, char *id)
             draw_town_world(level);
             return;
         }
+        FURI_LOG_I("Game", "Looping through enemy data");
         // Loop through the array
         for (int i = 0; i < MAX_ENEMIES; i++)
         {
@@ -43,6 +52,8 @@ void set_world(Level *level, GameManager *manager, char *id)
             if (!single_enemy_data || furi_string_empty(single_enemy_data))
             {
                 // No more enemy elements found
+                if (single_enemy_data)
+                    furi_string_free(single_enemy_data);
                 break;
             }
 
@@ -50,6 +61,7 @@ void set_world(Level *level, GameManager *manager, char *id)
             furi_string_free(single_enemy_data);
         }
         furi_string_free(enemy_data_str);
+        FURI_LOG_I("Game", "Finished loading world data");
     }
 }
 
@@ -63,7 +75,13 @@ static void level_start(Level *level, GameManager *manager, void *context)
 
     level_clear(level);
     player_spawn(level, manager);
+
     LevelContext *level_context = context;
+    if (!level_context)
+    {
+        FURI_LOG_E("Game", "Level context is NULL");
+        return;
+    }
 
     // check if the world exists
     if (!world_exists(level_context->id))
@@ -79,10 +97,14 @@ static void level_start(Level *level, GameManager *manager, void *context)
         furi_string_free(world_data);
 
         set_world(level, manager, level_context->id);
+
+        FURI_LOG_I("Game", "World set.");
     }
     else
     {
+        FURI_LOG_I("Game", "World exists.. loading now");
         set_world(level, manager, level_context->id);
+        FURI_LOG_I("Game", "World set.");
     }
 }
 

+ 43 - 48
game/player.c

@@ -47,62 +47,57 @@ void player_spawn(Level *level, GameManager *manager)
     // Box is centered in player x and y, and its size
     entity_collider_add_rect(game_context->players[0], 13, 11);
 
-    if (!player_context_loaded)
+    // Get player context
+    PlayerContext *player_context = entity_context_get(game_context->players[0]);
+    if (!player_context)
+    {
+        FURI_LOG_E(TAG, "Failed to get player context");
+        return;
+    }
+
+    // player context must be set each level or NULL pointer will be dereferenced
+    if (!load_player_context(player_context))
     {
-        player_context_loaded = true;
-        // Get player context
-        PlayerContext *player_context = entity_context_get(game_context->players[0]);
-        if (!player_context)
+        FURI_LOG_E(TAG, "Loading player context failed. Initializing default values.");
+
+        // Initialize default player context
+        player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
+        player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
+        player_context->direction = PLAYER_RIGHT; // default direction
+        player_context->health = 100;
+        player_context->strength = 10;
+        player_context->level = 1;
+        player_context->xp = 0;
+        player_context->start_position = entity_pos_get(game_context->players[0]);
+        player_context->attack_timer = 0.1f;
+        player_context->elapsed_attack_timer = player_context->attack_timer;
+        player_context->health_regen = 1; // 1 health per second
+        player_context->elapsed_health_regen = 0;
+        player_context->max_health = 100 + ((player_context->level - 1) * 10); // 10 health per level
+
+        // Set player username
+        if (!load_char("Flip-Social-Username", player_context->username, sizeof(player_context->username)))
         {
-            FURI_LOG_E(TAG, "Failed to get player context");
-            return;
+            // If loading username fails, default to "Player"
+            snprintf(player_context->username, sizeof(player_context->username), "Player");
         }
 
-        FURI_LOG_I(TAG, "Loading player context");
-        if (!load_player_context(player_context))
+        game_context->player_context = player_context;
+
+        // Save the initialized context
+        if (!save_player_context(player_context))
         {
-            FURI_LOG_E(TAG, "Loading player context failed. Initializing default values.");
-
-            // Initialize default player context
-            player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
-            player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
-            player_context->direction = PLAYER_RIGHT; // default direction
-            player_context->health = 100;
-            player_context->strength = 10;
-            player_context->level = 1;
-            player_context->xp = 0;
-            player_context->start_position = entity_pos_get(game_context->players[0]);
-            player_context->attack_timer = 0.1f;
-            player_context->elapsed_attack_timer = player_context->attack_timer;
-            player_context->health_regen = 1; // 1 health per second
-            player_context->elapsed_health_regen = 0;
-            player_context->max_health = 100 + ((player_context->level - 1) * 10); // 10 health per level
-
-            // Set player username
-            if (!load_char("Flip-Social-Username", player_context->username, sizeof(player_context->username)))
-            {
-                // If loading username fails, default to "Player"
-                snprintf(player_context->username, sizeof(player_context->username), "Player");
-            }
-
-            game_context->player_context = player_context;
-
-            // Save the initialized context
-            if (!save_player_context(player_context))
-            {
-                FURI_LOG_E(TAG, "Failed to save player context after initialization");
-            }
-
-            return;
+            FURI_LOG_E(TAG, "Failed to save player context after initialization");
         }
-        FURI_LOG_I(TAG, "Player context loaded successfully");
-        // Load player sprite (we'll add this to the JSON later when players can choose their sprite)
-        player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
-        player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
 
-        // Assign loaded player context to game context
-        game_context->player_context = player_context;
+        return;
     }
+    // Load player sprite (we'll add this to the JSON later when players can choose their sprite)
+    player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
+    player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
+
+    // Assign loaded player context to game context
+    game_context->player_context = player_context;
 }
 
 // Modify player_update to track direction

+ 9 - 2
game/world.c

@@ -79,12 +79,18 @@ bool draw_json_world(Level *level, const char *json_data)
     return levels_added > 0;
 }
 
-bool draw_json_world_furi(Level *level, FuriString *json_data)
+bool draw_json_world_furi(Level *level, const FuriString *json_data)
 {
+    if (!json_data)
+    {
+        FURI_LOG_E("Game", "JSON data is NULL");
+        return false;
+    }
     int levels_added = 0;
-
+    FURI_LOG_I("Game", "Looping through world data");
     for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
     {
+        FURI_LOG_I("Game", "Looping through world data: %d", i);
         FuriString *data = get_json_array_value_furi("json_data", i, json_data);
         if (!data)
         {
@@ -148,6 +154,7 @@ bool draw_json_world_furi(Level *level, FuriString *json_data)
         furi_string_free(horizontal);
         levels_added++;
     }
+    FURI_LOG_I("Game", "Finished loading world data");
     return levels_added > 0;
 }
 

+ 2 - 2
game/world.h

@@ -9,11 +9,11 @@
 #define WORLD_HEIGHT 192
 
 // Maximum number of world objects
-#define MAX_WORLD_OBJECTS 100
+#define MAX_WORLD_OBJECTS 25 // any more than that and we may run out of heap when switching worlds
 
 void draw_bounds(Canvas *canvas);
 void draw_tree_world(Level *level);
 void draw_town_world(Level *level);
 bool draw_json_world(Level *level, const char *json_data);
-bool draw_json_world_furi(Level *level, FuriString *json_data);
+bool draw_json_world_furi(Level *level, const FuriString *json_data);
 FuriString *fetch_world(const char *name);