-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
284 lines (221 loc) · 5.9 KB
/
main.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include "ray_window.h"
#include "wall_object.h"
#include "ray_caster.h"
#include "maze_generator.h"
// GLOBAL CONSTANTS
#define RESOLUTION_X 800
#define RESOLUTION_Y 600
#define CUBE_SIDE_LENGTH 100
#define UNIT_SIZE 100
#define FIELD_OF_VIEW 70
#define SPEED 250
#define TURN_RATE 100
// HELPER FUNCTIONS
double get_elapsed_time();
float get_stepx(float rate, int degrees);
float get_stepy(float rate, int degrees);
bool collides(float pos_x, float pos_y,
float rate, int degrees,
generate_maze &maze, int units,
int num);
bool has_won(float pos_x, float pos_y, generate_maze &maze,
int units, int num);
int main(int argc, char** argv)
{
bool is_running = true;
bool is_winner = false;
// if no argument or an even number entered,
// tell user and quit
if (argc !=2)
std::cout << "Usage: ./Pathfinder <Size>" << std::endl;
if (!(atoi(argv[1])%2))
std::cout << "Size of maze must be an odd number" << std::endl;
// generate maze with size from argument
generate_maze Maze(atoi(argv[1]));
// initialize the map
Maze.init_map();
// generate the map
Maze.generate_map();
/* End of changes I made in main() -Steve */
ray_window cast_display(RESOLUTION_X, RESOLUTION_Y);
keyboard_input inputs;
wall_object rays[RESOLUTION_X];
// Set default values for all the wall_objects in the rays array
for(int i = 0; i < RESOLUTION_X; i++)
{
// Set the max height of a ray
rays[i].set_max(RESOLUTION_Y);
// Define wall units
rays[i].set_unit(CUBE_SIDE_LENGTH);
// Set the actual size of the way, using distance.
// Will have to change later
rays[i].set_size(i);
// Set the color of the wall
rays[i].set_base_hex_color(0xFF,0x00,0x00);
}
ray_caster caster;
float angle = 0;
float time = 0;
double pos_x = (Maze.get_start_x() * UNIT_SIZE) + (UNIT_SIZE / 2);
double pos_y = (Maze.get_start_y() * UNIT_SIZE) + (UNIT_SIZE / 2);
double frame_rate;
bool collision;
// Game loop
while(is_running)
{
frame_rate = get_elapsed_time();
// Get the sizes of the walls to be drawn
caster.get_raycast_array(rays, RESOLUTION_X,
pos_x, pos_y,
Maze, atoi(argv[1]),
FIELD_OF_VIEW, angle,
CUBE_SIDE_LENGTH);
// Draw the ceiling
cast_display.draw_rectangle_to_buffer(0,0, RESOLUTION_X - 1,
RESOLUTION_Y / 2, 0xFFFFFF);
// Draw the floor
cast_display.draw_rectangle_to_buffer(0, RESOLUTION_Y / 2,
RESOLUTION_X - 1, RESOLUTION_Y - 1, 0xBA924E);
// Draw the wall
cast_display.line_cast_to_buffer(rays);
cast_display.draw_rectangle_to_buffer(
(RESOLUTION_X / 2) - 4,
(RESOLUTION_Y / 2) - 4,
(RESOLUTION_X / 2) + 4,
(RESOLUTION_Y / 2) + 4,
0x000000);
cast_display.draw_rectangle_to_buffer(
(RESOLUTION_X / 2) - 2,
(RESOLUTION_Y / 2) - 2,
(RESOLUTION_X / 2) + 2,
(RESOLUTION_Y / 2) + 2,
0xFFFFFF);
// Draw the buffer to the screen
cast_display.draw_buffer();
// Read the events and get keyboard inputs
cast_display.read_events(inputs);
// Modify player position
if(inputs.W_FORWARD)
{
// Check for collision
collision = collides(pos_x, pos_y,
SPEED * frame_rate + 10,
angle, Maze, UNIT_SIZE,
1);
// Move player forwards if there's no collision
if(!collision)
{
pos_x += get_stepx(frame_rate * SPEED, angle);
pos_y += get_stepy(frame_rate * SPEED, angle);
}
}
if(inputs.W_BACKWARD)
{
// Check for collision backwards
collision = collides(pos_x, pos_y,
SPEED * frame_rate + 10,
angle + 180, Maze, UNIT_SIZE,
1);
// Move player backwards if there's no collision
if(!collision)
{
pos_x += get_stepx(frame_rate * SPEED, 180 + angle);
pos_y += get_stepy(frame_rate * SPEED, 180 + angle);
}
}
if(inputs.W_LEFT)
{
// Turn player to the left
angle -= frame_rate * TURN_RATE;
}
if(inputs.W_RIGHT)
{
// Turn player to the right
angle += frame_rate * TURN_RATE;
}
// Check if quit button was pressed
if(inputs.W_QUIT)
is_running = false;
// Print out cheat map
if(inputs.W_SELECT)
Maze.print_position(pos_x / UNIT_SIZE,
pos_y / UNIT_SIZE);
if(has_won(pos_x, pos_y, Maze, UNIT_SIZE, 3))
{
is_running = false;
is_winner = true;
}
}
if(is_winner)
{
cast_display.clear();
cast_display.fill_box(0, 0,
RESOLUTION_X, RESOLUTION_Y,
0x2F2F2F);
cast_display.draw_text((RESOLUTION_X / 2) - 20,
(RESOLUTION_Y / 2) - 20,
"WINNER.", 0xFF0000);
while(!inputs.W_QUIT)
{
cast_display.read_events(inputs);
}
}
}
double get_elapsed_time()
{
static clock_t begin = 10;
clock_t end = clock();
double elapsed_time = double(end - begin) / CLOCKS_PER_SEC;
begin = clock();
}
/*
Takes in a direction and a rate and returns the rate of
travel on the X-Axis
*/
float get_stepx(float rate, int degrees)
{
return (((cos(degrees * M_PI / 180.0f))*rate));
}
/*
Takes in a direction and a rate and returns the rate of
travel on the Y-Axis
*/
float get_stepy(float rate, int degrees)
{
return (((sin(degrees * M_PI / 180.0f))*rate));
}
/*
Really simple collision detection. Just stops the player if there
is going to be a collision in the map.
*/
bool collides(float pos_x, float pos_y,
float rate, int degrees,
generate_maze &maze, int units,
int num)
{
float end_x = pos_x + get_stepx(rate, degrees);
float end_y = pos_y + get_stepy(rate, degrees);
int map_x = end_x / units;
int map_y = end_y / units;
if(maze.get_square(map_y, map_x) == num)
return true;
else
return false;
}
/*
Returns true if the player is in the defined winning square
*/
bool has_won(float pos_x, float pos_y, generate_maze &maze,
int units, int num)
{
float map_x = pos_x / units;
float map_y = pos_y / units;
if(maze.get_square(map_y, map_x) == num)
return true;
else
return false;
}