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

fix null pointer error + player position on death

jblanked 1 год назад
Родитель
Сommit
3b972deec5
5 измененных файлов с 26 добавлено и 27 удалено
  1. 19 16
      game/enemy.c
  2. 4 10
      game/game.c
  3. 1 0
      game/level.c
  4. 2 0
      game/player.c
  5. 0 1
      game/player.h

+ 19 - 16
game/enemy.c

@@ -4,8 +4,6 @@
 #include <stdio.h>
 #include <math.h>
 
-#define EPSILON 0.1f
-
 static EnemyContext *enemy_context_generic;
 
 // Allocation function
@@ -161,27 +159,29 @@ static void send_attack_notification(GameContext *game_context, EnemyContext *en
         return;
     }
 
-    bool vibration_allowed = strstr(yes_or_no_choices[game_vibration_on_index], "Yes") != NULL;
-    bool sound_allowed = strstr(yes_or_no_choices[game_sound_on_index], "Yes") != NULL;
+    NotificationApp *notifications = furi_record_open(RECORD_NOTIFICATION);
+
+    const bool vibration_allowed = strstr(yes_or_no_choices[game_vibration_on_index], "Yes") != NULL;
+    const bool sound_allowed = strstr(yes_or_no_choices[game_sound_on_index], "Yes") != NULL;
 
     if (player_attacked)
     {
         if (vibration_allowed && sound_allowed)
         {
-            notification_message(game_context->notifications, &sequence_success);
+            notification_message(notifications, &sequence_success);
         }
         else if (vibration_allowed && !sound_allowed)
         {
-            notification_message(game_context->notifications, &sequence_single_vibro);
+            notification_message(notifications, &sequence_single_vibro);
         }
         else if (!vibration_allowed && sound_allowed)
         {
             // change this to sound later
-            notification_message(game_context->notifications, &sequence_blink_blue_100);
+            notification_message(notifications, &sequence_blink_blue_100);
         }
         else
         {
-            notification_message(game_context->notifications, &sequence_blink_blue_100);
+            notification_message(notifications, &sequence_blink_blue_100);
         }
         FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
     }
@@ -189,24 +189,27 @@ static void send_attack_notification(GameContext *game_context, EnemyContext *en
     {
         if (vibration_allowed && sound_allowed)
         {
-            notification_message(game_context->notifications, &sequence_error);
+            notification_message(notifications, &sequence_error);
         }
         else if (vibration_allowed && !sound_allowed)
         {
-            notification_message(game_context->notifications, &sequence_single_vibro);
+            notification_message(notifications, &sequence_single_vibro);
         }
         else if (!vibration_allowed && sound_allowed)
         {
             // change this to sound later
-            notification_message(game_context->notifications, &sequence_blink_red_100);
+            notification_message(notifications, &sequence_blink_red_100);
         }
         else
         {
-            notification_message(game_context->notifications, &sequence_blink_red_100);
+            notification_message(notifications, &sequence_blink_red_100);
         }
 
         FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
     }
+
+    // close the notifications
+    furi_record_close(RECORD_NOTIFICATION);
 }
 
 // Enemy collision function
@@ -448,8 +451,8 @@ static void enemy_update(Entity *self, GameManager *manager, void *context)
         {
             // Determine the next state based on the current position
             Vector current_pos = entity_pos_get(self);
-            if (fabs(current_pos.x - enemy_context->start_position.x) < (double)EPSILON &&
-                fabs(current_pos.y - enemy_context->start_position.y) < (double)EPSILON)
+            if (fabs(current_pos.x - enemy_context->start_position.x) < (double)1.0 &&
+                fabs(current_pos.y - enemy_context->start_position.y) < (double)1.0)
             {
                 enemy_context->state = ENEMY_MOVING_TO_END;
             }
@@ -523,8 +526,8 @@ static void enemy_update(Entity *self, GameManager *manager, void *context)
         entity_pos_set(self, new_pos);
 
         // Check if the enemy has reached or surpassed the target_position
-        bool reached_x = fabs(new_pos.x - target_position.x) < (double)EPSILON;
-        bool reached_y = fabs(new_pos.y - target_position.y) < (double)EPSILON;
+        bool reached_x = fabs(new_pos.x - target_position.x) < (double)1.0;
+        bool reached_y = fabs(new_pos.y - target_position.y) < (double)1.0;
 
         // If reached the target position on both axes, transition to IDLE
         if (reached_x && reached_y)

+ 4 - 10
game/game.c

@@ -16,9 +16,6 @@ static void game_start(GameManager *game_manager, void *ctx)
     game_context->current_level = 0;
     allocate_level(game_manager, 0);
 
-    // Notifications - for LED light access
-    game_context->notifications = furi_record_open(RECORD_NOTIFICATION);
-
     // imu
     game_context->imu = imu_alloc();
     game_context->imu_present = imu_present(game_context->imu);
@@ -43,13 +40,10 @@ static void game_stop(void *ctx)
         FURI_LOG_E("Game", "Game context is NULL");
         return;
     }
-    if (game_context->imu)
-    {
-        imu_free(game_context->imu);
-        game_context->imu = NULL;
-    }
-    // close the notifications
-    furi_record_close(RECORD_NOTIFICATION);
+
+    imu_free(game_context->imu);
+    game_context->imu = NULL;
+
     if (game_context->player_context)
     {
         FURI_LOG_I("Game", "Game ending");

+ 1 - 0
game/level.c

@@ -77,6 +77,7 @@ static void set_world(Level *level, GameManager *manager, char *id)
         GameContext *game_context = game_manager_game_context_get(manager);
         game_context->ended_early = true;
         game_manager_game_stop(manager); // end game early
+        furi_string_free(json_data_str);
         return;
     }
 

+ 2 - 0
game/player.c

@@ -99,6 +99,8 @@ void player_spawn(Level *level, GameManager *manager)
     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->start_position = entity_pos_get(game_context->player);
+
     // Assign loaded player context to game context
     game_context->player_context = player_context;
 }

+ 0 - 1
game/player.h

@@ -59,7 +59,6 @@ typedef struct
     int enemy_count;
     int current_level;
     bool ended_early;
-    NotificationApp *notifications;
     Imu *imu;
     bool imu_present;
 } GameContext;