Explorar o código

slight game/draw and game/player clean up

jblanked hai 9 meses
pai
achega
46a069dc9b
Modificáronse 6 ficheiros con 54 adicións e 56 borrados
  1. 11 11
      game/draw.c
  2. 6 8
      game/draw.h
  3. 4 4
      game/enemy.c
  4. 2 2
      game/icon.c
  5. 4 4
      game/npc.c
  6. 27 27
      game/player.c

+ 11 - 11
game/draw.c

@@ -1,8 +1,9 @@
 #include <game/draw.h>
 #include <game/draw.h>
 
 
 // Global variables to store camera position
 // Global variables to store camera position
-int camera_x = 0;
-int camera_y = 0;
+int draw_camera_x = 0;
+int draw_camera_y = 0;
+char draw_g_name[32];
 
 
 // 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)
 void draw_user_stats(Canvas *canvas, Vector pos, GameManager *manager)
@@ -38,25 +39,24 @@ void draw_username(Canvas *canvas, Vector pos, char *username)
     // first draw a black rectangle to make the text more readable
     // first draw a black rectangle to make the text more readable
     // draw box around the username
     // draw box around the username
     canvas_invert_color(canvas);
     canvas_invert_color(canvas);
-    canvas_draw_box(canvas, pos.x - camera_x - (strlen(username) * 2) - 1, pos.y - camera_y - 14, strlen(username) * 4 + 1, 8);
+    canvas_draw_box(canvas, pos.x - draw_camera_x - (strlen(username) * 2) - 1, pos.y - draw_camera_y - 14, strlen(username) * 4 + 1, 8);
     canvas_invert_color(canvas);
     canvas_invert_color(canvas);
 
 
     // draw username over player's head
     // draw username over player's head
     canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
     canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
-    canvas_draw_str(canvas, pos.x - camera_x - (strlen(username) * 2), pos.y - camera_y - 7, username);
+    canvas_draw_str(canvas, pos.x - draw_camera_x - (strlen(username) * 2), pos.y - draw_camera_y - 7, username);
 }
 }
 
 
-char g_name[32];
 // Draw an icon at a specific position (with collision detection)
 // Draw an icon at a specific position (with collision detection)
-void spawn_icon(GameManager *manager, Level *level, const char *icon_id, float x, float y)
+void draw_spawn_icon(GameManager *manager, Level *level, const char *icon_id, float x, float y)
 {
 {
-    snprintf(g_name, sizeof(g_name), "%s", icon_id);
+    snprintf(draw_g_name, sizeof(draw_g_name), "%s", icon_id);
     Entity *e = level_add_entity(level, &icon_desc);
     Entity *e = level_add_entity(level, &icon_desc);
     entity_pos_set(e, (Vector){x, y});
     entity_pos_set(e, (Vector){x, y});
     UNUSED(manager);
     UNUSED(manager);
 }
 }
 // Draw a line of icons at a specific position (with collision detection)
 // Draw a line of icons at a specific position (with collision detection)
-void spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, float x, float y, uint8_t amount, bool horizontal, uint8_t spacing)
+void draw_spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, float x, float y, uint8_t amount, bool horizontal, uint8_t spacing)
 {
 {
     for (int i = 0; i < amount; i++)
     for (int i = 0; i < amount; i++)
     {
     {
@@ -68,7 +68,7 @@ void spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, fl
                 break;
                 break;
             }
             }
 
 
-            spawn_icon(manager, level, icon_id, x + (i * spacing), y);
+            draw_spawn_icon(manager, level, icon_id, x + (i * spacing), y);
         }
         }
         else
         else
         {
         {
@@ -78,7 +78,7 @@ void spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, fl
                 break;
                 break;
             }
             }
 
 
-            spawn_icon(manager, level, icon_id, x, y + (i * spacing));
+            draw_spawn_icon(manager, level, icon_id, x, y + (i * spacing));
         }
         }
     }
     }
 }
 }
@@ -171,7 +171,7 @@ static void draw_menu(GameManager *manager, Canvas *canvas)
     }
     }
 }
 }
 
 
-void background_render(Canvas *canvas, GameManager *manager)
+void draw_background_render(Canvas *canvas, GameManager *manager)
 {
 {
     if (!canvas || !manager)
     if (!canvas || !manager)
         return;
         return;

+ 6 - 8
game/draw.h

@@ -2,13 +2,11 @@
 #include "game.h"
 #include "game.h"
 #include "game/icon.h"
 #include "game/icon.h"
 #include <game/player.h>
 #include <game/player.h>
-
-// Global variables to store camera position
-extern int camera_x;
-extern int camera_y;
+extern char draw_g_name[32];
+extern int draw_camera_x;
+extern int draw_camera_y;
 void draw_user_stats(Canvas *canvas, Vector pos, GameManager *manager);
 void draw_user_stats(Canvas *canvas, Vector pos, GameManager *manager);
 void draw_username(Canvas *canvas, Vector pos, char *username);
 void draw_username(Canvas *canvas, Vector pos, char *username);
-void spawn_icon(GameManager *manager, Level *level, const char *icon_id, float x, float y);
-void spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, float x, float y, uint8_t amount, bool horizontal, uint8_t spacing);
-extern char g_name[32];
-void background_render(Canvas *canvas, GameManager *manager);
+void draw_spawn_icon(GameManager *manager, Level *level, const char *icon_id, float x, float y);
+void draw_spawn_icon_line(GameManager *manager, Level *level, const char *icon_id, float x, float y, uint8_t amount, bool horizontal, uint8_t spacing);
+void draw_background_render(Canvas *canvas, GameManager *manager);

+ 4 - 4
game/enemy.c

@@ -117,8 +117,8 @@ static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, voi
     Vector pos = entity_pos_get(self);
     Vector pos = entity_pos_get(self);
 
 
     // Get the camera position
     // Get the camera position
-    int x_pos = pos.x - camera_x - enemy_context->size.x / 2;
-    int y_pos = pos.y - camera_y - enemy_context->size.y / 2;
+    int x_pos = pos.x - draw_camera_x - enemy_context->size.x / 2;
+    int y_pos = pos.y - draw_camera_y - enemy_context->size.y / 2;
 
 
     // check if position is within the screen
     // check if position is within the screen
     if (x_pos + enemy_context->size.x < 0 || x_pos > SCREEN_WIDTH || y_pos + enemy_context->size.y < 0 || y_pos > SCREEN_HEIGHT)
     if (x_pos + enemy_context->size.x < 0 || x_pos > SCREEN_WIDTH || y_pos + enemy_context->size.y < 0 || y_pos > SCREEN_HEIGHT)
@@ -143,8 +143,8 @@ static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, voi
         canvas_draw_sprite(
         canvas_draw_sprite(
             canvas,
             canvas,
             current_sprite,
             current_sprite,
-            pos.x - camera_x - (enemy_context->size.x / 2),
-            pos.y - camera_y - (enemy_context->size.y / 2));
+            pos.x - draw_camera_x - (enemy_context->size.x / 2),
+            pos.y - draw_camera_y - (enemy_context->size.y / 2));
 
 
         // draw health of enemy
         // draw health of enemy
         char health_str[32];
         char health_str[32];

+ 2 - 2
game/icon.c

@@ -23,8 +23,8 @@ static void icon_group_render(Entity *self, GameManager *manager, Canvas *canvas
     for (int i = 0; i < igctx->count; i++)
     for (int i = 0; i < igctx->count; i++)
     {
     {
         IconSpec *spec = &igctx->icons[i];
         IconSpec *spec = &igctx->icons[i];
-        int x_pos = spec->pos.x - camera_x - (spec->size.x / 2);
-        int y_pos = spec->pos.y - camera_y - (spec->size.y / 2);
+        int x_pos = spec->pos.x - draw_camera_x - (spec->size.x / 2);
+        int y_pos = spec->pos.y - draw_camera_y - (spec->size.y / 2);
         if (x_pos + spec->size.x < 0 || x_pos > SCREEN_WIDTH ||
         if (x_pos + spec->size.x < 0 || x_pos > SCREEN_WIDTH ||
             y_pos + spec->size.y < 0 || y_pos > SCREEN_HEIGHT)
             y_pos + spec->size.y < 0 || y_pos > SCREEN_HEIGHT)
         {
         {

+ 4 - 4
game/npc.c

@@ -91,8 +91,8 @@ static void npc_render(Entity *self, GameManager *manager, Canvas *canvas, void
     // Get the position of the NPC
     // Get the position of the NPC
     Vector pos = entity_pos_get(self);
     Vector pos = entity_pos_get(self);
 
 
-    int x_pos = pos.x - camera_x - npc_context->size.x / 2;
-    int y_pos = pos.y - camera_y - npc_context->size.y / 2;
+    int x_pos = pos.x - draw_camera_x - npc_context->size.x / 2;
+    int y_pos = pos.y - draw_camera_y - npc_context->size.y / 2;
 
 
     // check if position is within the screen
     // check if position is within the screen
     if (x_pos + npc_context->size.x < 0 || x_pos > SCREEN_WIDTH || y_pos + npc_context->size.y < 0 || y_pos > SCREEN_HEIGHT)
     if (x_pos + npc_context->size.x < 0 || x_pos > SCREEN_WIDTH || y_pos + npc_context->size.y < 0 || y_pos > SCREEN_HEIGHT)
@@ -115,8 +115,8 @@ static void npc_render(Entity *self, GameManager *manager, Canvas *canvas, void
         canvas_draw_sprite(
         canvas_draw_sprite(
             canvas,
             canvas,
             current_sprite,
             current_sprite,
-            pos.x - camera_x - (npc_context->size.x / 2),
-            pos.y - camera_y - (npc_context->size.y / 2));
+            pos.x - draw_camera_x - (npc_context->size.x / 2),
+            pos.y - draw_camera_y - (npc_context->size.y / 2));
     }
     }
 }
 }
 
 

+ 27 - 27
game/player.c

@@ -6,7 +6,7 @@
 #include <math.h>
 #include <math.h>
 #include <engine/entity_i.h>
 #include <engine/entity_i.h>
 /****** Entities: Player ******/
 /****** Entities: Player ******/
-static Level *next_level(GameManager *manager)
+static Level *player_next_level(GameManager *manager)
 {
 {
     GameContext *game_context = game_manager_game_context_get(manager);
     GameContext *game_context = game_manager_game_context_get(manager);
     if (!game_context)
     if (!game_context)
@@ -56,7 +56,7 @@ static Level *next_level(GameManager *manager)
 }
 }
 
 
 // Update player stats based on XP using iterative method
 // Update player stats based on XP using iterative method
-static int get_player_level_iterative(uint32_t xp)
+static int player_level_iterative_get(uint32_t xp)
 {
 {
     int level = 1;
     int level = 1;
     uint32_t xp_required = 100; // Base XP for level 2
     uint32_t xp_required = 100; // Base XP for level 2
@@ -160,7 +160,7 @@ void player_spawn(Level *level, GameManager *manager)
     pctx->start_position = entity_pos_get(game_context->player);
     pctx->start_position = entity_pos_get(game_context->player);
 
 
     // Determine the player's level based on XP
     // Determine the player's level based on XP
-    pctx->level = get_player_level_iterative(pctx->xp);
+    pctx->level = player_level_iterative_get(pctx->xp);
 
 
     // Update strength and max health based on the new level
     // Update strength and max health based on the new level
     pctx->strength = 10 + (pctx->level * 1);           // 1 strength per level
     pctx->strength = 10 + (pctx->level * 1);           // 1 strength per level
@@ -172,13 +172,13 @@ void player_spawn(Level *level, GameManager *manager)
     free(sprite_context);
     free(sprite_context);
 }
 }
 
 
-static int vgm_increase(float value, float increase)
+static int player_vgm_increase(float value, float increase)
 {
 {
     const int val = abs((int)(round(value + increase) / 2));
     const int val = abs((int)(round(value + increase) / 2));
     return val < 1 ? 1 : val;
     return val < 1 ? 1 : val;
 }
 }
 
 
-static void vgm_direction(Imu *imu, PlayerContext *player, Vector *pos)
+static void player_vgm_direction(Imu *imu, PlayerContext *player, Vector *pos)
 {
 {
     const float pitch = -imu_pitch_get(imu);
     const float pitch = -imu_pitch_get(imu);
     const float roll = -imu_roll_get(imu);
     const float roll = -imu_roll_get(imu);
@@ -186,25 +186,25 @@ static void vgm_direction(Imu *imu, PlayerContext *player, Vector *pos)
     const float min_y = atof_(vgm_levels[vgm_y_index]) + 5.0; // minimum of 3
     const float min_y = atof_(vgm_levels[vgm_y_index]) + 5.0; // minimum of 3
     if (pitch > min_x)
     if (pitch > min_x)
     {
     {
-        pos->x += vgm_increase(pitch, min_x);
+        pos->x += player_vgm_increase(pitch, min_x);
         player->dx = 1;
         player->dx = 1;
         player->direction = ENTITY_RIGHT;
         player->direction = ENTITY_RIGHT;
     }
     }
     else if (pitch < -min_x)
     else if (pitch < -min_x)
     {
     {
-        pos->x += -vgm_increase(pitch, min_x);
+        pos->x += -player_vgm_increase(pitch, min_x);
         player->dx = -1;
         player->dx = -1;
         player->direction = ENTITY_LEFT;
         player->direction = ENTITY_LEFT;
     }
     }
     if (roll > min_y)
     if (roll > min_y)
     {
     {
-        pos->y += vgm_increase(roll, min_y);
+        pos->y += player_vgm_increase(roll, min_y);
         player->dy = 1;
         player->dy = 1;
         player->direction = ENTITY_DOWN;
         player->direction = ENTITY_DOWN;
     }
     }
     else if (roll < -min_y)
     else if (roll < -min_y)
     {
     {
-        pos->y += -vgm_increase(roll, min_y);
+        pos->y += -player_vgm_increase(roll, min_y);
         player->dy = -1;
         player->dy = -1;
         player->direction = ENTITY_UP;
         player->direction = ENTITY_UP;
     }
     }
@@ -212,7 +212,7 @@ static void vgm_direction(Imu *imu, PlayerContext *player, Vector *pos)
 
 
 // This static function handles collisions with icons.
 // This static function handles collisions with icons.
 // It receives the player entity pointer, the player's current position, and a pointer to PlayerContext.
 // It receives the player entity pointer, the player's current position, and a pointer to PlayerContext.
-static void handle_collision(Entity *playerEntity, Vector playerPos, PlayerContext *player)
+static void player_handle_collision(Entity *playerEntity, Vector playerPos, PlayerContext *player)
 {
 {
     // If there is no active icon group, do nothing.
     // If there is no active icon group, do nothing.
     if (!g_current_icon_group)
     if (!g_current_icon_group)
@@ -285,7 +285,7 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     player->old_position = pos;
     player->old_position = pos;
 
 
     // Determine the player's level based on XP
     // Determine the player's level based on XP
-    player->level = get_player_level_iterative(player->xp);
+    player->level = player_level_iterative_get(player->xp);
     player->strength = 10 + (player->level * 1);           // 1 strength per level
     player->strength = 10 + (player->level * 1);           // 1 strength per level
     player->max_health = 100 + ((player->level - 1) * 10); // 10 health per level
     player->max_health = 100 + ((player->level - 1) * 10); // 10 health per level
 
 
@@ -300,7 +300,7 @@ static void player_update(Entity *self, GameManager *manager, void *context)
     if (game_context->imu_present)
     if (game_context->imu_present)
     {
     {
         // update position using the IMU
         // update position using the IMU
-        vgm_direction(game_context->imu, player, &pos);
+        player_vgm_direction(game_context->imu, player, &pos);
     }
     }
 
 
     // Apply health regeneration
     // Apply health regeneration
@@ -421,7 +421,7 @@ static void player_update(Entity *self, GameManager *manager, void *context)
             game_context->is_switching_level = true;
             game_context->is_switching_level = true;
             save_player_context(player);
             save_player_context(player);
             furi_delay_ms(100);
             furi_delay_ms(100);
-            game_manager_next_level_set(manager, next_level(manager));
+            game_manager_next_level_set(manager, player_next_level(manager));
             return;
             return;
         }
         }
 
 
@@ -515,10 +515,10 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         player->state = ENTITY_MOVING;
         player->state = ENTITY_MOVING;
 
 
     // handle icon collision
     // handle icon collision
-    handle_collision(self, pos, player);
+    player_handle_collision(self, pos, player);
 }
 }
 
 
-static void draw_tutorial(Canvas *canvas, GameManager *manager)
+static void player_draw_tutorial(Canvas *canvas, GameManager *manager)
 {
 {
     GameContext *game_context = game_manager_game_context_get(manager);
     GameContext *game_context = game_manager_game_context_get(manager);
     canvas_set_font(canvas, FontPrimary);
     canvas_set_font(canvas, FontPrimary);
@@ -577,12 +577,12 @@ static void player_render(Entity *self, GameManager *manager, Canvas *canvas, vo
     Vector pos = entity_pos_get(self);
     Vector pos = entity_pos_get(self);
 
 
     // Calculate camera offset to center the player
     // Calculate camera offset to center the player
-    camera_x = pos.x - (SCREEN_WIDTH / 2);
-    camera_y = pos.y - (SCREEN_HEIGHT / 2);
+    draw_camera_x = pos.x - (SCREEN_WIDTH / 2);
+    draw_camera_y = pos.y - (SCREEN_HEIGHT / 2);
 
 
     // Clamp camera position to prevent showing areas outside the world
     // Clamp camera position to prevent showing areas outside the world
-    camera_x = CLAMP(camera_x, WORLD_WIDTH - SCREEN_WIDTH, 0);
-    camera_y = CLAMP(camera_y, WORLD_HEIGHT - SCREEN_HEIGHT, 0);
+    draw_camera_x = CLAMP(draw_camera_x, WORLD_WIDTH - SCREEN_WIDTH, 0);
+    draw_camera_y = CLAMP(draw_camera_y, WORLD_HEIGHT - SCREEN_HEIGHT, 0);
 
 
     // if player is moving right or left, draw the corresponding sprite
     // if player is moving right or left, draw the corresponding sprite
     if (player->direction == ENTITY_RIGHT || player->direction == ENTITY_LEFT)
     if (player->direction == ENTITY_RIGHT || player->direction == ENTITY_LEFT)
@@ -590,8 +590,8 @@ static void player_render(Entity *self, GameManager *manager, Canvas *canvas, vo
         canvas_draw_sprite(
         canvas_draw_sprite(
             canvas,
             canvas,
             player->direction == ENTITY_RIGHT ? player->sprite_right : player->sprite_left,
             player->direction == ENTITY_RIGHT ? player->sprite_right : player->sprite_left,
-            pos.x - camera_x - 5, // Center the sprite horizontally
-            pos.y - camera_y - 5  // Center the sprite vertically
+            pos.x - draw_camera_x - 5, // Center the sprite horizontally
+            pos.y - draw_camera_y - 5  // Center the sprite vertically
         );
         );
         player->left = false;
         player->left = false;
     }
     }
@@ -601,28 +601,28 @@ static void player_render(Entity *self, GameManager *manager, Canvas *canvas, vo
         canvas_draw_sprite(
         canvas_draw_sprite(
             canvas,
             canvas,
             player->left ? player->sprite_left : player->sprite_right,
             player->left ? player->sprite_left : player->sprite_right,
-            pos.x - camera_x - 5, // Center the sprite horizontally
-            pos.y - camera_y - 5  // Center the sprite vertically
+            pos.x - draw_camera_x - 5, // Center the sprite horizontally
+            pos.y - draw_camera_y - 5  // Center the sprite vertically
         );
         );
     }
     }
 
 
     // Draw the outer bounds adjusted by camera offset
     // Draw the outer bounds adjusted by camera offset
-    canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
+    canvas_draw_frame(canvas, -draw_camera_x, -draw_camera_y, WORLD_WIDTH, WORLD_HEIGHT);
 
 
     // render tutorial
     // render tutorial
     if (game_context->game_mode == GAME_MODE_STORY)
     if (game_context->game_mode == GAME_MODE_STORY)
     {
     {
-        draw_tutorial(canvas, manager);
+        player_draw_tutorial(canvas, manager);
 
 
         if (game_context->is_menu_open)
         if (game_context->is_menu_open)
         {
         {
-            background_render(canvas, manager);
+            draw_background_render(canvas, manager);
         }
         }
     }
     }
     else
     else
     {
     {
         // render background
         // render background
-        background_render(canvas, manager);
+        draw_background_render(canvas, manager);
     }
     }
 }
 }