-
Notifications
You must be signed in to change notification settings - Fork 0
/
currentmap.cpp
217 lines (178 loc) · 4.13 KB
/
currentmap.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "currentmap.h"
#include "perlinnoise.h"
#include <QDateTime>
#include <QPainter>
#include <QDebug>
namespace {
const int maxYGlobal = 150;
const int maxXGlobal = 240;
const int middleY = 75;
const int middleX = 120;
const int maxYWindow = 50;
const int maxXWindow = 80;
const int charYSz = 12;
const int charXSz = 12;
const QPoint middlePos(maxXGlobal/2, maxYGlobal/2);
const int maxXOffset = maxXGlobal-maxXWindow;
const int maxYOffset = maxYGlobal-maxYWindow;
void capCharacterPos(QPoint & p)
{
if(p.x() < 1) p.setX(1);
if(p.y() < 1) p.setY(1);
if(p.x() > maxXGlobal-2) p.setX(maxXGlobal-2);
if(p.y() > maxYGlobal-2) p.setY(maxYGlobal-2);
}
void capMapOffset(QPoint & offset)
{
if(offset.x() < 0) offset.setX(0);
if(offset.y() < 0) offset.setY(0);
if(offset.x() > maxXOffset) offset.setX(maxXOffset);
if(offset.y() > maxYOffset) offset.setY(maxYOffset);
}
}
Character::Character(CurrentMap & map)
: map_(map), pos_(middlePos)
{
}
Character::~Character()
{
}
QChar Character::getSymbol() const
{
return '@';
}
bool Character::move(Character::Move mv)
{
bool xChanged = false;
QPoint diff;
switch(mv) {
case Left:
xChanged = true;
diff = QPoint(-1,0);
break;
case Right:
xChanged = true;
diff = QPoint(1,0);
break;
case Up:
diff = QPoint(0,-1);
break;
case Down:
diff = QPoint(0,1);
break;
};
QPoint newPos = pos_ + diff;
if(map_.getGlobalAt(newPos) != '.') return false;
if (newPos.x() >= 0 && newPos.y() >= 0
&& newPos.x() <= maxXGlobal && newPos.y() < maxYGlobal)
{
pos_ = newPos;
capCharacterPos(pos_);
if ( ( xChanged && (pos_.x() >= (maxXWindow/2) && (pos_.x() <= (maxXOffset + (maxXWindow/2))))) ||
(!xChanged && (pos_.y() >= (maxYWindow/2) && (pos_.y() <= (maxYOffset + (maxYWindow/2))))) )
{
map_.offset_ += diff;
capMapOffset(map_.offset_);
}
return true;
}
return false;
}
void Character::draw(QPainter & p) const
{
if (pos_.x() > map_.offset_.x() && pos_.y() > map_.offset_.y()) {
QPoint pos = pos_ - map_.offset_;
p.drawText(charXSz*(pos.x()+1), charYSz*(pos.y()+2), getSymbol());
}
}
QChar Monster::getSymbol() const
{
return 'M';
}
CurrentMap::CurrentMap()
: offset_(80, 50), character_(*this)
{
monsters_.push_back(new Monster(*this));
monsters_[0]->setPos(QPoint(81,51));
const long seed = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000;
PerlinNoise pn(seed); //237
data_.resize(maxYGlobal);
for(int i = 0; i < maxYGlobal; ++i) {
data_[i].resize(maxXGlobal);
for (int j = 0; j < maxXGlobal; ++j) {
double x = (double)j/((double) maxXGlobal);
double y = (double)i/((double) maxYGlobal);
double n = pn.noise(10 * x, 10 * y, 0.8) + 0.7;
// Watter (or a Lakes)
if(n < 0.31) {
data_[i][j] = '~';
}
// Floors (or Planes)
else if (n >= 0.31 && n < 0.98) {
data_[i][j] = '.';
}
// Walls (or Mountains)
else if (n >= 0.98 && n < 1.2) {
data_[i][j] = '#';
}
// Ice (or Snow)
else {
data_[i][j] = 'S';
}
}
}
for(int i = 0; i < maxXGlobal; ++i) {
data_[0][i] = '|';
data_[maxYGlobal-1][i] = '|';
}
for(int i = 0; i < maxYGlobal; ++i) {
data_[i][0] = '-';
data_[i][maxXGlobal-1] = '-';
}
}
CurrentMap::~CurrentMap()
{
for(int i = 0; i < monsters_.size(); ++i) {
delete monsters_[i];
}
}
int CurrentMap::getMaxX() const
{
return maxXWindow;
}
int CurrentMap::getMaxY() const
{
return maxYWindow;
}
void CurrentMap::setData(int x, int y, const char & c)
{
data_[offset_.y() + y][offset_.x() + x] = c;
}
void CurrentMap::setData(const QPoint & pos, const char & c)
{
setData(pos.x(), pos.y(), c);
}
void CurrentMap::draw(QPainter & p) const
{
for (int y = 0; y < maxYWindow; ++y) {
for (int x = 0; x < maxXWindow; ++x) {
p.drawText(12*(x+1), 12*(y+2), QString(getWindowAt(x,y)));
}
}
Q_FOREACH(Monster* m, monsters_) {
m->draw(p);
}
character_.draw(p);
}
QChar CurrentMap::getWindowAt(int x, int y) const
{
return data_[offset_.y()+y][offset_.x() + x];
}
QChar CurrentMap::getWindowAt(const QPoint & pos) const
{
return getWindowAt(pos.x(), pos.y());
}
QChar CurrentMap::getGlobalAt(const QPoint & pos) const
{
return data_[pos.y()][pos.x()];
}