-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.cpp
More file actions
129 lines (110 loc) · 3.38 KB
/
Copy pathTile.cpp
File metadata and controls
129 lines (110 loc) · 3.38 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Created by luniminex on 10/7/23.
//
#include "Tile.h"
#include <utility>
Tile::Tile() {
chosen_ = nullptr;
pos_ = {0,0};
state_ = State::WAITING;
}
Tile::Tile(std::vector<std::shared_ptr<TextureTile>> textureTiles) {
tileOptions_ = std::move(textureTiles);
chosen_ = nullptr;
pos_ = {0,0};
state_ = State::WAITING;
}
TextureTile &Tile::GetRules(int idx) {
return *tileOptions_.at(idx);
}
int Tile::GetEntropy() const {
return static_cast<int>(tileOptions_.size());
}
void Tile::FillTileOptions(std::vector<TextureTile> &uniqueTiles) {
for(auto& unique : uniqueTiles){
tileOptions_.push_back(std::make_shared<TextureTile>(unique));
}
}
bool Tile::Collapse() {
if(tileOptions_.empty()){
std::cout << GetPos().x << ", " << GetPos().y <<": Tried to collapse a tile that has no tile options left" << std::endl;
return false;
}
int chosen = GetRandomFromRange(0, static_cast<int>(tileOptions_.size()) - 1);
chosen_ = tileOptions_.at(chosen);
tileOptions_.clear();
return true;
}
TextureTile &Tile::GetChosen() {
return *chosen_;
}
void Tile::SetPos(SDL_Point pos) {
pos_ = pos;
}
SDL_Point Tile::GetPos() const {
return pos_;
}
void Tile::RemoveOption(int idx) {
if(idx >= tileOptions_.size()){
std::cout << "Trying to remove an option from a tile at index that is out of bounds" << std::endl;
return;
}
tileOptions_.erase(tileOptions_.begin() + idx);
}
void Tile::CheckRules(Tile &other, Direction dir) {
int rulesPerSide;
if(!tileOptions_.empty()) {
rulesPerSide = static_cast<int>(GetRules(0).rules.size()) / TILESIDES;
}
else if(chosen_ != nullptr){
rulesPerSide = static_cast<int>(chosen_->rules.size()) / TILESIDES;
}
TextureTile otherRules;
int currentOffset = static_cast<int>(dir);
int mainOffset = currentOffset * rulesPerSide;
int otherOffset = ((currentOffset + TILESIDES / 2) % TILESIDES) * rulesPerSide;
//goes through all tile options
bool removed;
for(int i = 0; i < other.GetEntropy(); i++) {
otherRules = other.GetRules(i);
removed = false;
//goes through all rules on side
//PrintChosenRules(mainOffset, rulesPerSide);
//other.PrintRules(otherOffset, rulesPerSide, i);
for (int j = 0; j < rulesPerSide; j++) {
int mainIndex = mainOffset + j;
int otherIndex = otherOffset + j;
if (chosen_->rules.at(mainIndex) != otherRules.rules.at(otherIndex)){
other.RemoveOption(i);
//std::cout << "<- removed" << std::endl;
removed = true;
break;
}
}
//std::cout << std::endl;
//erase makes vector smaller, so it needs to check same position
//that has different element in it
if(removed){
i--;
}
}
// std::cout << "checked rules " << std::endl;
}
bool Tile::IsCollapsed() const {
if(chosen_ == nullptr)
return false;
return true;
}
State Tile::GetState() const {
return state_;
}
void Tile::SetState(State state) {
state_ = state;
}
int Tile::GetRandomFromRange(int bot, int top) {
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> uniDist =
std::uniform_int_distribution<int>(
bot, top);
return uniDist(rng);
}