Bläddra i källkod

Add second enemy, handle NULL pointer

jblanked 1 år sedan
förälder
incheckning
59562efadb
2 ändrade filer med 21 tillägg och 14 borttagningar
  1. 8 14
      game/enemy.c
  2. 13 0
      game/game.c

+ 8 - 14
game/enemy.c

@@ -54,21 +54,15 @@ static EnemyContext *enemy_generic_alloc(
 // Free function
 static void enemy_generic_free(void *context)
 {
-    if (context != NULL)
+    if (!context)
     {
-        EnemyContext *enemy_context = (EnemyContext *)context;
-
-        // Free sprites if they were dynamically loaded
-        if (enemy_context->sprite_right)
-        {
-            sprite_free(enemy_context->sprite_right);
-        }
-        if (enemy_context->sprite_left)
-        {
-            sprite_free(enemy_context->sprite_left);
-        }
-
-        free(context);
+        FURI_LOG_E("Game", "Enemy generic free: Invalid context");
+        return;
+    }
+    if (enemy_context_generic)
+    {
+        free(enemy_context_generic);
+        enemy_context_generic = NULL;
     }
 }
 

+ 13 - 0
game/game.c

@@ -205,6 +205,19 @@ static void game_start(GameManager *game_manager, void *ctx)
                                       10,
                                       10,
                                       100));
+
+    // add another enemy
+    level_add_entity(levels[0], enemy(game_manager,
+                                      "player",
+                                      1,
+                                      (Vector){10, 10},
+                                      (Vector){WORLD_WIDTH / 2 + 11, WORLD_HEIGHT / 2 + 32},
+                                      (Vector){WORLD_WIDTH / 2 - 11, WORLD_HEIGHT / 2 + 32},
+                                      1,
+                                      32,
+                                      10,
+                                      10,
+                                      100));
 }
 
 /*