Addsy

play_state.cpp

Apr 13th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include "play_state.hpp"
  2. #include "ai_snake.hpp"
  3. #include "player_snake.hpp"
  4. #include <iostream>
  5.  
  6. const size_t MaxCircles {rand() % ((5 - 1) + 1) + 1};
  7. const size_t MaxSquares {rand() % ((5 - 1) + 1) + 1};
  8. const size_t MaxTriangles {rand() % ((5 - 1) + 1) + 1};
  9. const unsigned int MaxScale {5};
  10. const unsigned int MINIMUM_WIDTH = 148;
  11. const unsigned int MAXIMUM_WIDTH = 876;
  12. const unsigned int MINIMUM_HEIGHT = 20;
  13. const unsigned int MAXIMUM_HEIGHT = 748;
  14.  
  15.  
  16. const int mapwidth = 20;
  17. const int mapheight = 20;
  18.  
  19.  
  20. bool PlayState::onCreate()
  21. {
  22.     snakes_.push_back(new AISnake);
  23.     snakes_.back()->setPosition(Position(40,40));
  24.     snakes_.push_back(new PlayerSnake);
  25.     snakes_.back()->setPosition(Position(20,20));
  26.  
  27.     double x, y;
  28.  
  29.     for(unsigned shape = 0;shape < MaxCircles;shape++)
  30.     {
  31.         x = (double)((rand() % (MAXIMUM_WIDTH - MINIMUM_WIDTH)) + MINIMUM_WIDTH);
  32.         y = (double)((rand() % (MAXIMUM_HEIGHT - MINIMUM_HEIGHT)) + MINIMUM_HEIGHT);
  33.  
  34.         shapes_.push_back(Circle({x, y}));
  35.  
  36.     }
  37.  
  38.  
  39.     for(unsigned shape = 0;shape < MaxSquares;shape++)
  40.     {
  41.         x = (double)((rand() % (MAXIMUM_WIDTH - MINIMUM_WIDTH)) + MINIMUM_WIDTH);
  42.         y = (double)((rand() % (MAXIMUM_HEIGHT - MINIMUM_HEIGHT)) + MINIMUM_HEIGHT);
  43.  
  44.         shapes_.push_back(Square({x, y}));
  45.  
  46.     }
  47.  
  48.     for(unsigned shape = 0;shape < MaxTriangles;shape++)
  49.     {
  50.         x = (double)((rand() % (MAXIMUM_WIDTH - MINIMUM_WIDTH)) + MINIMUM_WIDTH);
  51.         y = (double)((rand() % (MAXIMUM_HEIGHT - MINIMUM_HEIGHT)) + MINIMUM_HEIGHT);
  52.  
  53.         shapes_.push_back(Triangle({x, y}));
  54.  
  55.     }
  56.  
  57.     return true;
  58.  
  59. }
  60.  
  61. bool PlayState::onDestroy()
  62. {
  63.     return true;
  64. }
  65.  
  66. void PlayState::onEntry()
  67. {
  68.     prg::application.addKeyListener(*this);
  69.     game_timer_.start();
  70. }
  71.  
  72. void PlayState::onExit()
  73. {
  74.     prg::application.removeKeyListener(*this);
  75.     game_timer_.stop();
  76. }
  77.  
  78. void PlayState::onUpdate()
  79. {
  80.  
  81. }
  82.  
  83. void PlayState::onRender(prg::Canvas& canvas)
  84. {
  85.     const std::string text = "";
  86.  
  87.     canvas.blitFast(
  88.         background_,
  89.         canvas.getWidth() / 2 - background_.getWidth() / 2,
  90.         canvas.getHeight() / 2 - background_.getHeight() / 2
  91.     );
  92.  
  93.     prg::uint text_dims[2];
  94.     prg::Font::MASSIVE.computePrintDimensions(text_dims, text);
  95.     prg::Font::MASSIVE.print(
  96.       canvas,
  97.       prg::application.getScreenWidth() / 2 - text_dims[0] / 2,
  98.       prg::application.getScreenHeight() / 2 - text_dims[1] / 2,
  99.       prg::Colour::RED,
  100.       text);
  101.  
  102.     for(const auto snake : snakes_) {
  103.     snake->render(canvas);
  104.     }
  105.  
  106.     for(Shape shapes : shapes_) {
  107.     shapes.render(canvas);
  108.     }
  109. }
  110.  
  111. bool PlayState::onKey(const prg::IKeyEvent::KeyEvent& key_event)
  112. {
  113.     if(key_event.key_state == KeyEvent::KB_DOWN) {
  114.         switch(key_event.key) {
  115.         case KeyEvent::KB_ESC_KEY:
  116.             prg::application.exit();
  117.             break;
  118.  
  119.         }
  120.     }
  121.     return true;
  122. }
  123.  
  124. void PlayState::onTimer(prg::Timer& timer)
  125. {
  126.     for(auto snake : snakes_) {
  127.         snake->move();
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment