icon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 *icon_ctx = (IconContext *)context;
  7. if (!icon_ctx)
  8. {
  9. FURI_LOG_E("Game", "Icon context is NULL");
  10. return;
  11. }
  12. if (entity_description_get(other) == &player_desc)
  13. {
  14. PlayerContext *player = (PlayerContext *)entity_context_get(other);
  15. if (player)
  16. {
  17. Vector pos = entity_pos_get(other);
  18. // Bounce back by 2
  19. pos.x -= player->dx * 2;
  20. pos.y -= player->dy * 2;
  21. entity_pos_set(other, pos);
  22. // Reset movement to prevent re-collision
  23. player->dx = 0;
  24. player->dy = 0;
  25. }
  26. }
  27. }
  28. static void icon_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  29. {
  30. UNUSED(manager);
  31. IconContext *icon_ctx = (IconContext *)context;
  32. if (!icon_ctx)
  33. {
  34. FURI_LOG_E("Game", "Icon context is NULL");
  35. return;
  36. }
  37. Vector pos = entity_pos_get(self);
  38. // Draw the icon, centered
  39. canvas_draw_icon(
  40. canvas,
  41. pos.x - camera_x - icon_ctx->width / 2,
  42. pos.y - camera_y - icon_ctx->height / 2,
  43. icon_ctx->icon);
  44. }
  45. static void icon_start(Entity *self, GameManager *manager, void *context)
  46. {
  47. UNUSED(manager);
  48. IconContext *icon_ctx_self = (IconContext *)context;
  49. if (!icon_ctx_self)
  50. {
  51. FURI_LOG_E("Game", "Icon context self is NULL");
  52. return;
  53. }
  54. IconContext *icon_ctx = entity_context_get(self);
  55. if (!icon_ctx)
  56. {
  57. FURI_LOG_E("Game", "Icon context is NULL");
  58. return;
  59. }
  60. IconContext *loaded_data = get_icon_context(g_temp_spawn_name);
  61. if (!loaded_data)
  62. {
  63. FURI_LOG_E("Game", "Failed to find icon data for %s", g_temp_spawn_name);
  64. return;
  65. }
  66. icon_ctx_self->icon = loaded_data->icon;
  67. icon_ctx_self->width = loaded_data->width;
  68. icon_ctx_self->height = loaded_data->height;
  69. icon_ctx->icon = loaded_data->icon;
  70. icon_ctx->width = loaded_data->width;
  71. icon_ctx->height = loaded_data->height;
  72. Vector pos = entity_pos_get(self);
  73. pos.x += icon_ctx_self->width / 2;
  74. pos.y += icon_ctx_self->height / 2;
  75. entity_pos_set(self, pos);
  76. entity_collider_add_circle(
  77. self,
  78. (icon_ctx_self->width + icon_ctx_self->height) / 4);
  79. free(loaded_data);
  80. }
  81. // -------------- Stop callback --------------
  82. static void icon_free(Entity *self, GameManager *manager, void *context)
  83. {
  84. UNUSED(self);
  85. UNUSED(manager);
  86. UNUSED(context);
  87. if (context)
  88. {
  89. free(context);
  90. }
  91. }
  92. // -------------- Entity description --------------
  93. const EntityDescription icon_desc = {
  94. .start = icon_start,
  95. .stop = icon_free,
  96. .update = NULL,
  97. .render = icon_render,
  98. .collision = icon_collision,
  99. .event = NULL,
  100. .context_size = sizeof(IconContext),
  101. };
  102. static IconContext *icon_generic_alloc(const char *id, const Icon *icon, uint8_t width, uint8_t height)
  103. {
  104. IconContext *ctx = malloc(sizeof(IconContext));
  105. if (!ctx)
  106. {
  107. FURI_LOG_E("Game", "Failed to allocate IconContext");
  108. return NULL;
  109. }
  110. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  111. ctx->icon = icon;
  112. ctx->width = width;
  113. ctx->height = height;
  114. return ctx;
  115. }
  116. IconContext *get_icon_context(const char *name)
  117. {
  118. if (strcmp(name, "earth") == 0)
  119. {
  120. return icon_generic_alloc("earth", &I_icon_earth_15x16, 15, 16);
  121. }
  122. else if (strcmp(name, "home") == 0)
  123. {
  124. return icon_generic_alloc("home", &I_icon_home_15x16, 15, 16);
  125. }
  126. else if (strcmp(name, "house") == 0)
  127. {
  128. return icon_generic_alloc("house", &I_icon_house_48x32px, 48, 32);
  129. }
  130. else if (strcmp(name, "house_3d") == 0)
  131. {
  132. return icon_generic_alloc("house_3d", &I_icon_house_3d_34x45px, 34, 45);
  133. }
  134. else if (strcmp(name, "info") == 0)
  135. {
  136. return icon_generic_alloc("info", &I_icon_info_15x16, 15, 16);
  137. }
  138. else if (strcmp(name, "man") == 0)
  139. {
  140. return icon_generic_alloc("man", &I_icon_man_7x16, 7, 16);
  141. }
  142. else if (strcmp(name, "plant") == 0)
  143. {
  144. return icon_generic_alloc("plant", &I_icon_plant_16x16, 16, 16);
  145. }
  146. else if (strcmp(name, "plant_fern") == 0)
  147. {
  148. return icon_generic_alloc("plant_fern", &I_icon_plant_fern_18x16px, 18, 16);
  149. }
  150. else if (strcmp(name, "plant_pointy") == 0)
  151. {
  152. return icon_generic_alloc("plant_pointy", &I_icon_plant_pointy_13x16px, 13, 16);
  153. }
  154. else if (strcmp(name, "tree") == 0)
  155. {
  156. return icon_generic_alloc("tree", &I_icon_tree_16x16, 16, 16);
  157. }
  158. else if (strcmp(name, "tree_29x30") == 0)
  159. {
  160. return icon_generic_alloc("tree_29x30", &I_icon_tree_29x30px, 29, 30);
  161. }
  162. else if (strcmp(name, "tree_48x48") == 0)
  163. {
  164. return icon_generic_alloc("tree_48x48", &I_icon_tree_48x48px, 48, 48);
  165. }
  166. else if (strcmp(name, "woman") == 0)
  167. {
  168. return icon_generic_alloc("woman", &I_icon_woman_9x16, 9, 16);
  169. }
  170. else if (strcmp(name, "chest_closed") == 0)
  171. {
  172. return icon_generic_alloc("chest_closed", &I_icon_chest_closed_16x13px, 16, 13);
  173. }
  174. else if (strcmp(name, "chest_open") == 0)
  175. {
  176. return icon_generic_alloc("chest_open", &I_icon_chest_open_16x16px, 16, 16);
  177. }
  178. else if (strcmp(name, "fence") == 0)
  179. {
  180. return icon_generic_alloc("fence", &I_icon_fence_16x8px, 16, 8);
  181. }
  182. else if (strcmp(name, "fence_end") == 0)
  183. {
  184. return icon_generic_alloc("fence_end", &I_icon_fence_end_16x8px, 16, 8);
  185. }
  186. else if (strcmp(name, "fence_vertical_end") == 0)
  187. {
  188. return icon_generic_alloc("fence_vertical_end", &I_icon_fence_vertical_end_6x8px, 6, 8);
  189. }
  190. else if (strcmp(name, "fence_vertical_start") == 0)
  191. {
  192. return icon_generic_alloc("fence_vertical_start", &I_icon_fence_vertical_start_6x15px, 6, 15);
  193. }
  194. else if (strcmp(name, "flower") == 0)
  195. {
  196. return icon_generic_alloc("flower", &I_icon_flower_16x16, 16, 16);
  197. }
  198. else if (strcmp(name, "lake_bottom") == 0)
  199. {
  200. return icon_generic_alloc("lake_bottom", &I_icon_lake_bottom_31x12px, 31, 12);
  201. }
  202. else if (strcmp(name, "lake_bottom_left") == 0)
  203. {
  204. return icon_generic_alloc("lake_bottom_left", &I_icon_lake_bottom_left_24x22px, 24, 22);
  205. }
  206. else if (strcmp(name, "lake_bottom_right") == 0)
  207. {
  208. return icon_generic_alloc("lake_bottom_right", &I_icon_lake_bottom_right_24x22px, 24, 22);
  209. }
  210. else if (strcmp(name, "lake_left") == 0)
  211. {
  212. return icon_generic_alloc("lake_left", &I_icon_lake_left_11x31px, 11, 31);
  213. }
  214. else if (strcmp(name, "lake_right") == 0)
  215. {
  216. // Assuming 11x31
  217. return icon_generic_alloc("lake_right", &I_icon_lake_right_11x31, 11, 31);
  218. }
  219. else if (strcmp(name, "lake_top") == 0)
  220. {
  221. return icon_generic_alloc("lake_top", &I_icon_lake_top_31x12px, 31, 12);
  222. }
  223. else if (strcmp(name, "lake_top_left") == 0)
  224. {
  225. return icon_generic_alloc("lake_top_left", &I_icon_lake_top_left_24x22px, 24, 22);
  226. }
  227. else if (strcmp(name, "lake_top_right") == 0)
  228. {
  229. return icon_generic_alloc("lake_top_right", &I_icon_lake_top_right_24x22px, 24, 22);
  230. }
  231. else if (strcmp(name, "rock_large") == 0)
  232. {
  233. return icon_generic_alloc("rock_large", &I_icon_rock_large_18x19px, 18, 19);
  234. }
  235. else if (strcmp(name, "rock_medium") == 0)
  236. {
  237. return icon_generic_alloc("rock_medium", &I_icon_rock_medium_16x14px, 16, 14);
  238. }
  239. else if (strcmp(name, "rock_small") == 0)
  240. {
  241. return icon_generic_alloc("rock_small", &I_icon_rock_small_10x8px, 10, 8);
  242. }
  243. // If no match is found
  244. FURI_LOG_E("Game", "Icon not found: %s", name);
  245. return NULL;
  246. }
  247. IconContext *get_icon_context_furi(const FuriString *name)
  248. {
  249. if (furi_string_cmp(name, "earth") == 0)
  250. {
  251. return icon_generic_alloc("earth", &I_icon_earth_15x16, 15, 16);
  252. }
  253. else if (furi_string_cmp(name, "home") == 0)
  254. {
  255. return icon_generic_alloc("home", &I_icon_home_15x16, 15, 16);
  256. }
  257. else if (furi_string_cmp(name, "house") == 0)
  258. {
  259. return icon_generic_alloc("house", &I_icon_house_48x32px, 48, 32);
  260. }
  261. else if (furi_string_cmp(name, "house_3d") == 0)
  262. {
  263. return icon_generic_alloc("house_3d", &I_icon_house_3d_34x45px, 34, 45);
  264. }
  265. else if (furi_string_cmp(name, "info") == 0)
  266. {
  267. return icon_generic_alloc("info", &I_icon_info_15x16, 15, 16);
  268. }
  269. else if (furi_string_cmp(name, "man") == 0)
  270. {
  271. return icon_generic_alloc("man", &I_icon_man_7x16, 7, 16);
  272. }
  273. else if (furi_string_cmp(name, "plant") == 0)
  274. {
  275. return icon_generic_alloc("plant", &I_icon_plant_16x16, 16, 16);
  276. }
  277. else if (furi_string_cmp(name, "plant_fern") == 0)
  278. {
  279. return icon_generic_alloc("plant_fern", &I_icon_plant_fern_18x16px, 18, 16);
  280. }
  281. else if (furi_string_cmp(name, "plant_pointy") == 0)
  282. {
  283. return icon_generic_alloc("plant_pointy", &I_icon_plant_pointy_13x16px, 13, 16);
  284. }
  285. else if (furi_string_cmp(name, "tree") == 0)
  286. {
  287. return icon_generic_alloc("tree", &I_icon_tree_16x16, 16, 16);
  288. }
  289. else if (furi_string_cmp(name, "tree_29x30") == 0)
  290. {
  291. return icon_generic_alloc("tree_29x30", &I_icon_tree_29x30px, 29, 30);
  292. }
  293. else if (furi_string_cmp(name, "tree_48x48") == 0)
  294. {
  295. return icon_generic_alloc("tree_48x48", &I_icon_tree_48x48px, 48, 48);
  296. }
  297. else if (furi_string_cmp(name, "woman") == 0)
  298. {
  299. return icon_generic_alloc("woman", &I_icon_woman_9x16, 9, 16);
  300. }
  301. else if (furi_string_cmp(name, "chest_closed") == 0)
  302. {
  303. return icon_generic_alloc("chest_closed", &I_icon_chest_closed_16x13px, 16, 13);
  304. }
  305. else if (furi_string_cmp(name, "chest_open") == 0)
  306. {
  307. return icon_generic_alloc("chest_open", &I_icon_chest_open_16x16px, 16, 16);
  308. }
  309. else if (furi_string_cmp(name, "fence") == 0)
  310. {
  311. return icon_generic_alloc("fence", &I_icon_fence_16x8px, 16, 8);
  312. }
  313. else if (furi_string_cmp(name, "fence_end") == 0)
  314. {
  315. return icon_generic_alloc("fence_end", &I_icon_fence_end_16x8px, 16, 8);
  316. }
  317. else if (furi_string_cmp(name, "fence_vertical_end") == 0)
  318. {
  319. return icon_generic_alloc("fence_vertical_end", &I_icon_fence_vertical_end_6x8px, 6, 8);
  320. }
  321. else if (furi_string_cmp(name, "fence_vertical_start") == 0)
  322. {
  323. return icon_generic_alloc("fence_vertical_start", &I_icon_fence_vertical_start_6x15px, 6, 15);
  324. }
  325. else if (furi_string_cmp(name, "flower") == 0)
  326. {
  327. return icon_generic_alloc("flower", &I_icon_flower_16x16, 16, 16);
  328. }
  329. else if (furi_string_cmp(name, "lake_bottom") == 0)
  330. {
  331. return icon_generic_alloc("lake_bottom", &I_icon_lake_bottom_31x12px, 31, 12);
  332. }
  333. else if (furi_string_cmp(name, "lake_bottom_left") == 0)
  334. {
  335. return icon_generic_alloc("lake_bottom_left", &I_icon_lake_bottom_left_24x22px, 24, 22);
  336. }
  337. else if (furi_string_cmp(name, "lake_bottom_right") == 0)
  338. {
  339. return icon_generic_alloc("lake_bottom_right", &I_icon_lake_bottom_right_24x22px, 24, 22);
  340. }
  341. else if (furi_string_cmp(name, "lake_left") == 0)
  342. {
  343. return icon_generic_alloc("lake_left", &I_icon_lake_left_11x31px, 11, 31);
  344. }
  345. else if (furi_string_cmp(name, "lake_right") == 0)
  346. {
  347. // Assuming dimensions 11x31
  348. return icon_generic_alloc("lake_right", &I_icon_lake_right_11x31, 11, 31);
  349. }
  350. else if (furi_string_cmp(name, "lake_top") == 0)
  351. {
  352. return icon_generic_alloc("lake_top", &I_icon_lake_top_31x12px, 31, 12);
  353. }
  354. else if (furi_string_cmp(name, "lake_top_left") == 0)
  355. {
  356. return icon_generic_alloc("lake_top_left", &I_icon_lake_top_left_24x22px, 24, 22);
  357. }
  358. else if (furi_string_cmp(name, "lake_top_right") == 0)
  359. {
  360. return icon_generic_alloc("lake_top_right", &I_icon_lake_top_right_24x22px, 24, 22);
  361. }
  362. else if (furi_string_cmp(name, "rock_large") == 0)
  363. {
  364. return icon_generic_alloc("rock_large", &I_icon_rock_large_18x19px, 18, 19);
  365. }
  366. else if (furi_string_cmp(name, "rock_medium") == 0)
  367. {
  368. return icon_generic_alloc("rock_medium", &I_icon_rock_medium_16x14px, 16, 14);
  369. }
  370. else if (furi_string_cmp(name, "rock_small") == 0)
  371. {
  372. return icon_generic_alloc("rock_small", &I_icon_rock_small_10x8px, 10, 8);
  373. }
  374. // If no match is found
  375. FURI_LOG_E("Game", "Icon not found: %s", furi_string_get_cstr(name));
  376. return NULL;
  377. }
  378. const char *icon_get_id(const Icon *icon)
  379. {
  380. if (icon == &I_icon_earth_15x16)
  381. {
  382. return "earth";
  383. }
  384. else if (icon == &I_icon_home_15x16)
  385. {
  386. return "home";
  387. }
  388. else if (icon == &I_icon_house_48x32px)
  389. {
  390. return "house";
  391. }
  392. else if (icon == &I_icon_house_3d_34x45px)
  393. {
  394. return "house_3d";
  395. }
  396. else if (icon == &I_icon_info_15x16)
  397. {
  398. return "info";
  399. }
  400. else if (icon == &I_icon_man_7x16)
  401. {
  402. return "man";
  403. }
  404. else if (icon == &I_icon_plant_16x16)
  405. {
  406. return "plant";
  407. }
  408. else if (icon == &I_icon_plant_fern_18x16px)
  409. {
  410. return "plant_fern";
  411. }
  412. else if (icon == &I_icon_plant_pointy_13x16px)
  413. {
  414. return "plant_pointy";
  415. }
  416. else if (icon == &I_icon_tree_16x16)
  417. {
  418. return "tree";
  419. }
  420. else if (icon == &I_icon_tree_29x30px)
  421. {
  422. return "tree_29x30";
  423. }
  424. else if (icon == &I_icon_tree_48x48px)
  425. {
  426. return "tree_48x48";
  427. }
  428. else if (icon == &I_icon_woman_9x16)
  429. {
  430. return "woman";
  431. }
  432. else if (icon == &I_icon_chest_closed_16x13px)
  433. {
  434. return "chest_closed";
  435. }
  436. else if (icon == &I_icon_chest_open_16x16px)
  437. {
  438. return "chest_open";
  439. }
  440. else if (icon == &I_icon_fence_16x8px)
  441. {
  442. return "fence";
  443. }
  444. else if (icon == &I_icon_fence_end_16x8px)
  445. {
  446. return "fence_end";
  447. }
  448. else if (icon == &I_icon_fence_vertical_end_6x8px)
  449. {
  450. return "fence_vertical_end";
  451. }
  452. else if (icon == &I_icon_fence_vertical_start_6x15px)
  453. {
  454. return "fence_vertical_start";
  455. }
  456. else if (icon == &I_icon_flower_16x16)
  457. {
  458. return "flower";
  459. }
  460. else if (icon == &I_icon_lake_bottom_31x12px)
  461. {
  462. return "lake_bottom";
  463. }
  464. else if (icon == &I_icon_lake_bottom_left_24x22px)
  465. {
  466. return "lake_bottom_left";
  467. }
  468. else if (icon == &I_icon_lake_bottom_right_24x22px)
  469. {
  470. return "lake_bottom_right";
  471. }
  472. else if (icon == &I_icon_lake_left_11x31px)
  473. {
  474. return "lake_left";
  475. }
  476. else if (icon == &I_icon_lake_right_11x31)
  477. {
  478. return "lake_right";
  479. }
  480. else if (icon == &I_icon_lake_top_31x12px)
  481. {
  482. return "lake_top";
  483. }
  484. else if (icon == &I_icon_lake_top_left_24x22px)
  485. {
  486. return "lake_top_left";
  487. }
  488. else if (icon == &I_icon_lake_top_right_24x22px)
  489. {
  490. return "lake_top_right";
  491. }
  492. else if (icon == &I_icon_rock_large_18x19px)
  493. {
  494. return "rock_large";
  495. }
  496. else if (icon == &I_icon_rock_medium_16x14px)
  497. {
  498. return "rock_medium";
  499. }
  500. else if (icon == &I_icon_rock_small_10x8px)
  501. {
  502. return "rock_small";
  503. }
  504. // If no match is found
  505. FURI_LOG_E("Game", "Icon ID not found for given icon pointer.");
  506. return NULL;
  507. }