app_gameplay.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "app_gameplay.h"
  2. #include "game.h"
  3. #include "wave/data_structures/list.h"
  4. #include "wave/calc.h"
  5. #include <furi.h>
  6. #include <math.h>
  7. struct AppGameplayState {
  8. GameState* game;
  9. int lastActionAt;
  10. // Controls
  11. int selectedHandIndex;
  12. int selectedSuitIndex;
  13. };
  14. GameState* gameplay_get_game(AppGameplayState* gameplay) {
  15. return gameplay->game;
  16. }
  17. void gameplay_reset(AppGameplayState* gameplay) {
  18. game_reset(gameplay->game);
  19. gameplay->lastActionAt = furi_get_tick();
  20. gameplay->selectedHandIndex = 0;
  21. }
  22. AppGameplayState* gameplay_alloc() {
  23. AppGameplayState* gameplay = malloc(sizeof(AppGameplayState));
  24. gameplay->game = game_alloc();
  25. gameplay_reset(gameplay);
  26. return gameplay;
  27. }
  28. void gameplay_free(AppGameplayState* gameplay) {
  29. game_free(gameplay->game);
  30. free(gameplay);
  31. }
  32. void gameplay_selection_delta(AppGameplayState* gameplay, int delta) {
  33. int selection = gameplay->selectedHandIndex + delta;
  34. selection =
  35. wrap_single(selection, 0, game_get_player_hand_count(gameplay->game, PLAYER_NUMBER) - 1);
  36. gameplay->selectedHandIndex = selection;
  37. }
  38. int gameplay_selection_get_hand_index(AppGameplayState* gameplay) {
  39. gameplay_selection_delta(gameplay, 0); // Avoid out of bounds if the hand count changed
  40. return gameplay->selectedHandIndex;
  41. }
  42. int gameplay_get_selected_card(AppGameplayState* gameplay) {
  43. GameState* game = gameplay->game;
  44. int selectedHandIndex = gameplay_selection_get_hand_index(gameplay);
  45. List* cardsInHand = list_alloc(MAX_HAND_SIZE, sizeof(int));
  46. game_get_player_hand(game, PLAYER_NUMBER, cardsInHand);
  47. int cardIndex;
  48. list_get_at(cardsInHand, selectedHandIndex, &cardIndex);
  49. list_free(cardsInHand);
  50. return cardIndex;
  51. }