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

and mutlipalyer implementation is complete

now we just need to find some memory lmao
jblanked пре 9 месеци
родитељ
комит
9ddf6274bb
3 измењених фајлова са 79 додато и 3 уклоњено
  1. 0 1
      callback/callback.c
  2. 77 0
      game/enemy.c
  3. 2 2
      game/player.c

+ 0 - 1
callback/callback.c

@@ -1632,7 +1632,6 @@ static void callback_submenu_lobby_choices(void *context, uint32_t index)
             if (!flipper_http_websocket_start(fhttp, websocket_url, 80, "{\"Content-Type\":\"application/json\"}"))
             {
                 FURI_LOG_E(TAG, "Failed to start websocket session");
-                flipper_http_free(fhttp);
                 return false;
             }
             return true;

+ 77 - 0
game/enemy.c

@@ -401,6 +401,77 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
     }
 }
 
+static void pvp_position(GameContext *game_context, EntityContext *enemy)
+{
+    if (!game_context || !enemy)
+    {
+        FURI_LOG_E("Game", "PVP position: Invalid parameters");
+        return;
+    }
+
+    if (game_context->fhttp->last_response != NULL && strlen(game_context->fhttp->last_response) > 0)
+    {
+        // parse the response and set the enemy position
+        /* expected response:
+        {
+            "u": "JBlanked",
+            "xp": 37743,
+            "h": 207,
+            "ehr": 0.7,
+            "eat": 127.5,
+            "d": 2,
+            "s": 1,
+            "sp": {
+                "x": 381.0,
+                "y": 192.0
+            }
+        }
+        */
+
+        // FuriStrings are probably safer but we already last_response as a char*
+
+        // we need the health, elapsed attack timer, direction, and position
+        char *h = get_json_value("h", game_context->fhttp->last_response);
+        char *eat = get_json_value("eat", game_context->fhttp->last_response);
+        char *d = get_json_value("d", game_context->fhttp->last_response);
+        char *sp = get_json_value("sp", game_context->fhttp->last_response);
+        char *x = get_json_value("x", sp);
+        char *y = get_json_value("y", sp);
+
+        if (!h || !eat || !d || !sp || !x || !y)
+        {
+            FURI_LOG_E("Game", "PVP position: Failed to parse enemy data");
+            return;
+        }
+
+        // set enemy info
+        enemy->health = (float)atoi(h);
+        enemy->elapsed_attack_timer = (float)atof_(eat);
+        switch (atoi(d))
+        {
+        case 0:
+            enemy->direction = ENTITY_LEFT;
+            break;
+        case 1:
+            enemy->direction = ENTITY_RIGHT;
+            break;
+        case 2:
+            enemy->direction = ENTITY_UP;
+            break;
+        case 3:
+            enemy->direction = ENTITY_DOWN;
+            break;
+        default:
+            enemy->direction = ENTITY_RIGHT;
+            break;
+        }
+        enemy->start_position.x = (float)atoi(x);
+        enemy->start_position.y = (float)atoi(y);
+        enemy->end_position.x = (float)atoi(x);
+        enemy->end_position.y = (float)atoi(y);
+    }
+}
+
 // Enemy update function
 static void enemy_update(Entity *self, GameManager *manager, void *context)
 {
@@ -420,6 +491,12 @@ static void enemy_update(Entity *self, GameManager *manager, void *context)
         return;
     }
 
+    if (game_context->game_mode == GAME_MODE_PVP)
+    {
+        // update enemy position
+        pvp_position(game_context, enemy_context);
+    }
+
     float delta_time = 1.0f / game_context->fps;
 
     // Increment the elapsed_attack_timer for the enemy

+ 2 - 2
game/player.c

@@ -228,8 +228,8 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     if (game_context->game_mode == GAME_MODE_PVP && (player->old_position.x != pos.x || player->old_position.y != pos.y))
     {
         elapsed_ws_timer++;
-        // only send the websocket update every 200ms
-        if (elapsed_ws_timer >= (game_context->fps / 5))
+        // only send the websocket update every 100ms
+        if (elapsed_ws_timer >= (game_context->fps / 10))
         {
             if (game_context->fhttp)
             {