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

add player attack and enemy death reset

jblanked 1 год назад
Родитель
Сommit
dea0f800ea
3 измененных файлов с 82 добавлено и 24 удалено
  1. 78 24
      game/enemy.c
  2. 3 0
      game/game.c
  3. 1 0
      game/game.h

+ 78 - 24
game/enemy.c

@@ -155,58 +155,112 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
 
 
         // Retrieve enemy context
         // Retrieve enemy context
         EnemyContext *enemy_context = (EnemyContext *)context;
         EnemyContext *enemy_context = (EnemyContext *)context;
+        GameContext *game_context = game_manager_game_context_get(manager);
         if (!enemy_context)
         if (!enemy_context)
         {
         {
             FURI_LOG_E("Game", "Enemy collision: EnemyContext is NULL");
             FURI_LOG_E("Game", "Enemy collision: EnemyContext is NULL");
             return;
             return;
         }
         }
+        if (!game_context)
+        {
+            FURI_LOG_E("Game", "Enemy collision: GameContext is NULL");
+            return;
+        }
 
 
         // Get positions of the enemy and the player
         // Get positions of the enemy and the player
         Vector enemy_pos = entity_pos_get(self);
         Vector enemy_pos = entity_pos_get(self);
         Vector player_pos = entity_pos_get(other);
         Vector player_pos = entity_pos_get(other);
 
 
-        // Determine if the enemy is facing the player
-        bool is_facing_player = false;
+        // Determine if the enemy is facing the player or player is facing the enemy
+        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)
         if (enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x)
         {
         {
-            is_facing_player = true;
+            enemy_is_facing_player = true;
             enemy_context->state = ENEMY_ATTACKING;
             enemy_context->state = ENEMY_ATTACKING;
         }
         }
         else if (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x)
         else if (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x)
         {
         {
-            is_facing_player = true;
+            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;
             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)
+        {
+            player_is_facing_enemy = true;
+        }
+
+        // If the player is facing the enemy and pressed OK, perform an attack (log message)
+        if (player_is_facing_enemy && game_context->user_input == GameKeyOk)
+        {
+            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;
+
+            // 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;
+
+                // 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->health -= game_context->player_context->strength;
+                enemy_context->state = ENEMY_ATTACKED;
+            }
+        }
 
 
         // If the enemy is facing the player, perform an attack (log message)
         // If the enemy is facing the player, perform an attack (log message)
-        if (is_facing_player)
+        if (enemy_is_facing_player)
         {
         {
             FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
             FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
 
 
-            // Decrease player health
-            GameContext *game_context = game_manager_game_context_get(manager);
-            if (game_context)
+            // Decrease player health by enemy strength
+            if (game_context->player_context->health <= 0)
             {
             {
-                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->health -= enemy_context->strength;
-                    game_context->player_context->state = PLAYER_ATTACKED;
-                }
+                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
             else
             {
             {
-                FURI_LOG_E("Game", "Enemy collision: Failed to get GameContext");
+                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;
             }
             }
         }
         }
 
 

+ 3 - 0
game/game.c

@@ -13,9 +13,11 @@ static Level *get_next_level(GameManager *manager)
         if (game_context->levels[i] == current_level)
         if (game_context->levels[i] == current_level)
         {
         {
             // check if i+1 is out of bounds, if so, return the first level
             // check if i+1 is out of bounds, if so, return the first level
+            game_context->current_level = (i + 1) % game_context->level_count;
             return game_context->levels[(i + 1) % game_context->level_count] ? game_context->levels[(i + 1) % game_context->level_count] : game_context->levels[0];
             return game_context->levels[(i + 1) % game_context->level_count] ? game_context->levels[(i + 1) % game_context->level_count] : game_context->levels[0];
         }
         }
     }
     }
+    game_context->current_level = 0;
     return game_context->levels[0] ? game_context->levels[0] : game_manager_add_level(manager, generic_level("town_world", 0));
     return game_context->levels[0] ? game_context->levels[0] : game_manager_add_level(manager, generic_level("town_world", 0));
 }
 }
 
 
@@ -232,6 +234,7 @@ static void game_start(GameManager *game_manager, void *ctx)
                                                                                100));
                                                                                100));
 
 
     game_context->enemy_count = 2;
     game_context->enemy_count = 2;
+    game_context->current_level = 0;
 }
 }
 
 
 /*
 /*

+ 1 - 0
game/game.h

@@ -51,6 +51,7 @@ typedef struct
     float fps;
     float fps;
     int level_count;
     int level_count;
     int enemy_count;
     int enemy_count;
+    int current_level;
 } GameContext;
 } GameContext;
 
 
 extern const EntityDescription player_desc;
 extern const EntityDescription player_desc;