icon.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const Icon *get_icon(char *name)
  49. {
  50. if (strcmp(name, "earth") == 0)
  51. {
  52. return &I_icon_earth_15x16;
  53. }
  54. if (strcmp(name, "home") == 0)
  55. {
  56. return &I_icon_home_15x16;
  57. }
  58. if (strcmp(name, "house") == 0)
  59. {
  60. return &I_icon_house_48x48px;
  61. }
  62. if (strcmp(name, "house_3d") == 0)
  63. {
  64. return &I_icon_house_3d_34x45px;
  65. }
  66. if (strcmp(name, "info") == 0)
  67. {
  68. return &I_icon_info_15x16;
  69. }
  70. if (strcmp(name, "man") == 0)
  71. {
  72. return &I_icon_man_7x16;
  73. }
  74. if (strcmp(name, "plant") == 0)
  75. {
  76. return &I_icon_plant_16x16;
  77. }
  78. if (strcmp(name, "plant_fern") == 0)
  79. {
  80. return &I_icon_plant_fern_18x16px;
  81. }
  82. if (strcmp(name, "plant_pointy") == 0)
  83. {
  84. return &I_icon_plant_pointy_13x16px;
  85. }
  86. if (strcmp(name, "tree") == 0)
  87. {
  88. return &I_icon_tree_16x16;
  89. }
  90. if (strcmp(name, "tree_29x30") == 0)
  91. {
  92. return &I_icon_tree_29x30px;
  93. }
  94. if (strcmp(name, "tree_48x48") == 0)
  95. {
  96. return &I_icon_tree_48x48px;
  97. }
  98. if (strcmp(name, "woman") == 0)
  99. {
  100. return &I_icon_woman_9x16;
  101. }
  102. if (strcmp(name, "chest_closed") == 0)
  103. {
  104. return &I_icon_chest_closed_16x13px;
  105. }
  106. if (strcmp(name, "chest_open") == 0)
  107. {
  108. return &I_icon_chest_open_16x16px;
  109. }
  110. if (strcmp(name, "fence") == 0)
  111. {
  112. return &I_icon_fence_16x8px;
  113. }
  114. if (strcmp(name, "fence_end") == 0)
  115. {
  116. return &I_icon_fence_end_16x8px;
  117. }
  118. if (strcmp(name, "flower") == 0)
  119. {
  120. return &I_icon_flower_16x16;
  121. }
  122. if (strcmp(name, "lake_bottom") == 0)
  123. {
  124. return &I_icon_lake_bottom_31x31px;
  125. }
  126. if (strcmp(name, "lake_bottom_left") == 0)
  127. {
  128. return &I_icon_lake_bottom_left_31x31px;
  129. }
  130. if (strcmp(name, "lake_bottom_right") == 0)
  131. {
  132. return &I_icon_lake_bottom_right_31x31px;
  133. }
  134. if (strcmp(name, "lake_left") == 0)
  135. {
  136. return &I_icon_lake_left_31x31;
  137. }
  138. if (strcmp(name, "lake_right") == 0)
  139. {
  140. return &I_icon_lake_right_31x31px;
  141. }
  142. if (strcmp(name, "lake_top") == 0)
  143. {
  144. return &I_icon_lake_top_31x31px;
  145. }
  146. if (strcmp(name, "lake_top_left") == 0)
  147. {
  148. return &I_icon_lake_top_left_31x31px;
  149. }
  150. if (strcmp(name, "lake_top_right") == 0)
  151. {
  152. return &I_icon_lake_top_right_31x31px;
  153. }
  154. if (strcmp(name, "rock_large") == 0)
  155. {
  156. return &I_icon_rock_large_18x19px;
  157. }
  158. if (strcmp(name, "rock_medium") == 0)
  159. {
  160. return &I_icon_rock_medium_16x14px;
  161. }
  162. if (strcmp(name, "rock_small") == 0)
  163. {
  164. return &I_icon_rock_small_10x8px;
  165. }
  166. // If no match is found
  167. return NULL;
  168. }