jblanked 9 месяцев назад
Родитель
Сommit
6eed67337b
6 измененных файлов с 55 добавлено и 56 удалено
  1. 9 7
      game/draw.c
  2. 36 35
      game/enemy.c
  3. 1 1
      game/game.c
  4. 9 7
      game/npc.c
  5. 0 5
      game/player.c
  6. 0 1
      game/player.h

+ 9 - 7
game/draw.c

@@ -8,7 +8,7 @@ int camera_y = 0;
 void draw_user_stats(Canvas *canvas, Vector pos, GameManager *manager)
 {
     GameContext *game_context = game_manager_game_context_get(manager);
-    PlayerContext *player = game_context->player_context;
+    PlayerContext *player = entity_context_get(game_context->player);
 
     // first draw a black rectangle to make the text more readable
     canvas_invert_color(canvas);
@@ -87,6 +87,7 @@ void spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, fl
 static void draw_menu(GameManager *manager, Canvas *canvas)
 {
     GameContext *game_context = game_manager_game_context_get(manager);
+    PlayerContext *player_context = entity_context_get(game_context->player);
 
     // draw background rectangle
     canvas_draw_icon(
@@ -115,12 +116,12 @@ static void draw_menu(GameManager *manager, Canvas *canvas)
             char level[32];
             char strength[32];
 
-            snprintf(level, sizeof(level), "Level   : %ld", game_context->player_context->level);
-            snprintf(health, sizeof(health), "Health  : %ld", game_context->player_context->health);
-            snprintf(xp, sizeof(xp), "XP      : %ld", game_context->player_context->xp);
-            snprintf(strength, sizeof(strength), "Strength: %ld", game_context->player_context->strength);
+            snprintf(level, sizeof(level), "Level   : %ld", player_context->level);
+            snprintf(health, sizeof(health), "Health  : %ld", player_context->health);
+            snprintf(xp, sizeof(xp), "XP      : %ld", player_context->xp);
+            snprintf(strength, sizeof(strength), "Strength: %ld", player_context->strength);
             canvas_set_font(canvas, FontPrimary);
-            canvas_draw_str(canvas, 7, 16, game_context->player_context->username);
+            canvas_draw_str(canvas, 7, 16, player_context->username);
             canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
             canvas_draw_str(canvas, 7, 30, level);
             canvas_draw_str(canvas, 7, 37, health);
@@ -177,13 +178,14 @@ void background_render(Canvas *canvas, GameManager *manager)
         return;
 
     GameContext *game_context = game_manager_game_context_get(manager);
+    PlayerContext *player_context = entity_context_get(game_context->player);
     if (!game_context->is_menu_open)
     {
         // get player position
         Vector posi = entity_pos_get(game_context->player);
 
         // draw username over player's head
-        draw_username(canvas, posi, game_context->player_context->username);
+        draw_username(canvas, posi, player_context->username);
 
         if (game_context->is_switching_level)
             // draw switch world icon

+ 36 - 35
game/enemy.c

@@ -214,6 +214,7 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
     furi_check(enemy_context, "Enemy collision: EntityContext is NULL");
     GameContext *game_context = game_manager_game_context_get(manager);
     furi_check(game_context, "Enemy collision: GameContext is NULL");
+    PlayerContext *player_context = entity_context_get(game_context->player);
     if (game_context->game_mode == GAME_MODE_STORY && game_context->tutorial_step != 4)
     {
         // FURI_LOG_I("Game", "Enemy collision: No enemies in story mode");
@@ -241,10 +242,10 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
         }
 
         // Determine if the player is facing the enemy
-        if ((game_context->player_context->direction == ENTITY_LEFT && enemy_pos.x < player_pos.x) ||
-            (game_context->player_context->direction == ENTITY_RIGHT && enemy_pos.x > player_pos.x) ||
-            (game_context->player_context->direction == ENTITY_UP && enemy_pos.y < player_pos.y) ||
-            (game_context->player_context->direction == ENTITY_DOWN && enemy_pos.y > player_pos.y))
+        if ((player_context->direction == ENTITY_LEFT && enemy_pos.x < player_pos.x) ||
+            (player_context->direction == ENTITY_RIGHT && enemy_pos.x > player_pos.x) ||
+            (player_context->direction == ENTITY_UP && enemy_pos.y < player_pos.y) ||
+            (player_context->direction == ENTITY_DOWN && enemy_pos.y > player_pos.y))
         {
             player_is_facing_enemy = true;
         }
@@ -260,26 +261,26 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
             // Reset last button
             game_context->last_button = -1;
 
-            if (game_context->player_context->elapsed_attack_timer >= game_context->player_context->attack_timer)
+            if (player_context->elapsed_attack_timer >= player_context->attack_timer)
             {
                 atk_notify(game_context, enemy_context, true);
 
                 // Reset player's elapsed attack timer
-                game_context->player_context->elapsed_attack_timer = 0.0f;
+                player_context->elapsed_attack_timer = 0.0f;
                 enemy_context->elapsed_attack_timer = 0.0f; // Reset enemy's attack timer to block enemy attack
 
                 // Increase XP by the enemy's strength
-                game_context->player_context->xp += enemy_context->strength;
+                player_context->xp += enemy_context->strength;
 
                 // Increase healthy by 10% of the enemy's strength
-                game_context->player_context->health += enemy_context->strength * 0.1f;
-                if (game_context->player_context->health > game_context->player_context->max_health)
+                player_context->health += enemy_context->strength * 0.1f;
+                if (player_context->health > player_context->max_health)
                 {
-                    game_context->player_context->health = game_context->player_context->max_health;
+                    player_context->health = player_context->max_health;
                 }
 
                 // Decrease enemy health by player strength
-                enemy_context->health -= game_context->player_context->strength;
+                enemy_context->health -= player_context->strength;
 
                 if (enemy_context->health <= 0)
                 {
@@ -300,18 +301,18 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
                     enemy_context->state = ENTITY_ATTACKED;
                     // Vector old_pos = entity_pos_get(self);
                     //  Bounce the enemy back by X units opposite their last movement direction
-                    enemy_pos.x -= game_context->player_context->dx * enemy_context->radius + game_context->icon_offset;
-                    // enemy_pos.y -= game_context->player_context->dy * enemy_context->radius + game_context->icon_offset;
+                    enemy_pos.x -= player_context->dx * enemy_context->radius + game_context->icon_offset;
+                    // enemy_pos.y -= player_context->dy * enemy_context->radius + game_context->icon_offset;
                     entity_pos_set(self, enemy_pos);
 
                     // Reset enemy's movement direction to prevent immediate re-collision
-                    game_context->player_context->dx = 0;
-                    game_context->player_context->dy = 0;
+                    player_context->dx = 0;
+                    player_context->dy = 0;
                 }
             }
             else
             {
-                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));
+                FURI_LOG_I("Game", "Player attack on enemy '%s' is on cooldown: %f seconds remaining", enemy_context->id, (double)(player_context->attack_timer - player_context->elapsed_attack_timer));
             }
         }
         // Handle Enemy Attacking Player (enemy facing player)
@@ -325,54 +326,54 @@ static void enemy_collision(Entity *self, Entity *other, GameManager *manager, v
                 enemy_context->elapsed_attack_timer = 0.0f;
 
                 // Decrease player health by enemy strength
-                game_context->player_context->health -= enemy_context->strength;
+                player_context->health -= enemy_context->strength;
 
-                if (game_context->player_context->health <= 0)
+                if (player_context->health <= 0)
                 {
                     FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
-                    game_context->player_context->state = ENTITY_DEAD;
+                    player_context->state = ENTITY_DEAD;
 
                     // Reset player position and health
-                    entity_pos_set(other, game_context->player_context->start_position);
-                    game_context->player_context->health = game_context->player_context->max_health;
+                    entity_pos_set(other, player_context->start_position);
+                    player_context->health = player_context->max_health;
 
                     // subtract player's XP by the enemy's strength
-                    game_context->player_context->xp -= enemy_context->strength;
-                    if ((int)game_context->player_context->xp < 0)
+                    player_context->xp -= enemy_context->strength;
+                    if ((int)player_context->xp < 0)
                     {
-                        game_context->player_context->xp = 0;
+                        player_context->xp = 0;
                     }
                 }
                 else
                 {
                     FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
-                    game_context->player_context->state = ENTITY_ATTACKED;
+                    player_context->state = ENTITY_ATTACKED;
 
                     // Bounce the player back by X units opposite their last movement direction
-                    player_pos.x -= game_context->player_context->dx * enemy_context->radius + game_context->icon_offset;
-                    // player_pos.y -= game_context->player_context->dy * enemy_context->radius + game_context->icon_offset;
+                    player_pos.x -= player_context->dx * enemy_context->radius + game_context->icon_offset;
+                    // player_pos.y -= player_context->dy * enemy_context->radius + game_context->icon_offset;
                     entity_pos_set(other, player_pos);
 
                     // Reset player's movement direction to prevent immediate re-collision
-                    game_context->player_context->dx = 0;
-                    game_context->player_context->dy = 0;
+                    player_context->dx = 0;
+                    player_context->dy = 0;
                 }
             }
         }
         else // handle other collisions
         {
             // Set the player's old position to prevent collision
-            entity_pos_set(other, game_context->player_context->old_position);
+            entity_pos_set(other, player_context->old_position);
             // Reset player's movement direction to prevent immediate re-collision
-            game_context->player_context->dx = 0;
-            game_context->player_context->dy = 0;
+            player_context->dx = 0;
+            player_context->dy = 0;
         }
 
-        if (game_context->player_context->state == ENTITY_DEAD)
+        if (player_context->state == ENTITY_DEAD)
         {
             // Reset player's position and health
-            entity_pos_set(other, game_context->player_context->start_position);
-            game_context->player_context->health = game_context->player_context->max_health;
+            entity_pos_set(other, player_context->start_position);
+            player_context->health = player_context->max_health;
         }
     }
     // if not player than must be an icon or npc; so push back

+ 1 - 1
game/game.c

@@ -14,7 +14,7 @@ static void game_start(GameManager *game_manager, void *ctx)
     // For simplicity, we will just set it to 0.
     GameContext *game_context = ctx;
     game_context->fps = atof_(fps_choices_str[fps_index]);
-    game_context->player_context = NULL;
+    game_context->player = NULL;
     game_context->ended_early = false;
     game_context->current_level = 0;
     game_context->level_count = 0;

+ 9 - 7
game/npc.c

@@ -135,8 +135,10 @@ static void npc_collision(Entity *self, Entity *other, GameManager *manager, voi
         // Retrieve NPC context
         EntityContext *npc_context = (EntityContext *)context;
         GameContext *game_context = game_manager_game_context_get(manager);
+        PlayerContext *player_context = entity_context_get(game_context->player);
         furi_check(npc_context);
         furi_check(game_context);
+        furi_check(player_context);
 
         // Get positions of the NPC and the player
         Vector npc_pos = entity_pos_get(self);
@@ -146,20 +148,20 @@ static void npc_collision(Entity *self, Entity *other, GameManager *manager, voi
         bool player_is_facing_npc = false;
 
         // Determine if the player is facing the NPC
-        if ((game_context->player_context->direction == ENTITY_LEFT && npc_pos.x < player_pos.x) ||
-            (game_context->player_context->direction == ENTITY_RIGHT && npc_pos.x > player_pos.x) ||
-            (game_context->player_context->direction == ENTITY_UP && npc_pos.y < player_pos.y) ||
-            (game_context->player_context->direction == ENTITY_DOWN && npc_pos.y > player_pos.y))
+        if ((player_context->direction == ENTITY_LEFT && npc_pos.x < player_pos.x) ||
+            (player_context->direction == ENTITY_RIGHT && npc_pos.x > player_pos.x) ||
+            (player_context->direction == ENTITY_UP && npc_pos.y < player_pos.y) ||
+            (player_context->direction == ENTITY_DOWN && npc_pos.y > player_pos.y))
         {
             player_is_facing_npc = true;
         }
 
         // bounce the player back to where it came from
         // Set the player's old position to prevent collision
-        entity_pos_set(other, game_context->player_context->old_position);
+        entity_pos_set(other, player_context->old_position);
         // Reset player's movement direction to prevent immediate re-collision
-        game_context->player_context->dx = 0;
-        game_context->player_context->dy = 0;
+        player_context->dx = 0;
+        player_context->dy = 0;
 
         // Press OK and facing NPC
         if (player_is_facing_npc && game_context->last_button == GameKeyOk)

+ 0 - 5
game/player.c

@@ -143,9 +143,6 @@ void player_spawn(Level *level, GameManager *manager)
                 snprintf(pctx->username, sizeof(pctx->username), "Player");
             }
         }
-
-        game_context->player_context = pctx;
-
         // Save the initialized context
         if (!save_player_context(pctx))
         {
@@ -171,8 +168,6 @@ void player_spawn(Level *level, GameManager *manager)
     // set the player's left sprite direction
     pctx->left = pctx->direction == ENTITY_LEFT ? true : false;
 
-    // Assign loaded player context to game context
-    game_context->player_context = pctx;
     free(sprite_context);
 }
 

+ 0 - 1
game/player.h

@@ -73,7 +73,6 @@ typedef enum
 
 typedef struct
 {
-    PlayerContext *player_context;
     Level *levels[MAX_LEVELS];
     Entity *enemies[MAX_ENEMIES];
     Entity *npcs[MAX_NPCS];