Ship.cpp 1.4 KB

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