Advertisement
Guest User

init y update

a guest
Apr 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. void PingPongPhysics::init(GameObject* o) {
  2. int direction = 1 - 2 * (rand() % 2);
  3. int angle = 60 - (rand() % 121);
  4. int dx = direction * speed_ * std::cos(angle * M_PI / 180.0f);
  5. int dy = speed_ * std::sin(angle * M_PI / 180.0f);
  6.  
  7. o->setDirection(dx,dy);
  8.  
  9. hits_ = 0;
  10. speed_ = 8;
  11.  
  12. }
  13.  
  14. void PingPongPhysics::update(GameObject* o) {
  15. Vector2D<int> ballNextPos = o->getPosition() + o->getDirection(); //Calculamos la siguiente dirección
  16.  
  17. //Comprobar si esa dirección ha colisionado con algun borde. Si es superior/inferior rebota, si no llamas a BORDER EXIT suma puntos según el lado y se teletransporta al medio
  18. //(Y toma una direccion distinta)
  19.  
  20. //Por encima
  21. if (ballNextPos.getY() <= 0) {
  22. ballNextPos.setY(0);
  23. o->setPosition(ballNextPos);
  24. o->setDirectionY(-1 * o->getDirection().getY());
  25.  
  26. for (int i = 0; i < ballObserver_.size(); i++) {
  27. ballObserver_[i]->onCollision(o, nullptr);
  28. }
  29. }
  30.  
  31. //Por debajo
  32. if (ballNextPos.getY()
  33. >= o->getGame()->getWindowHeight() - o->getHeight()) {
  34. ballNextPos.setY(o->getGame()->getWindowHeight() - o->getHeight());
  35. o->setDirectionY(-1 * o->getDirection().getY());
  36. }
  37.  
  38. //por la izquierda
  39. if (ballNextPos.getX() <= 0) {
  40. for (int i = 0; i < ballObserver_.size(); i++) {
  41. ballObserver_[i]->onBorderExit(o, BallObserver::LEFT);
  42. }
  43. }
  44.  
  45. //por la derecha
  46. if (ballNextPos.getX() >= o->getGame()->getWindowWidth() - o->getWidth()) {
  47. for (int i = 0; i < ballObserver_.size(); i++) {
  48. ballObserver_[i]->onBorderExit(o, BallObserver::RIGHT);
  49. }
  50. }
  51.  
  52. //Si TAMBIEN se ha colisionado con alguna pala, pues hace sus movidas con el hits y el speed y rebota. Y la colision la hace el GAME MANAGER (junto con la suma y eso)
  53. //Pala izquierda
  54. if (ballNextPos.getX() >= left_paddle_->getPosition().getX() && ballNextPos.getX() <= (left_paddle_->getPosition().getX() + left_paddle_->getWidth())
  55. && ballNextPos.getY() >= left_paddle_->getPosition().getY() && ballNextPos.getY() <= (left_paddle_->getPosition().getY() + left_paddle_->getHeight())) {
  56.  
  57. hits_++;
  58.  
  59. for (int i = 0; i < ballObserver_.size(); i++) {
  60. ballObserver_[i]->onCollision(o, left_paddle_);
  61. }
  62.  
  63. //Redireccionamos la bola
  64. int sign = o->getDirection().getX() < 0 ? 1 : -1;
  65. int rel_y = (o->getPosition().getY() - left_paddle_->getPosition().getY() + o->getHeight());
  66. float angle = (2.14f * rel_y - 75.0f);
  67. int dx = sign * speed_ * std::cos(angle * M_PI / 180.0f);
  68. int dy = speed_ * std::sin(angle * M_PI / 180.0f);
  69.  
  70. o->setDirection(dx, dy);
  71. }
  72.  
  73. //Pala derecha
  74. else if (ballNextPos.getX() >= right_paddle_->getPosition().getX() && ballNextPos.getX() <= (right_paddle_->getPosition().getX() + right_paddle_->getWidth())
  75. && ballNextPos.getY() >= right_paddle_->getPosition().getY() && ballNextPos.getY() <= (right_paddle_->getPosition().getY() + right_paddle_->getHeight())) {
  76.  
  77. hits_++;
  78.  
  79. for (int i = 0; i < ballObserver_.size(); i++) {
  80. ballObserver_[i]->onCollision(o, right_paddle_);
  81. }
  82.  
  83. //Redireccionamos la bola
  84. int sign = o->getDirection().getX() < 0 ? 1 : -1;
  85. int rel_y = (o->getPosition().getY() - right_paddle_->getPosition().getY() + o->getHeight());
  86. float angle = (2.14f * rel_y - 75.0f);
  87. int dx = sign * speed_ * std::cos(angle * M_PI / 180.0f);
  88. int dy = speed_ * std::sin(angle * M_PI / 180.0f);
  89.  
  90. o->setDirection(dx, dy);
  91.  
  92. }
  93.  
  94. if (hits_ == 5) {
  95. speed_++;
  96. hits_ = 0;
  97. }
  98.  
  99. o->setPosition(ballNextPos);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement