Terminus
train.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "train.h"
4 
5 #include <assert.h>
6 
9 
10 #include <QDebug>
11 
12 namespace terminus
13 {
14 
15 template<typename WagonType>
17 {
18  insertWagon<WagonType>(-1);
19 }
20 
21 template<typename WagonType>
22 void Train::insertWagon(int targetPos)
23 {
24  assert(targetPos < static_cast<int>(m_wagons.size()));
25  assert(targetPos >= -1);
26 
27  auto wagonRaw = new WagonType(m_world, this);
28 
29  if((dynamic_cast<EngineWagon*>(wagonRaw) != nullptr))
30  {
31  assert(!m_hasEngine);
32  m_hasEngine = true;
33  }
34 
35  auto newWagon = std::unique_ptr<WagonType>(wagonRaw);
36 
37  if(targetPos == -1)
38  {
39  m_wagons.push_back(std::move(newWagon));
40  }
41  else
42  {
43  m_wagons.insert(m_wagons.begin() + targetPos, std::move(newWagon));
44  }
45 
47 }
48 
49 }
void addWagon()
Adds a wagon of the given WagonType to the end of the train.
Definition: train.hpp:16
bool m_hasEngine
Every train needs exactly one engine.
Definition: train.h:131
void calculateWagonOffset()
Calculates offset for every wagon relative to train head.
Definition: train.cpp:159
std::vector< std::unique_ptr< AbstractWagon > > m_wagons
The vector containing all wagons.
Definition: train.h:129
void insertWagon(int targetPos)
Inserts a wagon of the given WagonType at the given position.
Definition: train.hpp:22
Definition: eventhandler.cpp:18
World & m_world
Definition: abstractgraphicsobject.h:253
WagonType
Definition: abstractwagon.h:13