-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.cpp
140 lines (128 loc) · 3.59 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
#include "Map.h"
#include "StateManager.h"
#include <math.h>
Map::Map(SharedContext* sharedContext, StateBase* stateBase) :
m_sharedContext(sharedContext),
m_defaultTile(sharedContext),
m_maxMapSize(32, 32),
m_tileCount(0),
m_tileSetCount(0),
m_mapGravity(512.f),
m_loadNextMap(false),
m_currentState(stateBase)
{
m_sharedContext->m_gameMap = this;
//loadTiles("tiles.cfg");
}
Map::~Map()
{
purgeMap();
purgeTileSet();
m_sharedContext->m_gameMap = nullptr;
}
Tile * Map::getTile(unsigned int x, unsigned int y)
{
auto tile = m_tileMap.find(convertCoords(x, y));
return (tile != m_tileMap.end() ? tile->second : nullptr);
}
unsigned int Map::convertCoords(unsigned int x, unsigned int y)
{
return (x * m_maxMapSize.x) + y;
}
void Map::loadTiles(const std::string & path)
{
std::ifstream file;
file.open(Utils::GetWorkingDirectory() + path);
if (file.is_open())
{
std::string line;
while (std::getline(file, line))
{
if (line[0] == '|') { continue; }
std::stringstream keyStream(line);
int tileID;
keyStream >> tileID;
if (tileID < 0) { continue; }
TileInfo* tile = new TileInfo(m_sharedContext, "TileSheet", tileID); //Create new tile
keyStream >> tile->m_name >> tile->m_friction.x >> tile->m_friction.y >> tile->m_deadly; //Assign properties from file
if (!m_tileSet.emplace(tileID, tile).second)
{
//Duplicate tile detected
std::cerr << "Duplicate tile detected: " << tile->m_name << "\n";
delete tile;
tile = nullptr;
}
}
file.close();
}
}
void Map::purgeMap()
{
m_tileCount = 0;
for (auto &i : m_tileMap) {
delete i.second;
}
m_tileMap.clear();
//m_sharedContext->entity->purge();
if (m_backgroundTexture != "")
{
m_sharedContext->m_textureManager->releaseResource(m_backgroundTexture);
m_backgroundTexture = "";
}
}
void Map::purgeTileSet()
{
for (auto &i : m_tileSet) {
delete i.second;
}
m_tileSet.clear();
m_tileSetCount = 0;
}
void Map::update(const float timer)
{
//If there is another map to load
if (m_loadNextMap)
{
//Get rid of the current map
purgeMap();
m_loadNextMap = false;
if (m_nextMap != "") {
loadMap("media/maps/" + m_nextMap);
}
else {
m_currentState->getStateManager()->switchTo(GameState::MainMenu);
}
}
//Update current viewpoint of map
else
{
//Set position for the maps background - view
sf::FloatRect viewSpace = m_sharedContext->m_window->getViewSpace();
m_background.setPosition(viewSpace.left, viewSpace.top);
}
}
//Not sure whats going on here
void Map::draw()
{
sf::RenderWindow* window = &m_sharedContext->m_window->getWindow();
window->draw(m_background); //Draw the background
//Set the view to draw - Features culling
sf::FloatRect viewSpace = m_sharedContext->m_window->getViewSpace();
sf::Vector2i tileBegin(floor(viewSpace.left / m_tileSheet.m_tileSize), floor(viewSpace.top / m_tileSheet.m_tileSize));
sf::Vector2i tileEnd((ceil(viewSpace.left + viewSpace.width) / m_tileSheet.m_tileSize), (ceil(viewSpace.top + viewSpace.height) / m_tileSheet.m_tileSize));
//Draw in the few selected tiles from the map that appear within the viewspace
unsigned int count = 0;
for (int x = tileBegin.x; x <= tileEnd.x; x++)
{
for (int y = tileBegin.y; y <= tileEnd.y; y++)
{
if (x < 0 || x < 0) { continue; }
Tile* tile = getTile(x, y);
if (!tile) { continue; }
sf::Sprite* sprite = &tile->m_properties->m_sprite;
sprite->setPosition(x * m_tileSheet.m_tileSize, y * m_tileSheet.m_tileSize);
window->draw(*sprite);
count++;
}
}
}