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.

69 lines
1.3 KiB

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