icon.c 7.3 KB

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