Просмотр исходного кода

save data when switch lvl, fix typos, improve load

jblanked 1 год назад
Родитель
Сommit
8f6b120127
5 измененных файлов с 65 добавлено и 57 удалено
  1. 1 0
      callback/callback.c
  2. 2 1
      flip_world.c
  3. 2 1
      flip_world.h
  4. 8 8
      game/enemy.c
  5. 52 47
      game/player.c

+ 1 - 0
callback/callback.c

@@ -583,6 +583,7 @@ 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)

+ 2 - 1
flip_world.c

@@ -4,4 +4,5 @@ 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;
+FlipWorldApp *app_instance = NULL;
+bool player_context_loaded = false;

+ 2 - 1
flip_world.h

@@ -79,4 +79,5 @@ 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 FlipWorldApp *app_instance;
+extern bool player_context_loaded;

+ 8 - 8
game/enemy.c

@@ -221,9 +221,9 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
 
                 // Increase healthy by 10% of the enemy's strength
                 game_context->player_context->health += enemy_context->strength * 0.1f;
-                if (game_context->player_context->health > 100)
+                if (game_context->player_context->health > game_context->player_context->max_health)
                 {
-                    game_context->player_context->health = 100;
+                    game_context->player_context->health = game_context->player_context->max_health;
                 }
 
                 // Decrease enemy health by player strength
@@ -235,7 +235,7 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
                     enemy_context->state = ENEMY_DEAD;
 
                     // Reset enemy position and health
-                    enemy_context->health = 100;
+                    enemy_context->health = 100; // this needs to be set to the enemy's max health
 
                     // remove from game context and set in safe zone
                     game_context->enemies[enemy_context->index] = NULL;
@@ -284,7 +284,7 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
 
                     // Reset player position and health
                     entity_pos_set(other, game_context->player_context->start_position);
-                    game_context->player_context->health = 100;
+                    game_context->player_context->health = game_context->player_context->max_health;
 
                     // subtract player's XP by the enemy's strength
                     game_context->player_context->xp -= enemy_context->strength;
@@ -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;
-            player_pos.y -= direction_vector.y * enemy_context->radius;
+            player_pos.x -= direction_vector.x * (enemy_context->radius / 2);
+            player_pos.y -= direction_vector.y * (enemy_context->radius / 2);
             entity_pos_set(other, player_pos);
 
-            enemy_pos.x += direction_vector.x * enemy_context->radius;
-            enemy_pos.y += direction_vector.y * enemy_context->radius;
+            enemy_pos.x += direction_vector.x * (enemy_context->radius / 2);
+            enemy_pos.y += direction_vector.y * (enemy_context->radius / 2);
             entity_pos_set(self, enemy_pos);
 
             // Reset player's movement direction to prevent immediate re-collision

+ 52 - 47
game/player.c

@@ -44,62 +44,65 @@ void player_spawn(Level *level, GameManager *manager)
     // Set player position.
     entity_pos_set(game_context->players[0], (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
 
-    // Add collision box to player entity
-    // Box is centered in player x and y, and its size is 10x10
-    entity_collider_add_rect(game_context->players[0], 10, 10);
+    // Box is centered in player x and y, and its size
+    entity_collider_add_rect(game_context->players[0], 13, 11);
 
-    // 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;
-    }
-
-    FURI_LOG_I(TAG, "Loading player context");
-    if (!load_player_context(player_context))
+    if (!player_context_loaded)
     {
-        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_naked_10x10px.fxbm");
-        player_context->sprite_left = game_manager_sprite_load(manager, "player_left_naked_10x10px.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)))
+        player_context_loaded = true;
+        // Get player context
+        PlayerContext *player_context = entity_context_get(game_context->players[0]);
+        if (!player_context)
         {
-            // If loading username fails, default to "Player"
-            snprintf(player_context->username, sizeof(player_context->username), "Player");
+            FURI_LOG_E(TAG, "Failed to get player context");
+            return;
         }
 
-        game_context->player_context = player_context;
-
-        // Save the initialized context
-        if (!save_player_context(player_context))
+        FURI_LOG_I(TAG, "Loading player context");
+        if (!load_player_context(player_context))
         {
-            FURI_LOG_E(TAG, "Failed to save player context after initialization");
+            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_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");
 
-        return;
+        // Assign loaded player context to game context
+        game_context->player_context = player_context;
     }
-    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_naked_10x10px.fxbm");
-    player_context->sprite_left = game_manager_sprite_load(manager, "player_left_naked_10x10px.fxbm");
-
-    // Assign loaded player context to game context
-    game_context->player_context = player_context;
 }
 
 // Modify player_update to track direction
@@ -173,6 +176,8 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         // otherwise the "OK" button will be used to attack
         if (game_context->enemy_count == 0)
         {
+            FURI_LOG_I(TAG, "Switching levels");
+            save_player_context(player);
             game_manager_next_level_set(manager, get_next_level(manager));
             furi_delay_ms(500);
         }