-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
73 lines (63 loc) · 2 KB
/
Map.cpp
File metadata and controls
73 lines (63 loc) · 2 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
#include "Map.h"
#include "TileFactory.h"
#include "surface.h"
using namespace Tmpl8;
Map::Map(int rows, int colls, Difficulty difficulty, Surface& screen) :
rows(rows), colls(colls), difficulty(difficulty), screen(screen)
{
width = colls + 2 * border_width;
map.assign(rows * width, tileFactory.getTile(Tile::Terrains_t::Snow, Tile::Objects_t::None));
}
Map::~Map()
{
delete player;
for (size_t i = 0; i < map.size(); i++)
map.pop_back();
map.erase(map.begin());
}
void Map::DrawBackground()
{
for (int r = 0; r < rows; r++) {
for (int c = 0; c < colls + 2 * border_width; c++) {
int x = screen.GetWidth() / 2 - (colls / 2 - c + border_width) * TILE;
map[((first_row + r) % rows * width) + c].DrawBackground(x, (int)(r * TILE - current_position), screen);
}
}
}
void Map::DrawForeground()
{
for (int r = 0; r < rows; r++) {
for (int c = 0; c < colls + 2 * border_width; c++) {
int x = screen.GetWidth() / 2 - (colls / 2 - c + border_width) * TILE;
map[((first_row + r) % rows * width) + c].DrawForegound(x, (int)(r * TILE - current_position), screen);
}
}
}
void Map::DrawHearts()
{
Sprite heart = Sprite(new Surface("assets/heart.png"), 1);
for (int i = 0; i < player->health; i++) {
heart.Draw(&screen, heart.GetWidth() * i + 16, 16);
}
}
void Map::AddBorder()
{
Tile* row = &map[first_row * width];
// Left border
for (int i = 0; i < border_width; i++) {
int r = rand() % 4;
if (r == 0) row[i] = tileFactory.getTile(Tile::Terrains_t::Snow, Tile::Objects_t::None);
else row[i] = tileFactory.getTile(Tile::Terrains_t::Snow, Tile::Objects_t(r + 11)); // 12, 13, 14
}
// Right border
for (int i = 0; i < border_width; i++) {
int r = rand() % 4;
if (r == 0) row[border_width + colls + i] = tileFactory.getTile(Tile::Terrains_t::Snow, Tile::Objects_t::None);
else row[border_width + colls + i] = tileFactory.getTile(Tile::Terrains_t::Snow, Tile::Objects_t(r + 11)); // 12, 13, 14
}
}
void Map::ReduceCurrentPosition() {
while (current_position >= TILE) {
current_position -= TILE;
}
}