-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathground.cpp
131 lines (111 loc) · 4.23 KB
/
ground.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
/***********************************************************************
* Implementation File:
* GROUND : A class representing the lunar landscape
************************************************************************/
#include "ground.h"
/********************************************
* GROUND :: CONSTRUCTOR
********************************************/
Ground :: Ground(Point tl, Point br) : topLeft(tl), bottomRight(br)
{
Point pt; // create a point to get the dimensions
xSize = bottomRight.getX() - topLeft.getX();
// create the ground
ground = new float[xSize];
generateGround();
}
/*********************************************
* GROUND :: GET GROUND HEIGHT
* For a given x position, determine the height
* of the ground at that point;
*********************************************/
Point Ground :: getGround(const Point & ptSrc) const
{
Point pt(ptSrc);
// handle invalid cases
if (ptSrc.getX() < topLeft.getX() || ptSrc.getX() > bottomRight.getX())
pt.setY(bottomRight.getY());
// handle the platform case
else if (ptSrc.getX() - platform.getX() < PLATFORM / 2.0 &&
platform.getX() - ptSrc.getX() < PLATFORM / 2.0)
pt.setY(platform.getY());
// otherwise, the normal ground
else
pt.setY(ground[(int)(ptSrc.getX() - bottomRight.getX())]);
return pt;
}
/***************************************
* GROUND :: DRAW
* display the ground on the screen
***************************************/
void Ground :: draw() const
{
// set the initial position at the platform location
Point pt1 = platform;
Point pt2 = platform;
// draw the platform
pt1.addX(-PLATFORM / 2.0);
pt2.addX( PLATFORM / 2.0);
drawLine(pt1, pt2, 1.0 /*red*/, 1.0 /*green*/, 0 /*blue*/);
// draw the platform supports
pt2.setY(bottomRight.getY());
pt2.setX(pt1.getX());
for (int i = 0; i < PLATFORM; i += 5)
{
drawLine(pt1, pt2, 1.0 /*red*/, 1.0 /*green*/, 0 /*blue*/);
pt1.addX(5);
pt2.addX(5);
}
// draw the ground now
for (int i = 0; i < xSize; i++)
{
Point ptBottom(topLeft.getX() + i, bottomRight.getY());
Point ptTop (topLeft.getX() + i, ground[i]);
drawLine(ptBottom, ptTop, 0.9 /*red*/, 0.9 /*green*/, 0.9 /*blue*/);
}
}
/*********************************************
* GROUND :: IS ABOVE GROUND
* did we leave the screen or enter the ground?
*********************************************/
bool Ground :: isAboveGround(const Point & point) const
{
return point.getX() > topLeft.getX() &&
point.getY() > bottomRight.getY() &&
point.getX() < bottomRight.getX() &&
point.getY() > ground[(int)(point.getX() - topLeft.getX())];
}
/*****************************************
* GROUND :: GENERATE GROUND
* generate a new terrain
*******************************************/
void Ground :: generateGround()
{
Point pt; // to get the screen size
float yMiddle = bottomRight.getY() + // the midpoint of the screen
(topLeft.getY() - bottomRight.getY()) / 2.0;
// create the ground
ground[0] = 0.0 + bottomRight.getY();
float slope = 1.0;
for (int i = 1; i < xSize; i++)
{
bool up = // can the ground slope up?
(ground[i - 1] < yMiddle) && // not too high
(ground[i - 1] - bottomRight.getY() < xSize - i) && // not near the end
(slope < 3.0); // not too steep
bool down = // can the ground slope down
(ground[i - 1] > bottomRight.getY() + 10) && // not too low
(i > 10) && // not near the beginning
(slope > -3.0); // not too steep;
slope += random((down ? -BUMPY : 0.0), (up ? BUMPY : 0.0));
ground[i] = ground[i - 1] + slope;
}
// create the platform
int iPlatform = random(xSize / 4, xSize * 3 / 4);
int yMax = ground[iPlatform - PLATFORM / 2];
for (int i = iPlatform - PLATFORM / 2 + 1; i < iPlatform + PLATFORM / 2; i++)
if (ground[i] > yMax)
yMax = ground[i];
platform.setX(iPlatform + topLeft.getX());
platform.setY(yMax + 10);
}