Car.cpp 1.39 KB
Newer Older
Martin Kröning's avatar
Martin Kröning committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "Car.hpp"
#include "MapAppABIWrapper.hpp"
#include <algorithm>
#include <utility>

Car::Car(std::string name, const double topSpeed, const double timeOfStart,
         const double fuelConsumption, const double fuelCapacity,
         const double time)
    : Vehicle(std::move(name), topSpeed, timeOfStart, time),
      fuelConsumption(fuelConsumption), fuelCapacity(fuelCapacity),
      fuel(fuelCapacity / 2) {}

void Car::draw(MapAppABIWrapper &mapApp, const std::string &laneName,
               const double laneLength, const double speedLimit) const {
  const auto positionOnLane = getDistanceOnLane() / laneLength;
  mapApp.addOrReplaceVehicle(getName(), laneName, positionOnLane,
                             getSpeed(speedLimit), fuel, 1, 0, 0);
}

double Car::getSpeed(const double speedLimit) const {
  const auto potentialSpeed = std::min(speedLimit, getTopSpeed());
  return isParking() || fuel <= 0 ? 0 : potentialSpeed;
}

void Car::process(const double time, const double maximumDistanceOnLane,
                  const double speedLimit) {
  const auto oldDistance = getGesamtStrecke();
  Vehicle::process(time, maximumDistanceOnLane, speedLimit);
  fuel -= fuelConsumption * (getGesamtStrecke() - oldDistance) / 100;
}

double Car::refuel(const double availableFuel) {
  const auto refueledAmount = std::min(availableFuel, fuelCapacity - fuel);
  fuel += refueledAmount;
  return refueledAmount;
}