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

in pvp mode, remove player/enemy on death

jblanked пре 9 месеци
родитељ
комит
926b907734
2 измењених фајлова са 41 додато и 8 уклоњено
  1. 20 0
      game/enemy.c
  2. 21 8
      game/player.c

+ 20 - 0
game/enemy.c

@@ -2,6 +2,7 @@
 #include <game/enemy.h>
 #include <notification/notification_messages.h>
 #include <flip_storage/storage.h>
+#include <game/storage.h>
 
 static EntityContext *enemy_context_generic;
 // Allocation function
@@ -297,6 +298,16 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
                 {
                     enemy_context->state = ENTITY_DEAD;
 
+                    // if pvp, end the game
+                    if (game_context->game_mode == GAME_MODE_PVP)
+                    {
+                        player_context->health = player_context->max_health;
+                        save_player_context(player_context);
+                        furi_delay_ms(100);
+                        game_manager_game_stop(manager);
+                        return;
+                    }
+
                     // Reset enemy position and health
                     enemy_context->health = 100; // this needs to be set to the enemy's max health
 
@@ -344,6 +355,15 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
                     FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
                     player_context->state = ENTITY_DEAD;
 
+                    // if pvp, end the game
+                    if (game_context->game_mode == GAME_MODE_PVP)
+                    {
+                        save_player_context(player_context);
+                        furi_delay_ms(100);
+                        game_manager_game_stop(manager);
+                        return;
+                    }
+
                     // Reset player position and health
                     entity_pos_set(other, player_context->start_position);
                     player_context->health = player_context->max_health;

+ 21 - 8
game/player.c

@@ -220,18 +220,31 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     GameContext *game_context = game_manager_game_context_get(manager);
 
     // update websocket player context
-    if (game_context->game_mode == GAME_MODE_PVP && (player->old_position.x != pos.x || player->old_position.y != pos.y))
+    if (game_context->game_mode == GAME_MODE_PVP)
     {
-        elapsed_ws_timer++;
-        // only send the websocket update every 100ms
-        if (elapsed_ws_timer >= (game_context->fps / 10))
+        // if pvp, end the game if the player is dead
+        if (player->health <= 0)
         {
-            if (game_context->fhttp)
+            player->health = player->max_health;
+            save_player_context(player);
+            furi_delay_ms(100);
+            game_manager_game_stop(manager);
+            return;
+        }
+
+        if (player->old_position.x != pos.x || player->old_position.y != pos.y)
+        {
+            elapsed_ws_timer++;
+            // only send the websocket update every 100ms
+            if (elapsed_ws_timer >= (game_context->fps / 10))
             {
-                player->start_position = player->old_position;
-                websocket_player_context(player, game_context->fhttp);
+                if (game_context->fhttp)
+                {
+                    player->start_position = player->old_position;
+                    websocket_player_context(player, game_context->fhttp);
+                }
+                elapsed_ws_timer = 0;
             }
-            elapsed_ws_timer = 0;
         }
     }