-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
237 lines (206 loc) · 5.1 KB
/
map.cpp
File metadata and controls
237 lines (206 loc) · 5.1 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "map.hpp"
using namespace std;
Map::Map() {
dirty = true;
}
Map::~Map() {
while (!tileDict.empty()) {
tileDict.pop_back();
}
while (!roomDict.empty()) {
roomDict.pop_back();
}
while (!matDict.empty()) {
matDict.pop_back();
}
delete[] tiles;
delete[] rooms;
delete[] tasked;
delete[] colorize;
delete[] walkable;
}
void Map::update(float dt) {
}
void Map::render(sf::RenderTarget *screen) {
if (dirty)
reRender();
screen->draw(mapSpr);
}
// Redraw the map texture
void Map::reRender() {
mapTex.clear();
// Draw each tile
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Draw the tile onto this texture
Tile* thisTile = tileDict.at(tiles[x][y]);
if (tasked[x][y] && colorize[x][y] == COLOR_NONE) {
colorize[x][y] = COLOR_TASKED;
}
if (thisTile->lastColor != colorize[x][y]) {
thisTile->lastColor = colorize[x][y];
thisTile->spr.setColor(sf::Color(colorDict.at(colorize[x][y])));
}
thisTile->render(&mapTex, (x*2 + y%2)*HALF_TILE_WIDTH, y*HALF_TILE_HEIGHT);
}
}
mapTex.display(); // Update the texture
mapSpr.setTexture(mapTex.getTexture());
dirty = false;
}
void Map::init(int width, int height) {
// Set up the tiles
this->width = width;
this->height = height;
tiles = new int*[width];
colorize = new int*[width];
rooms = new int*[width];
tasked = new bool*[width];
walkable = new bool*[width];
for (int x = 0; x < width; x++) {
tiles[x] = new int[height];
colorize[x] = new int[height];
rooms[x] = new int[height];
tasked[x] = new bool[height];
walkable[x] = new bool[height];
for (int y = 0; y < height; y++) {
tiles[x][y] = 1;
colorize[x][y] = 0;
rooms[x][y] = 0;
tasked[x][y] = false;
walkable[x][y] = false;
}
}
mapTex.create(HALF_TILE_WIDTH*(2*width+1), HALF_TILE_HEIGHT*(height)+WALL_HEIGHT);
mapSpr.setTexture(mapTex.getTexture());
for (int i = 0; i < NUM_DEFAULT_COLORS; i++) {
colorDict.push_back(DEFAULT_TILE_COLORS[i]);
}
dirty = true;
}
Tile* Map::getTile(int x, int y) {
return tileDict.at(tiles[x][y]);
}
void Map::addTile(Tile* tileToAdd) {
tileDict.push_back(tileToAdd);
}
void Map::addColor(sf::Color colorToAdd) {
colorDict.push_back(colorToAdd);
}
void Map::addRoom(Room* roomToAdd) {
roomDict.push_back(roomToAdd);
}
void Map::addMaterial(Material* matToAdd) {
matDict.push_back(matToAdd);
}
bool Map::inBounds(int x, int y) {
return x >= 0 && y >= 0 && x < width && y < height;
}
void Map::setTile(int x, int y, Tile* tileToAdd) {
// Look for this tile in the tileDict
for (unsigned int i = 0; i < tileDict.size(); i++) {
if (tileDict.at(i) == tileToAdd) {
setTile(x,y,i);
return;
}
}
// Not in the dict, add it and go
addTile(tileToAdd);
setTile(x,y,tileDict.size()-1);
}
void Map::setTile(int x, int y, int tileToAdd) {
if (!inBounds(x,y))
return;
dirty = true;
tiles[x][y] = tileToAdd;
walkable[x][y] = tileDict.at(tileToAdd)->hasTag(IS_WALKABLE);
}
void Map::setColor(int x, int y, sf::Color colorToAdd) {
// Look for this color in the colorDict
for (unsigned int i = 0; i < colorDict.size(); i++) {
if (colorDict.at(i) == colorToAdd) {
setColor(x,y,i-1);
return;
}
}
// Not in the dict, add it and go
addColor(colorToAdd);
setColor(x,y,colorDict.size()-1);
}
void Map::setColor(int x, int y, int colorToAdd) {
if (!inBounds(x,y))
return;
dirty |= colorize[x][y] != colorToAdd; // Only dirty if it's a change
colorize[x][y] = colorToAdd;
}
void Map::setRoom(int x, int y, Room* roomToAdd) {
int roomID = getRoomID(roomToAdd);
if (roomID > -2)
setRoom(x, y, roomID);
// Not in the dict, add it and go
cerr << "Room set without being in the dictionary" << endl;
addRoom(roomToAdd);
setRoom(x,y,roomDict.size()-1);
}
void Map::setRoom(int x, int y, int roomToAdd) {
if (!inBounds(x,y))
return;
rooms[x][y] = roomToAdd;
}
void Map::setRoom(int x0, int y0, int x1, int y1, int roomToAdd) {
int start_x = x0 < x1 ? x0 : x1;
int end_x = x0 < x1 ? x1 : x0;
int start_y = y0 < y1 ? y0 : y1;
int end_y = y0 < y1 ? y1 : y0;
for (int x = start_x; x <= end_x; x++) {
for (int y = start_y; y <= end_y; y++) {
setRoom(x,y,roomToAdd);
}
}
}
void Map::setRoom(int x0, int y0, int x1, int y1, Room* roomToAdd) {
setRoom(x0, y0, x1, y1, getRoomID(roomToAdd));
}
int Map::getRoomID(Room* room) {
for (unsigned int i = 0; i < roomDict.size(); i++) {
if (roomDict.at(i) == room) {
return i-1;
}
}
return -2;
}
int Map::getTileID(Tile* tile) {
for (unsigned int i = 0; i < tileDict.size(); i++) {
if (tileDict.at(i) == tile) {
return i;
}
}
return -1;
}
void Map::setTasked(int x, int y, bool tasked) {
if (!inBounds(x,y))
return;
this->tasked[x][y] = tasked;
dirty = true;
}
bool Map::getTasked(int x, int y) {
return tasked[x][y];
}
void Map::clearColor(int x, int y) {
setColor(x,y,0);
}
bool Map::isWalkable(int x, int y) {
return (inBounds(x, y) && walkable[x][y]);
}
void Map::resetWalkableStatus(int x, int y) {
if (!inBounds(x,y)) {
return;
}
walkable[x][y] = tileDict.at(tiles[x][y])->hasTag(IS_WALKABLE);
}
void Map::setWalkableStatus(int x, int y, bool status) {
if (!inBounds(x,y)) {
return;
}
walkable[x][y] = status;
}