intro_animation.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "./intro_animation.h"
  2. #include <dolphin/dolphin.h>
  3. #include "../../game_state.h"
  4. #include "../util/helpers.h"
  5. #include "play_screen.h"
  6. static bool animation_running = true;
  7. static int8_t curr_tableau = 0;
  8. static Vector animation_target = VECTOR_ZERO;
  9. static Vector animation_from = VECTOR_ZERO;
  10. static double accumulated_delta = 0;
  11. void start_animation(GameState* state) {
  12. accumulated_delta = 0;
  13. check_pointer(state->deck->tail);
  14. state->animated_card.card = list_peek_back(state->deck);
  15. check_pointer(state->animated_card.card);
  16. animation_from = (Vector){2, 1};
  17. animation_target.x = 2.0f + (float)curr_tableau * 18;
  18. check_pointer(state->tableau[curr_tableau]);
  19. animation_target.y = MIN(25.0f + (float)state->tableau[curr_tableau]->count * 4, 36);
  20. }
  21. void start_intro_screen(void* data) {
  22. curr_tableau = 0;
  23. animation_running = true;
  24. dolphin_deed(DolphinDeedPluginGameStart);
  25. GameState* state = (GameState*)data;
  26. check_pointer(state->deck);
  27. list_free(state->deck);
  28. check_pointer(state->hand);
  29. list_free_data(state->hand);
  30. check_pointer(state->waste);
  31. list_free_data(state->waste);
  32. for(int i = 0; i < 7; i++) {
  33. if(i < 4) {
  34. check_pointer(state->foundation[i]);
  35. list_free_data(state->foundation[i]);
  36. }
  37. check_pointer(state->tableau[i]);
  38. list_free_data(state->tableau[i]);
  39. }
  40. state->deck = deck_generate(1);
  41. check_pointer(state->deck->tail);
  42. start_animation(state);
  43. }
  44. void render_intro_screen(void* data) {
  45. GameState* state = (GameState*)data;
  46. render_play_screen(data);
  47. if(state->animated_card.card) {
  48. card_render_back(
  49. state->animated_card.position.x,
  50. state->animated_card.position.y,
  51. false,
  52. state->buffer,
  53. 22);
  54. }
  55. }
  56. static bool animation_done(GameState* state) {
  57. accumulated_delta += state->delta_time * 4;
  58. vector_lerp(
  59. &(animation_from), &animation_target, accumulated_delta, &(state->animated_card.position));
  60. double dist = vector_distance(&(state->animated_card.position), &animation_target);
  61. return dist < 1;
  62. }
  63. void update_intro_screen(void* data) {
  64. GameState* state = (GameState*)data;
  65. state->isDirty = true;
  66. if(curr_tableau < 7 && animation_running) {
  67. if(animation_done(state)) {
  68. if(curr_tableau < 7) {
  69. check_pointer(state->deck);
  70. check_pointer(state->deck->tail);
  71. check_pointer(state->tableau);
  72. check_pointer(state->tableau[curr_tableau]);
  73. list_push_back(list_pop_back(state->deck), state->tableau[curr_tableau]);
  74. if((curr_tableau + 1) == (uint8_t)state->tableau[curr_tableau]->count) {
  75. Card* c = ((Card*)list_peek_back(state->tableau[curr_tableau]));
  76. check_pointer(c);
  77. c->exposed = true;
  78. curr_tableau++;
  79. }
  80. start_animation(state);
  81. }
  82. }
  83. } else {
  84. state->animated_card.card = NULL;
  85. state->scene_switch = 1;
  86. return;
  87. }
  88. }
  89. static void quick_finish(GameState* state) {
  90. if(state->animated_card.card) {
  91. list_push_back(list_pop_back(state->deck), state->tableau[curr_tableau]);
  92. }
  93. while(curr_tableau < 7) {
  94. while((uint8_t)(state->tableau[curr_tableau]->count) < (curr_tableau + 1)) {
  95. list_push_back(list_pop_back(state->deck), state->tableau[curr_tableau]);
  96. }
  97. ((Card*)list_peek_back(state->tableau[curr_tableau]))->exposed = true;
  98. curr_tableau++;
  99. }
  100. animation_running = false;
  101. }
  102. void input_intro_screen(void* data, InputKey key, InputType type) {
  103. GameState* state = (GameState*)data;
  104. if(key == InputKeyOk && type == InputTypePress) {
  105. quick_finish(state);
  106. animation_running = false;
  107. }
  108. }