icon.c 11 KB

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