icon.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. if (ictx)
  25. {
  26. Vector pos = entity_pos_get(self);
  27. // Draw the icon, centered
  28. canvas_draw_icon(
  29. canvas,
  30. pos.x - camera_x - ictx->size.x / 2,
  31. pos.y - camera_y - ictx->size.y / 2,
  32. ictx->icon);
  33. }
  34. }
  35. static void icon_start(Entity *self, GameManager *manager, void *context)
  36. {
  37. UNUSED(manager);
  38. IconContext *ictx_self = (IconContext *)context;
  39. if (!ictx_self)
  40. {
  41. FURI_LOG_E("Game", "Icon context self is NULL");
  42. return;
  43. }
  44. IconContext *ictx = entity_context_get(self);
  45. if (!ictx)
  46. {
  47. FURI_LOG_E("Game", "Icon context is NULL");
  48. return;
  49. }
  50. IconContext *loaded_data = get_icon_context(g_name);
  51. if (!loaded_data)
  52. {
  53. FURI_LOG_E("Game", "Failed to find icon data for %s", g_name);
  54. return;
  55. }
  56. ictx_self->icon = loaded_data->icon;
  57. ictx_self->size = (Vector){loaded_data->size.x, loaded_data->size.y};
  58. ictx->icon = loaded_data->icon;
  59. ictx->size = (Vector){loaded_data->size.x, loaded_data->size.y};
  60. Vector pos = entity_pos_get(self);
  61. pos.x += ictx_self->size.x / 2;
  62. pos.y += ictx_self->size.y / 2;
  63. entity_pos_set(self, pos);
  64. entity_collider_add_circle(
  65. self,
  66. (ictx_self->size.x + ictx_self->size.y) / 4);
  67. free(loaded_data);
  68. }
  69. // -------------- Stop callback --------------
  70. static void icon_free(Entity *self, GameManager *manager, void *context)
  71. {
  72. UNUSED(self);
  73. UNUSED(manager);
  74. if (context)
  75. {
  76. free(context);
  77. }
  78. }
  79. // -------------- Entity description --------------
  80. const EntityDescription icon_desc = {
  81. .start = icon_start,
  82. .stop = icon_free,
  83. .update = NULL,
  84. .render = icon_render,
  85. .collision = icon_collision,
  86. .event = NULL,
  87. .context_size = sizeof(IconContext),
  88. };
  89. static IconContext *icon_generic_alloc(const char *id, const Icon *icon, uint8_t width, uint8_t height)
  90. {
  91. IconContext *ctx = malloc(sizeof(IconContext));
  92. if (!ctx)
  93. {
  94. FURI_LOG_E("Game", "Failed to allocate IconContext");
  95. return NULL;
  96. }
  97. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  98. ctx->icon = icon;
  99. ctx->size = (Vector){width, height};
  100. return ctx;
  101. }
  102. IconContext *get_icon_context(const char *name)
  103. {
  104. if (is_str(name, "house"))
  105. {
  106. return icon_generic_alloc("house", &I_icon_house_48x32px, 48, 32);
  107. }
  108. else if (is_str(name, "man"))
  109. {
  110. return icon_generic_alloc("man", &I_icon_man_7x16, 7, 16);
  111. }
  112. else if (is_str(name, "plant"))
  113. {
  114. return icon_generic_alloc("plant", &I_icon_plant_16x16, 16, 16);
  115. }
  116. // else if (is_str(name, "plant_fern") )
  117. // {
  118. // return icon_generic_alloc("plant_fern", &I_icon_plant_fern_18x16px, 18, 16);
  119. // }
  120. // else if (is_str(name, "plant_pointy") )
  121. // {
  122. // return icon_generic_alloc("plant_pointy", &I_icon_plant_pointy_13x16px, 13, 16);
  123. // }
  124. else if (is_str(name, "tree"))
  125. {
  126. return icon_generic_alloc("tree", &I_icon_tree_16x16, 16, 16);
  127. }
  128. else if (is_str(name, "woman"))
  129. {
  130. return icon_generic_alloc("woman", &I_icon_woman_9x16, 9, 16);
  131. }
  132. // else if (is_str(name, "chest_closed"))
  133. // {
  134. // return icon_generic_alloc("chest_closed", &I_icon_chest_closed_16x13px, 16, 13);
  135. // }
  136. // else if (is_str(name, "chest_open") )
  137. // {
  138. // return icon_generic_alloc("chest_open", &I_icon_chest_open_16x16px, 16, 16);
  139. // }
  140. else if (is_str(name, "fence"))
  141. {
  142. return icon_generic_alloc("fence", &I_icon_fence_16x8px, 16, 8);
  143. }
  144. else if (is_str(name, "fence_end"))
  145. {
  146. return icon_generic_alloc("fence_end", &I_icon_fence_end_16x8px, 16, 8);
  147. }
  148. // else if (is_str(name, "fence_vertical_end") )
  149. // {
  150. // return icon_generic_alloc("fence_vertical_end", &I_icon_fence_vertical_end_6x8px, 6, 8);
  151. // }
  152. // else if (is_str(name, "fence_vertical_start") )
  153. // {
  154. // return icon_generic_alloc("fence_vertical_start", &I_icon_fence_vertical_start_6x15px, 6, 15);
  155. // }
  156. else if (is_str(name, "flower"))
  157. {
  158. return icon_generic_alloc("flower", &I_icon_flower_16x16, 16, 16);
  159. }
  160. else if (is_str(name, "lake_bottom"))
  161. {
  162. return icon_generic_alloc("lake_bottom", &I_icon_lake_bottom_31x12px, 31, 12);
  163. }
  164. else if (is_str(name, "lake_bottom_left"))
  165. {
  166. return icon_generic_alloc("lake_bottom_left", &I_icon_lake_bottom_left_24x22px, 24, 22);
  167. }
  168. else if (is_str(name, "lake_bottom_right"))
  169. {
  170. return icon_generic_alloc("lake_bottom_right", &I_icon_lake_bottom_right_24x22px, 24, 22);
  171. }
  172. else if (is_str(name, "lake_left"))
  173. {
  174. return icon_generic_alloc("lake_left", &I_icon_lake_left_11x31px, 11, 31);
  175. }
  176. else if (is_str(name, "lake_right"))
  177. {
  178. return icon_generic_alloc("lake_right", &I_icon_lake_right_11x31, 11, 31);
  179. }
  180. else if (is_str(name, "lake_top"))
  181. {
  182. return icon_generic_alloc("lake_top", &I_icon_lake_top_31x12px, 31, 12);
  183. }
  184. else if (is_str(name, "lake_top_left"))
  185. {
  186. return icon_generic_alloc("lake_top_left", &I_icon_lake_top_left_24x22px, 24, 22);
  187. }
  188. else if (is_str(name, "lake_top_right"))
  189. {
  190. return icon_generic_alloc("lake_top_right", &I_icon_lake_top_right_24x22px, 24, 22);
  191. }
  192. else if (is_str(name, "rock_large"))
  193. {
  194. return icon_generic_alloc("rock_large", &I_icon_rock_large_18x19px, 18, 19);
  195. }
  196. else if (is_str(name, "rock_medium"))
  197. {
  198. return icon_generic_alloc("rock_medium", &I_icon_rock_medium_16x14px, 16, 14);
  199. }
  200. else if (is_str(name, "rock_small"))
  201. {
  202. return icon_generic_alloc("rock_small", &I_icon_rock_small_10x8px, 10, 8);
  203. }
  204. // If no match is found
  205. FURI_LOG_E("Game", "Icon not found: %s", name);
  206. return NULL;
  207. }
  208. const char *icon_get_id(const Icon *icon)
  209. {
  210. if (icon == &I_icon_house_48x32px)
  211. {
  212. return "house";
  213. }
  214. else if (icon == &I_icon_man_7x16)
  215. {
  216. return "man";
  217. }
  218. else if (icon == &I_icon_plant_16x16)
  219. {
  220. return "plant";
  221. }
  222. // else if (icon == &I_icon_plant_fern_18x16px)
  223. // {
  224. // return "plant_fern";
  225. // }
  226. // else if (icon == &I_icon_plant_pointy_13x16px)
  227. // {
  228. // return "plant_pointy";
  229. // }
  230. else if (icon == &I_icon_tree_16x16)
  231. {
  232. return "tree";
  233. }
  234. else if (icon == &I_icon_woman_9x16)
  235. {
  236. return "woman";
  237. }
  238. // else if (icon == &I_icon_chest_closed_16x13px)
  239. // {
  240. // return "chest_closed";
  241. // }
  242. // else if (icon == &I_icon_chest_open_16x16px)
  243. // {
  244. // return "chest_open";
  245. // }
  246. else if (icon == &I_icon_fence_16x8px)
  247. {
  248. return "fence";
  249. }
  250. else if (icon == &I_icon_fence_end_16x8px)
  251. {
  252. return "fence_end";
  253. }
  254. // else if (icon == &I_icon_fence_vertical_end_6x8px)
  255. // {
  256. // return "fence_vertical_end";
  257. // }
  258. // else if (icon == &I_icon_fence_vertical_start_6x15px)
  259. // {
  260. // return "fence_vertical_start";
  261. // }
  262. else if (icon == &I_icon_flower_16x16)
  263. {
  264. return "flower";
  265. }
  266. else if (icon == &I_icon_lake_bottom_31x12px)
  267. {
  268. return "lake_bottom";
  269. }
  270. else if (icon == &I_icon_lake_bottom_left_24x22px)
  271. {
  272. return "lake_bottom_left";
  273. }
  274. else if (icon == &I_icon_lake_bottom_right_24x22px)
  275. {
  276. return "lake_bottom_right";
  277. }
  278. else if (icon == &I_icon_lake_left_11x31px)
  279. {
  280. return "lake_left";
  281. }
  282. else if (icon == &I_icon_lake_right_11x31)
  283. {
  284. return "lake_right";
  285. }
  286. else if (icon == &I_icon_lake_top_31x12px)
  287. {
  288. return "lake_top";
  289. }
  290. else if (icon == &I_icon_lake_top_left_24x22px)
  291. {
  292. return "lake_top_left";
  293. }
  294. else if (icon == &I_icon_lake_top_right_24x22px)
  295. {
  296. return "lake_top_right";
  297. }
  298. else if (icon == &I_icon_rock_large_18x19px)
  299. {
  300. return "rock_large";
  301. }
  302. else if (icon == &I_icon_rock_medium_16x14px)
  303. {
  304. return "rock_medium";
  305. }
  306. else if (icon == &I_icon_rock_small_10x8px)
  307. {
  308. return "rock_small";
  309. }
  310. // If no match is found
  311. FURI_LOG_E("Game", "Icon ID not found for given icon pointer.");
  312. return NULL;
  313. }