-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_map.cpp
More file actions
75 lines (61 loc) · 1.81 KB
/
game_map.cpp
File metadata and controls
75 lines (61 loc) · 1.81 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
#include "game_map.h"
GameMap::GameMap(int map_width, int map_height) {
if (map_width < MIN_MAP_SIZE)
width = MIN_MAP_SIZE;
else if (map_width > MAX_MAP_SIZE)
width = MAX_MAP_SIZE;
else
width = map_width;
if (map_height < MIN_MAP_SIZE)
height = MIN_MAP_SIZE;
else if (map_height > MAX_MAP_SIZE)
height = MAX_MAP_SIZE;
else
height = map_height;
grid.resize(height, std::vector<MapCell>(width));
}
GameMap::GameMap(const GameMap& other)
: width(other.width), height(other.height) {
grid.resize(height);
for (int i = 0; i < height; ++i) {
grid[i] = other.grid[i];
}
}
GameMap::GameMap(GameMap&& other)
: width(other.width), height(other.height), grid(std::move(other.grid)) {
other.width = 0;
other.height = 0;
}
GameMap& GameMap::operator=(const GameMap& other) {
if (this != &other) {
width = other.width;
height = other.height;
grid.resize(height);
for (int i = 0; i < height; ++i) {
grid[i] = other.grid[i];
}
}
return *this;
}
GameMap& GameMap::operator=(GameMap&& other) {
if (this != &other) {
width = other.width;
height = other.height;
grid = std::move(other.grid);
other.width = 0;
other.height = 0;
}
return *this;
}
bool GameMap::isPositionValid(const Position& pos) const {
return pos.getX() >= 0 && pos.getX() < width &&
pos.getY() >= 0 && pos.getY() < height;
}
MapCell& GameMap::getCell(const Position& pos) {
return grid[pos.getY()][pos.getX()];
}
const MapCell& GameMap::getCell(const Position& pos) const {
return grid[pos.getY()][pos.getX()];
}
int GameMap::getWidth() const { return width; }
int GameMap::getHeight() const { return height; }