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

fix enemy->icon collision and unnecessary freeing

jblanked 11 месяцев назад
Родитель
Сommit
bf0812557c
3 измененных файлов с 41 добавлено и 51 удалено
  1. 34 32
      game/enemy.c
  2. 1 2
      game/icon.c
  3. 6 17
      game/npc.c

+ 34 - 32
game/enemy.c

@@ -47,21 +47,6 @@ static EntityContext *enemy_generic_alloc(
     return enemy_context_generic;
 }
 
-// Free function
-static void enemy_generic_free(void *context)
-{
-    if (context)
-    {
-        free(context);
-        context = NULL;
-    }
-    if (enemy_context_generic)
-    {
-        free(enemy_context_generic);
-        enemy_context_generic = NULL;
-    }
-}
-
 // Enemy start function
 static void enemy_start(Entity *self, GameManager *manager, void *context)
 {
@@ -210,24 +195,13 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
         FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
         return;
     }
-
+    EntityContext *enemy_context = (EntityContext *)context;
+    furi_check(enemy_context, "Enemy collision: EntityContext is NULL");
+    GameContext *game_context = game_manager_game_context_get(manager);
+    furi_check(game_context, "Enemy collision: GameContext is NULL");
     // Check if the enemy collided with the player
     if (entity_description_get(other) == &player_desc)
     {
-        // Retrieve enemy context
-        EntityContext *enemy_context = (EntityContext *)context;
-        GameContext *game_context = game_manager_game_context_get(manager);
-        // InputState input = game_manager_input_get(manager);
-        if (!enemy_context)
-        {
-            FURI_LOG_E("Game", "Enemy collision: EntityContext is NULL");
-            return;
-        }
-        if (!game_context)
-        {
-            FURI_LOG_E("Game", "Enemy collision: GameContext is NULL");
-            return;
-        }
 
         // Get positions of the enemy and the player
         Vector enemy_pos = entity_pos_get(self);
@@ -382,6 +356,30 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
             game_context->player_context->health = game_context->player_context->max_health;
         }
     }
+    // if not player than must be an icon or npc; so push back
+    else
+    {
+        // push enemy back
+        Vector enemy_pos = entity_pos_get(self);
+        switch (enemy_context->direction)
+        {
+        case ENTITY_LEFT:
+            enemy_pos.x += (enemy_context->size.x + game_context->icon_offset);
+            break;
+        case ENTITY_RIGHT:
+            enemy_pos.x -= (enemy_context->size.x + game_context->icon_offset);
+            break;
+        case ENTITY_UP:
+            enemy_pos.y += (enemy_context->size.y + game_context->icon_offset);
+            break;
+        case ENTITY_DOWN:
+            enemy_pos.y -= (enemy_context->size.y + game_context->icon_offset);
+            break;
+        default:
+            break;
+        }
+        entity_pos_set(self, enemy_pos);
+    }
 }
 
 // Enemy update function
@@ -516,8 +514,12 @@ static void enemy_free(Entity *self, GameManager *manager, void *context)
 {
     UNUSED(self);
     UNUSED(manager);
-    if (context)
-        enemy_generic_free(context);
+    UNUSED(context);
+    if (enemy_context_generic)
+    {
+        free(enemy_context_generic);
+        enemy_context_generic = NULL;
+    }
 }
 
 // Enemy behavior structure

+ 1 - 2
game/icon.c

@@ -75,8 +75,7 @@ static void icon_free(Entity *self, GameManager *manager, void *context)
 {
     UNUSED(self);
     UNUSED(manager);
-    if (context)
-        free(context);
+    UNUSED(context);
 }
 
 // -------------- Entity description --------------

+ 6 - 17
game/npc.c

@@ -40,21 +40,6 @@ static EntityContext *npc_generic_alloc(
     return npc_context_generic;
 }
 
-// Free function
-static void npc_generic_free(void *context)
-{
-    if (context)
-    {
-        free(context);
-        context = NULL;
-    }
-    if (npc_context_generic)
-    {
-        free(npc_context_generic);
-        npc_context_generic = NULL;
-    }
-}
-
 // NPC start function
 static void npc_start(Entity *self, GameManager *manager, void *context)
 {
@@ -306,8 +291,12 @@ static void npc_free(Entity *self, GameManager *manager, void *context)
 {
     UNUSED(self);
     UNUSED(manager);
-    if (context)
-        npc_generic_free(context);
+    UNUSED(context);
+    if (npc_context_generic)
+    {
+        free(npc_context_generic);
+        npc_context_generic = NULL;
+    }
 }
 
 // NPC Behavior structure