Lane.cpp 2.56 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "Lane.hpp"
#include "Bicycle.hpp"
#include "Car.hpp"
#include "Junction.hpp"
#include "Vehicle.hpp"
#include <algorithm>
#include <memory>
#include <utility>
class MapAppABIWrapper;

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Lane::Lane(Lane &&other) noexcept = default;

Lane::Lane(std::string name, std::string oppositeLaneName, const double length,
           const double speedLimit, const bool noPassing,
           std::weak_ptr<Junction> destinationJunction)
    : Named(std::move(name)), length(length),
      destinationJunction(std::move(destinationJunction)),
      oppositeLaneName(std::move(oppositeLaneName)), speedLimit(speedLimit),
      noPassing(noPassing) {}

void Lane::drawVehicles(MapAppABIWrapper &mapApp) const {
  for (auto &&fahrzeug : vehicles) {
    fahrzeug->draw(mapApp, getName(), length, speedLimit);
  }
}

void Lane::processVehicles(const double time) {
  const auto furthestFirstComparator = [](const auto &vehicleA,
                                          const auto &vehicleB) {
    return vehicleA->getDistanceOnLane() > vehicleB->getDistanceOnLane();
  };
  std::sort(vehicles.begin(), vehicles.end(), furthestFirstComparator);
  double maximumPosition = length;
  for (auto &&fahrzeug : vehicles) {
    fahrzeug->process(time, maximumPosition, speedLimit);
    if (noPassing && fahrzeug->getSpeed(speedLimit) != 0) {
      maximumPosition = fahrzeug->getDistanceOnLane();
    }
  }
  std::sort(vehicles.begin(), vehicles.end(), furthestFirstComparator);
  while (!vehicles.empty() && length <= vehicles.at(0)->getDistanceOnLane()) {
    destinationJunction.lock()->acceptVehicle(std::move(vehicles.front()),
                                              oppositeLaneName);
    vehicles.erase(vehicles.begin());
  }
}

void Lane::acceptVehicle(std::unique_ptr<Vehicle> vehicle) {
  vehicles.push_back(std::move(vehicle));
}

void Lane::createCar(std::string name, const double maximumVelocity,
                     const double fuelConsumption, const double fuelCapacity,
                     const double timeOfStart, const double time) {
  vehicles.push_back(std::make_unique<Car>(std::move(name), maximumVelocity,
                                           timeOfStart, fuelConsumption,
                                           fuelCapacity, time));
}

void Lane::createBicycle(std::string name, const double maximumVelocity,
                         const double timeOfStart, const double time) {
  vehicles.push_back(std::make_unique<Bicycle>(std::move(name), maximumVelocity,
                                               timeOfStart, time));
}