forked from toksaitov/simple-breakout-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.cpp
More file actions
132 lines (107 loc) · 3.88 KB
/
Copy pathlevel.cpp
File metadata and controls
132 lines (107 loc) · 3.88 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
#include "level.h"
#include "ball.h"
#include "game.h"
#include "graphics.h"
#include "paddle.h"
#include "raylib.h"
char* current_level_data;
void load_level(const int offset)
{
current_level_index += offset;
if (current_level_index >= level_count) {
game_state = victory_state;
ClearBackground(BLACK);
init_victory_menu();
current_level_index = 0;
return;
}
const size_t rows = levels[current_level_index].rows;
const size_t columns = levels[current_level_index].columns;
current_level_blocks = 0;
current_level_data = new char[rows * columns];
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column) {
current_level_data[row * columns + column] = levels[current_level_index].data[row * columns + column];
if (current_level_data[row * columns + column] == BLOCKS || current_level_data[row * columns + column] == STRONG_BLOCK) {
++current_level_blocks;
}
}
}
current_level = { rows, columns, current_level_data,
levels[current_level_index].name,
levels[current_level_index].description };
spawn_ball();
spawn_paddle();
derive_graphics_metrics();
}
void unload_level()
{
delete[] current_level_data;
}
bool is_inside_level(const int row, const int column)
{
return row >= 0 && row < current_level.rows && column >= 0 && column < current_level.columns;
}
char& get_level_cell(const size_t row, const size_t column)
{
return current_level.data[row * current_level.columns + column];
}
void set_level_cell(const size_t row, const size_t column, const char cell)
{
get_level_cell(row, column) = cell;
}
bool is_colliding_with_level_cell(const Vector2 pos, const Vector2 size, const char cell)
{
const Rectangle hitbox = { pos.x, pos.y, size.x, size.y };
for (int row = static_cast<int>(pos.y); row <= static_cast<int>(pos.y + size.y); ++row) {
for (int column = static_cast<int>(pos.x); column <= static_cast<int>(pos.x + size.x); ++column) {
if (!is_inside_level(row, column)) {
continue;
}
if (get_level_cell(row, column) == cell) {
if (const Rectangle block_hitbox = { static_cast<float>(column), static_cast<float>(row), 1.0f, 1.0f }; CheckCollisionRecs(hitbox, block_hitbox)) {
return true;
}
}
}
}
return false;
}
char& get_colliding_level_cell(const Vector2 pos, const Vector2 size, const char look_for)
{
const Rectangle hitbox = { pos.x, pos.y, size.x, size.y };
for (int row = static_cast<int>(pos.y); row <= static_cast<int>(pos.y + size.y); ++row) {
for (int column = static_cast<int>(pos.x); column <= static_cast<int>(pos.x + size.x); ++column) {
if (!is_inside_level(row, column)) {
continue;
}
if (get_level_cell(row, column) == look_for) {
if (const Rectangle block_hitbox = { static_cast<float>(column), static_cast<float>(row), 1.0f, 1.0f }; CheckCollisionRecs(hitbox, block_hitbox)) {
return get_level_cell(row, column);
}
}
}
}
return get_level_cell(static_cast<size_t>(pos.x), static_cast<size_t>(pos.y));
}
void load_pvp_level()
{
const size_t rows = pvp_level.rows;
const size_t columns = pvp_level.columns;
current_level_blocks = 0;
current_level_data = new char[rows * columns];
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column) {
current_level_data[row * columns + column] = pvp_level.data[row * columns + column];
}
}
current_level = {
rows,
columns,
current_level_data,
pvp_level.name,
pvp_level.description
};
spawn_ball();
derive_graphics_metrics();
}