forked from stefanhendriks/Dune-II---The-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.cpp
159 lines (133 loc) · 5.71 KB
/
Map.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
#include "Map.hpp"
#include "MapLoader.hpp"
#include <memory> /* std::unique_ptr */
#include <cmath> /* ceil */
#include "Unit.hpp"
#include <boost/cast.hpp>
Map::Map(sf::Texture &terrain, sf::Texture &shroud_edges, MessageSystem &messages) :
terrain(terrain),
shroudEdges(shroud_edges),
messages(messages)
{
maxWidth = MAX_WIDTH - 1;
maxHeight = MAX_HEIGHT - 1;
for (int x = 0; x < MAX_WIDTH; x++) {
for (int y = 0; y < MAX_HEIGHT; y++) {
int i = (y * MAX_WIDTH) + x;
cells[i].init(x, y);
}
}
this->messages.connect(Messages::premove, [this](const Message& message){
const PreMoveMessage* received = boost::polymorphic_downcast<const PreMoveMessage*>(&message);
sf::Vector2i mapPoint = toMapPoint(received->unit.getCenter());
if (getCell(mapPoint.x, mapPoint.y).terrainType != Terrain::Mountain)
received->unit.shouldMove = true;
});
this->messages.connect(Messages::unitMove, [this](const Message& message){
const MoveMessage* received = boost::polymorphic_downcast<const MoveMessage*>(&message);
removeShroud(received->unit.getCenter(), received->unit.getViewRange());
});
}
void Map::load(std::string file) {
MapLoader::load(file, this);
for (auto& cell : cells)
cell.setIndex(determineCellTile(cell));
updateShroud();
}
void Map::updateShroud()
{
for (auto& cell : cells)
cell.setShroudIndex(determineShroudEdge(cell));
}
void Map::prepare(const sf::Vector2f& topLeft) const
{
vertexArray.clear();
shroudArray.clear();
sf::Vector2f viewSize(800,600); //TODO: cheating - this should be passed by the window
sf::Vector2i mapCell = toMapPoint(topLeft);
// TODO: remove magic numbers
const int nofTilesInRow = (viewSize.x/Cell::TILE_SIZE) + 3;
const int nofRows = (viewSize.y/Cell::TILE_SIZE) + 3;
for (int x=-1; x < nofTilesInRow; ++x) {
for (int y=-1; y < nofRows; ++y) {
int index = ((y + mapCell.y) * MAX_WIDTH) + x + mapCell.x;
if ((index<0) || (index>=MAX_SIZE)) continue;
const auto& vertices = cells[index].getQuad();
vertexArray.insert(vertexArray.end(), vertices.begin(), vertices.end());
const auto& shroudVertices = cells[index].getShroudQuad();
shroudArray.insert(shroudArray.end(), shroudVertices.begin(), shroudVertices.end());
}
}
}
void Map::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(vertexArray.data(), vertexArray.size(), sf::Quads, &terrain);
}
void Map::drawShrouded(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(shroudArray.data(), shroudArray.size(), sf::Quads, &shroudEdges);
}
sf::Vector2i Map::toMapPoint(const sf::Vector2f &world_point) const{
sf::Vector2i result(world_point.x / Cell::TILE_SIZE, world_point.y / Cell::TILE_SIZE);
return result;
}
int Map::determineCellTile(Cell& c) {
bool cell_up = !c.shouldSmoothWithTerrainType(getCell(c.x, c.y-1));
bool cell_down = !c.shouldSmoothWithTerrainType(getCell(c.x, c.y+1));
bool cell_left = !c.shouldSmoothWithTerrainType(getCell(c.x-1, c.y));
bool cell_right = !c.shouldSmoothWithTerrainType(getCell(c.x+1, c.y));
return determineTerrainTile(cell_up, cell_down, cell_left, cell_right);
}
int Map::determineTerrainTile(bool cellUp, bool cellDown, bool cellLeft, bool cellRight) const {
if ( cellUp && cellDown && cellLeft && cellRight) return 0;
if ( cellUp && cellDown && !cellLeft && cellRight) return 1;
if ( cellUp && cellDown && cellLeft && !cellRight) return 2;
if (!cellUp && cellDown && cellLeft && cellRight) return 3;
if ( cellUp && !cellDown && cellLeft && cellRight) return 4;
if (!cellUp && cellDown && !cellLeft && cellRight) return 5;
if ( cellUp && !cellDown && cellLeft && !cellRight) return 6;
if (!cellUp && cellDown && cellLeft && !cellRight) return 7;
if ( cellUp && !cellDown && !cellLeft && cellRight) return 8;
if (!cellUp && !cellDown && !cellLeft && !cellRight) return 9;
if (!cellUp && !cellDown && cellLeft && cellRight) return 10;
if (!cellUp && !cellDown && cellLeft && !cellRight) return 11;
if (!cellUp && !cellDown && !cellLeft && cellRight) return 12;
if ( cellUp && !cellDown && !cellLeft && !cellRight) return 13;
if (!cellUp && cellDown && !cellLeft && !cellRight) return 14;
if ( cellUp && cellDown && !cellLeft && !cellRight) return 15;
return -1;
}
int Map::determineShroudEdge(Cell& cell) {
if (cell.shrouded) return 0;
bool cellUp = getCell(cell.x, cell.y-1).shrouded;
bool cellDown = getCell(cell.x, cell.y+1).shrouded;
bool cellLeft = getCell(cell.x-1, cell.y).shrouded;
bool cellRight = getCell(cell.x+1, cell.y).shrouded;
// Its harder then you think to make static consts for these 'magic values'.
if (!cellUp && !cellDown && !cellLeft && !cellRight) return -1;
if ( cellUp && !cellDown && cellLeft && !cellRight) return 1;
if ( cellUp && !cellDown && !cellLeft && !cellRight) return 2;
if ( cellUp && !cellDown && !cellLeft && cellRight) return 3;
if (!cellUp && !cellDown && !cellLeft && cellRight) return 4;
if (!cellUp && cellDown && !cellLeft && cellRight) return 5;
if (!cellUp && cellDown && !cellLeft && !cellRight) return 6;
if (!cellUp && cellDown && cellLeft && !cellRight) return 7;
if (!cellUp && !cellDown && cellLeft && !cellRight) return 8;
if ( cellUp && cellDown && cellLeft && !cellRight) return 9;
if ( cellUp && !cellDown && cellLeft && cellRight) return 10;
if ( cellUp && cellDown && !cellLeft && cellRight) return 11;
if (!cellUp && cellDown && cellLeft && cellRight) return 12;
return -1;
}
int Map::getMaxWidth() const
{
return maxWidth;
}
int Map::getMaxHeight() const
{
return maxHeight;
}
void Map::setBoundaries(int maxWidth, int maxHeight) {
this->maxWidth = maxWidth;
this->maxHeight = maxHeight;
}