Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void PingPongPhysics::init(GameObject* o) {
- int direction = 1 - 2 * (rand() % 2);
- int angle = 60 - (rand() % 121);
- int dx = direction * speed_ * std::cos(angle * M_PI / 180.0f);
- int dy = speed_ * std::sin(angle * M_PI / 180.0f);
- o->setDirection(dx,dy);
- hits_ = 0;
- speed_ = 8;
- }
- void PingPongPhysics::update(GameObject* o) {
- Vector2D<int> ballNextPos = o->getPosition() + o->getDirection(); //Calculamos la siguiente dirección
- //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
- //(Y toma una direccion distinta)
- //Por encima
- if (ballNextPos.getY() <= 0) {
- ballNextPos.setY(0);
- o->setPosition(ballNextPos);
- o->setDirectionY(-1 * o->getDirection().getY());
- for (int i = 0; i < ballObserver_.size(); i++) {
- ballObserver_[i]->onCollision(o, nullptr);
- }
- }
- //Por debajo
- if (ballNextPos.getY()
- >= o->getGame()->getWindowHeight() - o->getHeight()) {
- ballNextPos.setY(o->getGame()->getWindowHeight() - o->getHeight());
- o->setDirectionY(-1 * o->getDirection().getY());
- }
- //por la izquierda
- if (ballNextPos.getX() <= 0) {
- for (int i = 0; i < ballObserver_.size(); i++) {
- ballObserver_[i]->onBorderExit(o, BallObserver::LEFT);
- }
- }
- //por la derecha
- if (ballNextPos.getX() >= o->getGame()->getWindowWidth() - o->getWidth()) {
- for (int i = 0; i < ballObserver_.size(); i++) {
- ballObserver_[i]->onBorderExit(o, BallObserver::RIGHT);
- }
- }
- //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)
- //Pala izquierda
- if (ballNextPos.getX() >= left_paddle_->getPosition().getX() && ballNextPos.getX() <= (left_paddle_->getPosition().getX() + left_paddle_->getWidth())
- && ballNextPos.getY() >= left_paddle_->getPosition().getY() && ballNextPos.getY() <= (left_paddle_->getPosition().getY() + left_paddle_->getHeight())) {
- hits_++;
- for (int i = 0; i < ballObserver_.size(); i++) {
- ballObserver_[i]->onCollision(o, left_paddle_);
- }
- //Redireccionamos la bola
- int sign = o->getDirection().getX() < 0 ? 1 : -1;
- int rel_y = (o->getPosition().getY() - left_paddle_->getPosition().getY() + o->getHeight());
- float angle = (2.14f * rel_y - 75.0f);
- int dx = sign * speed_ * std::cos(angle * M_PI / 180.0f);
- int dy = speed_ * std::sin(angle * M_PI / 180.0f);
- o->setDirection(dx, dy);
- }
- //Pala derecha
- else if (ballNextPos.getX() >= right_paddle_->getPosition().getX() && ballNextPos.getX() <= (right_paddle_->getPosition().getX() + right_paddle_->getWidth())
- && ballNextPos.getY() >= right_paddle_->getPosition().getY() && ballNextPos.getY() <= (right_paddle_->getPosition().getY() + right_paddle_->getHeight())) {
- hits_++;
- for (int i = 0; i < ballObserver_.size(); i++) {
- ballObserver_[i]->onCollision(o, right_paddle_);
- }
- //Redireccionamos la bola
- int sign = o->getDirection().getX() < 0 ? 1 : -1;
- int rel_y = (o->getPosition().getY() - right_paddle_->getPosition().getY() + o->getHeight());
- float angle = (2.14f * rel_y - 75.0f);
- int dx = sign * speed_ * std::cos(angle * M_PI / 180.0f);
- int dy = speed_ * std::sin(angle * M_PI / 180.0f);
- o->setDirection(dx, dy);
- }
- if (hits_ == 5) {
- speed_++;
- hits_ = 0;
- }
- o->setPosition(ballNextPos);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement