forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_layout.cpp
106 lines (86 loc) · 3.05 KB
/
map_layout.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
#include "stdafx.h"
#include "map_layout.h"
MapLayout::MapLayout(
int screenW, int screenH,
int leftM, int topM, int rightM, int bottomM,
vector<ViewLayer> l) :screenWidth(screenW), screenHeight(screenH),
leftMargin(leftM), topMargin(topM), rightMargin(rightM), bottomMargin(bottomM), layers(l) {}
Rectangle MapLayout::getBounds() {
return Rectangle(leftMargin, topMargin, screenWidth - rightMargin, screenHeight - bottomMargin);
}
void MapLayout::updateScreenSize(int width, int height) {
screenWidth = width;
screenHeight = height;
}
vector<ViewLayer> MapLayout::getLayers() const {
return layers;
}
class GridLayout : public MapLayout {
public:
GridLayout(
int screenW, int screenH,
int sW, int sH,
int leftM, int topM,
int rightM, int bottomM,
vector<ViewLayer> layers)
: MapLayout(screenW, screenH, leftM - 2 * sW, topM - 2 * sH, rightM - 2 * sW, bottomM - 2 * sH, layers),
squareW(sW), squareH(sH) {}
virtual double squareHeight() override {
return squareH;
}
virtual double squareWidth() override {
return squareW;
}
virtual void increaseSize() override {
++squareW;
++squareH;
}
virtual void decreaseSize() {
--squareW;
--squareH;
}
virtual Vec2 projectOnScreen(Vec2 mapPos) override {
return getBounds().middle() + (mapPos).mult(Vec2(squareW, squareH)) - center;
}
virtual Vec2 projectOnMap(Vec2 screenPos) override {
Vec2 pos = (screenPos + center - getBounds().middle()).div(Vec2(squareW, squareH));
return pos;
}
virtual void updatePlayerPos(Vec2 pos) override {
center = pos;
}
virtual Rectangle getAllTiles(Rectangle bounds) override {
vector<Vec2> ret;
Rectangle grid(getBounds().getW() / squareW, getBounds().getH() / squareH);
Vec2 offset = center.div(Vec2(squareW, squareH)) - grid.middle();
return Rectangle(max(bounds.getPX(), grid.getPX() + offset.x), max(bounds.getPY(), grid.getPY() + offset.y),
min(bounds.getKX(), grid.getKX() + offset.x), min(bounds.getKY(), grid.getKY() + offset.y));
}
private:
Vec2 center;
int squareW;
int squareH;
};
MapLayout* MapLayout::gridLayout(int screenW, int screenH,
int squareWidth, int squareHeight,
int leftMargin, int topMargin,
int rightMargin, int bottomMargin,
vector<ViewLayer> layers) {
return new GridLayout(
screenW, screenH, squareWidth, squareHeight, leftMargin, topMargin, rightMargin, bottomMargin, layers);
}
class WorldLayout : public GridLayout {
public:
WorldLayout(int screenW, int screenH, int leftM, int topM, int rightM, int bottomM)
: GridLayout(screenW, screenH, 1, 1, leftM, topM, rightM, bottomM,
{ViewLayer::FLOOR_BACKGROUND, ViewLayer::FLOOR, ViewLayer::CREATURE}) {}
virtual Vec2 projectOnScreen(Vec2 mapPos) override {
return mapPos;
}
};
MapLayout* MapLayout::worldLayout(int screenW, int screenH,
int leftMargin, int topMargin,
int rightMargin, int bottomMargin) {
return new WorldLayout(
screenW, screenH, leftMargin, topMargin, rightMargin, bottomMargin);
}