jblanked 1 год назад
Родитель
Сommit
651cad511d
3 измененных файлов с 35 добавлено и 14 удалено
  1. 23 5
      game/game.c
  2. 1 0
      game/level.c
  3. 11 9
      game/player.c

+ 23 - 5
game/game.c

@@ -15,13 +15,31 @@ static void game_start(GameManager *game_manager, void *ctx)
     game_context->player_context = NULL;
     game_context->current_level = 0;
     game_context->ended_early = false;
-    if (!allocate_level(game_manager, 0))
+    game_context->level_count = 0;
+
+    // set all levels to NULL
+    for (int i = 0; i < MAX_LEVELS; i++)
     {
-        FURI_LOG_E("Game", "Failed to allocate level 0");
-        return;
+        game_context->levels[i] = NULL;
+    }
+
+    // attempt to allocate all levels
+    for (int i = 0; i < MAX_LEVELS; i++)
+    {
+        if (!allocate_level(game_manager, i))
+        {
+            if (i == 0)
+            {
+                FURI_LOG_E("Game", "Failed to allocate level %d, loading default level", i);
+                game_context->levels[0] = game_manager_add_level(game_manager, generic_level("town_world_v2", 0));
+                game_context->level_count = 1;
+                break;
+            }
+            FURI_LOG_E("Game", "No more levels to load");
+            break;
+        }
+        game_context->level_count++;
     }
-    game_context->level_count = 1;
-    game_context->levels[1] = NULL;
 
     // imu
     game_context->imu = imu_alloc();

+ 1 - 0
game/level.c

@@ -23,6 +23,7 @@ bool allocate_level(GameManager *manager, int index)
         furi_string_free(world_list);
         return false;
     }
+    FURI_LOG_I("Game", "Allocating level %d for world %s", index, furi_string_get_cstr(world_name));
     game_context->levels[game_context->current_level] = game_manager_add_level(manager, generic_level(furi_string_get_cstr(world_name), index));
     furi_string_free(world_name);
     furi_string_free(world_list);

+ 11 - 9
game/player.c

@@ -9,16 +9,21 @@ static Level *get_next_level(GameManager *manager)
         FURI_LOG_E(TAG, "Failed to get game context");
         return NULL;
     }
-    game_context->current_level = game_context->current_level == 0 ? 1 : 0;
-    if (!game_context->levels[game_context->current_level])
+    for (int i = game_context->current_level + 1; i < game_context->level_count; i++)
     {
-        if (!allocate_level(manager, game_context->current_level))
+        if (!game_context->levels[i])
         {
-            FURI_LOG_E(TAG, "Failed to allocate level %d", game_context->current_level);
-            return NULL;
+            if (!allocate_level(manager, i))
+            {
+                FURI_LOG_E(TAG, "Failed to allocate level %d", i);
+                return NULL;
+            }
         }
+        game_context->current_level = i;
+        return game_context->levels[i];
     }
-    return game_context->levels[game_context->current_level];
+    FURI_LOG_I(TAG, "No more levels to load");
+    return NULL;
 }
 
 void player_spawn(Level *level, GameManager *manager)
@@ -262,7 +267,6 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     // switch levels if holding OK
     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)
@@ -274,11 +278,9 @@ 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);
         }
-        FURI_LOG_I(TAG, "Player is done pressing OK");
     }
 
     // If the player is not moving, retain the last movement direction