icon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "game/icon.h"
  2. static IconContext *icon_context_generic = NULL;
  3. static void icon_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  4. {
  5. UNUSED(manager);
  6. UNUSED(self);
  7. IconContext *ictx = (IconContext *)context;
  8. if (ictx && entity_description_get(other) == &player_desc)
  9. {
  10. PlayerContext *player = (PlayerContext *)entity_context_get(other);
  11. if (player)
  12. {
  13. // Set the player's old position to prevent collision
  14. entity_pos_set(other, player->old_position);
  15. // Reset movement to prevent re-collision
  16. player->dx = 0;
  17. player->dy = 0;
  18. }
  19. }
  20. }
  21. static void icon_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  22. {
  23. UNUSED(manager);
  24. IconContext *ictx = (IconContext *)context;
  25. furi_check(ictx, "Icon context is NULL");
  26. Vector pos = entity_pos_get(self);
  27. int x_pos = pos.x - camera_x - ictx->size.x / 2;
  28. int y_pos = pos.y - camera_y - ictx->size.y / 2;
  29. // Check if position is within the screen
  30. if (x_pos + ictx->size.x < 0 || x_pos > SCREEN_WIDTH ||
  31. y_pos + ictx->size.y < 0 || y_pos > SCREEN_HEIGHT)
  32. {
  33. return;
  34. }
  35. canvas_draw_icon(canvas, x_pos, y_pos, ictx->icon);
  36. }
  37. static void icon_start(Entity *self, GameManager *manager, void *context)
  38. {
  39. UNUSED(manager);
  40. // The entity's instance context
  41. IconContext *ictx_instance = (IconContext *)context;
  42. if (!ictx_instance)
  43. {
  44. FURI_LOG_E("Game", "Icon context instance is NULL");
  45. return;
  46. }
  47. // Instead of allocating a temporary context and freeing it immediately,
  48. // reuse the global generic context (similar to npc_context_generic in npc.c)
  49. IconContext *generic_ctx = get_icon_context(g_name);
  50. if (!generic_ctx)
  51. {
  52. FURI_LOG_E("Game", "Failed to get icon context for %s", g_name);
  53. return;
  54. }
  55. // Copy generic icon data into the instance context
  56. ictx_instance->icon = generic_ctx->icon;
  57. ictx_instance->size = generic_ctx->size;
  58. // Adjust position to be centered on the icon
  59. Vector pos = entity_pos_get(self);
  60. pos.x += ictx_instance->size.x / 2;
  61. pos.y += ictx_instance->size.y / 2;
  62. entity_pos_set(self, pos);
  63. // Add a circular collider based on the icon's dimensions
  64. entity_collider_add_circle(self, (ictx_instance->size.x + ictx_instance->size.y) / 4);
  65. }
  66. static void icon_free(Entity *self, GameManager *manager, void *context)
  67. {
  68. UNUSED(self);
  69. UNUSED(manager);
  70. UNUSED(context);
  71. if (icon_context_generic)
  72. {
  73. free(icon_context_generic);
  74. icon_context_generic = NULL;
  75. }
  76. }
  77. // -------------- Entity description --------------
  78. const EntityDescription icon_desc = {
  79. .start = icon_start,
  80. .stop = icon_free,
  81. .update = NULL,
  82. .render = icon_render,
  83. .collision = icon_collision,
  84. .event = NULL,
  85. .context_size = sizeof(IconContext),
  86. };
  87. // Generic allocation using a global static pointer
  88. static IconContext *icon_generic_alloc(IconID id, const Icon *icon, uint8_t width, uint8_t height)
  89. {
  90. if (!icon_context_generic)
  91. {
  92. icon_context_generic = malloc(sizeof(IconContext));
  93. if (!icon_context_generic)
  94. {
  95. FURI_LOG_E("Game", "Failed to allocate IconContext");
  96. return NULL;
  97. }
  98. }
  99. icon_context_generic->id = id;
  100. icon_context_generic->icon = icon;
  101. icon_context_generic->size = (Vector){width, height};
  102. return icon_context_generic;
  103. }
  104. IconContext *get_icon_context(const char *name)
  105. {
  106. if (is_str(name, "house"))
  107. return icon_generic_alloc(ICON_ID_HOUSE, &I_icon_house_48x32px, 48, 32);
  108. else if (is_str(name, "man"))
  109. return icon_generic_alloc(ICON_ID_MAN, &I_icon_man_7x16, 7, 16);
  110. else if (is_str(name, "plant"))
  111. return icon_generic_alloc(ICON_ID_PLANT, &I_icon_plant_16x16, 16, 16);
  112. else if (is_str(name, "tree"))
  113. return icon_generic_alloc(ICON_ID_TREE, &I_icon_tree_16x16, 16, 16);
  114. else if (is_str(name, "woman"))
  115. return icon_generic_alloc(ICON_ID_WOMAN, &I_icon_woman_9x16, 9, 16);
  116. else if (is_str(name, "fence"))
  117. return icon_generic_alloc(ICON_ID_FENCE, &I_icon_fence_16x8px, 16, 8);
  118. else if (is_str(name, "fence_end"))
  119. return icon_generic_alloc(ICON_ID_FENCE_END, &I_icon_fence_end_16x8px, 16, 8);
  120. else if (is_str(name, "fence_vertical_end"))
  121. return icon_generic_alloc(ICON_ID_FENCE_VERTICAL_END, &I_icon_fence_vertical_end_6x8px, 6, 8);
  122. else if (is_str(name, "fence_vertical_start"))
  123. return icon_generic_alloc(ICON_ID_FENCE_VERTICAL_START, &I_icon_fence_vertical_start_6x15px, 6, 15);
  124. else if (is_str(name, "flower"))
  125. return icon_generic_alloc(ICON_ID_FLOWER, &I_icon_flower_16x16, 16, 16);
  126. else if (is_str(name, "lake_bottom"))
  127. return icon_generic_alloc(ICON_ID_LAKE_BOTTOM, &I_icon_lake_bottom_31x12px, 31, 12);
  128. else if (is_str(name, "lake_bottom_left"))
  129. return icon_generic_alloc(ICON_ID_LAKE_BOTTOM_LEFT, &I_icon_lake_bottom_left_24x22px, 24, 22);
  130. else if (is_str(name, "lake_bottom_right"))
  131. return icon_generic_alloc(ICON_ID_LAKE_BOTTOM_RIGHT, &I_icon_lake_bottom_right_24x22px, 24, 22);
  132. else if (is_str(name, "lake_left"))
  133. return icon_generic_alloc(ICON_ID_LAKE_LEFT, &I_icon_lake_left_11x31px, 11, 31);
  134. else if (is_str(name, "lake_right"))
  135. return icon_generic_alloc(ICON_ID_LAKE_RIGHT, &I_icon_lake_right_11x31, 11, 31);
  136. else if (is_str(name, "lake_top"))
  137. return icon_generic_alloc(ICON_ID_LAKE_TOP, &I_icon_lake_top_31x12px, 31, 12);
  138. else if (is_str(name, "lake_top_left"))
  139. return icon_generic_alloc(ICON_ID_LAKE_TOP_LEFT, &I_icon_lake_top_left_24x22px, 24, 22);
  140. else if (is_str(name, "lake_top_right"))
  141. return icon_generic_alloc(ICON_ID_LAKE_TOP_RIGHT, &I_icon_lake_top_right_24x22px, 24, 22);
  142. else if (is_str(name, "rock_large"))
  143. return icon_generic_alloc(ICON_ID_ROCK_LARGE, &I_icon_rock_large_18x19px, 18, 19);
  144. else if (is_str(name, "rock_medium"))
  145. return icon_generic_alloc(ICON_ID_ROCK_MEDIUM, &I_icon_rock_medium_16x14px, 16, 14);
  146. else if (is_str(name, "rock_small"))
  147. return icon_generic_alloc(ICON_ID_ROCK_SMALL, &I_icon_rock_small_10x8px, 10, 8);
  148. // If no match is found, log an error and return NULL
  149. FURI_LOG_E("Game", "Icon not found: %s", name);
  150. return NULL;
  151. }