Intal version with working trainoverlord

This commit is contained in:
2022-03-21 21:23:22 +01:00
parent a1f9fa172b
commit 872cc04a54
23 changed files with 992 additions and 46 deletions

65
src/trainOverlord/block.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef BLOCK_H
#define BLOCK_H
#include <vector>
#include <memory>
#include <QObject>
#include <QRandomGenerator>
#include <QJsonObject>
#include "blockborder.h"
#include "train.h"
class BlockBorder;
class Layout;
class Block: public QObject
{
Q_OBJECT
protected:
static constexpr int WAIT_TYPE_BLOCK = 0;
static constexpr int WAIT_TYPE_TRAVERSE = 1;
struct TrainWait
{
int type;
int direction;
std::weak_ptr<Train> train;
std::weak_ptr<Block> targetBlock;
std::weak_ptr<BlockBorder> border;
};
std::vector< std::shared_ptr<BlockBorder> > borders_;
std::vector< std::weak_ptr<Train> > trains_;
std::vector<TrainWait> waits_;
uint32_t id_;
protected slots:
void unsuspendedTrain(uint32_t id, int direction);
protected:
void checkWaits(bool blocked = false);
void updateBorders();
public:
Block(uint32_t id = QRandomGenerator::global()->generate());
void addBorder(std::shared_ptr<BlockBorder> border);
bool blocked();
bool ownsTrain(std::weak_ptr<Train> train);
bool ownsBorder(std::shared_ptr<BlockBorder> border);
std::vector< std::shared_ptr<BlockBorder> > getBorders(){return borders_;}
uint32_t id(){return id_;}
void removeTrain(std::weak_ptr<Train> train);
void store(QJsonObject& json);
void load(const QJsonObject& json);
void populate(const QJsonObject& json, Layout* layout);
public slots:
bool pushTrain(std::weak_ptr<Train> train);
void addTrain(std::weak_ptr<Train> train);
void trainArrivedAtBorder(std::weak_ptr<BlockBorder> border, std::weak_ptr<Train> train);
signals:
void blockedChanged(bool blocked);
void trainAddedToBlock(int blockId, std::weak_ptr<Train> train);
};
#endif // BLOCK_H