forked from Nimdoc/pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
201 lines (156 loc) · 4.47 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
#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 640
#define RESOLUTION_Y 480
#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();
// Chris these should be in your player class.
// They're here so we can experiment.
float get_stepx(float rate, int degrees);
float get_stepy(float rate, int degrees);
/*
Unfinished raycast engine.
Check events stops the engine until a key is pressed.
Spin around the map as a demo for things to come.
Chris: Look in the ray_window.cpp file for the check_events function.
Thats where you'll want to read events from.
John: Look in the ray_caster.cpp file. See what the best way to implement
your maze class is in the get_raycast_array function.
*/
int main(int argc, char** argv)
{
bool is_running = true;
// 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();
// print map
Maze.print_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;
// 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)
{
// Move player forwards
pos_x += get_stepx(frame_rate * SPEED, angle);
pos_y += get_stepy(frame_rate * SPEED, angle);
}
if(inputs.W_BACKWARD)
{
// Move player backwards
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;
}
}
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));
}