icon.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. furi_check(ictx, "Icon context is NULL");
  25. Vector pos = entity_pos_get(self);
  26. int x_pos = pos.x - camera_x - ictx->size.x / 2;
  27. int y_pos = pos.y - camera_y - ictx->size.y / 2;
  28. // check if position is within the screen
  29. if (x_pos + ictx->size.x < 0 || x_pos > SCREEN_WIDTH || y_pos + ictx->size.y < 0 || y_pos > SCREEN_HEIGHT)
  30. return;
  31. canvas_draw_icon(canvas, x_pos, y_pos, ictx->icon);
  32. }
  33. static void icon_start(Entity *self, GameManager *manager, void *context)
  34. {
  35. UNUSED(manager);
  36. IconContext *ictx_self = (IconContext *)context;
  37. if (!ictx_self)
  38. {
  39. FURI_LOG_E("Game", "Icon context self is NULL");
  40. return;
  41. }
  42. IconContext *ictx = entity_context_get(self);
  43. if (!ictx)
  44. {
  45. FURI_LOG_E("Game", "Icon context is NULL");
  46. return;
  47. }
  48. IconContext *loaded_data = get_icon_context(g_name);
  49. if (!loaded_data)
  50. {
  51. FURI_LOG_E("Game", "Failed to find icon data for %s", g_name);
  52. return;
  53. }
  54. ictx_self->icon = loaded_data->icon;
  55. ictx_self->size = (Vector){loaded_data->size.x, loaded_data->size.y};
  56. ictx->icon = loaded_data->icon;
  57. ictx->size = (Vector){loaded_data->size.x, loaded_data->size.y};
  58. Vector pos = entity_pos_get(self);
  59. pos.x += ictx_self->size.x / 2;
  60. pos.y += ictx_self->size.y / 2;
  61. entity_pos_set(self, pos);
  62. entity_collider_add_circle(
  63. self,
  64. (ictx_self->size.x + ictx_self->size.y) / 4);
  65. free(loaded_data);
  66. }
  67. // -------------- Entity description --------------
  68. const EntityDescription icon_desc = {
  69. .start = icon_start,
  70. .stop = NULL,
  71. .update = NULL,
  72. .render = icon_render,
  73. .collision = icon_collision,
  74. .event = NULL,
  75. .context_size = sizeof(IconContext),
  76. };
  77. static IconContext *icon_generic_alloc(IconID id, const Icon *icon, uint8_t width, uint8_t height)
  78. {
  79. IconContext *ctx = malloc(sizeof(IconContext));
  80. if (!ctx)
  81. {
  82. FURI_LOG_E("Game", "Failed to allocate IconContext");
  83. return NULL;
  84. }
  85. ctx->id = id;
  86. ctx->icon = icon;
  87. ctx->size = (Vector){width, height};
  88. return ctx;
  89. }
  90. IconContext *get_icon_context(const char *name)
  91. {
  92. if (is_str(name, "house"))
  93. return icon_generic_alloc(ICON_ID_HOUSE, &I_icon_house_48x32px, 48, 32);
  94. else if (is_str(name, "man"))
  95. return icon_generic_alloc(ICON_ID_MAN, &I_icon_man_7x16, 7, 16);
  96. else if (is_str(name, "plant"))
  97. return icon_generic_alloc(ICON_ID_PLANT, &I_icon_plant_16x16, 16, 16);
  98. else if (is_str(name, "tree"))
  99. return icon_generic_alloc(ICON_ID_TREE, &I_icon_tree_16x16, 16, 16);
  100. else if (is_str(name, "woman"))
  101. return icon_generic_alloc(ICON_ID_WOMAN, &I_icon_woman_9x16, 9, 16);
  102. else if (is_str(name, "fence"))
  103. return icon_generic_alloc(ICON_ID_FENCE, &I_icon_fence_16x8px, 16, 8);
  104. else if (is_str(name, "fence_end"))
  105. return icon_generic_alloc(ICON_ID_FENCE_END, &I_icon_fence_end_16x8px, 16, 8);
  106. else if (is_str(name, "fence_vertical_end"))
  107. return icon_generic_alloc(ICON_ID_FENCE_VERTICAL_END, &I_icon_fence_vertical_end_6x8px, 6, 8);
  108. else if (is_str(name, "fence_vertical_start"))
  109. return icon_generic_alloc(ICON_ID_FENCE_VERTICAL_START, &I_icon_fence_vertical_start_6x15px, 6, 15);
  110. else if (is_str(name, "flower"))
  111. return icon_generic_alloc(ICON_ID_FLOWER, &I_icon_flower_16x16, 16, 16);
  112. else if (is_str(name, "lake_bottom"))
  113. return icon_generic_alloc(ICON_ID_LAKE_BOTTOM, &I_icon_lake_bottom_31x12px, 31, 12);
  114. else if (is_str(name, "lake_bottom_left"))
  115. return icon_generic_alloc(ICON_ID_LAKE_BOTTOM_LEFT, &I_icon_lake_bottom_left_24x22px, 24, 22);
  116. else if (is_str(name, "lake_bottom_right"))
  117. return icon_generic_alloc(ICON_ID_LAKE_BOTTOM_RIGHT, &I_icon_lake_bottom_right_24x22px, 24, 22);
  118. else if (is_str(name, "lake_left"))
  119. return icon_generic_alloc(ICON_ID_LAKE_LEFT, &I_icon_lake_left_11x31px, 11, 31);
  120. else if (is_str(name, "lake_right"))
  121. return icon_generic_alloc(ICON_ID_LAKE_RIGHT, &I_icon_lake_right_11x31, 11, 31);
  122. else if (is_str(name, "lake_top"))
  123. return icon_generic_alloc(ICON_ID_LAKE_TOP, &I_icon_lake_top_31x12px, 31, 12);
  124. else if (is_str(name, "lake_top_left"))
  125. return icon_generic_alloc(ICON_ID_LAKE_TOP_LEFT, &I_icon_lake_top_left_24x22px, 24, 22);
  126. else if (is_str(name, "lake_top_right"))
  127. return icon_generic_alloc(ICON_ID_LAKE_TOP_RIGHT, &I_icon_lake_top_right_24x22px, 24, 22);
  128. else if (is_str(name, "rock_large"))
  129. return icon_generic_alloc(ICON_ID_ROCK_LARGE, &I_icon_rock_large_18x19px, 18, 19);
  130. else if (is_str(name, "rock_medium"))
  131. return icon_generic_alloc(ICON_ID_ROCK_MEDIUM, &I_icon_rock_medium_16x14px, 16, 14);
  132. else if (is_str(name, "rock_small"))
  133. return icon_generic_alloc(ICON_ID_ROCK_SMALL, &I_icon_rock_small_10x8px, 10, 8);
  134. // If no match is found
  135. FURI_LOG_E("Game", "Icon not found: %s", name);
  136. return NULL;
  137. }