falling_card.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <notification/notification.h>
  2. #include <notification/notification_messages.h>
  3. #include "falling_card.h"
  4. #include "../../game_state.h"
  5. #include "play_screen.h"
  6. #include "../util/helpers.h"
  7. static const NotificationSequence sequence_bounce = {
  8. &message_vibro_on,
  9. // &message_note_c4,
  10. &message_delay_10,
  11. &message_vibro_off,
  12. // &message_sound_off,
  13. NULL,
  14. };
  15. static uint8_t start_index = 0;
  16. static size_t tempTime = 0;
  17. void start_falling_screen(void* data) {
  18. //draw the play screen once (without any other thing to have the initial state)
  19. GameState* state = (GameState*)data;
  20. buffer_clear(state->buffer);
  21. render_play_screen(data);
  22. state->clearBuffer = false;
  23. state->isDirty = true;
  24. start_index = 0;
  25. tempTime = 0;
  26. state->game_end = furi_get_tick();
  27. }
  28. void render_falling_screen(void* data) {
  29. GameState* state = (GameState*)data;
  30. if(state->animated_card.card != NULL) {
  31. card_render_front(
  32. state->animated_card.card,
  33. state->animated_card.position.x,
  34. state->animated_card.position.y,
  35. false,
  36. state->buffer,
  37. 22);
  38. }
  39. }
  40. void update_falling_screen(void* data) {
  41. GameState* state = (GameState*)data;
  42. state->clearBuffer = false;
  43. state->isDirty = true;
  44. if(state->animated_card.card) {
  45. if((curr_time() - tempTime) > 12) {
  46. tempTime = curr_time();
  47. state->animated_card.position.x += state->animated_card.velocity.x;
  48. state->animated_card.position.y -= state->animated_card.velocity.y;
  49. //bounce on the bottom
  50. if(state->animated_card.position.y > 41) {
  51. state->animated_card.velocity.y *= -0.8f;
  52. state->animated_card.position.y = 41;
  53. notification_message(
  54. state->notification_app, (const NotificationSequence*)&sequence_bounce);
  55. } else {
  56. state->animated_card.velocity.y--;
  57. if(state->animated_card.velocity.y < -10) state->animated_card.velocity.y = -10;
  58. }
  59. //delete the card if it is outside the screen
  60. if(state->animated_card.position.x < -18 || state->animated_card.position.x > 128) {
  61. free(state->animated_card.card);
  62. state->animated_card.card = NULL;
  63. }
  64. }
  65. } else {
  66. //When we find a foundation without any card means that we finished the animation
  67. if(state->foundation[start_index]->count == 0) {
  68. state->scene_switch = 1;
  69. return;
  70. }
  71. //start with the next card
  72. state->animated_card.card = list_pop_back(state->foundation[start_index]);
  73. state->animated_card.position = (Vector){56 + start_index * 18, 1};
  74. float r1 = 2.0 * (float)(rand() % 2) - 1.0; // random number in range -1 to 1
  75. if(r1 == 0) r1 = 0.1;
  76. float r2 = inverse_tanh(r1);
  77. float vx = (float)(tanh(r2)) * (rand() % 3 + 1);
  78. state->animated_card.velocity.x = vx == 0 ? 1 : vx;
  79. state->animated_card.velocity.y = (rand() % 3 + 1);
  80. start_index = (start_index + 1) % 4;
  81. }
  82. }
  83. void input_falling_screen(void* data, InputKey key, InputType type) {
  84. GameState* state = (GameState*)data;
  85. if(key == InputKeyOk && type == InputTypePress) {
  86. //remove card that is currently animated
  87. if(state->animated_card.card != NULL) {
  88. free(state->animated_card.card);
  89. state->animated_card.card = NULL;
  90. }
  91. state->scene_switch = 1;
  92. }
  93. }