Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Game::Game() :
- camera_(0),
- background_(0),
- player_(0),
- collision_(0),
- bullet_(0)
- {
- camera_ = new OrthoCamera();
- camera_->SetPosition(XMFLOAT3(0.0f, 0.0f, 0.0f));
- camera_->SetFrustum(800.0f, 600.0f, -100.0f, 100.0f);
- background_ = new Background(800.0f, 600.0f);
- collision_ = new Collision();
- }
- Game::~Game()
- {
- delete camera_;
- delete background_;
- delete player_;
- DeleteAllBullets();
- DeleteAllAsteroids();
- DeleteAllExplosions();
- delete collision_;
- }
- void Game::DoCollision(GameEntity *a, GameEntity *b)
- {
- Ship *player = static_cast<Ship *>(a == player_ ? a : (b == player_ ?
- b : 0));
- Bullet *bullet = static_cast<Bullet *>(IsBullet(a) ? a : (IsBullet(b)
- ? b : 0));
- Asteroid *asteroid = static_cast<Asteroid *>(IsAsteroid(a) ? a :
- (IsAsteroid(b) ? b : 0));
- if (player_->shieldActive)
- return;
- else
- {
- if (player && asteroid)
- {
- AsteroidHit(asteroid);
- DeletePlayer();
- }
- }
- if (bullet && asteroid)
- {
- AsteroidHit(asteroid);
- DeleteAllBullets();
- }
- }
- void Game::AsteroidHit(Asteroid *asteroid)
- {
- int oldSize = asteroid->GetSize();
- if (oldSize > 1)
- {
- int smallerSize = oldSize -1;
- XMVECTOR position = asteroid->GetPosition();
- SpawnAsteroidAt(position, smallerSize);
- SpawnAsteroidAt(position, smallerSize);
- }
- DeleteAsteroid(asteroid);
- }
- void Game::DeleteAsteroid(Asteroid *asteroid)
- {
- asteroids_.remove(asteroid);
- delete asteroid;
- }
- void Game::DeletePlayer()
- {
- delete player_;
- player_ = 0;
- }
- void Game::DeleteAllBullets()
- {
- for (BulletList::const_iterator bulletIt = bullet_.begin(),
- end = bullet_.end();
- bulletIt != end;
- ++bulletIt)
- {
- delete (*bulletIt);
- }
- bullet_.clear();
- }
- void Game::SpawnAsteroids(int numAsteroids)
- {
- float halfWidth = 800.0f * 0.5f;
- float halfHeight = 600.0f * 0.5f;
- for (int i = 0; i < numAsteroids; i++)
- {
- float x = Random::GetFloat(-halfWidth, halfWidth);
- float y = Random::GetFloat(-halfHeight, halfHeight);
- XMVECTOR position = XMVectorSet(x, y, 0.0f, 0.0f);
- SpawnAsteroidAt(position, 3);
- }
- }
- void Game::SpawnAsteroidAt(XMVECTOR position, int size)
- {
- const float MAX_ASTEROID_SPEED = 1.0f;
- float angle = Random::GetFloat(Maths::TWO_PI);
- XMMATRIX randomRotation = XMMatrixRotationZ(angle);
- XMVECTOR velocity = XMVectorSet(0.0f, Random::GetFloat(MAX_ASTEROID_SPEED),
- 0.0f, 0.0f);
- velocity = XMVector3TransformNormal(velocity, randomRotation);
- Asteroid *asteroid = new Asteroid(position, velocity, size);
- asteroid->EnableCollisions(collision_, size * 5.0f);
- asteroids_.push_back(asteroid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement