icon.c 15 KB

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