Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. /*********************************************************************
  2. * File: game.cpp
  3. * Description: Contains the implementaiton of the game class
  4. * methods.
  5. *
  6. *********************************************************************/
  7.  
  8. #include "game.h"
  9. #include "rocks.h"
  10.  
  11. #include <limits>
  12. #include <algorithm>
  13. using namespace std;
  14.  
  15. #define OFF_SCREEN_BORDER_AMOUNT 5
  16.  
  17.  
  18. // You may find this function helpful...
  19.  
  20. /**********************************************************
  21. * Function: getClosestDistance
  22. * Description: Determine how close these two objects will
  23. * get in between the frames.
  24. **********************************************************/
  25. /*
  26. float Game :: getClosestDistance(const FlyingObject &obj1, const FlyingObject &obj2) const
  27. {
  28. // find the maximum distance traveled
  29. float dMax = max(abs(obj1.getVelocity().getDx()), abs(obj1.getVelocity().getDy()));
  30. dMax = max(dMax, abs(obj2.getVelocity().getDx()));
  31. dMax = max(dMax, abs(obj2.getVelocity().getDy()));
  32. dMax = max(dMax, 0.1f); // when dx and dy are 0.0. Go through the loop once.
  33.  
  34. float distMin = std::numeric_limits<float>::max();
  35. for (float i = 0.0; i <= dMax; i++)
  36. {
  37. Point point1(obj1.getPoint().getX() + (obj1.getVelocity().getDx() * i / dMax),
  38. obj1.getPoint().getY() + (obj1.getVelocity().getDy() * i / dMax));
  39. Point point2(obj2.getPoint().getX() + (obj2.getVelocity().getDx() * i / dMax),
  40. obj2.getPoint().getY() + (obj2.getVelocity().getDy() * i / dMax));
  41.  
  42. float xDiff = point1.getX() - point2.getX();
  43. float yDiff = point1.getY() - point2.getY();
  44.  
  45. float distSquared = (xDiff * xDiff) +(yDiff * yDiff);
  46.  
  47. distMin = min(distMin, distSquared);
  48. }
  49.  
  50. return sqrt(distMin);
  51. }
  52. */
  53. Game::Game(Point tl, Point br)
  54. {
  55. pShip = NULL;
  56. }
  57.  
  58. Game::~Game()
  59. {
  60. }
  61.  
  62. void Game::handleInput(const Interface & ui)
  63. {
  64. if (ui.isLeft())
  65. {
  66. pShip->rotateLeft();
  67. }
  68.  
  69. if (ui.isRight())
  70. {
  71. pShip->rotateRight();
  72. }
  73.  
  74. if (ui.isUp())
  75. {
  76. pShip->applyThrust();
  77. }
  78.  
  79. if (ui.isSpace())
  80. {
  81. Bullet newBullet;
  82. newBullet.fire(pShip->getPoint(), pShip->getAngle(), pShip->getVelocity());
  83.  
  84. bullets.push_back(newBullet);
  85. }
  86. }
  87.  
  88. void Game::advance()
  89. {
  90. advanceRock();
  91. advanceShip();
  92. advanceBullets();
  93. handleCollisions();
  94. cleanUpZombies();
  95. }
  96.  
  97. void Game::advanceBullets()
  98. {
  99. // Move each of the bullets forward if it is alive
  100. for (int i = 0; i < bullets.size(); i++)
  101. {
  102. bullets[i].setFrames(bullets[i].getFrames() + 1);
  103.  
  104. if (bullets[i].getFrames() > 40)
  105. {
  106. bullets[i].kill();
  107. }
  108.  
  109. if (bullets[i].isAlive())
  110. {
  111. // this bullet is alive, so tell it to move forward
  112. bullets[i].advance();
  113. //bullets[i].kill();
  114. }
  115. }
  116. }
  117.  
  118. void Game :: handleCollisions()
  119. {
  120. // now check for a hit (if it is close enough to any live bullets)
  121. vector <Rock*> :: iterator rockIt = rocks.end();
  122. for (vector <Rock*> :: iterator it = rocks.begin(); it != rockIt; ++it)
  123. {
  124.  
  125. // this bullet is alive, see if its too close
  126.  
  127. // check if the Asteroid is at this point (in case it was hit)
  128. // BTW, this logic could be more sophisiticated, but this will
  129. // get the job done for now...
  130.  
  131. for(int i = 0; i < bullets.size(); i++)
  132. {
  133.  
  134. if (fabs(bullets[i].getPoint().getX() - (*it)->getPoint().getX()) < CLOSE_ENOUGH
  135. && fabs(bullets[i].getPoint().getY() - (*it)->getPoint().getY()) < CLOSE_ENOUGH
  136. && bullets[i].isAlive())
  137. {
  138. bullets[i].kill();
  139. if((*it) != NULL)
  140. (*it)->kill();
  141. if((*it) != NULL)
  142. (*it)->splitApart(rocks);
  143.  
  144.  
  145. }
  146.  
  147. //we have a hit!
  148.  
  149. // hit the Asteroid
  150.  
  151.  
  152. // the bullet is dead as well
  153. } // if bullet is alive
  154. } // for bullets
  155. }
  156.  
  157. void Game :: createRock()
  158. {
  159. for (int i = 0; i < 5; i++)
  160. {
  161. Rock* newRock = NULL;
  162. newRock = new BigRock();
  163. rocks.push_back(newRock);
  164. }
  165. }
  166.  
  167.  
  168. // bool Game :: isOnScreen(const Point & point)
  169. // {
  170. // return (point.getX() >= topLeft.getX()
  171. // && point.getX() <= bottomRight.getX()
  172. // && point.getY() >= bottomRight.getY()
  173. // && point.getY() <= topLeft.getY());
  174. // }
  175.  
  176. void Game :: draw(const Interface & ui)
  177. {
  178. for (vector <Rock*> :: iterator it = rocks.begin(); it != rocks.end(); ++it)
  179. {
  180. //cout << (void*)(*it) << endl;
  181. if((*it) != NULL && (*it)->isAlive())
  182. (*it)->draw();
  183.  
  184.  
  185. }
  186. pShip->draw();
  187.  
  188. for (int i = 0; i < bullets.size(); i++)
  189. {
  190. if (bullets[i].isAlive())
  191. {
  192. bullets[i].draw();
  193. }
  194. }
  195.  
  196. }
  197. //void Game :: advanceBullets()
  198. //{
  199. //}
  200.  
  201. void Game :: advanceShip()
  202. {
  203. if (pShip == NULL)
  204. {
  205. Point startPos = Point(0,0);
  206. pShip = new Ship(startPos);
  207. }
  208.  
  209. pShip->advance();
  210.  
  211. }
  212.  
  213. void Game :: advanceRock()
  214. {
  215. if (rocks.empty())
  216. {
  217. createRock();
  218.  
  219. }
  220. else
  221. {
  222.  
  223. for (vector <Rock*> ::iterator it = rocks.begin(); it != rocks.end(); ++it)
  224. {
  225. (*it)->advance();
  226. (*it)->setRotation((*it)->getRotation() + (*it)->getRotationChange());
  227. }
  228. }
  229. }
  230.  
  231.  
  232. void Game :: cleanUpZombies()
  233. {
  234. for (vector <Bullet> :: iterator it = bullets.begin(); it != bullets.end(); )
  235.  
  236. {
  237. if(!(*it).isAlive())
  238. bullets.erase(it);
  239. else
  240. it++;
  241.  
  242. }
  243. /*
  244. for (vector <Rock*> ::iterator it = rocks.begin(); it != rocks.end(); ++it)
  245. {
  246. cout << (*it)->isAlive();
  247. if((*it) != NULL && !(*it)->isAlive())
  248. {
  249. delete *it;
  250. *it = NULL;
  251. }
  252. */
  253.  
  254. vector <Rock*> :: iterator it = rocks.begin();
  255. while (it != rocks.end())
  256. {
  257. Rock* pRock = *it;
  258.  
  259. if (!pRock->isAlive())
  260. {
  261. delete pRock;
  262. it = rocks.erase(it);
  263. }
  264. else
  265. {
  266. it++;
  267. }
  268. }
  269. }
  270. // {
  271. // // Look for dead bullets
  272. // vector<Bullet>::iterator bulletIt = bullets.begin();
  273. // while (bulletIt != bullets.end())
  274. // {
  275. // Bullet bullet = *bulletIt;
  276. // // Asteroids Hint:
  277. // // If we had a list of pointers, we would need this line instead:
  278. // //Bullet* pBullet = *bulletIt;
  279.  
  280. // if (!bullet.isAlive())
  281. // {
  282. // // If we had a list of pointers, we would need to delete the memory here...
  283.  
  284.  
  285.  
  286. // // remove from list and advance
  287. // bulletIt = bullets.erase(bulletIt);
  288. // }
  289. // else
  290. // {
  291. // bulletIt++; // advance
  292. // }
  293. // }
  294.  
  295. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement