Ship.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 "Ship.h"
  9. #include "SpriteComponent.h"
  10. #include "InputComponent.h"
  11. #include "Game.h"
  12. #include "Laser.h"
  13. #include "InputSystem.h"
  14. Ship::Ship(Game* game)
  15. :Actor(game)
  16. ,mLaserCooldown(0.0f)
  17. {
  18. // Create a sprite component
  19. SpriteComponent* sc = new SpriteComponent(this, 150);
  20. sc->SetTexture(game->GetTexture("Assets/Ship.png"));
  21. // Create an input component and set keys/speed
  22. InputComponent* ic = new InputComponent(this);
  23. ic->SetForwardKey(SDL_SCANCODE_W);
  24. ic->SetBackKey(SDL_SCANCODE_S);
  25. ic->SetClockwiseKey(SDL_SCANCODE_A);
  26. ic->SetCounterClockwiseKey(SDL_SCANCODE_D);
  27. ic->SetMaxForwardSpeed(300.0f);
  28. ic->SetMaxAngularSpeed(Math::TwoPi);
  29. }
  30. void Ship::UpdateActor(float deltaTime)
  31. {
  32. mLaserCooldown -= deltaTime;
  33. }
  34. void Ship::ActorInput(const InputState& state)
  35. {
  36. if (state.Keyboard.GetKeyValue(SDL_SCANCODE_SPACE)
  37. && mLaserCooldown <= 0.0f)
  38. {
  39. // Create a laser and set its position/rotation to mine
  40. Laser* laser = new Laser(GetGame());
  41. laser->SetPosition(GetPosition());
  42. laser->SetRotation(GetRotation());
  43. // Reset laser cooldown (half second)
  44. mLaserCooldown = 0.5f;
  45. }
  46. }