Car.cpp 1.37 KB
Newer Older
Martin Kröning's avatar
Martin Kröning committed
1
#include "Car.hpp"
2
#include "MapAppAPIWrapper.hpp"
Martin Kröning's avatar
Martin Kröning committed
3
4
5
6
7
8
9
10
11
12
#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) {}

13
void Car::draw(MapAppAPIWrapper &mapApp, std::string_view laneName,
Martin Kröning's avatar
Martin Kröning committed
14
15
               const double laneLength, const double speedLimit) const {
  const auto positionOnLane = getDistanceOnLane() / laneLength;
16
17
  mapApp.addOrReplaceCar(getName(), laneName, positionOnLane,
                         getSpeed(speedLimit), fuel);
Martin Kröning's avatar
Martin Kröning committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
}

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;
}