#include <L:\\Soft\\Dev-Cpp\\MinGW\\include\\graphics.h>
#include <conio.h>
#include <cmath>
#include <iostream>
#define pi 3.14159265
using namespace std;
double map[15][15]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,2,12,1,1,0,1,1,1,0,1,1,1},
{1,0,0,0,1,0,0,0,1,0,0,0,0,0,1},
{1,1,1,1,1,0,1,1,1,1,1,1,1,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,1},
{1,1,1,0,1,0,1,1,1,0,1,1,1,1,1},
{0,0,0,0,0,0,1,0,0,0,1,0,0,0,0},
{1,1,1,1,1,0,1,1,1,0,1,0,1,1,1},
{1,0,0,0,0,0,1,0,0,0,1,0,1,0,1},
{1,0,1,1,0,1,1,0,1,1,1,0,1,0,1},
{1,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,1,1,1,0,1,0,1,1,1,0,1,1,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int main(){
int gd = DETECT, gm;
int c=0;
double distance=0.0;
double width=640.0;
double height=480.0;
double proj_height=0.0;
double num_rays=640.0;
double ray_width=width/num_rays;
double plyr_angle=45.0;
double view_angle=80.0;
double half_fov=view_angle/2.0;
double step_angle=view_angle/num_rays;
double start_rndr_angle=0.0;
double rndr_angle=0.0;
double plyr_x=1.5;
double plyr_y=1.5;
double test_plyr_x=1.5;
double test_plyr_y=1.5;
double top=0.0;
double rndr_pos_x=1.5;
double rndr_pos_y=1.5;
double plyr_spd=0.05;
double nxt_pos_step_y=0.22;
double nxt_pos_step_x=0.22;
bool exitOk=false;
initgraph(&gd, &gm, NULL);
while (!exitOk){
c = getch();
cleardevice(); //Считываем с клавиатуры символ
switch (c){ //Если была нажата клавиша-стрелка,..
case -22:
plyr_angle=45;
plyr_x=1.5;
plyr_y=1.5;
break;
case 13:
plyr_angle=45;
plyr_x=1.5;
plyr_y=1.5;
break;
case 27: //Выход по "Escape"
exitOk = !0;
case 75: //Left
plyr_angle-=1;
break;
case 72: //Up
test_plyr_y=plyr_y+sin(plyr_angle*pi/180)*plyr_spd;
test_plyr_x=plyr_x+cos(plyr_angle*pi/180)*plyr_spd;
if(map[(int)test_plyr_y][(int)plyr_x]==0){plyr_y=test_plyr_y;}
if(map[(int)plyr_y][(int)test_plyr_x]==0){plyr_x=test_plyr_x;}
break;
case 77: //Right
plyr_angle+=1;
break;
case 80: //Down
test_plyr_y=plyr_y-sin(plyr_angle*pi/180)*plyr_spd;
test_plyr_x=plyr_x-cos(plyr_angle*pi/180)*plyr_spd;
if(map[(int)test_plyr_y][(int)plyr_x]==0){plyr_y=test_plyr_y;}
if(map[(int)plyr_y][(int)test_plyr_x]==0){plyr_x=test_plyr_x;}
case -10: //w
test_plyr_y=plyr_y+sin(plyr_angle*pi/180)*plyr_spd;
test_plyr_x=plyr_x+cos(plyr_angle*pi/180)*plyr_spd;
if(map[(int)test_plyr_y][(int)plyr_x]==0){plyr_y=test_plyr_y;}
if(map[(int)plyr_y][(int)test_plyr_x]==0){plyr_x=test_plyr_x;}
break;
case -12: //a
test_plyr_y=plyr_y+sin((plyr_angle-90)*pi/180)*plyr_spd;
test_plyr_x=plyr_x+cos((plyr_angle-90)*pi/180)*plyr_spd;
if(map[(int)test_plyr_y][(int)plyr_x]==0){plyr_y=test_plyr_y;}
if(map[(int)plyr_y][(int)test_plyr_x]==0){plyr_x=test_plyr_x;}
break;
case -5: //s
test_plyr_y=plyr_y-sin(plyr_angle*pi/180)*plyr_spd;
test_plyr_x=plyr_x-cos(plyr_angle*pi/180)*plyr_spd;
if(map[(int)test_plyr_y][(int)plyr_x]==0){plyr_y=test_plyr_y;}
if(map[(int)plyr_y][(int)test_plyr_x]==0){plyr_x=test_plyr_x;}
break;
case -30: //d
test_plyr_y=plyr_y-sin((plyr_angle-90)*pi/180)*plyr_spd;
test_plyr_x=plyr_x-cos((plyr_angle-90)*pi/180)*plyr_spd;
if(map[(int)test_plyr_y][(int)plyr_x]==0){plyr_y=test_plyr_y;}
if(map[(int)plyr_y][(int)test_plyr_x]==0){plyr_x=test_plyr_x;}
break;
}
start_rndr_angle=plyr_angle-half_fov;
rndr_angle=start_rndr_angle;
for(int rndr_ray=0;rndr_ray<900;rndr_ray+=1){
rndr_pos_x=plyr_x;
rndr_pos_y=plyr_y;
nxt_pos_step_x=cos(rndr_angle*pi/180)/50.0;
nxt_pos_step_y=sin(rndr_angle*pi/180)/50.0;
rndr_angle+=step_angle;
distance=0.0;
while(map[(int)rndr_pos_y][(int)rndr_pos_x]==0){
distance+=1.0/50.0;
rndr_pos_x+=nxt_pos_step_x;
rndr_pos_y+=nxt_pos_step_y;
}
proj_height=min((height/(distance+0.001)),height);
top=(height-proj_height)/2;
//setcolor(16-map[(int)rndr_pos_y][(int)rndr_pos_x]);
setcolor(15-(int)distance);
line(rndr_ray,top,rndr_ray,top+proj_height);
}
swapbuffers();
}
getch();
closegraph();
return 0;
}