icon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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, "earth") )
  105. // {
  106. // return icon_generic_alloc("earth", &I_icon_earth_15x16, 15, 16);
  107. // }
  108. // else if (is_str(name, "home") )
  109. // {
  110. // return icon_generic_alloc("home", &I_icon_home_15x16, 15, 16);
  111. // }
  112. if (is_str(name, "house"))
  113. {
  114. return icon_generic_alloc("house", &I_icon_house_48x32px, 48, 32);
  115. }
  116. // else if (is_str(name, "house_3d") )
  117. // {
  118. // return icon_generic_alloc("house_3d", &I_icon_house_3d_34x45px, 34, 45);
  119. // }
  120. // else if (is_str(name, "info") )
  121. // {
  122. // return icon_generic_alloc("info", &I_icon_info_15x16, 15, 16);
  123. // }
  124. else if (is_str(name, "man"))
  125. {
  126. return icon_generic_alloc("man", &I_icon_man_7x16, 7, 16);
  127. }
  128. else if (is_str(name, "plant"))
  129. {
  130. return icon_generic_alloc("plant", &I_icon_plant_16x16, 16, 16);
  131. }
  132. // else if (is_str(name, "plant_fern") )
  133. // {
  134. // return icon_generic_alloc("plant_fern", &I_icon_plant_fern_18x16px, 18, 16);
  135. // }
  136. // else if (is_str(name, "plant_pointy") )
  137. // {
  138. // return icon_generic_alloc("plant_pointy", &I_icon_plant_pointy_13x16px, 13, 16);
  139. // }
  140. else if (is_str(name, "tree"))
  141. {
  142. return icon_generic_alloc("tree", &I_icon_tree_16x16, 16, 16);
  143. }
  144. // else if (is_str(name, "tree_29x30") )
  145. // {
  146. // return icon_generic_alloc("tree_29x30", &I_icon_tree_29x30px, 29, 30);
  147. // }
  148. // else if (is_str(name, "tree_48x48") )
  149. // {
  150. // return icon_generic_alloc("tree_48x48", &I_icon_tree_48x48px, 48, 48);
  151. // }
  152. else if (is_str(name, "woman"))
  153. {
  154. return icon_generic_alloc("woman", &I_icon_woman_9x16, 9, 16);
  155. }
  156. // else if (is_str(name, "chest_closed"))
  157. // {
  158. // return icon_generic_alloc("chest_closed", &I_icon_chest_closed_16x13px, 16, 13);
  159. // }
  160. // else if (is_str(name, "chest_open") )
  161. // {
  162. // return icon_generic_alloc("chest_open", &I_icon_chest_open_16x16px, 16, 16);
  163. // }
  164. else if (is_str(name, "fence"))
  165. {
  166. return icon_generic_alloc("fence", &I_icon_fence_16x8px, 16, 8);
  167. }
  168. else if (is_str(name, "fence_end"))
  169. {
  170. return icon_generic_alloc("fence_end", &I_icon_fence_end_16x8px, 16, 8);
  171. }
  172. // else if (is_str(name, "fence_vertical_end") )
  173. // {
  174. // return icon_generic_alloc("fence_vertical_end", &I_icon_fence_vertical_end_6x8px, 6, 8);
  175. // }
  176. // else if (is_str(name, "fence_vertical_start") )
  177. // {
  178. // return icon_generic_alloc("fence_vertical_start", &I_icon_fence_vertical_start_6x15px, 6, 15);
  179. // }
  180. else if (is_str(name, "flower"))
  181. {
  182. return icon_generic_alloc("flower", &I_icon_flower_16x16, 16, 16);
  183. }
  184. else if (is_str(name, "lake_bottom"))
  185. {
  186. return icon_generic_alloc("lake_bottom", &I_icon_lake_bottom_31x12px, 31, 12);
  187. }
  188. else if (is_str(name, "lake_bottom_left"))
  189. {
  190. return icon_generic_alloc("lake_bottom_left", &I_icon_lake_bottom_left_24x22px, 24, 22);
  191. }
  192. else if (is_str(name, "lake_bottom_right"))
  193. {
  194. return icon_generic_alloc("lake_bottom_right", &I_icon_lake_bottom_right_24x22px, 24, 22);
  195. }
  196. else if (is_str(name, "lake_left"))
  197. {
  198. return icon_generic_alloc("lake_left", &I_icon_lake_left_11x31px, 11, 31);
  199. }
  200. else if (is_str(name, "lake_right"))
  201. {
  202. return icon_generic_alloc("lake_right", &I_icon_lake_right_11x31, 11, 31);
  203. }
  204. else if (is_str(name, "lake_top"))
  205. {
  206. return icon_generic_alloc("lake_top", &I_icon_lake_top_31x12px, 31, 12);
  207. }
  208. else if (is_str(name, "lake_top_left"))
  209. {
  210. return icon_generic_alloc("lake_top_left", &I_icon_lake_top_left_24x22px, 24, 22);
  211. }
  212. else if (is_str(name, "lake_top_right"))
  213. {
  214. return icon_generic_alloc("lake_top_right", &I_icon_lake_top_right_24x22px, 24, 22);
  215. }
  216. else if (is_str(name, "rock_large"))
  217. {
  218. return icon_generic_alloc("rock_large", &I_icon_rock_large_18x19px, 18, 19);
  219. }
  220. else if (is_str(name, "rock_medium"))
  221. {
  222. return icon_generic_alloc("rock_medium", &I_icon_rock_medium_16x14px, 16, 14);
  223. }
  224. else if (is_str(name, "rock_small"))
  225. {
  226. return icon_generic_alloc("rock_small", &I_icon_rock_small_10x8px, 10, 8);
  227. }
  228. // If no match is found
  229. FURI_LOG_E("Game", "Icon not found: %s", name);
  230. return NULL;
  231. }
  232. const char *icon_get_id(const Icon *icon)
  233. {
  234. // if (icon == &I_icon_earth_15x16)
  235. // {
  236. // return "earth";
  237. // }
  238. // else if (icon == &I_icon_home_15x16)
  239. // {
  240. // return "home";
  241. // }
  242. if (icon == &I_icon_house_48x32px)
  243. {
  244. return "house";
  245. }
  246. // else if (icon == &I_icon_house_3d_34x45px)
  247. // {
  248. // return "house_3d";
  249. // }
  250. // else if (icon == &I_icon_info_15x16)
  251. // {
  252. // return "info";
  253. // }
  254. else if (icon == &I_icon_man_7x16)
  255. {
  256. return "man";
  257. }
  258. else if (icon == &I_icon_plant_16x16)
  259. {
  260. return "plant";
  261. }
  262. // else if (icon == &I_icon_plant_fern_18x16px)
  263. // {
  264. // return "plant_fern";
  265. // }
  266. // else if (icon == &I_icon_plant_pointy_13x16px)
  267. // {
  268. // return "plant_pointy";
  269. // }
  270. else if (icon == &I_icon_tree_16x16)
  271. {
  272. return "tree";
  273. }
  274. // else if (icon == &I_icon_tree_29x30px)
  275. // {
  276. // return "tree_29x30";
  277. // }
  278. // else if (icon == &I_icon_tree_48x48px)
  279. // {
  280. // return "tree_48x48";
  281. // }
  282. else if (icon == &I_icon_woman_9x16)
  283. {
  284. return "woman";
  285. }
  286. // else if (icon == &I_icon_chest_closed_16x13px)
  287. // {
  288. // return "chest_closed";
  289. // }
  290. // else if (icon == &I_icon_chest_open_16x16px)
  291. // {
  292. // return "chest_open";
  293. // }
  294. else if (icon == &I_icon_fence_16x8px)
  295. {
  296. return "fence";
  297. }
  298. else if (icon == &I_icon_fence_end_16x8px)
  299. {
  300. return "fence_end";
  301. }
  302. // else if (icon == &I_icon_fence_vertical_end_6x8px)
  303. // {
  304. // return "fence_vertical_end";
  305. // }
  306. // else if (icon == &I_icon_fence_vertical_start_6x15px)
  307. // {
  308. // return "fence_vertical_start";
  309. // }
  310. else if (icon == &I_icon_flower_16x16)
  311. {
  312. return "flower";
  313. }
  314. else if (icon == &I_icon_lake_bottom_31x12px)
  315. {
  316. return "lake_bottom";
  317. }
  318. else if (icon == &I_icon_lake_bottom_left_24x22px)
  319. {
  320. return "lake_bottom_left";
  321. }
  322. else if (icon == &I_icon_lake_bottom_right_24x22px)
  323. {
  324. return "lake_bottom_right";
  325. }
  326. else if (icon == &I_icon_lake_left_11x31px)
  327. {
  328. return "lake_left";
  329. }
  330. else if (icon == &I_icon_lake_right_11x31)
  331. {
  332. return "lake_right";
  333. }
  334. else if (icon == &I_icon_lake_top_31x12px)
  335. {
  336. return "lake_top";
  337. }
  338. else if (icon == &I_icon_lake_top_left_24x22px)
  339. {
  340. return "lake_top_left";
  341. }
  342. else if (icon == &I_icon_lake_top_right_24x22px)
  343. {
  344. return "lake_top_right";
  345. }
  346. else if (icon == &I_icon_rock_large_18x19px)
  347. {
  348. return "rock_large";
  349. }
  350. else if (icon == &I_icon_rock_medium_16x14px)
  351. {
  352. return "rock_medium";
  353. }
  354. else if (icon == &I_icon_rock_small_10x8px)
  355. {
  356. return "rock_small";
  357. }
  358. // If no match is found
  359. FURI_LOG_E("Game", "Icon ID not found for given icon pointer.");
  360. return NULL;
  361. }