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

add on-screen menu and username above player

jblanked 1 год назад
Родитель
Сommit
2afed35296
5 измененных файлов с 41 добавлено и 0 удалено
  1. 26 0
      game/draw.c
  2. 1 0
      game/draw.h
  3. 3 0
      game/enemy.c
  4. 10 0
      game/game.c
  5. 1 0
      game/game.h

+ 26 - 0
game/draw.c

@@ -22,6 +22,32 @@ void draw_background(Canvas *canvas, Vector pos)
     draw_bounds(canvas);
 }
 
+// Draw the user stats (health, xp, and level)
+// Draw the user stats (health, xp, and level)
+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;
+
+    // first draw a black rectangle to make the text more readable
+    canvas_invert_color(canvas);
+    canvas_draw_box(canvas, pos.x - 1, pos.y - 7, 32, 21);
+    canvas_invert_color(canvas);
+
+    char health[32];
+    char xp[32];
+    char level[32];
+
+    snprintf(health, sizeof(health), "HP : %ld", player->health);
+    snprintf(xp, sizeof(xp), "XP : %ld", player->xp);
+    snprintf(level, sizeof(level), "LVL: %ld", player->level);
+
+    canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
+    canvas_draw_str(canvas, pos.x, pos.y, health);
+    canvas_draw_str(canvas, pos.x, pos.y + 7, xp);
+    canvas_draw_str(canvas, pos.x, pos.y + 14, level);
+}
+
 // Draw a line of icons (16 width)
 void draw_icon_line(Canvas *canvas, Vector pos, int amount, bool horizontal, const Icon *icon)
 {

+ 1 - 0
game/draw.h

@@ -5,6 +5,7 @@
 extern int camera_x;
 extern int camera_y;
 void draw_background(Canvas *canvas, Vector pos);
+void draw_user_stats(Canvas *canvas, Vector pos, GameManager *manager);
 void draw_icon_line(Canvas *canvas, Vector pos, int amount, bool horizontal, const Icon *icon);
 void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon);
 void spawn_icon(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height);

+ 3 - 0
game/enemy.c

@@ -138,6 +138,9 @@ static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, voi
         current_sprite,
         pos.x - camera_x - (enemy_context->size.x / 2),
         pos.y - camera_y - (enemy_context->size.y / 2));
+
+    // Draw user stats (this has to be done for all enemies)
+    draw_user_stats(canvas, (Vector){0, 7}, manager);
 }
 
 // Enemy collision function

+ 10 - 0
game/game.c

@@ -48,6 +48,12 @@ void player_spawn(Level *level, GameManager *manager)
     player_context->attack_timer = 0.5f;
     player_context->elapsed_attack_timer = player_context->attack_timer;
 
+    // Set player username
+    if (!load_char("Flip-Social-Username", player_context->username, 32))
+    {
+        snprintf(player_context->username, 32, "Player");
+    }
+
     game_context->player_context = player_context;
 }
 
@@ -165,6 +171,10 @@ static void player_render(Entity *self, GameManager *manager, Canvas *canvas, vo
         pos.x - camera_x - 5, // Center the sprite horizontally
         pos.y - camera_y - 5  // Center the sprite vertically
     );
+
+    // draw username over player's head
+    canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
+    canvas_draw_str(canvas, pos.x - camera_x - (strlen(player->username) * 2), pos.y - camera_y - 7, player->username);
 }
 
 const EntityDescription player_desc = {

+ 1 - 0
game/game.h

@@ -41,6 +41,7 @@ typedef struct
     uint32_t strength;          // player strength
     float attack_timer;         // Cooldown duration between attacks
     float elapsed_attack_timer; // Time elapsed since the last attack
+    char username[32];          // player username
 } PlayerContext;
 
 typedef struct