You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.5 KiB

  1. #include "Controller.h"
  2. #include <string>
  3. #include <iostream>
  4. Controller::Controller()
  5. : input_{0}, score_{0}, snack_{Point(0,0,0)}
  6. {
  7. generateSnack(&snack_);
  8. }
  9. uint32_t Controller::getCurrScore() const {
  10. return score_;
  11. }
  12. void Controller::resetScore() {
  13. score_ = 0;
  14. }
  15. void Controller::printScore(uint32_t score) const {
  16. const std::string str = "Score: " + std::to_string(score);
  17. // locate message at (-1,-1) because otherwise it'll be printed inside the game box
  18. Graphics::get().printMsg(-1, -1, str);
  19. }
  20. int Controller::act() {
  21. if (snake_.hasBitSnack(snack_.getY(), snack_.getX())) {
  22. score_ += 10;
  23. snake_.incSize();
  24. generateSnack(&snack_);
  25. Graphics::get().advanceDifficulty();
  26. printScore(score_);
  27. }
  28. switch (input_) {
  29. case UP:
  30. snake_.moveUp();
  31. Graphics::get().setVertical(true);
  32. break;
  33. case DOWN:
  34. snake_.moveDown();
  35. Graphics::get().setVertical(true);
  36. break;
  37. case LEFT:
  38. snake_.moveLeft();
  39. Graphics::get().setVertical(false);
  40. break;
  41. case RIGHT:
  42. snake_.moveRight();
  43. Graphics::get().setVertical(false);
  44. break;
  45. default:
  46. snake_.move();
  47. }
  48. Graphics::get().refreshScreen();
  49. if (snake_.isBitten() || snake_.hasCrashedWall()) {
  50. return DEFEAT;
  51. }
  52. return 0;
  53. }
  54. int Controller::readInput() {
  55. input_ = Graphics::get().readInpt();
  56. return input_;
  57. }
  58. bool Controller::wantsToQuit() const {
  59. return input_ == EXIT_GAME;
  60. }