-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelMap.cpp
164 lines (143 loc) · 2.96 KB
/
LevelMap.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "LevelMap.hpp"
#pragma optimize("", off)
// Pixel Class
// Public Interface
Pixel::Pixel(int x, int y)
: x(x), y(y), maxHeight(Height::GROUND) {}
Pixel::Pixel(int x, int y, std::unique_ptr<GameElement> startingElem)
: x(x), y(y), maxHeight(Height::GROUND)
{
stack.emplace_back(std::move(startingElem));
}
void Pixel::update()
{
for (auto& elem : stack)
{
elem->update();
}
}
void Pixel::render()
{
if (stack.size() > 0)
{
stack.back()->render();
}
}
void Pixel::addElem(std::unique_ptr<GameElement> elem)
{
insertByHeight(std::move(elem));
}
std::unique_ptr<GameElement> Pixel::removeBySymbol(const char symbol)
{
std::unique_ptr<GameElement> ret;
for (auto& elem : stack)
{
if (elem->getSymbol() == symbol)
{
ret = std::move(stack.back());
stack.pop_back();
return std::move(ret);
//return std::move(stack.back());
}
}
return nullptr;
}
int Pixel::getX() const
{
return x;
}
int Pixel::getY() const
{
return y;
}
// Private Interface
void Pixel::insertByHeight(std::unique_ptr<GameElement> elemPtr)
{
// update elemPtr height
elemPtr->setXY(x, y);
if (elemPtr->getHeight() >= maxHeight)
{
stack.push_back(std::move(elemPtr));
return;
}
auto it = stack.begin();
for (; it != stack.end(); ++it)
{
if (elemPtr->getHeight() > (*it)->getHeight())
{
stack.insert(it + 1, std::move(elemPtr));
return;
}
}
maxHeight = elemPtr->getHeight();
// it == stack.end() --> insert is equivalent to push_back()
stack.insert(it, std::move(elemPtr));
}
LevelMap::LevelMap()
: playableX(0), playableY(0)
{
for (int i = 0; i < MAP_HEIGHT; ++i)
{
for (int j = 0; j < MAP_WIDTH; ++j)
{
map[i][j] = std::make_unique<Pixel>(j, i);
}
}
// add a tile on every pixel
for (const auto& row : map)
{
for (const auto& pixel : row)
{
pixel->addElem(std::make_unique<Tile>(pixel->getX(), pixel->getY()));
}
}
// add player at (0, 0)
map[playableX][playableY]->addElem(std::make_unique<Player>(playableX, playableY));
}
void LevelMap::update()
{
for (const auto& row : map)
{
for (const auto& pixel : row)
{
pixel->update();
}
}
}
/*
Call the render() method of the element at the top of each Pixel's stack
*/
void LevelMap::render()
{
for (auto& row : map)
{
for (auto& pixel : row)
{
pixel->render();
}
}
}
bool LevelMap::moveBySymbol(int x, int y, char symbol, std::pair<int, int> direction)
{
if (x + direction.first >= 0 && x + direction.first < MAP_WIDTH &&
y + direction.second >= 0 && y + direction.second < MAP_HEIGHT)
{
std::unique_ptr<GameElement> elem = map[y][x]->removeBySymbol(symbol);
if (elem != nullptr) {
map[y + direction.second][x + direction.first]->addElem(std::move(elem));
return true;
}
}
return false;
}
#pragma optimize("", on)
bool LevelMap::movePlayable(const std::pair<int, int>& direction)
{
bool moved = moveBySymbol(playableX, playableY, Symbols::PLAYER, direction);
if (moved)
{
playableX += direction.first;
playableY += direction.second;
}
return moved;
}