-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathground.cpp
More file actions
95 lines (80 loc) · 2.58 KB
/
Copy pathground.cpp
File metadata and controls
95 lines (80 loc) · 2.58 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
#include "ground.h"
Ground::Ground(QQueue<Ground*> &container, qreal x, qreal y, qreal max_length, qreal speed, QObject* parent)
: QObject{ parent }, m_speed(speed)
{
// 生成 [0, 100) 之间的随机整数
int r = QRandomGenerator::global()->bounded(100);
// 由随机数决定地面种类
// 如果当前无地面/最后的地面为斜向的,则必须生成平直地面
if (r < 80||container.isEmpty()||container.back()->m_endPoint.y()!=container.back()->m_startPoint.y()) // 正常地面
{
m_startPoint = QPointF(x, y);
double percent = QRandomGenerator::global()->generateDouble();
m_endPoint = QPointF(x + (0.4 * percent + 0.6) * max_length, y);
container.enqueue(this);
}
else // 凹陷或突出地面
{
//随机百分数
double percent = QRandomGenerator::global()->generateDouble();
//maxLength需要缩小,以确保凹陷/突出长度较小
max_length=50;
//Y方向偏移量
int offset = QRandomGenerator::global()->bounded(-10, 10);
//偏移区间长度(偏移地面+斜坡*2)
qreal len = max_length*(0.4 * percent + 0.6);
//斜坡X方向长度:offset*0.5~offset*1.5=5~15
qreal rampX=abs((0.5+percent)*offset); //offset为负?所以需要ABS
//偏移地面长度
qreal groundLen=len-2*rampX;
//斜坡1
Ground* ramp1=new Ground(QPointF(x,y),QPointF(x+rampX,y+offset),speed);
container.enqueue(ramp1);
//偏移地面(自己)
this->setStartPoint(QPointF(x+rampX,y+offset));
this->setEndPoint(QPointF(x+rampX+groundLen,y+offset));
container.enqueue(this);
//斜坡2
Ground* ramp2=new Ground(QPointF(x+rampX+groundLen,y+offset),QPointF(x+len,y),speed);
container.enqueue(ramp2);
}
}
Ground::Ground(QPointF start, QPointF end, qreal speed, QObject *parent)
:QObject{ parent },m_startPoint(start),m_endPoint(end),m_speed(speed)
{
}
void Ground::setStartPoint(const QPointF& p)
{
m_startPoint = p;
}
void Ground::setEndPoint(const QPointF& p)
{
m_endPoint = p;
}
QPointF Ground::startPoint() const
{
return m_startPoint;
}
QPointF Ground::endPoint() const
{
return m_endPoint;
}
void Ground::setSpeed(const qreal s)
{
m_speed=s;
}
qreal Ground::speed() const
{
return m_speed;
}
void Ground::shift()
{
m_startPoint.rx() -= m_speed;
m_endPoint.rx() -= m_speed;
}
void Ground::paintGround(QPainter& painter)
{
painter.drawLine(m_startPoint, m_endPoint);
//qreal width=m_endPoint.x()-m_startPoint.x();
//painter.drawRect(m_startPoint.x(),m_startPoint.y(),width,1000);
}