Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************************
- * File: game.cpp
- * Description: Contains the implementaiton of the game class
- * methods.
- *
- *********************************************************************/
- #include "game.h"
- #include "rocks.h"
- #include <limits>
- #include <algorithm>
- using namespace std;
- #define OFF_SCREEN_BORDER_AMOUNT 5
- // You may find this function helpful...
- /**********************************************************
- * Function: getClosestDistance
- * Description: Determine how close these two objects will
- * get in between the frames.
- **********************************************************/
- /*
- float Game :: getClosestDistance(const FlyingObject &obj1, const FlyingObject &obj2) const
- {
- // find the maximum distance traveled
- float dMax = max(abs(obj1.getVelocity().getDx()), abs(obj1.getVelocity().getDy()));
- dMax = max(dMax, abs(obj2.getVelocity().getDx()));
- dMax = max(dMax, abs(obj2.getVelocity().getDy()));
- dMax = max(dMax, 0.1f); // when dx and dy are 0.0. Go through the loop once.
- float distMin = std::numeric_limits<float>::max();
- for (float i = 0.0; i <= dMax; i++)
- {
- Point point1(obj1.getPoint().getX() + (obj1.getVelocity().getDx() * i / dMax),
- obj1.getPoint().getY() + (obj1.getVelocity().getDy() * i / dMax));
- Point point2(obj2.getPoint().getX() + (obj2.getVelocity().getDx() * i / dMax),
- obj2.getPoint().getY() + (obj2.getVelocity().getDy() * i / dMax));
- float xDiff = point1.getX() - point2.getX();
- float yDiff = point1.getY() - point2.getY();
- float distSquared = (xDiff * xDiff) +(yDiff * yDiff);
- distMin = min(distMin, distSquared);
- }
- return sqrt(distMin);
- }
- */
- Game::Game(Point tl, Point br)
- {
- pShip = NULL;
- }
- Game::~Game()
- {
- }
- void Game::handleInput(const Interface & ui)
- {
- if (ui.isLeft())
- {
- pShip->rotateLeft();
- }
- if (ui.isRight())
- {
- pShip->rotateRight();
- }
- if (ui.isUp())
- {
- pShip->applyThrust();
- }
- if (ui.isSpace())
- {
- Bullet newBullet;
- newBullet.fire(pShip->getPoint(), pShip->getAngle(), pShip->getVelocity());
- bullets.push_back(newBullet);
- }
- }
- void Game::advance()
- {
- advanceRock();
- advanceShip();
- advanceBullets();
- handleCollisions();
- cleanUpZombies();
- }
- void Game::advanceBullets()
- {
- // Move each of the bullets forward if it is alive
- for (int i = 0; i < bullets.size(); i++)
- {
- bullets[i].setFrames(bullets[i].getFrames() + 1);
- if (bullets[i].getFrames() > 40)
- {
- bullets[i].kill();
- }
- if (bullets[i].isAlive())
- {
- // this bullet is alive, so tell it to move forward
- bullets[i].advance();
- //bullets[i].kill();
- }
- }
- }
- void Game :: handleCollisions()
- {
- // now check for a hit (if it is close enough to any live bullets)
- vector <Rock*> :: iterator rockIt = rocks.end();
- for (vector <Rock*> :: iterator it = rocks.begin(); it != rockIt; ++it)
- {
- // this bullet is alive, see if its too close
- // check if the Asteroid is at this point (in case it was hit)
- // BTW, this logic could be more sophisiticated, but this will
- // get the job done for now...
- for(int i = 0; i < bullets.size(); i++)
- {
- if (fabs(bullets[i].getPoint().getX() - (*it)->getPoint().getX()) < CLOSE_ENOUGH
- && fabs(bullets[i].getPoint().getY() - (*it)->getPoint().getY()) < CLOSE_ENOUGH
- && bullets[i].isAlive())
- {
- bullets[i].kill();
- if((*it) != NULL)
- (*it)->kill();
- if((*it) != NULL)
- (*it)->splitApart(rocks);
- }
- //we have a hit!
- // hit the Asteroid
- // the bullet is dead as well
- } // if bullet is alive
- } // for bullets
- }
- void Game :: createRock()
- {
- for (int i = 0; i < 5; i++)
- {
- Rock* newRock = NULL;
- newRock = new BigRock();
- rocks.push_back(newRock);
- }
- }
- // bool Game :: isOnScreen(const Point & point)
- // {
- // return (point.getX() >= topLeft.getX()
- // && point.getX() <= bottomRight.getX()
- // && point.getY() >= bottomRight.getY()
- // && point.getY() <= topLeft.getY());
- // }
- void Game :: draw(const Interface & ui)
- {
- for (vector <Rock*> :: iterator it = rocks.begin(); it != rocks.end(); ++it)
- {
- //cout << (void*)(*it) << endl;
- if((*it) != NULL && (*it)->isAlive())
- (*it)->draw();
- }
- pShip->draw();
- for (int i = 0; i < bullets.size(); i++)
- {
- if (bullets[i].isAlive())
- {
- bullets[i].draw();
- }
- }
- }
- //void Game :: advanceBullets()
- //{
- //}
- void Game :: advanceShip()
- {
- if (pShip == NULL)
- {
- Point startPos = Point(0,0);
- pShip = new Ship(startPos);
- }
- pShip->advance();
- }
- void Game :: advanceRock()
- {
- if (rocks.empty())
- {
- createRock();
- }
- else
- {
- for (vector <Rock*> ::iterator it = rocks.begin(); it != rocks.end(); ++it)
- {
- (*it)->advance();
- (*it)->setRotation((*it)->getRotation() + (*it)->getRotationChange());
- }
- }
- }
- void Game :: cleanUpZombies()
- {
- for (vector <Bullet> :: iterator it = bullets.begin(); it != bullets.end(); )
- {
- if(!(*it).isAlive())
- bullets.erase(it);
- else
- it++;
- }
- /*
- for (vector <Rock*> ::iterator it = rocks.begin(); it != rocks.end(); ++it)
- {
- cout << (*it)->isAlive();
- if((*it) != NULL && !(*it)->isAlive())
- {
- delete *it;
- *it = NULL;
- }
- */
- vector <Rock*> :: iterator it = rocks.begin();
- while (it != rocks.end())
- {
- Rock* pRock = *it;
- if (!pRock->isAlive())
- {
- delete pRock;
- it = rocks.erase(it);
- }
- else
- {
- it++;
- }
- }
- }
- // {
- // // Look for dead bullets
- // vector<Bullet>::iterator bulletIt = bullets.begin();
- // while (bulletIt != bullets.end())
- // {
- // Bullet bullet = *bulletIt;
- // // Asteroids Hint:
- // // If we had a list of pointers, we would need this line instead:
- // //Bullet* pBullet = *bulletIt;
- // if (!bullet.isAlive())
- // {
- // // If we had a list of pointers, we would need to delete the memory here...
- // // remove from list and advance
- // bulletIt = bullets.erase(bulletIt);
- // }
- // else
- // {
- // bulletIt++; // advance
- // }
- // }
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement