Browse Source

complete in-game menu

jblanked 1 year ago
parent
commit
0698812525
3 changed files with 139 additions and 18 deletions
  1. 74 6
      game/enemy.c
  2. 55 12
      game/player.c
  3. 10 0
      game/player.h

+ 74 - 6
game/enemy.c

@@ -105,6 +105,78 @@ static void enemy_start(Entity *self, GameManager *manager, void *context)
     entity_collider_add_circle(self, enemy_context->radius);
 }
 
+static void draw_menu(GameManager *manager, Canvas *canvas)
+{
+    GameContext *game_context = game_manager_game_context_get(manager);
+
+    // draw background rectangle
+    canvas_draw_icon(
+        canvas,
+        0,
+        0,
+        &I_icon_menu_128x64px);
+
+    // draw menu options
+    switch (game_context->menu_screen)
+    {
+    case GAME_MENU_INFO:
+        // draw info
+        // first option is highlighted
+        char health[32];
+        char xp[32];
+        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);
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str(canvas, 7, 16, game_context->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);
+        canvas_draw_str(canvas, 7, 44, xp);
+        canvas_draw_str(canvas, 7, 51, strength);
+
+        // draw a box around the selected option
+        canvas_draw_frame(canvas, 80, 18, 36, 30);
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str(canvas, 86, 30, "Info");
+        canvas_set_font(canvas, FontSecondary);
+        canvas_draw_str(canvas, 86, 42, "More");
+        break;
+    case GAME_MENU_MORE:
+        // draw settings
+        switch (game_context->menu_selection)
+        {
+        case 0:
+            // first option is highlighted
+            break;
+        case 1:
+            // second option is highlighted
+            break;
+        default:
+            break;
+        }
+
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str(canvas, 7, 16, "FlipWorld v0.4");
+        canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
+        canvas_draw_str_multi(canvas, 7, 25, "Developed by\nJBlanked and Derek \nJamison. Graphics\nfrom Pr3!\n\nwww.github.com/jblanked");
+
+        // draw a box around the selected option
+        canvas_draw_frame(canvas, 80, 18, 36, 30);
+        canvas_set_font(canvas, FontSecondary);
+        canvas_draw_str(canvas, 86, 30, "Info");
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str(canvas, 86, 42, "More");
+        break;
+    default:
+        break;
+    }
+}
+
 // Enemy render function
 static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
 {
@@ -159,12 +231,8 @@ static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, voi
 
     if (game_context->is_menu_open)
     {
-        // draw menu icon
-        canvas_draw_icon(
-            canvas,
-            0,
-            0,
-            &I_icon_menu_128x64px);
+        // draw menu
+        draw_menu(manager, canvas);
     }
 }
 

+ 55 - 12
game/player.c

@@ -260,9 +260,18 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         else
             game_context->elapsed_button_timer = 0;
 
-        pos.y -= 2;
-        player->dy = -1;
-        player->direction = PLAYER_UP;
+        if (!game_context->is_menu_open)
+        {
+            pos.y -= 2;
+            player->dy = -1;
+            player->direction = PLAYER_UP;
+        }
+        else
+        {
+            // next menu view
+            // we can only go up to info from settings
+            game_context->menu_screen = GAME_MENU_INFO;
+        }
         game_context->last_button = GameKeyUp;
     }
     if (input.held & GameKeyDown)
@@ -272,9 +281,18 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         else
             game_context->elapsed_button_timer = 0;
 
-        pos.y += 2;
-        player->dy = 1;
-        player->direction = PLAYER_DOWN;
+        if (!game_context->is_menu_open)
+        {
+            pos.y += 2;
+            player->dy = 1;
+            player->direction = PLAYER_DOWN;
+        }
+        else
+        {
+            // next menu view
+            // we can only go down to more from info
+            game_context->menu_screen = GAME_MENU_MORE;
+        }
         game_context->last_button = GameKeyDown;
     }
     if (input.held & GameKeyLeft)
@@ -284,9 +302,20 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         else
             game_context->elapsed_button_timer = 0;
 
-        pos.x -= 2;
-        player->dx = -1;
-        player->direction = PLAYER_LEFT;
+        if (!game_context->is_menu_open)
+        {
+            pos.x -= 2;
+            player->dx = -1;
+            player->direction = PLAYER_LEFT;
+        }
+        else
+        {
+            // if the menu is open, move the selection left
+            if (game_context->menu_selection < 1)
+            {
+                game_context->menu_selection += 1;
+            }
+        }
         game_context->last_button = GameKeyLeft;
     }
     if (input.held & GameKeyRight)
@@ -296,9 +325,20 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         else
             game_context->elapsed_button_timer = 0;
 
-        pos.x += 2;
-        player->dx = 1;
-        player->direction = PLAYER_RIGHT;
+        if (!game_context->is_menu_open)
+        {
+            pos.x += 2;
+            player->dx = 1;
+            player->direction = PLAYER_RIGHT;
+        }
+        else
+        {
+            // if the menu is open, move the selection right
+            if (game_context->menu_selection < 1)
+            {
+                game_context->menu_selection += 1;
+            }
+        }
         game_context->last_button = GameKeyRight;
     }
     if (input.held & GameKeyOk)
@@ -323,6 +363,9 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         // if the OK button is held for 1 seconds,show the menu
         if (game_context->elapsed_button_timer > (1 * game_context->fps))
         {
+            // open up menu on the INFO screen
+            game_context->menu_screen = GAME_MENU_INFO;
+            game_context->menu_selection = 0;
             game_context->is_menu_open = true;
             FURI_LOG_I(TAG, "Menu opened");
         }

+ 10 - 0
game/player.h

@@ -46,6 +46,13 @@ typedef struct
     char username[32];          // player username
 } PlayerContext;
 
+// two screens for the game menu
+typedef enum
+{
+    GAME_MENU_INFO, // level, health, xp, etc.
+    GAME_MENU_MORE, // more settings
+} GameMenuScreen;
+
 typedef struct
 {
     PlayerContext *player_context;
@@ -65,6 +72,9 @@ typedef struct
     //
     uint32_t elapsed_button_timer;
     uint32_t last_button;
+    //
+    GameMenuScreen menu_screen;
+    uint8_t menu_selection;
 } GameContext;
 
 typedef struct