123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- //
- // Created by jovian on 17/08/17.
- //
- #include "Planet.h"
- #include "../Maths/b2Angle.h"
- int Planet::m_incr = 0;
- Planet::Planet(const PlanetDef &def)
- : m_def(def), m_imgId(1 + (unsigned int) m_incr % 5),
- m_angle(b2Angle(def.pos, b2Vec2(1.0f, 1.0f))) {
- // A better name
- if (m_def.name == "---")
- m_def.name = Planet::randomName();
- // Identification
- m_idPlanet = Planet::m_incr;
- Planet::m_incr++;
- }
- Planet::~Planet() {
- // Clear vector (do not destroy pointers !)
- m_landedShips.clear();
- }
- Visual *Planet::makeVisual() {
- return new Visual(m_imgId, m_def.pos, m_angle, m_def.size);
- }
- void Planet::applyRule(PlanetStep *rule) {
- rule->callApply(m_def);
- }
- const PlanetDef &Planet::getDef() const {
- return m_def;
- }
- int Planet::getId() const {
- return m_idPlanet;
- }
- void Planet::landShip(StarShip *ship) {
- // Find a empty place in the hangar
- m_landedShips.push_back(ship);
- // Unload this ship
- m_def.stock += ship->unload();
- }
- bool Planet::readyShip() {
- return !m_landedShips.empty();
- }
- std::string Planet::randomName() {
- int size(5 + rand() % 9);
- std::string voyel("aeiou");
- std::string conson("bcdfghjklmnpqrstvwxyz");
- std::string newName;
- for (int k(0); k < size; k++) {
- if (k % 2) { // Voyelle
- newName.append(1, voyel[rand() % voyel.length()]);
- } else { // Consonne
- newName.append(1, conson[rand() % conson.length()]);
- }
- }
- newName[0] = toupper(newName[0]);
- return newName;
- }
- StarShip *Planet::launchShip(const Stock &avg) {
- // Security : empty space port
- if (m_landedShips.empty())
- return nullptr;
- // Design a good stock
- Stock pack;
- pack = m_def.stock - avg;
- pack.setPositive();
- // Launch ship
- StarShip* ship(m_landedShips.back());
- m_def.stock -= pack;
- ship->load(pack);
- m_landedShips.pop_back();
- return ship;
- }
- bool Planet::need(Resource res, Stock average) {
- // Is above average
- Stock pack;
- pack = m_def.stock - average;
- return pack.get(res) < 0;
- }
|