Planet.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // Created by jovian on 17/08/17.
  3. //
  4. #include "Planet.h"
  5. #include "../Maths/b2Angle.h"
  6. int Planet::m_incr = 0;
  7. Planet::Planet(const PlanetDef &def)
  8. : m_def(def), m_imgId(1 + (unsigned int) m_incr % 5),
  9. m_angle(b2Angle(def.pos, b2Vec2(1.0f, 1.0f))) {
  10. // A better name
  11. if (m_def.name == "---")
  12. m_def.name = Planet::randomName();
  13. // Identification
  14. m_idPlanet = Planet::m_incr;
  15. Planet::m_incr++;
  16. }
  17. Planet::~Planet() {
  18. // Clear vector (do not destroy pointers !)
  19. m_landedShips.clear();
  20. }
  21. Visual *Planet::makeVisual() {
  22. return new Visual(m_imgId, m_def.pos, m_angle, m_def.size);
  23. }
  24. void Planet::applyRule(PlanetStep *rule) {
  25. rule->callApply(m_def);
  26. }
  27. const PlanetDef &Planet::getDef() const {
  28. return m_def;
  29. }
  30. int Planet::getId() const {
  31. return m_idPlanet;
  32. }
  33. void Planet::landShip(StarShip *ship) {
  34. // Find a empty place in the hangar
  35. m_landedShips.push_back(ship);
  36. // Unload this ship
  37. m_def.stock += ship->unload();
  38. }
  39. bool Planet::readyShip() {
  40. return !m_landedShips.empty();
  41. }
  42. std::string Planet::randomName() {
  43. int size(5 + rand() % 9);
  44. std::string voyel("aeiou");
  45. std::string conson("bcdfghjklmnpqrstvwxyz");
  46. std::string newName;
  47. for (int k(0); k < size; k++) {
  48. if (k % 2) { // Voyelle
  49. newName.append(1, voyel[rand() % voyel.length()]);
  50. } else { // Consonne
  51. newName.append(1, conson[rand() % conson.length()]);
  52. }
  53. }
  54. newName[0] = toupper(newName[0]);
  55. return newName;
  56. }
  57. StarShip *Planet::launchShip(const Stock &avg) {
  58. // Security : empty space port
  59. if (m_landedShips.empty())
  60. return nullptr;
  61. // Design a good stock
  62. Stock pack;
  63. pack = m_def.stock - avg;
  64. pack.setPositive();
  65. // Launch ship
  66. StarShip* ship(m_landedShips.back());
  67. m_def.stock -= pack;
  68. ship->load(pack);
  69. m_landedShips.pop_back();
  70. return ship;
  71. }
  72. bool Planet::need(Resource res, Stock average) {
  73. // Is above average
  74. Stock pack;
  75. pack = m_def.stock - average;
  76. return pack.get(res) < 0;
  77. }