mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
norme
This commit is contained in:
50
move/ft_collision.c
Normal file
50
move/ft_collision.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_collision.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:09:52 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/11 20:35:31 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
void calculate_future_position(t_position_params *params)
|
||||
{
|
||||
*(params->future_px) = params->px + params->pdx + params->collisionBuffer;
|
||||
*(params->future_py) = params->py + params->pdy + params->collisionBuffer;
|
||||
}
|
||||
|
||||
int check_collision(t_structure_main *w, int future_px, int future_py)
|
||||
{
|
||||
int future_ipx;
|
||||
int future_ipy;
|
||||
|
||||
future_ipx = future_px / w->s_map.mapS;
|
||||
future_ipy = future_py / w->s_map.mapS;
|
||||
return (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0');
|
||||
}
|
||||
|
||||
void calculate_future_position_right_left(t_position_params *params)
|
||||
{
|
||||
double angle;
|
||||
int collision_px;
|
||||
int collision_py;
|
||||
|
||||
angle = params->pa;
|
||||
if (params->direction == 'd')
|
||||
angle += M_PI_2;
|
||||
else
|
||||
angle -= M_PI_2;
|
||||
*(params->future_px) = params->px + (int)(cos(angle) * 5);
|
||||
*(params->future_py) = params->py + (int)(sin(angle) * 5);
|
||||
collision_px = *(params->future_px) + (int)(cos(params->pa)
|
||||
* params->collisionBuffer);
|
||||
collision_py = *(params->future_py) + (int)(sin(params->pa)
|
||||
* params->collisionBuffer);
|
||||
*(params->future_px) = collision_px;
|
||||
*(params->future_py) = collision_py;
|
||||
}
|
||||
85
move/ft_key.c
Normal file
85
move/ft_key.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_key.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:17:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/12 20:28:07 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
int *kill_prog(t_structure_main *w)
|
||||
{
|
||||
int i;
|
||||
|
||||
mlx_destroy_window(w->s_win.mlx, w->s_win.win);
|
||||
mlx_destroy_image(w->s_win.mlx, w->s_img.buffer);
|
||||
mlx_destroy_image(w->s_win.mlx, w->s_img.img_player);
|
||||
mlx_destroy_image(w->s_win.mlx, w->s_img.img_wall);
|
||||
mlx_destroy_image(w->s_win.mlx, w->s_img.pedro_wall);
|
||||
i = -1;
|
||||
while (++i <= 10)
|
||||
mlx_destroy_image(w->s_win.mlx, w->s_img.roomadslam[i]);
|
||||
if (w->s_map.map)
|
||||
{
|
||||
free(w->s_map.map);
|
||||
w->s_map.map = NULL;
|
||||
}
|
||||
mlx_destroy_display(w->s_win.mlx);
|
||||
free(w->s_win.mlx);
|
||||
exit(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void adjust_player_angle(t_structure_main *w, int key)
|
||||
{
|
||||
double angle_adjustment;
|
||||
|
||||
if (key != 65361 && key != 65363)
|
||||
return ;
|
||||
angle_adjustment = 16 * ((PI / 3) / NUMRAY);
|
||||
if (key == 65361)
|
||||
{
|
||||
w->s_player.pa -= angle_adjustment;
|
||||
if (w->s_player.pa < 0)
|
||||
w->s_player.pa += 2 * PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
w->s_player.pa += angle_adjustment;
|
||||
if (w->s_player.pa > 2 * PI)
|
||||
w->s_player.pa -= 2 * PI;
|
||||
}
|
||||
w->s_player.pdx = cos(w->s_player.pa) * 5;
|
||||
w->s_player.pdy = sin(w->s_player.pa) * 5;
|
||||
}
|
||||
|
||||
void handle_movement_keys(int key, t_structure_main *w)
|
||||
{
|
||||
if (key == 119 || key == 100 || key == 115 || key == 97)
|
||||
move(key, w);
|
||||
}
|
||||
|
||||
int deal_key(int key, t_structure_main *w)
|
||||
{
|
||||
if (key == 65307)
|
||||
{
|
||||
kill_prog(w);
|
||||
}
|
||||
else if (key == 65361 || key == 65363)
|
||||
{
|
||||
adjust_player_angle(w, key);
|
||||
}
|
||||
else if (key == 65362 || key == 65364 || key == 114 || key == 102)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
handle_movement_keys(key, w);
|
||||
}
|
||||
return (key);
|
||||
}
|
||||
113
move/ft_move.c
Normal file
113
move/ft_move.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_move.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:08:25 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/11 20:35:09 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
void move_forward(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 119)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pdx = w->s_player.pdx;
|
||||
params.pdy = w->s_player.pdy;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
calculate_future_position(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px += w->s_player.pdx;
|
||||
w->s_player.py += w->s_player.pdy;
|
||||
}
|
||||
}
|
||||
|
||||
void move_backward(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 115)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pdx = -w->s_player.pdx;
|
||||
params.pdy = -w->s_player.pdy;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
calculate_future_position(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px -= w->s_player.pdx;
|
||||
w->s_player.py -= w->s_player.pdy;
|
||||
}
|
||||
}
|
||||
|
||||
void move_right(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 100)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pa = w->s_player.pa;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
params.direction = 'd';
|
||||
calculate_future_position_right_left(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
|
||||
void move_left(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 97)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pa = w->s_player.pa;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
params.direction = 'a';
|
||||
calculate_future_position_right_left(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
|
||||
void move(int key, t_structure_main *w)
|
||||
{
|
||||
move_forward(w, key);
|
||||
move_backward(w, key);
|
||||
move_right(w, key);
|
||||
move_left(w, key);
|
||||
}
|
||||
Reference in New Issue
Block a user