Explorar el Código

simplify enemy/npc context and creation

jblanked hace 11 meses
padre
commit
94c5b238dd
Se han modificado 6 ficheros con 51 adiciones y 68 borrados
  1. 16 16
      game/enemy.c
  2. 1 23
      game/enemy.h
  3. 1 1
      game/level.c
  4. 10 10
      game/npc.c
  5. 0 18
      game/npc.h
  6. 23 0
      game/player.h

+ 16 - 16
game/enemy.c

@@ -2,10 +2,10 @@
 #include <game/enemy.h>
 #include <notification/notification_messages.h>
 
-static EnemyContext *enemy_context_generic;
+static EntityContext *enemy_context_generic;
 
 // Allocation function
-static EnemyContext *enemy_generic_alloc(
+static EntityContext *enemy_generic_alloc(
     const char *id,
     int index,
     Vector size,
@@ -19,11 +19,11 @@ static EnemyContext *enemy_generic_alloc(
 {
     if (!enemy_context_generic)
     {
-        enemy_context_generic = malloc(sizeof(EnemyContext));
+        enemy_context_generic = malloc(sizeof(EntityContext));
     }
     if (!enemy_context_generic)
     {
-        FURI_LOG_E("Game", "Failed to allocate EnemyContext");
+        FURI_LOG_E("Game", "Failed to allocate EntityContext");
         return NULL;
     }
     snprintf(enemy_context_generic->id, sizeof(enemy_context_generic->id), "%s", id);
@@ -77,7 +77,7 @@ static void enemy_start(Entity *self, GameManager *manager, void *context)
         return;
     }
 
-    EnemyContext *enemy_context = (EnemyContext *)context;
+    EntityContext *enemy_context = (EntityContext *)context;
     // Copy fields from generic context
     snprintf(enemy_context->id, sizeof(enemy_context->id), "%s", enemy_context_generic->id);
     enemy_context->index = enemy_context_generic->index;
@@ -109,7 +109,7 @@ static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, voi
     if (!self || !context || !canvas || !manager)
         return;
 
-    EnemyContext *enemy_context = (EnemyContext *)context;
+    EntityContext *enemy_context = (EntityContext *)context;
 
     // Get the position of the enemy
     Vector pos = entity_pos_get(self);
@@ -141,7 +141,7 @@ static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, voi
     draw_user_stats(canvas, (Vector){0, 50}, manager);
 }
 
-static void atk_notify(GameContext *game_context, EnemyContext *enemy_context, bool player_attacked)
+static void atk_notify(GameContext *game_context, EntityContext *enemy_context, bool player_attacked)
 {
     if (!game_context || !enemy_context)
     {
@@ -215,12 +215,12 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
     if (entity_description_get(other) == &player_desc)
     {
         // Retrieve enemy context
-        EnemyContext *enemy_context = (EnemyContext *)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: EnemyContext is NULL");
+            FURI_LOG_E("Game", "Enemy collision: EntityContext is NULL");
             return;
         }
         if (!game_context)
@@ -385,12 +385,12 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
 }
 
 // Enemy update function
-static void entity_update(Entity *self, GameManager *manager, void *context)
+static void enemy_update(Entity *self, GameManager *manager, void *context)
 {
     if (!self || !context || !manager)
         return;
 
-    EnemyContext *enemy_context = (EnemyContext *)context;
+    EntityContext *enemy_context = (EntityContext *)context;
     if (!enemy_context || enemy_context->state == ENTITY_DEAD)
     {
         return;
@@ -524,11 +524,11 @@ static void enemy_free(Entity *self, GameManager *manager, void *context)
 static const EntityDescription _generic_enemy = {
     .start = enemy_start,
     .stop = enemy_free,
-    .update = entity_update,
+    .update = enemy_update,
     .render = enemy_render,
     .collision = enemy_collision,
     .event = NULL,
-    .context_size = sizeof(EnemyContext),
+    .context_size = sizeof(EntityContext),
 };
 
 // Enemy function to return the entity description
@@ -551,7 +551,7 @@ const EntityDescription *enemy(
         return NULL;
     }
 
-    // Allocate a new EnemyContext with provided parameters
+    // Allocate a new EntityContext with provided parameters
     enemy_context_generic = enemy_generic_alloc(
         id,
         index,
@@ -565,7 +565,7 @@ const EntityDescription *enemy(
         health);
     if (!enemy_context_generic)
     {
-        FURI_LOG_E("Game", "Failed to allocate EnemyContext");
+        FURI_LOG_E("Game", "Failed to allocate EntityContext");
         return NULL;
     }
 
@@ -595,7 +595,7 @@ const EntityDescription *enemy(
     return &_generic_enemy;
 }
 
-void spawn_enemy_json_furi(Level *level, GameManager *manager, FuriString *json)
+void spawn_enemy(Level *level, GameManager *manager, FuriString *json)
 {
     if (!level || !manager || !json)
     {

+ 1 - 23
game/enemy.h

@@ -2,26 +2,4 @@
 #include <game/game.h>
 #include "flip_world.h"
 
-// EnemyContext definition
-typedef struct
-{
-    char id[64];                // Unique ID for the enemy type
-    int index;                  // Index for the specific enemy instance
-    Vector size;                // Size of the enemy
-    Sprite *sprite_right;       // Enemy sprite when looking right
-    Sprite *sprite_left;        // Enemy sprite when looking left
-    EntityDirection direction;  // Direction the enemy is facing
-    EntityState state;          // Current state of the enemy
-    Vector start_position;      // Start position of the enemy
-    Vector end_position;        // End position of the enemy
-    float move_timer;           // Timer for the enemy movement
-    float elapsed_move_timer;   // Elapsed time for the enemy movement
-    float radius;               // Collision radius for the enemy
-    float speed;                // Speed of the enemy
-    float attack_timer;         // Cooldown duration between attacks
-    float elapsed_attack_timer; // Time elapsed since the last attack
-    float strength;             // Damage the enemy deals
-    float health;               // Health of the enemy
-} EnemyContext;
-
-void spawn_enemy_json_furi(Level *level, GameManager *manager, FuriString *json);
+void spawn_enemy(Level *level, GameManager *manager, FuriString *json);

+ 1 - 1
game/level.c

@@ -89,7 +89,7 @@ void set_world(Level *level, GameManager *manager, char *id)
                 break;
             }
 
-            spawn_enemy_json_furi(level, manager, single_enemy_data);
+            spawn_enemy(level, manager, single_enemy_data);
             furi_string_free(single_enemy_data);
         }
         furi_string_free(enemy_data_str);

+ 10 - 10
game/npc.c

@@ -1,8 +1,8 @@
 #include <game/npc.h>
-static NPCContext *npc_context_generic;
+static EntityContext *npc_context_generic;
 
 // Allocation function
-static NPCContext *npc_generic_alloc(
+static EntityContext *npc_generic_alloc(
     const char *id,
     int index,
     Vector size,
@@ -13,11 +13,11 @@ static NPCContext *npc_generic_alloc(
 {
     if (!npc_context_generic)
     {
-        npc_context_generic = malloc(sizeof(NPCContext));
+        npc_context_generic = malloc(sizeof(EntityContext));
     }
     if (!npc_context_generic)
     {
-        FURI_LOG_E("Game", "Failed to allocate NPCContext");
+        FURI_LOG_E("Game", "Failed to allocate EntityContext");
         return NULL;
     }
     snprintf(npc_context_generic->id, sizeof(npc_context_generic->id), "%s", id);
@@ -68,7 +68,7 @@ static void npc_start(Entity *self, GameManager *manager, void *context)
         return;
     }
 
-    NPCContext *npc_context = (NPCContext *)context;
+    EntityContext *npc_context = (EntityContext *)context;
     // Copy fields from generic context
     snprintf(npc_context->id, sizeof(npc_context->id), "%s", npc_context_generic->id);
     npc_context->index = npc_context_generic->index;
@@ -97,7 +97,7 @@ static void npc_render(Entity *self, GameManager *manager, Canvas *canvas, void
     if (!self || !context || !canvas || !manager)
         return;
 
-    NPCContext *npc_context = (NPCContext *)context;
+    EntityContext *npc_context = (EntityContext *)context;
 
     // Get the position of the NPC
     Vector pos = entity_pos_get(self);
@@ -129,7 +129,7 @@ static void npc_update(Entity *self, GameManager *manager, void *context)
     if (!self || !context || !manager)
         return;
 
-    NPCContext *npc_context = (NPCContext *)context;
+    EntityContext *npc_context = (EntityContext *)context;
     if (!npc_context || npc_context->state == ENTITY_DEAD)
     {
         return;
@@ -264,7 +264,7 @@ static const EntityDescription _generic_npc = {
     .render = npc_render,
     .collision = NULL,
     .event = NULL,
-    .context_size = sizeof(NPCContext),
+    .context_size = sizeof(EntityContext),
 };
 
 // Spawn function to return the entity description
@@ -284,7 +284,7 @@ const EntityDescription *npc(
         return NULL;
     }
 
-    // Allocate a new NPCContext with provided parameters
+    // Allocate a new EntityContext with provided parameters
     npc_context_generic = npc_generic_alloc(
         id,
         index,
@@ -295,7 +295,7 @@ const EntityDescription *npc(
         speed);
     if (!npc_context_generic)
     {
-        FURI_LOG_E("Game", "Failed to allocate NPCContext");
+        FURI_LOG_E("Game", "Failed to allocate EntityContext");
         return NULL;
     }
 

+ 0 - 18
game/npc.h

@@ -2,22 +2,4 @@
 #include <game/game.h>
 #include "flip_world.h"
 
-// NPCContext definition
-typedef struct
-{
-    char id[64];               // Unique ID for the NPC type
-    int index;                 // Index for the specific NPC instance
-    Vector size;               // Size of the NPC
-    Sprite *sprite_right;      // NPC sprite when looking right
-    Sprite *sprite_left;       // NPC sprite when looking left
-    EntityDirection direction; // Direction the NPC is facing
-    EntityState state;         // Current state of the NPC
-    Vector start_position;     // Start position of the NPC
-    Vector end_position;       // End position of the NPC
-    float move_timer;          // Timer for the NPC movement
-    float elapsed_move_timer;  // Elapsed time for the NPC movement
-    float radius;              // Collision radius for the NPC
-    float speed;               // Speed of the NPC
-} NPCContext;
-
 void spawn_npc(Level *level, GameManager *manager, FuriString *json);

+ 23 - 0
game/player.h

@@ -8,6 +8,29 @@
 #define MAX_LEVELS 10
 #define MAX_NPCS 10
 
+// EntityContext definition
+typedef struct
+{
+    char id[64];                // Unique ID for the entity type
+    int index;                  // Index for the specific entity instance
+    Vector size;                // Size of the entity
+    Sprite *sprite_right;       // Entity sprite when looking right
+    Sprite *sprite_left;        // Entity sprite when looking left
+    EntityDirection direction;  // Direction the entity is facing
+    EntityState state;          // Current state of the entity
+    Vector start_position;      // Start position of the entity
+    Vector end_position;        // End position of the entity
+    float move_timer;           // Timer for the entity movement
+    float elapsed_move_timer;   // Elapsed time for the entity movement
+    float radius;               // Collision radius for the entity
+    float speed;                // Speed of the entity
+    float attack_timer;         // Cooldown duration between attacks
+    float elapsed_attack_timer; // Time elapsed since the last attack
+    float strength;             // Damage the entity deals
+    float health;               // Health of the entity
+    char message[64];           // Message to display when interacting with the entity
+} EntityContext;
+
 typedef struct
 {
     Vector old_position;        // previous position of the player