-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.h
133 lines (100 loc) · 3.17 KB
/
map.h
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
#ifndef MAP_H
#define MAP_H
#include "player.h"
#define GRID_SIZE 1500
#define RENDER_DISTANCE 1
class Map {
private:
public:
static void createRect (Rectangle obj, std::map<int, std::vector<Rectangle>>& mObjects);
static std::map<int, std::vector<Rectangle>> serializeRead (std::string fn, Player& player);
static void serializeWrite(std::map<int, std::vector<Rectangle>> mObjects, Player& player, std::string fn);
static void listMaps(std::string path, std::vector<std::string>& stringvec);
static void drawSectors(int norm_x);
};
bool findRectInVec(Rectangle rect, std::vector<Rectangle> vec){
for(int i = 0; i < vec.size(); i ++ ){
if(vec[i].x == rect.x &&
vec[i].width == rect.width &&
vec[i].y == rect.y &&
vec[i].height == rect.height) {
return true;
}
}
return false;
}
void Map::createRect (Rectangle obj, std::map<int, std::vector<Rectangle>>& mObjects){
int normX = obj.x / GRID_SIZE;
int normBigX = (obj.x + obj.width) / GRID_SIZE;
while(normBigX >= normX){
mObjects[normBigX].push_back(obj);
normBigX --;
}
}
std::map<int, std::vector<Rectangle>> Map::serializeRead(std::string fn, Player& player){
std::map<int, std::vector<Rectangle>> mObjects;
std::cout << "LOADING.. " << fn << std::endl;
std::ifstream infile(fn);
std::string type;
int x, y, w, h;
while(infile >> type >> x >> y >> w >> h){
if(type == "PLAYER"){
player.setX(x);
player.setY(y);
}
else if(type == "OBJECT"){
Rectangle rect = {x, y, w, h};
int normX = rect.x / GRID_SIZE;
int normBigX = (rect.x + rect.width) / GRID_SIZE;
while(normBigX >= normX){
mObjects[normBigX].push_back(rect);
normBigX --;
}
}
}
return mObjects;
}
void Map::serializeWrite(std::map<int, std::vector<Rectangle>> mObjects, Player& player, std::string fn){
std::cout << "SAVING.. " << fn << std::endl;
std::ofstream outfile;
outfile.open (fn, std::ofstream::out | std::ofstream::trunc);
//std::vector<Rectangle> objects = mObjects[player.getX() / GRID_SIZE];
std::vector<Rectangle> objects;
//in the future check if object is already in
for (const auto& elem : mObjects) {
std::vector<Rectangle> vec = elem.second;
for(int i = 0; i < vec.size(); i ++ ){
if(!findRectInVec(vec[i], objects))
objects.push_back(vec[i]);
}
}
outfile << "PLAYER" << " " << player.getX() << " " << player.getY() << " " << 0 << " " << 0 << std::endl;
for( int i = 0; i < objects.size(); i ++ ){
outfile << "OBJECT" << " " << objects[i].x << " " << objects[i].y << " " << objects[i].width << " " << objects[i].height << std::endl;
}
outfile.close();
}
void Map::listMaps(std::string path, std::vector<std::string>& stringvec){
for (const auto & entry : std::filesystem::directory_iterator(path)){
std::string temp = entry.path();
if(temp.find(".map") != std::string::npos){
stringvec.push_back(entry.path());
}
}
}
void Map::drawSectors(int norm_x){
for(int i = 0; i < 100; i ++ ){
Rectangle line;
line.x = i * GRID_SIZE;
line.y = -1000;
line.width = 5;
line.height = 2000;
if(norm_x == i || norm_x == i - 1){
//active
DrawRectangleRec(line, DARKGREEN);
}else{
DrawRectangleRec(line, GREEN);
}
}
}
#endif