StarShip.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // Created by jovian on 17/08/17.
  3. //
  4. #include "StarShip.h"
  5. #include "../Maths/b2Angle.h"
  6. StarShip::StarShip(const StarShipDef &def) : m_def(def), m_angle(0.0f),
  7. m_targetPlanet(NO_TARGET_PLANET), m_arrived(true) {}
  8. StarShip::~StarShip() {}
  9. Visual *StarShip::makeVisual() {
  10. return new Visual((unsigned int) 6 + m_def.motor, m_def.pos, m_angle, 0.0142f * m_def.cargo);
  11. }
  12. void StarShip::update() {
  13. if (!m_arrived) {
  14. //todo Speed from motor
  15. float speed(0.005f);
  16. // Distance between this ship and targeted planet
  17. b2Vec2 dir(m_targetPos - m_def.pos);
  18. float dist(dir.Length());
  19. if (dist < speed) {
  20. // Arriving
  21. m_arrived = true;
  22. } else {
  23. // Moving
  24. dir *= speed / dist;
  25. m_def.pos += dir;
  26. }
  27. }
  28. }
  29. Stock StarShip::unload() {
  30. // Send into a probe
  31. Stock probe(m_def.stock);
  32. // Clear local stock
  33. m_def.stock.setZero();
  34. // Return probe to the planet
  35. return probe;
  36. }
  37. void StarShip::load(Stock stock) {
  38. // todo Check capacity
  39. m_def.stock = stock;
  40. }
  41. void StarShip::travelTo(int targetPlanet, b2Vec2 targetPos) {
  42. // Plot a course
  43. m_targetPlanet = targetPlanet;
  44. m_targetPos = targetPos;
  45. // Let's go
  46. m_arrived = false;
  47. m_angle = b2Angle(m_targetPos - m_def.pos, b2Vec2(0.0f, -1.0f));
  48. }
  49. bool StarShip::isArrived() const {
  50. return m_arrived;
  51. }
  52. int StarShip::getTargetPlanet() const {
  53. return m_targetPlanet;
  54. }
  55. const StarShipDef &StarShip::getDef() const {
  56. return m_def;
  57. }