Laser.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "Laser.h"
  9. #include "SpriteComponent.h"
  10. #include "MoveComponent.h"
  11. #include "Game.h"
  12. #include "CircleComponent.h"
  13. #include "Asteroid.h"
  14. Laser::Laser(Game* game)
  15. :Actor(game)
  16. ,mDeathTimer(1.0f)
  17. {
  18. // Create a sprite component
  19. SpriteComponent* sc = new SpriteComponent(this);
  20. sc->SetTexture(game->GetTexture("Assets/Laser.png"));
  21. // Create a move component, and set a forward speed
  22. MoveComponent* mc = new MoveComponent(this);
  23. mc->SetForwardSpeed(800.0f);
  24. // Create a circle component (for collision)
  25. mCircle = new CircleComponent(this);
  26. mCircle->SetRadius(11.0f);
  27. }
  28. void Laser::UpdateActor(float deltaTime)
  29. {
  30. // If we run out of time, laser is dead
  31. mDeathTimer -= deltaTime;
  32. if (mDeathTimer <= 0.0f)
  33. {
  34. SetState(EDead);
  35. }
  36. else
  37. {
  38. // Do we intersect with an asteroid?
  39. for (auto ast : GetGame()->GetAsteroids())
  40. {
  41. if (Intersect(*mCircle, *(ast->GetCircle())))
  42. {
  43. // The first asteroid we intersect with,
  44. // set ourselves and the asteroid to dead
  45. SetState(EDead);
  46. ast->SetState(EDead);
  47. break;
  48. }
  49. }
  50. }
  51. }