76 lines
1.8 KiB
C++

#ifndef BLOCK_H
#define BLOCK_H
#include <vector>
#include <memory>
#include <QObject>
#include <QRandomGenerator>
#include <QJsonObject>
#include "blockborder.h"
#include "train.h"
#include "overlorditemstore.h"
class BlockBorder;
class Layout;
class Block: public QObject
{
Q_OBJECT
protected:
static constexpr int WAIT_TYPE_BLOCK = 0;
static constexpr int WAIT_TYPE_TIMED_TRAVERSE = 1;
struct Wait
{
int type;
int direction;
NfcUid tag;
std::weak_ptr<Train> train;
std::weak_ptr<Block> targetBlock;
std::weak_ptr<BlockBorder> border;
};
std::vector< std::shared_ptr<BlockBorder> > borders_;
std::vector<NfcUid> tags_;
std::vector<Wait> waits_;
uint32_t id_;
OverlordItemStore* items_;
protected slots:
void unsuspendedTrain(uint32_t id, int direction);
void removeTraverse(std::shared_ptr<Train> train);
protected:
void checkWaits(bool blocked = false);
void addTraverseWait(const NfcUid& tag);
void addBlockWait(const NfcUid& tag, std::shared_ptr<Block> block, std::shared_ptr<BlockBorder> border);
void removeTag(const NfcUid& tag);
bool tagIsOfOwnedTrain(const NfcUid& tag);
public:
Block(OverlordItemStore* items, uint32_t id = QRandomGenerator::global()->generate());
uint32_t id(){return id_;}
void addBorder(std::shared_ptr<BlockBorder> border);
std::vector< std::shared_ptr<BlockBorder> > getBorders(){return borders_;}
bool ownsBorder(std::shared_ptr<BlockBorder> border);
bool pushTag(const NfcUid& tag);
void addTag(const NfcUid& tag);
void purgeTag(const NfcUid& tag);
bool ownsTag(const NfcUid& tag);
bool blocked();
void store(QJsonObject& json);
void load(const QJsonObject& json);
void populate(const QJsonObject& json, Layout* layout);
public slots:
void tagArrivedAtBorder(NfcUid tag, std::weak_ptr<BlockBorder> border);
signals:
void blockedChanged(bool blocked);
};
#endif // BLOCK_H