فهرست منبع

Update attack timer duration

jblanked 1 سال پیش
والد
کامیت
2d39eac866
4فایلهای تغییر یافته به همراه115 افزوده شده و 115 حذف شده
  1. 63 73
      game/enemy.c
  2. 17 16
      game/enemy.h
  3. 22 15
      game/game.c
  4. 13 11
      game/game.h

+ 63 - 73
game/enemy.c

@@ -152,7 +152,6 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
     // Check if the enemy collided with the player
     if (entity_description_get(other) == &player_desc)
     {
-
         // Retrieve enemy context
         EnemyContext *enemy_context = (EnemyContext *)context;
         GameContext *game_context = game_manager_game_context_get(manager);
@@ -175,101 +174,94 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
         bool enemy_is_facing_player = false;
         bool player_is_facing_enemy = false;
 
-        // setting enemy_is_facing_player to true if the enemy is facing the player
-        if (enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x)
+        // Determine if the enemy is facing the player
+        if ((enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x) ||
+            (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x) ||
+            (enemy_context->direction == ENEMY_UP && player_pos.y < enemy_pos.y) ||
+            (enemy_context->direction == ENEMY_DOWN && player_pos.y > enemy_pos.y))
         {
             enemy_is_facing_player = true;
-            enemy_context->state = ENEMY_ATTACKING;
-        }
-        else if (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x)
-        {
-            enemy_is_facing_player = true;
-            enemy_context->state = ENEMY_ATTACKING;
-        }
-        else if (enemy_context->direction == ENEMY_UP && player_pos.y < enemy_pos.y)
-        {
-            enemy_is_facing_player = true;
-            enemy_context->state = ENEMY_ATTACKING;
-        }
-        else if (enemy_context->direction == ENEMY_DOWN && player_pos.y > enemy_pos.y)
-        {
-            enemy_is_facing_player = true;
-            enemy_context->state = ENEMY_ATTACKING;
         }
 
-        // setting player_is_facing_enemy to true if the player is facing the enemy
-        if (game_context->player_context->direction == PLAYER_LEFT && enemy_pos.x < player_pos.x)
-        {
-            player_is_facing_enemy = true;
-        }
-        else if (game_context->player_context->direction == PLAYER_RIGHT && enemy_pos.x > player_pos.x)
-        {
-            player_is_facing_enemy = true;
-        }
-        else if (game_context->player_context->direction == PLAYER_UP && enemy_pos.y < player_pos.y)
-        {
-            player_is_facing_enemy = true;
-        }
-        else if (game_context->player_context->direction == PLAYER_DOWN && enemy_pos.y > player_pos.y)
+        // Determine if the player is facing the enemy
+        if ((game_context->player_context->direction == PLAYER_LEFT && enemy_pos.x < player_pos.x) ||
+            (game_context->player_context->direction == PLAYER_RIGHT && enemy_pos.x > player_pos.x) ||
+            (game_context->player_context->direction == PLAYER_UP && enemy_pos.y < player_pos.y) ||
+            (game_context->player_context->direction == PLAYER_DOWN && enemy_pos.y > player_pos.y))
         {
             player_is_facing_enemy = true;
         }
 
-        // If the player is facing the enemy and pressed OK, perform an attack (log message)
+        // Handle Player Attacking Enemy
         if (player_is_facing_enemy && game_context->user_input == GameKeyOk)
         {
-            FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
+            if (game_context->player_context->elapsed_attack_timer >= game_context->player_context->attack_timer)
+            {
+                FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
 
-            // increase xp by the enemy's strength
-            game_context->player_context->xp += enemy_context->strength;
+                // Reset player's elapsed attack timer
+                game_context->player_context->elapsed_attack_timer = 0.0f;
 
-            // Decrease enemy health by player strength
-            if (enemy_context->health <= 0)
-            {
-                FURI_LOG_I("Game", "Enemy '%s' is dead.. resetting enemy position and health", enemy_context->id);
-                enemy_context->state = ENEMY_DEAD;
+                // Increase XP by the enemy's strength
+                game_context->player_context->xp += enemy_context->strength;
 
-                // reset enemy position and health
-                entity_pos_set(self, enemy_context->start_position);
-                enemy_context->health = 100;
+                // Decrease enemy health by player strength
+                enemy_context->health -= game_context->player_context->strength;
+
+                if (enemy_context->health <= 0)
+                {
+                    FURI_LOG_I("Game", "Enemy '%s' is dead.. resetting enemy position and health", enemy_context->id);
+                    enemy_context->state = ENEMY_DEAD;
+
+                    // Reset enemy position and health
+                    entity_pos_set(self, enemy_context->start_position);
+                    enemy_context->health = 100;
+                }
+                else
+                {
+                    FURI_LOG_I("Game", "Enemy '%s' took %f damage from player", enemy_context->id, (double)game_context->player_context->strength);
+                    enemy_context->state = ENEMY_ATTACKED;
+                }
             }
             else
             {
-                FURI_LOG_I("Game", "Enemy '%s' took %f damage from player", enemy_context->id, (double)game_context->player_context->strength);
-                enemy_context->health -= game_context->player_context->strength;
-                enemy_context->state = ENEMY_ATTACKED;
+                FURI_LOG_I("Game", "Player attack on enemy '%s' is on cooldown: %f seconds remaining", enemy_context->id, (double)(game_context->player_context->attack_timer - game_context->player_context->elapsed_attack_timer));
             }
         }
 
-        // If the enemy is facing the player, perform an attack (log message)
+        // Handle Enemy Attacking Player
         if (enemy_is_facing_player)
         {
-            FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
-
-            // Decrease player health by enemy strength
-            if (game_context->player_context->health <= 0)
+            if (enemy_context->elapsed_attack_timer >= enemy_context->attack_timer)
             {
-                FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
-                game_context->player_context->state = PLAYER_DEAD;
+                FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
+
+                // Reset enemy's elapsed attack timer
+                enemy_context->elapsed_attack_timer = 0.0f;
 
-                // reset player position and health
-                entity_pos_set(other, game_context->player_context->start_position);
-                game_context->player_context->health = 100;
+                // Decrease player health by enemy strength
+                game_context->player_context->health -= enemy_context->strength;
+
+                if (game_context->player_context->health <= 0)
+                {
+                    FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
+                    game_context->player_context->state = PLAYER_DEAD;
+
+                    // Reset player position and health
+                    entity_pos_set(other, game_context->player_context->start_position);
+                    game_context->player_context->health = 100;
+                }
+                else
+                {
+                    FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
+                    game_context->player_context->state = PLAYER_ATTACKED;
+                }
             }
             else
             {
-                FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
-                game_context->player_context->health -= enemy_context->strength;
-                game_context->player_context->state = PLAYER_ATTACKED;
+                FURI_LOG_I("Game", "Enemy '%s' attack on player is on cooldown: %f seconds remaining", enemy_context->id, (double)(enemy_context->attack_timer - enemy_context->elapsed_attack_timer));
             }
         }
-
-        // 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);
     }
 }
 
@@ -290,6 +282,9 @@ static void enemy_update(Entity *self, GameManager *manager, void *context)
 
     float delta_time = 1.0f / game_context->fps;
 
+    // Increment the elapsed_attack_timer for the enemy
+    enemy_context->elapsed_attack_timer += delta_time;
+
     switch (enemy_context->state)
     {
     case ENEMY_IDLE:
@@ -311,8 +306,6 @@ static void enemy_update(Entity *self, GameManager *manager, void *context)
                 enemy_context->state = ENEMY_MOVING_TO_START;
             }
             enemy_context->elapsed_move_timer = 0.0f;
-
-            FURI_LOG_D("Game", "Enemy %s transitioning to state %d", enemy_context->id, enemy_context->state);
         }
         break;
 
@@ -386,14 +379,11 @@ static void enemy_update(Entity *self, GameManager *manager, void *context)
         {
             enemy_context->state = ENEMY_IDLE;
             enemy_context->elapsed_move_timer = 0.0f;
-
-            FURI_LOG_D("Game", "Enemy %s reached target and transitioning to IDLE", enemy_context->id);
         }
     }
     break;
 
     default:
-        FURI_LOG_E("Game", "Enemy update: Unknown state %d", enemy_context->state);
         break;
     }
 }

+ 17 - 16
game/enemy.h

@@ -24,22 +24,23 @@ typedef enum
 // 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
-    EnemyDirection direction; // Direction the enemy is facing
-    EnemyState 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;       // Timer for the enemy attack
-    float strength;           // Damage the enemy deals
-    float health;             // Health of the enemy
+    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
+    EnemyDirection direction;   // Direction the enemy is facing
+    EnemyState 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;
 
 const EntityDescription *enemy(

+ 22 - 15
game/game.c

@@ -1,11 +1,9 @@
 #include "game.h"
 
 /****** Entities: Player ******/
-static bool has_pressed_ok = false;
 
 static Level *get_next_level(GameManager *manager)
 {
-    has_pressed_ok = false;
     Level *current_level = game_manager_current_level_get(manager);
     GameContext *game_context = game_manager_game_context_get(manager);
     for (int i = 0; i < game_context->level_count; i++)
@@ -47,6 +45,8 @@ void player_spawn(Level *level, GameManager *manager)
     player_context->level = 1;
     player_context->xp = 0;
     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;
 
     game_context->player_context = player_context;
 }
@@ -59,6 +59,9 @@ 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);
 
+    // Increment the elapsed_attack_timer for the player
+    player->elapsed_attack_timer += 1.0f / game_context->fps;
+
     // Store previous direction
     int prev_dx = player->dx;
     int prev_dy = player->dy;
@@ -97,35 +100,39 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         game_context->user_input = GameKeyRight;
     }
 
+    // Clamp the player's position to stay within world bounds
+    pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
+    pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
+
+    // Update player position
+    entity_pos_set(self, pos);
+
     // switch levels if holding OK
     if (input.held & GameKeyOk)
     {
-        game_context->user_input = GameKeyOk;
-
         // if all enemies are dead, allow the "OK" button to switch levels
         // otherwise the "OK" button will be used to attack
-        if (!has_pressed_ok && game_context->enemy_count == 0)
+        if (game_context->enemy_count == 0)
         {
-            has_pressed_ok = true; // prevent multiple level switches
             game_manager_next_level_set(manager, get_next_level(manager));
             furi_delay_ms(500);
         }
+        else
+        {
+            game_context->user_input = GameKeyOk;
+            FURI_LOG_I("Game", "Player is attacking");
+            furi_delay_ms(100);
+        }
         return;
     }
 
-    // Clamp the player's position to stay within world bounds
-    pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
-    pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
-
-    // Update player position
-    entity_pos_set(self, pos);
-
     // If the player is not moving, retain the last movement direction
     if (player->dx == 0 && player->dy == 0)
     {
         player->dx = prev_dx;
         player->dy = prev_dy;
         player->state = PLAYER_IDLE;
+        game_context->user_input = -1; // reset user input
     }
     else
     {
@@ -216,7 +223,7 @@ static void game_start(GameManager *game_manager, void *ctx)
                                                                                (Vector){WORLD_WIDTH / 2 - 11, WORLD_HEIGHT / 2 + 16},
                                                                                1,
                                                                                32,
-                                                                               10,
+                                                                               0.5f,
                                                                                10,
                                                                                100));
 
@@ -229,7 +236,7 @@ static void game_start(GameManager *game_manager, void *ctx)
                                                                                (Vector){WORLD_WIDTH / 2 - 11, WORLD_HEIGHT / 2 + 32},
                                                                                1,
                                                                                32,
-                                                                               10,
+                                                                               0.5f,
                                                                                10,
                                                                                100));
 

+ 13 - 11
game/game.h

@@ -28,17 +28,19 @@ typedef enum
 
 typedef struct
 {
-    PlayerDirection direction; // direction the player is facing
-    PlayerState state;         // current state of the player
-    Vector start_position;     // starting position 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
+    PlayerDirection direction;  // direction the player is facing
+    PlayerState state;          // current state of the player
+    Vector start_position;      // starting position 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
+    float attack_timer;         // Cooldown duration between attacks
+    float elapsed_attack_timer; // Time elapsed since the last attack
 } PlayerContext;
 
 typedef struct