Преглед изворни кода

Take away health when enemy attacks player

damage done is the absolute value of the strength of the enemy subtracted by the strength of the player
jblanked пре 1 година
родитељ
комит
3cfbe089d5
3 измењених фајлова са 68 додато и 23 уклоњено
  1. 51 10
      game/enemy.c
  2. 5 1
      game/game.c
  3. 12 12
      game/game.h

+ 51 - 10
game/enemy.c

@@ -147,27 +147,68 @@ static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, voi
 // Enemy collision function
 static void enemy_collision(Entity *self, Entity *other, GameManager *manager, void *context)
 {
-    UNUSED(self);
-    UNUSED(context);
+    // Ensure that 'self', 'other', and 'context' are valid
+    if (!self || !other || !context)
+    {
+        FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
+        return;
+    }
 
     // Check if the enemy collided with the player
     if (entity_description_get(other) == &player_desc)
     {
-        // Increase score
+
+        // Retrieve enemy context
+        EnemyContext *enemy_context = (EnemyContext *)context;
+        if (!enemy_context)
+        {
+            FURI_LOG_E("Game", "Enemy collision: EnemyContext is NULL");
+            return;
+        }
+
+        // Decrease player health
         GameContext *game_context = game_manager_game_context_get(manager);
         if (game_context)
         {
-            game_context->xp++;
+            // damage done is the absolute value of the strength of the enemy subtracted by the strength of the player
+            double damage_done = fabs(enemy_context->strength - game_context->player->strength);
+            game_context->player->health -= damage_done;
+            FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", damage_done, enemy_context->id);
+        }
+        else
+        {
+            FURI_LOG_E("Game", "Enemy collision: Failed to get GameContext");
         }
 
-        // Move enemy to original position or handle respawn logic
-        EnemyContext *enemy_context = (EnemyContext *)context;
-        if (enemy_context)
+        // Get positions of the enemy and the player
+        Vector enemy_pos = entity_pos_get(self);
+        Vector player_pos = entity_pos_get(other);
+
+        // Determine if the enemy is facing the player
+        bool is_facing_player = false;
+
+        if (enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x)
         {
-            entity_pos_set(self, enemy_context->start_position);
-            enemy_context->state = ENEMY_IDLE;
-            enemy_context->elapsed_move_timer = 0.0f;
+            is_facing_player = true;
+        }
+        else if (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x)
+        {
+            is_facing_player = true;
         }
+
+        // If the enemy is facing the player, perform an attack (log message)
+        if (is_facing_player)
+        {
+            FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
+            // Future Implementation: Apply damage to the player here
+        }
+
+        // Reset enemy's position and state
+        entity_pos_set(self, enemy_context->start_position);
+        enemy_context->state = ENEMY_IDLE;
+        enemy_context->elapsed_move_timer = 0.0f;
+
+        FURI_LOG_D("Game", "Enemy '%s' reset to start position after collision", enemy_context->id);
     }
 }
 

+ 5 - 1
game/game.c

@@ -35,6 +35,10 @@ void player_spawn(Level *level, GameManager *manager)
     // Get player context
     PlayerContext *player_context = entity_context_get(player);
 
+    // add player context to game context
+    GameContext *game_context = game_manager_game_context_get(manager);
+    game_context->player = player_context;
+
     // Load player sprite
     player_context->sprite_right = game_manager_sprite_load(manager, "player_right.fxbm");
     player_context->sprite_left = game_manager_sprite_load(manager, "player_left.fxbm");
@@ -154,8 +158,8 @@ static void game_start(GameManager *game_manager, void *ctx)
     // Do some initialization here, for example you can load score from storage.
     // For simplicity, we will just set it to 0.
     GameContext *game_context = ctx;
-    game_context->level = 1; // we'll load this from SD later
     game_context->fps = game_fps_choices_2[game_fps_index];
+    game_context->player = NULL;
 
     // open the world list from storage, then create a level for each world
     char file_path[128];

+ 12 - 12
game/game.h

@@ -9,18 +9,6 @@
 #define PLAYER_COLLISION_VERTICAL 5
 #define PLAYER_COLLISION_HORIZONTAL 5
 
-typedef struct
-{
-    float fps;
-    //
-    uint32_t xp;
-    uint32_t level;
-    uint32_t health;
-    //
-    uint32_t strength; // for later uppdate
-    uint32_t defense;  // for later uppdate
-} GameContext;
-
 typedef struct
 {
     Vector trajectory;    // Direction player would like to move.
@@ -30,8 +18,20 @@ typedef struct
     Sprite *sprite_right; // player sprite
     Sprite *sprite_left;  // player sprite looking left
     bool is_looking_left; // player is looking left
+                          //
+    uint32_t xp;
+    uint32_t level;
+    uint32_t health;
+    //
+    uint32_t strength; // for later uppdate
 } PlayerContext;
 
+typedef struct
+{
+    float fps;
+    PlayerContext *player;
+} GameContext;
+
 extern const EntityDescription player_desc;
 void player_spawn(Level *level, GameManager *manager);