123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- //
- // Created by jovian on 17/08/17.
- //
- #include "StarShip.h"
- #include "../Maths/b2Angle.h"
- StarShip::StarShip(const StarShipDef &def) : m_def(def), m_angle(0.0f),
- m_targetPlanet(NO_TARGET_PLANET), m_arrived(true) {}
- StarShip::~StarShip() {}
- Visual *StarShip::makeVisual() {
- return new Visual((unsigned int) 6 + m_def.motor, m_def.pos, m_angle, 0.0142f * m_def.cargo);
- }
- void StarShip::update() {
- if (!m_arrived) {
- //todo Speed from motor
- float speed(0.005f);
- // Distance between this ship and targeted planet
- b2Vec2 dir(m_targetPos - m_def.pos);
- float dist(dir.Length());
- if (dist < speed) {
- // Arriving
- m_arrived = true;
- } else {
- // Moving
- dir *= speed / dist;
- m_def.pos += dir;
- }
- }
- }
- Stock StarShip::unload() {
- // Send into a probe
- Stock probe(m_def.stock);
- // Clear local stock
- m_def.stock.setZero();
- // Return probe to the planet
- return probe;
- }
- void StarShip::load(Stock stock) {
- // todo Check capacity
- m_def.stock = stock;
- }
- void StarShip::travelTo(int targetPlanet, b2Vec2 targetPos) {
- // Plot a course
- m_targetPlanet = targetPlanet;
- m_targetPos = targetPos;
- // Let's go
- m_arrived = false;
- m_angle = b2Angle(m_targetPos - m_def.pos, b2Vec2(0.0f, -1.0f));
- }
- bool StarShip::isArrived() const {
- return m_arrived;
- }
- int StarShip::getTargetPlanet() const {
- return m_targetPlanet;
- }
- const StarShipDef &StarShip::getDef() const {
- return m_def;
- }
|