Ship.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "Game.h"
  11. #include "Laser.h"
  12. #include "InputSystem.h"
  13. Ship::Ship(Game* game)
  14. :Actor(game)
  15. ,mSpeed(400.0f)
  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. }
  22. void Ship::UpdateActor(float deltaTime)
  23. {
  24. mLaserCooldown -= deltaTime;
  25. // Update position based on velocity
  26. Vector2 pos = GetPosition();
  27. pos += mVelocityDir * mSpeed * deltaTime;
  28. SetPosition(pos);
  29. // Update rotation
  30. float angle = Math::Atan2(mRotationDir.y, mRotationDir.x);
  31. SetRotation(angle);
  32. }
  33. void Ship::ActorInput(const InputState& state)
  34. {
  35. if (state.Controller.GetRightTrigger() > 0.25f
  36. && mLaserCooldown <= 0.0f)
  37. {
  38. // Create a laser and set its position/rotation to mine
  39. Laser* laser = new Laser(GetGame());
  40. laser->SetPosition(GetPosition());
  41. laser->SetRotation(GetRotation());
  42. // Reset laser cooldown (quarter second)
  43. mLaserCooldown = 0.25f;
  44. }
  45. if (state.Controller.GetIsConnected())
  46. {
  47. mVelocityDir = state.Controller.GetLeftStick();
  48. if (!Math::NearZero(state.Controller.GetRightStick().Length()))
  49. {
  50. mRotationDir = state.Controller.GetRightStick();
  51. }
  52. }
  53. }