PlayingState.hxx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #ifndef SNAKE_PLAYINGSTATE_HXX
  3. #define SNAKE_PLAYINGSTATE_HXX
  4. #include "GameState.hxx"
  5. #include "AssetManager.hxx"
  6. #include <deque>
  7. #include <random>
  8. enum class Direction {
  9. Up,
  10. Right,
  11. Down,
  12. Left,
  13. };
  14. class PlayingState final : public GameState {
  15. public:
  16. static int constexpr CELLS_X = 64;
  17. static int constexpr CELLS_Y = 48;
  18. static float constexpr ACCELERATION = 1.05f;
  19. static float constexpr MAX_DISTANCE = 1.0f; // one cell per cycle
  20. static float constexpr START_SPEED = 0.005f; // 5 cell per second
  21. static float constexpr MAX_SPEED = 0.01f; // 20 cells per second
  22. PlayingState();
  23. void on_enter(GameStateManager& gsm) override;
  24. void on_event(GameStateManager& gsm, SDL_Event const& evt) override;
  25. void update(GameStateManager& gsm, std::chrono::milliseconds delta_time) override;
  26. void render(SDLRenderer& renderer) override;
  27. static unsigned last_high_score();
  28. private:
  29. void place_head();
  30. void place_target();
  31. void render_ui(SDLRenderer& renderer, SDL_Rect const& playing_field);
  32. void render_target(SDLRenderer& renderer, SDL_Rect const& playing_field);
  33. void render_snake(SDLRenderer& renderer, SDL_Rect const& playing_field);
  34. bool detect_death(SDL_Point const& position);
  35. static unsigned last_high_score_;
  36. std::default_random_engine generator_;
  37. std::uniform_int_distribution<int> distribution_position_x_{0, CELLS_X-1};
  38. std::uniform_int_distribution<int> distribution_position_y_{0, CELLS_Y-1};
  39. SDL_Point target_;
  40. unsigned length_{0u};
  41. Direction direction_{Direction::Left};
  42. SDL_FPoint head_;
  43. std::deque<SDL_Point> tail_;
  44. float speed_{0.001f};
  45. Asset<TTF_Font*> font_;
  46. };
  47. #endif // SNAKE_PLAYINGSTATE_HXX