icon.c 7.5 KB

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