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

subtract health when attacked, update attributes

jblanked 1 год назад
Родитель
Сommit
89a459381f
3 измененных файлов с 62 добавлено и 36 удалено
  1. 20 15
      game/enemy.c
  2. 16 8
      game/game.c
  3. 26 13
      game/game.h

+ 20 - 15
game/enemy.c

@@ -166,20 +166,6 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
             return;
         }
 
-        // Decrease player health
-        GameContext *game_context = game_manager_game_context_get(manager);
-        if (game_context)
-        {
-            // 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");
-        }
-
         // Get positions of the enemy and the player
         Vector enemy_pos = entity_pos_get(self);
         Vector player_pos = entity_pos_get(other);
@@ -200,7 +186,26 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
         if (is_facing_player)
         {
             FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
-            // Future Implementation: Apply damage to the player here
+
+            // Decrease player health
+            GameContext *game_context = game_manager_game_context_get(manager);
+            if (game_context)
+            {
+                if (game_context->player->health <= 0)
+                {
+                    FURI_LOG_I("Game", "Player is dead");
+                    game_context->player->state = PLAYER_DEAD;
+                }
+                else
+                {
+                    game_context->player->health -= enemy_context->strength;
+                    FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
+                }
+            }
+            else
+            {
+                FURI_LOG_E("Game", "Enemy collision: Failed to get GameContext");
+            }
         }
 
         // Reset enemy's position and state

+ 16 - 8
game/game.c

@@ -35,14 +35,17 @@ 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");
-    player_context->is_looking_left = false; // player starts looking right
+    player_context->direction = PLAYER_RIGHT; // default direction
+    player_context->health = 100;
+    player_context->strength = 10;
+    player_context->level = 1;
+    player_context->xp = 0;
+
+    GameContext *game_context = game_manager_game_context_get(manager);
+    game_context->player = player_context;
 }
 
 // Modify player_update to track direction
@@ -75,13 +78,13 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     {
         pos.x -= 2;
         player->dx = -1;
-        player->is_looking_left = true;
+        player->direction = PLAYER_LEFT;
     }
     if (input.held & GameKeyRight)
     {
         pos.x += 2;
         player->dx = 1;
-        player->is_looking_left = false;
+        player->direction = PLAYER_RIGHT;
     }
 
     // switch levels if holding OK
@@ -108,6 +111,11 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     {
         player->dx = prev_dx;
         player->dy = prev_dy;
+        player->state = PLAYER_IDLE;
+    }
+    else
+    {
+        player->state = PLAYER_MOVING;
     }
 
     // Handle back button to stop the game
@@ -132,7 +140,7 @@ static void player_render(Entity *self, GameManager *manager, Canvas *canvas, vo
     // Draw player sprite relative to camera, centered on the player's position
     canvas_draw_sprite(
         canvas,
-        player->is_looking_left ? player->sprite_left : player->sprite_right,
+        player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
         pos.x - camera_x - 5, // Center the sprite horizontally
         pos.y - camera_y - 5  // Center the sprite vertically
     );

+ 26 - 13
game/game.h

@@ -9,21 +9,34 @@
 #define PLAYER_COLLISION_VERTICAL 5
 #define PLAYER_COLLISION_HORIZONTAL 5
 
+typedef enum
+{
+    PLAYER_IDLE,
+    PLAYER_MOVING,
+    PLAYER_ATTACKING,
+    PLAYER_DEAD,
+} PlayerState;
+
+typedef enum
+{
+    PLAYER_UP,
+    PLAYER_DOWN,
+    PLAYER_LEFT,
+    PLAYER_RIGHT
+} PlayerDirection;
+
 typedef struct
 {
-    Vector trajectory;    // Direction player would like to move.
-    float radius;         // collision radius
-    int8_t dx;            // x direction
-    int8_t dy;            // y direction
-    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
+    PlayerDirection direction; // direction the player is facing
+    PlayerState state;         // current state of the player
+    Sprite *sprite_right;      // player sprite looking right
+    Sprite *sprite_left;       // player sprite looking left
+    int8_t dx;                 // x direction
+    int8_t dy;                 // y direction
+    uint32_t xp;               // experience points
+    uint32_t level;            // player level
+    uint32_t health;           // player health
+    uint32_t strength;         // player strength
 } PlayerContext;
 
 typedef struct