Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. Game::Game() :
  2. camera_(0),
  3. background_(0),
  4. player_(0),
  5. collision_(0),
  6. bullet_(0)
  7. {
  8. camera_ = new OrthoCamera();
  9. camera_->SetPosition(XMFLOAT3(0.0f, 0.0f, 0.0f));
  10. camera_->SetFrustum(800.0f, 600.0f, -100.0f, 100.0f);
  11. background_ = new Background(800.0f, 600.0f);
  12. collision_ = new Collision();
  13. }
  14.  
  15. Game::~Game()
  16. {
  17. delete camera_;
  18. delete background_;
  19. delete player_;
  20. DeleteAllBullets();
  21. DeleteAllAsteroids();
  22. DeleteAllExplosions();
  23. delete collision_;
  24. }
  25.  
  26. void Game::DoCollision(GameEntity *a, GameEntity *b)
  27. {
  28. Ship *player = static_cast<Ship *>(a == player_ ? a : (b == player_ ?
  29. b : 0));
  30. Bullet *bullet = static_cast<Bullet *>(IsBullet(a) ? a : (IsBullet(b)
  31. ? b : 0));
  32. Asteroid *asteroid = static_cast<Asteroid *>(IsAsteroid(a) ? a :
  33. (IsAsteroid(b) ? b : 0));
  34.  
  35. if (player_->shieldActive)
  36. return;
  37. else
  38. {
  39. if (player && asteroid)
  40. {
  41. AsteroidHit(asteroid);
  42. DeletePlayer();
  43. }
  44. }
  45.  
  46.  
  47. if (bullet && asteroid)
  48. {
  49. AsteroidHit(asteroid);
  50. DeleteAllBullets();
  51. }
  52. }
  53.  
  54. void Game::AsteroidHit(Asteroid *asteroid)
  55. {
  56. int oldSize = asteroid->GetSize();
  57. if (oldSize > 1)
  58. {
  59. int smallerSize = oldSize -1;
  60. XMVECTOR position = asteroid->GetPosition();
  61. SpawnAsteroidAt(position, smallerSize);
  62. SpawnAsteroidAt(position, smallerSize);
  63. }
  64. DeleteAsteroid(asteroid);
  65. }
  66.  
  67. void Game::DeleteAsteroid(Asteroid *asteroid)
  68. {
  69. asteroids_.remove(asteroid);
  70. delete asteroid;
  71. }
  72.  
  73. void Game::DeletePlayer()
  74. {
  75. delete player_;
  76. player_ = 0;
  77. }
  78.  
  79. void Game::DeleteAllBullets()
  80. {
  81. for (BulletList::const_iterator bulletIt = bullet_.begin(),
  82. end = bullet_.end();
  83. bulletIt != end;
  84. ++bulletIt)
  85. {
  86. delete (*bulletIt);
  87. }
  88.  
  89. bullet_.clear();
  90. }
  91.  
  92. void Game::SpawnAsteroids(int numAsteroids)
  93. {
  94. float halfWidth = 800.0f * 0.5f;
  95. float halfHeight = 600.0f * 0.5f;
  96. for (int i = 0; i < numAsteroids; i++)
  97. {
  98. float x = Random::GetFloat(-halfWidth, halfWidth);
  99. float y = Random::GetFloat(-halfHeight, halfHeight);
  100. XMVECTOR position = XMVectorSet(x, y, 0.0f, 0.0f);
  101. SpawnAsteroidAt(position, 3);
  102. }
  103. }
  104.  
  105. void Game::SpawnAsteroidAt(XMVECTOR position, int size)
  106. {
  107. const float MAX_ASTEROID_SPEED = 1.0f;
  108.  
  109. float angle = Random::GetFloat(Maths::TWO_PI);
  110. XMMATRIX randomRotation = XMMatrixRotationZ(angle);
  111. XMVECTOR velocity = XMVectorSet(0.0f, Random::GetFloat(MAX_ASTEROID_SPEED),
  112. 0.0f, 0.0f);
  113. velocity = XMVector3TransformNormal(velocity, randomRotation);
  114.  
  115. Asteroid *asteroid = new Asteroid(position, velocity, size);
  116. asteroid->EnableCollisions(collision_, size * 5.0f);
  117. asteroids_.push_back(asteroid);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement