-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.cpp
More file actions
88 lines (74 loc) · 2.36 KB
/
world.cpp
File metadata and controls
88 lines (74 loc) · 2.36 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
#include "world.h"
World::World(){
cells[std::make_pair(0,0)] = new RoadLocation(0,0,RoadLocation::NorthSouth);
}
World::~World(){
}
void World::drawMap(std::pair<int,int> loc){
drawMap(loc.first, loc.second);
}
// Input: center world location
void World::drawMap(int x, int y){
int mapw, maph;
getmaxyx(Display::map_win, maph, mapw);
int left = x - mapw/2;
int top = y + maph/2;
WorldLocation* currentLoc = getLocationAt(x,y);
Logger::debug("drawing centered at (%d,%d)\n",x,y);
wclear(Display::map_win);
// Generate cells adjacent to (x,y)
//
auto test = cells.find(std::make_pair(x-1,y));
if (test == cells.end()){
Logger::debug("generating (%d,%d)\n",x-1,y);
cells[std::make_pair(x-1,y)] = currentLoc->connect(Util::West);
}
test = cells.find(std::make_pair(x,y+1));
if (test == cells.end()){
Logger::debug("generating (%d,%d)\n",x,y+1);
cells[std::make_pair(x,y+1)] = currentLoc->connect(Util::North);
}
test = cells.find(std::make_pair(x,y-1));
if (test == cells.end()){
Logger::debug("generating (%d,%d)\n",x,y-1);
cells[std::make_pair(x,y-1)] = currentLoc->connect(Util::South);
}
test = cells.find(std::make_pair(x+1,y));
if (test == cells.end()){
Logger::debug("generating (%d,%d)\n",x+1,y);
cells[std::make_pair(x+1,y)] = currentLoc->connect(Util::East);
}
//Logger::debug("checking from cell (%d, %d) to (%d, %d)\n", x, y, x+width, y+height);
int row = 0;
int col = 0;
// Draw cells
for(int i = top; i > y - maph; i--){
col = 0;
for(int j = left; j < left + mapw; j++){
//Logger::debug("checking cell (%d, %d)\n", i, j);
auto search = cells.find(std::make_pair(j,i));
if(i == y && j == x){
mvwaddstr(Display::map_win, row, col, "\u25A3");
} else if(search != cells.end()){
Logger::debug("drawing cell (%d, %d)\n", j, i);
search->second->render(row, col);
}
col++;
}
row++;
}
wrefresh(Display::map_win);
}
WorldLocation* World::getLocationAt(std::pair<int,int> l){
return getLocationAt(l.first, l.second);
}
WorldLocation* World::getLocationAt(int x, int y){
auto search = cells.find(std::make_pair(x,y));
if(search != cells.end()){
return search->second;
} else {
// Since we only move one at a time, and all adjacent
// get generated, should be fine for now...
throw "Location invalid!";
}
};