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

add player regen and set max health per level

jblanked 1 год назад
Родитель
Сommit
177d05c6e8
2 измененных файлов с 15 добавлено и 3 удалено
  1. 11 2
      game/game.c
  2. 4 1
      game/game.h

+ 11 - 2
game/game.c

@@ -25,8 +25,6 @@ void player_spawn(Level *level, GameManager *manager)
     game_context->players[0] = level_add_entity(level, &player_desc);
 
     // Set player position.
-
-    // Depends on your game logic, it can be done in start entity function, but also can be done here.
     entity_pos_set(game_context->players[0], (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
 
     // Add collision box to player entity
@@ -47,6 +45,9 @@ void player_spawn(Level *level, GameManager *manager)
     player_context->start_position = entity_pos_get(game_context->players[0]);
     player_context->attack_timer = 0.5f;
     player_context->elapsed_attack_timer = player_context->attack_timer;
+    player_context->health_regen = 1; // 1 health per second
+    player_context->elapsed_health_regen = 0;
+    player_context->max_health = 100 + (player_context->level * 10); // 10 health per level
 
     // Set player username
     if (!load_char("Flip-Social-Username", player_context->username, 32))
@@ -65,6 +66,14 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     Vector pos = entity_pos_get(self);
     GameContext *game_context = game_manager_game_context_get(manager);
 
+    // apply health regeneration
+    player->elapsed_health_regen += 1.0f / game_context->fps;
+    if (player->elapsed_health_regen >= 1.0f)
+    {
+        player->health += player->health_regen;
+        player->elapsed_health_regen = 0;
+    }
+
     // Increment the elapsed_attack_timer for the player
     player->elapsed_attack_timer += 1.0f / game_context->fps;
 

+ 4 - 1
game/game.h

@@ -37,8 +37,11 @@ typedef struct
     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
+    uint32_t health;            // player health
+    uint32_t max_health;        // player maximum health
+    uint32_t health_regen;      // player health regeneration rate per second/frame
+    float elapsed_health_regen; // time elapsed since last health regeneration
     float attack_timer;         // Cooldown duration between attacks
     float elapsed_attack_timer; // Time elapsed since the last attack
     char username[32];          // player username