items.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <gui/elements.h>
  2. #include "applications.h"
  3. #include "items_i.h"
  4. #include "emotes.h"
  5. #include <gui/icon_i.h>
  6. const Item Food = {
  7. .layer = 4,
  8. .timeout = 100,
  9. .pos =
  10. {
  11. .x = 0,
  12. .y = 90,
  13. },
  14. .width = 60,
  15. .height = 50,
  16. .draw = food_redraw,
  17. .callback = food_callback};
  18. const Item Console = {
  19. .layer = 4,
  20. .timeout = 100,
  21. .pos =
  22. {
  23. .x = 357,
  24. .y = 190,
  25. },
  26. .width = 40,
  27. .height = 20,
  28. .draw = console_redraw,
  29. .callback = console_callback};
  30. const Item* Home[] = {&Food, &Console};
  31. const Item** Scenes[] = {Home};
  32. const Item** get_scene(SceneState* state) {
  33. return Scenes[state->scene_id];
  34. }
  35. static void dolphin_scene_start_app(SceneState* state, const FlipperApplication* flipper_app) {
  36. furi_assert(state);
  37. furi_assert(flipper_app);
  38. state->scene_app_thread = furi_thread_alloc();
  39. furi_assert(flipper_app->app);
  40. furi_assert(flipper_app->name);
  41. furi_thread_set_name(state->scene_app_thread, flipper_app->name);
  42. furi_thread_set_stack_size(state->scene_app_thread, flipper_app->stack_size);
  43. furi_thread_set_callback(state->scene_app_thread, flipper_app->app);
  44. furi_thread_start(state->scene_app_thread);
  45. }
  46. uint16_t roll_new(uint16_t prev, uint16_t max) {
  47. uint16_t val = 999;
  48. while(val != prev) {
  49. val = random() % max;
  50. break;
  51. }
  52. return val;
  53. }
  54. static void dolphin_scene_type_text(
  55. Canvas* canvas,
  56. SceneState* state,
  57. uint8_t x,
  58. uint8_t y,
  59. const char* text) {
  60. char dialog_str[64];
  61. char buf[64];
  62. strcpy(dialog_str, (char*)text);
  63. if(state->dialog_progress <= strlen(dialog_str)) {
  64. if(HAL_GetTick() / 10 % 2 == 0) state->dialog_progress++;
  65. dialog_str[state->dialog_progress] = '\0';
  66. snprintf(buf, state->dialog_progress, dialog_str);
  67. } else {
  68. snprintf(buf, 64, dialog_str);
  69. }
  70. canvas_draw_str_aligned(canvas, x, y, AlignCenter, AlignCenter, buf);
  71. }
  72. const void scene_activate_item_callback(SceneState* state, Canvas* canvas) {
  73. furi_assert(state);
  74. furi_assert(canvas);
  75. const Item* near = is_nearby(state);
  76. if(near && state->use_pending == true) {
  77. state->action_timeout = near->timeout;
  78. near->callback(canvas, state);
  79. state->use_pending = false;
  80. } else if(near) {
  81. near->callback(canvas, state);
  82. }
  83. }
  84. const Vec2 item_get_pos(SceneState* state, ItemsEnum item) {
  85. const Item** current = get_scene(state);
  86. Vec2 rel_pos = {0, 0};
  87. rel_pos.x = DOLPHIN_WIDTH / 2 + (current[item]->pos.x * PARALLAX(current[item]->layer));
  88. rel_pos.y = DOLPHIN_WIDTH / 4 + (current[item]->pos.y * PARALLAX(current[item]->layer));
  89. return rel_pos;
  90. }
  91. const Item* is_nearby(SceneState* state) {
  92. furi_assert(state);
  93. uint8_t item = 0;
  94. bool found = false;
  95. const Item** current = get_scene(state);
  96. while(item < ItemsEnumTotal) {
  97. int32_t rel_x =
  98. (DOLPHIN_CENTER + DOLPHIN_WIDTH / 2 -
  99. (current[item]->pos.x - state->player_global.x) * PARALLAX(current[item]->layer));
  100. uint8_t item_height = current[item]->height;
  101. uint8_t item_width = current[item]->width;
  102. int32_t rel_y = current[item]->pos.y - state->player_global.y;
  103. if(abs(rel_x) <= item_width && abs(rel_y) <= item_height) {
  104. found = !found;
  105. break;
  106. }
  107. ++item;
  108. }
  109. return found ? current[item] : NULL;
  110. }
  111. void food_redraw(Canvas* canvas, void* s) {
  112. furi_assert(s);
  113. SceneState* state = s;
  114. const Icon* food_frames[] = {
  115. &I_food1_61x98,
  116. &I_food2_61x98,
  117. &I_food3_61x98,
  118. &I_food4_61x98,
  119. &I_food5_61x98,
  120. &I_food6_61x98,
  121. &I_food7_61x98,
  122. &I_food8_61x98,
  123. &I_food9_61x98,
  124. &I_food10_61x98,
  125. &I_food11_61x98,
  126. &I_food12_61x98,
  127. };
  128. uint8_t frame = ((HAL_GetTick() / 200) % SIZEOF_ARRAY(food_frames));
  129. if(is_nearby(state) && (state->player_global.y > Food.pos.y)) {
  130. dolphin_scene_type_text(
  131. canvas,
  132. state,
  133. (Food.pos.x - state->player_global.x) * PARALLAX(Food.layer) + 90,
  134. state->screen.y + 8,
  135. console_emotes[state->emote_id]);
  136. } else {
  137. state->dialog_progress = 0;
  138. state->emote_id = roll_new(state->previous_emote, SIZEOF_ARRAY(console_emotes));
  139. }
  140. canvas_draw_icon(
  141. canvas,
  142. (Food.pos.x - state->player_global.x) * PARALLAX(Food.layer),
  143. Food.pos.y - state->player_global.y,
  144. food_frames[frame]);
  145. canvas_set_bitmap_mode(canvas, true);
  146. }
  147. void food_callback(Canvas* canvas, void* s) {
  148. furi_assert(s);
  149. SceneState* state = s;
  150. if(state->use_pending) {
  151. dolphin_scene_start_app(state, &FLIPPER_SCENE_APPS[1]);
  152. }
  153. }
  154. void console_redraw(Canvas* canvas, void* s) {
  155. furi_assert(s);
  156. SceneState* state = s;
  157. const Icon* console[] = {
  158. &I_Console_74x67_0,
  159. &I_Console_74x67_1,
  160. &I_Console_74x67_2,
  161. &I_Console_74x67_3,
  162. &I_Console_74x67_4,
  163. &I_Console_74x67_5,
  164. &I_Console_74x67_6,
  165. &I_Console_74x67_7,
  166. &I_Console_74x67_8,
  167. };
  168. uint8_t frame = ((HAL_GetTick() / 100) % SIZEOF_ARRAY(console));
  169. canvas_draw_icon(
  170. canvas,
  171. (Console.pos.x - state->player_global.x) * PARALLAX(Console.layer),
  172. Console.pos.y - state->player_global.y,
  173. console[frame]);
  174. canvas_set_bitmap_mode(canvas, true);
  175. if(is_nearby(state)) {
  176. dolphin_scene_type_text(
  177. canvas,
  178. state,
  179. (Console.pos.x - state->player_global.x) * PARALLAX(Console.layer) - 25,
  180. Console.pos.y - state->player_global.y + 14,
  181. console_emotes[state->emote_id]);
  182. } else {
  183. state->dialog_progress = 0;
  184. state->emote_id = roll_new(state->previous_emote, SIZEOF_ARRAY(console_emotes));
  185. }
  186. }
  187. void console_callback(Canvas* canvas, void* s) {
  188. furi_assert(s);
  189. SceneState* state = s;
  190. if(state->use_pending) {
  191. dolphin_scene_start_app(state, &FLIPPER_SCENE_APPS[1]);
  192. }
  193. }