icon.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. UNUSED(context);
  68. }
  69. // -------------- Entity description --------------
  70. const EntityDescription icon_desc = {
  71. .start = icon_start,
  72. .stop = icon_free,
  73. .update = NULL,
  74. .render = icon_render,
  75. .collision = icon_collision,
  76. .event = NULL,
  77. .context_size = sizeof(IconContext),
  78. };
  79. static IconContext *icon_generic_alloc(const char *id, const Icon *icon, uint8_t width, uint8_t height)
  80. {
  81. IconContext *ctx = malloc(sizeof(IconContext));
  82. if (!ctx)
  83. {
  84. FURI_LOG_E("Game", "Failed to allocate IconContext");
  85. return NULL;
  86. }
  87. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  88. ctx->icon = icon;
  89. ctx->size = (Vector){width, height};
  90. return ctx;
  91. }
  92. IconContext *get_icon_context(const char *name)
  93. {
  94. if (is_str(name, "house"))
  95. return icon_generic_alloc("house", &I_icon_house_48x32px, 48, 32);
  96. else if (is_str(name, "man"))
  97. return icon_generic_alloc("man", &I_icon_man_7x16, 7, 16);
  98. else if (is_str(name, "plant"))
  99. return icon_generic_alloc("plant", &I_icon_plant_16x16, 16, 16);
  100. else if (is_str(name, "tree"))
  101. return icon_generic_alloc("tree", &I_icon_tree_16x16, 16, 16);
  102. else if (is_str(name, "woman"))
  103. return icon_generic_alloc("woman", &I_icon_woman_9x16, 9, 16);
  104. else if (is_str(name, "fence"))
  105. return icon_generic_alloc("fence", &I_icon_fence_16x8px, 16, 8);
  106. else if (is_str(name, "fence_end"))
  107. return icon_generic_alloc("fence_end", &I_icon_fence_end_16x8px, 16, 8);
  108. // else if (is_str(name, "fence_vertical_end") )
  109. // return icon_generic_alloc("fence_vertical_end", &I_icon_fence_vertical_end_6x8px, 6, 8);
  110. // else if (is_str(name, "fence_vertical_start") )
  111. // return icon_generic_alloc("fence_vertical_start", &I_icon_fence_vertical_start_6x15px, 6, 15);
  112. else if (is_str(name, "flower"))
  113. return icon_generic_alloc("flower", &I_icon_flower_16x16, 16, 16);
  114. else if (is_str(name, "lake_bottom"))
  115. return icon_generic_alloc("lake_bottom", &I_icon_lake_bottom_31x12px, 31, 12);
  116. else if (is_str(name, "lake_bottom_left"))
  117. return icon_generic_alloc("lake_bottom_left", &I_icon_lake_bottom_left_24x22px, 24, 22);
  118. else if (is_str(name, "lake_bottom_right"))
  119. return icon_generic_alloc("lake_bottom_right", &I_icon_lake_bottom_right_24x22px, 24, 22);
  120. else if (is_str(name, "lake_left"))
  121. return icon_generic_alloc("lake_left", &I_icon_lake_left_11x31px, 11, 31);
  122. else if (is_str(name, "lake_right"))
  123. return icon_generic_alloc("lake_right", &I_icon_lake_right_11x31, 11, 31);
  124. else if (is_str(name, "lake_top"))
  125. return icon_generic_alloc("lake_top", &I_icon_lake_top_31x12px, 31, 12);
  126. else if (is_str(name, "lake_top_left"))
  127. return icon_generic_alloc("lake_top_left", &I_icon_lake_top_left_24x22px, 24, 22);
  128. else if (is_str(name, "lake_top_right"))
  129. return icon_generic_alloc("lake_top_right", &I_icon_lake_top_right_24x22px, 24, 22);
  130. else if (is_str(name, "rock_large"))
  131. return icon_generic_alloc("rock_large", &I_icon_rock_large_18x19px, 18, 19);
  132. else if (is_str(name, "rock_medium"))
  133. return icon_generic_alloc("rock_medium", &I_icon_rock_medium_16x14px, 16, 14);
  134. else if (is_str(name, "rock_small"))
  135. return icon_generic_alloc("rock_small", &I_icon_rock_small_10x8px, 10, 8);
  136. // If no match is found
  137. FURI_LOG_E("Game", "Icon not found: %s", name);
  138. return NULL;
  139. }
  140. const char *icon_get_id(const Icon *icon)
  141. {
  142. if (icon == &I_icon_house_48x32px)
  143. return "house";
  144. else if (icon == &I_icon_man_7x16)
  145. return "man";
  146. else if (icon == &I_icon_plant_16x16)
  147. return "plant";
  148. else if (icon == &I_icon_tree_16x16)
  149. return "tree";
  150. else if (icon == &I_icon_woman_9x16)
  151. return "woman";
  152. else if (icon == &I_icon_fence_16x8px)
  153. return "fence";
  154. else if (icon == &I_icon_fence_end_16x8px)
  155. return "fence_end";
  156. // else if (icon == &I_icon_fence_vertical_end_6x8px)
  157. // return "fence_vertical_end";
  158. // else if (icon == &I_icon_fence_vertical_start_6x15px)
  159. // return "fence_vertical_start";
  160. else if (icon == &I_icon_flower_16x16)
  161. return "flower";
  162. else if (icon == &I_icon_lake_bottom_31x12px)
  163. return "lake_bottom";
  164. else if (icon == &I_icon_lake_bottom_left_24x22px)
  165. return "lake_bottom_left";
  166. else if (icon == &I_icon_lake_bottom_right_24x22px)
  167. return "lake_bottom_right";
  168. else if (icon == &I_icon_lake_left_11x31px)
  169. return "lake_left";
  170. else if (icon == &I_icon_lake_right_11x31)
  171. return "lake_right";
  172. else if (icon == &I_icon_lake_top_31x12px)
  173. return "lake_top";
  174. else if (icon == &I_icon_lake_top_left_24x22px)
  175. return "lake_top_left";
  176. else if (icon == &I_icon_lake_top_right_24x22px)
  177. return "lake_top_right";
  178. else if (icon == &I_icon_rock_large_18x19px)
  179. return "rock_large";
  180. else if (icon == &I_icon_rock_medium_16x14px)
  181. return "rock_medium";
  182. else if (icon == &I_icon_rock_small_10x8px)
  183. return "rock_small";
  184. // If no match is found
  185. FURI_LOG_E("Game", "Icon ID not found for given icon pointer.");
  186. return NULL;
  187. }