-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironmentsystem.cpp
More file actions
232 lines (199 loc) · 4.96 KB
/
Copy pathenvironmentsystem.cpp
File metadata and controls
232 lines (199 loc) · 4.96 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
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
218
219
220
221
222
223
224
225
226
227
228
#include "environmentsystem.h"
EnvironmentSystem::EnvironmentSystem(QRect r,qreal speed,QObject *parent)
: QObject{parent},m_rect(r),m_standardSpeed(speed)
{
m_white=QColor(240,240,240,0);
m_head=QPixmap(":/images/head_big.png");
}
qreal EnvironmentSystem::standardSpeed() const
{
return m_standardSpeed;
}
void EnvironmentSystem::setStandardSpeed(const qreal s)
{
m_standardSpeed=s;
//更新地板速度
foreach (Ground* g ,grounds) {
g->setSpeed(s);
}
//更新云朵速度
foreach (Cloud* c, clouds) {
c->setSpeed(s);
}
}
void EnvironmentSystem::reset()
{
m_white=QColor(240,240,240,0);
}
void EnvironmentSystem::updateGroundsState(qreal y)
{
removeGrounds(); //删除地面
addGrounds(y); //添加地面
}
void EnvironmentSystem::addGrounds(qreal y)
{
while (!grounds.isEmpty() && grounds.head() == nullptr)
{
grounds.dequeue();
}
do
{
if(grounds.isEmpty())
{
Ground *g=new Ground(grounds,rect().right(),y,300,standardSpeed(),this);
}
else
{
Ground *g=new Ground(grounds,grounds.back()->endPoint().x(),y,300,standardSpeed(),this);
}
} while (grounds.back()->endPoint().x() <= 300+m_rect.right());
}
void EnvironmentSystem::removeGrounds()
{
if(grounds.isEmpty()) return;
if(grounds.head()==nullptr)
{
grounds.dequeue();
return;
}
if(grounds.head()->endPoint().x()<rect().left()-100)
{
Ground *g=grounds.dequeue();
/*if(g->isUneven())
{
if(g->ramp[0]!=nullptr)delete g->ramp[0];
if(g->ramp[1]!=nullptr)delete g->ramp[1];
}*/
delete g;
g=nullptr;
}
}
void EnvironmentSystem::moveGrounds()
{
foreach (Ground* g, grounds)
{
if (g!=nullptr)
{
g->shift();
}
}
}
void EnvironmentSystem::updateCloudState()
{
removeClouds();
addClouds();
}
void EnvironmentSystem::addClouds()
{
if(grounds.empty()) return;
//云朵不可距离过近
if(!clouds.isEmpty())
{
if(clouds.back()->position().x()>m_rect.width()*0.8)
return;
}
//随机Y坐标
qreal maxY=grounds.head()->endPoint().y()*0.55;
qreal minY=grounds.head()->endPoint().y()*0.15;
double percentY = QRandomGenerator::global()->generateDouble();
qreal cloudY=minY+(maxY-minY)*percentY;
//随机X坐标
double percentX = QRandomGenerator::global()->generateDouble();
qreal cloudX=m_rect.width()+percentX*m_rect.width()/5;
QPointF pos(cloudX,cloudY);
//生成云朵
Cloud *c=new Cloud(this,m_standardSpeed,pos);
clouds.enqueue(c);
}
void EnvironmentSystem::removeClouds()
{
if(clouds.isEmpty()) return;
if(clouds.head()==nullptr)
{
clouds.dequeue();
return;
}
if(clouds.head()->isOut())
{
Cloud* c=clouds.dequeue();
delete c;
c=nullptr;
return;
}
}
void EnvironmentSystem::moveClouds()
{
foreach (Cloud* c, clouds) {
c->shift();
}
}
QColor EnvironmentSystem::white()
{
return m_white;
}
void EnvironmentSystem::setRect(const QRect &r)
{
m_rect=r;
}
QRect EnvironmentSystem::rect() const
{
return m_rect;
}
void EnvironmentSystem::updateMovement()
{
//地板移动
moveGrounds();
//云朵移动
moveClouds();
}
void EnvironmentSystem::updateState(qreal groundY)
{
//地面增删
updateGroundsState(groundY);
//云朵删增
updateCloudState();
}
void EnvironmentSystem::paintEnvironment(QPainter &painter)
{
//绘制地面
foreach (Ground* g, grounds)
{
if(g!=nullptr) g->paintGround(painter);
}
//绘制云朵
foreach (Cloud* c, clouds) {
if(c!=nullptr) c->paint(painter);
}
}
void EnvironmentSystem::paintEnd(QPainter &painter)
{
//白色背景
int a=std::min(m_white.alpha()+2,255);
if(m_white.alpha()<255)
{
m_white.setAlpha(a);
}
painter.setPen(Qt::NoPen);
painter.setBrush(m_white);
painter.drawRect(rect());
//龙头
qreal head_x=rect().width()/2-m_head.width()/2;
qreal head_y=rect().height()/2-m_head.height()/2;
QPointF headPos(head_x,head_y);
qreal scale=qreal(a)/255;
painter.drawPixmap(headPos,m_head.scaled(scale*m_head.width(),scale*m_head.height(),Qt::KeepAspectRatio, Qt::SmoothTransformation));
//文字
QPoint tr1(0,0);
QPoint tr2(rect().width(),head_y);
QRect text_rect(tr1,tr2);
QFont font("Arial", 25);
QString text("恭喜,奶龙又被您击退!\n看来,今天恐龙还不会灭绝。。。\n利用好收集到的能量精粹,\n为明日的逃亡做好准备吧!");
painter.setFont(font);
painter.setPen(QColor(40,40,40,a));
painter.drawText(text_rect, Qt::AlignCenter | Qt::TextWordWrap, text);
}
void EnvironmentSystem::paintWhiteBackground(QPainter &painter)
{
painter.setBrush(QColor(240,240,240));
painter.drawRect(m_rect);
}