Repository navigation
Expand file tree
/
Copy pathlevel.cpp
More file actions
132 lines (119 loc) · 2.49 KB
/
Copy pathlevel.cpp
File metadata and controls
132 lines (119 loc) · 2.49 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
#include"level.h"
/**
* Default Constructor
* Purpose: Sets up an empty level with default values
*/
level::level()
{
obstacle_count = 0;
scroll_speed = 5.0;
bg_color = COLOR_BLACK;
started = false;
}
/**
* Procedure: add_obstacle
* Purpose: Adds an obstacle to the levels array called by
* Level_Loader when reading the file
*
* @param obstacle obstacle to be added to the array
*/
void level::add_obstacle(obstacle obstacle)
{
if (obstacle_count < MAX_OBSTACLES)
{
obstacles[obstacle_count] = obstacle;
obstacle_count++;
started = true;
}
}
/**
* Procedure: update
* Purpose: Scrolls all obstacles left each frame, removes
* offscreen ones, increments speed
*/
void level::update()
{
// Increase scroll speed over time - game gets harder the longer you survive
scroll_speed += SPEED_INCREMENT;
// Move all active obstacles left
int i = 0;
while (i < obstacle_count)
{
obstacles[i].scroll_left(scroll_speed);
// Remove if offscreen
if (obstacles[i].get_x() + obstacles[i].get_width() < 0)
{
obstacles[i] = obstacles[obstacle_count - 1];
obstacle_count--;
}
else
{
i++;
}
}
}
/**
* Function: check_collision
* Purpose: Returns true if player overlaps any obstacle
*
* @param player the player block taken by reference to check
* the intersection with.
*/
bool level::check_collision(const player &player)
{
rectangle player_bounds = player.get_bounds();
for (int i = 0; i < obstacle_count; i++)
{
if (rectangles_intersect(player_bounds, obstacles[i].get_bounds()))
{
return true;
}
}
return false;
}
/**
* Procedure: draw
* Purpose: Draws background and all obstacles
*/
void level::draw()
{
clear_screen(bg_color);
for (int i = 0; i < obstacle_count; i++)
{
obstacles[i].draw();
}
}
/**
* Function: is_complete
* Purpose: Returns true when all obstacles have scrolled off screen
*/
bool level::is_complete()
{
return started && obstacle_count == 0;
}
/**
* Setter Methods
*/
void level::set_scroll_speed(double speed)
{
scroll_speed = speed;
}
void level::set_bg_color(color color)
{
bg_color = color;
}
/**
* Getter Methods
*/
double level::get_scroll_speed() const
{
return scroll_speed;
}
color level::get_bg_color() const
{
return bg_color;
}
int level::get_obstacle_count() const
{
return obstacle_count;
}