icon.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "game/icon.h"
  2. static void icon_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  3. {
  4. UNUSED(manager);
  5. UNUSED(self);
  6. IconContext *ictx = (IconContext *)context;
  7. if (ictx && entity_description_get(other) == &player_desc)
  8. {
  9. PlayerContext *player = (PlayerContext *)entity_context_get(other);
  10. if (player)
  11. {
  12. // Set the player's old position to prevent collision
  13. entity_pos_set(other, player->old_position);
  14. // Reset movement to prevent re-collision
  15. player->dx = 0;
  16. player->dy = 0;
  17. }
  18. }
  19. }
  20. static void icon_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  21. {
  22. UNUSED(manager);
  23. IconContext *ictx = (IconContext *)context;
  24. Vector pos = entity_pos_get(self);
  25. if (ictx)
  26. canvas_draw_icon(canvas, pos.x - camera_x - ictx->size.x / 2, pos.y - camera_y - ictx->size.y / 2, ictx->icon);
  27. }
  28. static void icon_start(Entity *self, GameManager *manager, void *context)
  29. {
  30. UNUSED(manager);
  31. IconContext *ictx_self = (IconContext *)context;
  32. if (!ictx_self)
  33. {
  34. FURI_LOG_E("Game", "Icon context self is NULL");
  35. return;
  36. }
  37. IconContext *ictx = entity_context_get(self);
  38. if (!ictx)
  39. {
  40. FURI_LOG_E("Game", "Icon context is NULL");
  41. return;
  42. }
  43. IconContext *loaded_data = get_icon_context(g_name);
  44. if (!loaded_data)
  45. {
  46. FURI_LOG_E("Game", "Failed to find icon data for %s", g_name);
  47. return;
  48. }
  49. ictx_self->icon = loaded_data->icon;
  50. ictx_self->size = (Vector){loaded_data->size.x, loaded_data->size.y};
  51. ictx->icon = loaded_data->icon;
  52. ictx->size = (Vector){loaded_data->size.x, loaded_data->size.y};
  53. Vector pos = entity_pos_get(self);
  54. pos.x += ictx_self->size.x / 2;
  55. pos.y += ictx_self->size.y / 2;
  56. entity_pos_set(self, pos);
  57. entity_collider_add_circle(
  58. self,
  59. (ictx_self->size.x + ictx_self->size.y) / 4);
  60. free(loaded_data);
  61. }
  62. // -------------- Stop callback --------------
  63. static void icon_free(Entity *self, GameManager *manager, void *context)
  64. {
  65. UNUSED(self);
  66. UNUSED(manager);
  67. if (context)
  68. free(context);
  69. }
  70. // -------------- Entity description --------------
  71. const EntityDescription icon_desc = {
  72. .start = icon_start,
  73. .stop = icon_free,
  74. .update = NULL,
  75. .render = icon_render,
  76. .collision = icon_collision,
  77. .event = NULL,
  78. .context_size = sizeof(IconContext),
  79. };
  80. static IconContext *icon_generic_alloc(const char *id, const Icon *icon, uint8_t width, uint8_t height)
  81. {
  82. IconContext *ctx = malloc(sizeof(IconContext));
  83. if (!ctx)
  84. {
  85. FURI_LOG_E("Game", "Failed to allocate IconContext");
  86. return NULL;
  87. }
  88. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  89. ctx->icon = icon;
  90. ctx->size = (Vector){width, height};
  91. return ctx;
  92. }
  93. IconContext *get_icon_context(const char *name)
  94. {
  95. if (is_str(name, "house"))
  96. return icon_generic_alloc("house", &I_icon_house_48x32px, 48, 32);
  97. else if (is_str(name, "man"))
  98. return icon_generic_alloc("man", &I_icon_man_7x16, 7, 16);
  99. else if (is_str(name, "plant"))
  100. return icon_generic_alloc("plant", &I_icon_plant_16x16, 16, 16);
  101. else if (is_str(name, "tree"))
  102. return icon_generic_alloc("tree", &I_icon_tree_16x16, 16, 16);
  103. else if (is_str(name, "woman"))
  104. return icon_generic_alloc("woman", &I_icon_woman_9x16, 9, 16);
  105. else if (is_str(name, "fence"))
  106. return icon_generic_alloc("fence", &I_icon_fence_16x8px, 16, 8);
  107. else if (is_str(name, "fence_end"))
  108. return icon_generic_alloc("fence_end", &I_icon_fence_end_16x8px, 16, 8);
  109. // else if (is_str(name, "fence_vertical_end") )
  110. // return icon_generic_alloc("fence_vertical_end", &I_icon_fence_vertical_end_6x8px, 6, 8);
  111. // else if (is_str(name, "fence_vertical_start") )
  112. // return icon_generic_alloc("fence_vertical_start", &I_icon_fence_vertical_start_6x15px, 6, 15);
  113. else if (is_str(name, "flower"))
  114. return icon_generic_alloc("flower", &I_icon_flower_16x16, 16, 16);
  115. else if (is_str(name, "lake_bottom"))
  116. return icon_generic_alloc("lake_bottom", &I_icon_lake_bottom_31x12px, 31, 12);
  117. else if (is_str(name, "lake_bottom_left"))
  118. return icon_generic_alloc("lake_bottom_left", &I_icon_lake_bottom_left_24x22px, 24, 22);
  119. else if (is_str(name, "lake_bottom_right"))
  120. return icon_generic_alloc("lake_bottom_right", &I_icon_lake_bottom_right_24x22px, 24, 22);
  121. else if (is_str(name, "lake_left"))
  122. return icon_generic_alloc("lake_left", &I_icon_lake_left_11x31px, 11, 31);
  123. else if (is_str(name, "lake_right"))
  124. return icon_generic_alloc("lake_right", &I_icon_lake_right_11x31, 11, 31);
  125. else if (is_str(name, "lake_top"))
  126. return icon_generic_alloc("lake_top", &I_icon_lake_top_31x12px, 31, 12);
  127. else if (is_str(name, "lake_top_left"))
  128. return icon_generic_alloc("lake_top_left", &I_icon_lake_top_left_24x22px, 24, 22);
  129. else if (is_str(name, "lake_top_right"))
  130. return icon_generic_alloc("lake_top_right", &I_icon_lake_top_right_24x22px, 24, 22);
  131. else if (is_str(name, "rock_large"))
  132. return icon_generic_alloc("rock_large", &I_icon_rock_large_18x19px, 18, 19);
  133. else if (is_str(name, "rock_medium"))
  134. return icon_generic_alloc("rock_medium", &I_icon_rock_medium_16x14px, 16, 14);
  135. else if (is_str(name, "rock_small"))
  136. return icon_generic_alloc("rock_small", &I_icon_rock_small_10x8px, 10, 8);
  137. // If no match is found
  138. FURI_LOG_E("Game", "Icon not found: %s", name);
  139. return NULL;
  140. }
  141. const char *icon_get_id(const Icon *icon)
  142. {
  143. if (icon == &I_icon_house_48x32px)
  144. return "house";
  145. else if (icon == &I_icon_man_7x16)
  146. return "man";
  147. else if (icon == &I_icon_plant_16x16)
  148. return "plant";
  149. else if (icon == &I_icon_tree_16x16)
  150. return "tree";
  151. else if (icon == &I_icon_woman_9x16)
  152. return "woman";
  153. else if (icon == &I_icon_fence_16x8px)
  154. return "fence";
  155. else if (icon == &I_icon_fence_end_16x8px)
  156. return "fence_end";
  157. // else if (icon == &I_icon_fence_vertical_end_6x8px)
  158. // return "fence_vertical_end";
  159. // else if (icon == &I_icon_fence_vertical_start_6x15px)
  160. // return "fence_vertical_start";
  161. else if (icon == &I_icon_flower_16x16)
  162. return "flower";
  163. else if (icon == &I_icon_lake_bottom_31x12px)
  164. return "lake_bottom";
  165. else if (icon == &I_icon_lake_bottom_left_24x22px)
  166. return "lake_bottom_left";
  167. else if (icon == &I_icon_lake_bottom_right_24x22px)
  168. return "lake_bottom_right";
  169. else if (icon == &I_icon_lake_left_11x31px)
  170. return "lake_left";
  171. else if (icon == &I_icon_lake_right_11x31)
  172. return "lake_right";
  173. else if (icon == &I_icon_lake_top_31x12px)
  174. return "lake_top";
  175. else if (icon == &I_icon_lake_top_left_24x22px)
  176. return "lake_top_left";
  177. else if (icon == &I_icon_lake_top_right_24x22px)
  178. return "lake_top_right";
  179. else if (icon == &I_icon_rock_large_18x19px)
  180. return "rock_large";
  181. else if (icon == &I_icon_rock_medium_16x14px)
  182. return "rock_medium";
  183. else if (icon == &I_icon_rock_small_10x8px)
  184. return "rock_small";
  185. // If no match is found
  186. FURI_LOG_E("Game", "Icon ID not found for given icon pointer.");
  187. return NULL;
  188. }