Asteroid.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Asteroid.h"
  9. #include "SpriteComponent.h"
  10. #include "MoveComponent.h"
  11. #include "Game.h"
  12. #include "Random.h"
  13. #include "CircleComponent.h"
  14. Asteroid::Asteroid(Game* game)
  15. :Actor(game)
  16. ,mCircle(nullptr)
  17. {
  18. // Initialize to random position/orientation
  19. Vector2 randPos = Random::GetVector(Vector2(-512.0f, -384.0f),
  20. Vector2(512.0f, 384.0f));
  21. SetPosition(randPos);
  22. SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));
  23. // Create a sprite component
  24. SpriteComponent* sc = new SpriteComponent(this);
  25. sc->SetTexture(game->GetTexture("Assets/Asteroid.png"));
  26. // Create a move component, and set a forward speed
  27. MoveComponent* mc = new MoveComponent(this);
  28. mc->SetForwardSpeed(150.0f);
  29. // Create a circle component (for collision)
  30. mCircle = new CircleComponent(this);
  31. mCircle->SetRadius(40.0f);
  32. // Add to mAsteroids in game
  33. game->AddAsteroid(this);
  34. }
  35. Asteroid::~Asteroid()
  36. {
  37. GetGame()->RemoveAsteroid(this);
  38. }