simonsays_controller.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include "utilities.h"
  3. #include <stdbool.h>
  4. #include <assert.h>
  5. enum game_state
  6. {
  7. preloading = 0,
  8. mainMenu,
  9. inGame,
  10. gameOver,
  11. gameVictory
  12. };
  13. enum shape_names
  14. {
  15. left = 0,
  16. top,
  17. right,
  18. bottom
  19. };
  20. struct SimonSays
  21. {
  22. enum game_state gameState;
  23. int highScore;
  24. int currentScore;
  25. int numberOfMillisecondsBeforeShapeDisappears;
  26. enum shape_names simonMoves[1000];
  27. int playerMoveIndex;
  28. };
  29. struct SimonSays currentGame;
  30. void resetGame()
  31. {
  32. currentGame = (struct SimonSays){
  33. .gameState = preloading,
  34. .currentScore = 0,
  35. .playerMoveIndex = 0,
  36. .numberOfMillisecondsBeforeShapeDisappears = 500,
  37. .simonMoves[0] = getRandomIntInRange(0, 3)};
  38. }
  39. void preLoadGame()
  40. {
  41. // TODO: load game stuff
  42. currentGame.highScore = 0; // TODO: fetch this from storage
  43. setCurrentGameState(mainMenu);
  44. }
  45. void startGame()
  46. {
  47. assert(currentGame.gameState == mainMenu);
  48. setCurrentGameState(inGame);
  49. }
  50. void setCurrentGameState(enum game_state gameState)
  51. {
  52. currentGame.gameState = gameState;
  53. }
  54. enum game_state getCurrentGameState()
  55. {
  56. return currentGame.gameState;
  57. }
  58. void addNewSimonMove(int addAtIndex)
  59. {
  60. currentGame.simonMoves[addAtIndex] = getRandomIntInRange(0, 3);
  61. }
  62. void startNewRound()
  63. {
  64. addNewSimonMove(currentGame.currentScore);
  65. currentGame.playerMoveIndex = 0;
  66. }
  67. void onPlayerAnsweredCorrect()
  68. {
  69. currentGame.playerMoveIndex++;
  70. }
  71. void onPlayerAnsweredWrong()
  72. {
  73. setCurrentGameState(gameOver);
  74. }
  75. bool isRoundComplete()
  76. {
  77. return currentGame.playerMoveIndex == currentGame.currentScore;
  78. }
  79. enum shape_names getCurrentSimonMove()
  80. {
  81. return currentGame.simonMoves[currentGame.playerMoveIndex];
  82. }
  83. void onPlayerSelectedShapeCallback(enum shape_names shape)
  84. {
  85. if (shape == getCurrentSimonMove())
  86. {
  87. onPlayerAnsweredCorrect();
  88. }
  89. else
  90. {
  91. onPlayerAnsweredWrong();
  92. }
  93. }