mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
manqueunefonctionnorme
This commit is contained in:
@@ -6,29 +6,54 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:09:52 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/25 20:43:43 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/29 17:43:10 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
int can_move_to(t_structure_main *w, double future_x, double future_y) {
|
||||
int map_x, map_y;
|
||||
static int check_collision(t_structure_main *w, double future_x,
|
||||
double future_y, t_collision_params *coll_params)
|
||||
{
|
||||
int i;
|
||||
int map_x;
|
||||
int map_y;
|
||||
char map_position;
|
||||
|
||||
// Vérification manuelle de chaque position autour de la future position
|
||||
int dx[] = {0, 0, -COLBUF, COLBUF, -COLBUF, COLBUF, -COLBUF, COLBUF};
|
||||
int dy[] = {-COLBUF, COLBUF, 0, 0, -COLBUF, -COLBUF, COLBUF, COLBUF};
|
||||
i = 0;
|
||||
while (i < DX_LENGTH)
|
||||
{
|
||||
map_x = (int)((future_x + coll_params->dx[i]) / w->s_map.map_s);
|
||||
map_y = (int)((future_y + coll_params->dy[i]) / w->s_map.map_s);
|
||||
map_position = w->s_map.map[map_y * w->s_map.map_x + map_x];
|
||||
if (map_position != '0' && map_position != '3')
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
map_x = (int)((future_x + dx[i]) / w->s_map.mapS);
|
||||
map_y = (int)((future_y + dy[i]) / w->s_map.mapS);
|
||||
char map_position = w->s_map.map[map_y * w->s_map.mapX + map_x];
|
||||
if (map_position != '0' && map_position != '3') {
|
||||
printf("Collision détectée à x = %d, y = %d\n", map_x, map_y);
|
||||
return 0; // Collision détectée, ne peut pas se déplacer
|
||||
}
|
||||
}
|
||||
int can_move_to(t_structure_main *w, double future_x, double future_y)
|
||||
{
|
||||
t_collision_params coll_params;
|
||||
|
||||
printf("Pas de collision, peut se déplacer\n");
|
||||
return 1; // Pas de collision, peut se déplacer
|
||||
coll_params.dx[0] = 0;
|
||||
coll_params.dx[1] = 0;
|
||||
coll_params.dx[2] = -COLBUF;
|
||||
coll_params.dx[3] = COLBUF;
|
||||
coll_params.dx[4] = -COLBUF;
|
||||
coll_params.dx[5] = COLBUF;
|
||||
coll_params.dx[6] = -COLBUF;
|
||||
coll_params.dx[7] = COLBUF;
|
||||
coll_params.dy[0] = -COLBUF;
|
||||
coll_params.dy[1] = COLBUF;
|
||||
coll_params.dy[2] = 0;
|
||||
coll_params.dy[3] = 0;
|
||||
coll_params.dy[4] = -COLBUF;
|
||||
coll_params.dy[5] = -COLBUF;
|
||||
coll_params.dy[6] = COLBUF;
|
||||
coll_params.dy[7] = COLBUF;
|
||||
if (check_collision(w, future_x, future_y, &coll_params) == 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:17:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/25 18:45:14 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/29 17:53:45 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -41,7 +41,7 @@ void adjust_player_angle(t_structure_main *w, int key)
|
||||
|
||||
if (key != 65361 && key != 65363)
|
||||
return ;
|
||||
angle_adjustment = 4 * (PI/180);
|
||||
angle_adjustment = 4 * (PI / 180);
|
||||
if (key == 65361)
|
||||
{
|
||||
w->s_player.pa -= angle_adjustment;
|
||||
@@ -67,27 +67,28 @@ void handle_movement_keys(int key, t_structure_main *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 if (key == 101) //e door
|
||||
else if (key == 101)
|
||||
{
|
||||
printf("Touche 101\n");
|
||||
if (w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + cos(w->s_player.pa) * 7)/w->s_map.mapS)] == '2')
|
||||
w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + cos(w->s_player.pa) * 7)/w->s_map.mapS)] = '3';
|
||||
else if (w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + cos(w->s_player.pa) * 7)/w->s_map.mapS)] == '3')
|
||||
w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + cos(w->s_player.pa) * 7)/w->s_map.mapS)] = '2';
|
||||
if (w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)
|
||||
/ w->s_map.map_s) *w->s_map.map_x + (int)((w->s_player.px
|
||||
+ cos(w->s_player.pa) * 7) / w->s_map.map_s)] == '2')
|
||||
w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)
|
||||
/ w->s_map.map_s) *w->s_map.map_x + (int)((w->s_player.px
|
||||
+ cos(w->s_player.pa) * 7) / w->s_map.map_s)] = '3';
|
||||
else if (w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)
|
||||
/ w->s_map.map_s) *w->s_map.map_x + (int)((w->s_player.px
|
||||
+ cos(w->s_player.pa) * 7) / w->s_map.map_s)] == '3')
|
||||
w->s_map.map[(int)((w->s_player.py + sin(w->s_player.pa) * 7)
|
||||
/ w->s_map.map_s) *w->s_map.map_x + (int)((w->s_player.px
|
||||
+ cos(w->s_player.pa) * 7) / w->s_map.map_s)] = '2';
|
||||
}
|
||||
else
|
||||
{
|
||||
handle_movement_keys(key, w);
|
||||
}
|
||||
return (key);
|
||||
}
|
||||
|
||||
110
move/ft_move.c
110
move/ft_move.c
@@ -6,70 +6,82 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:08:25 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/25 18:33:49 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/29 17:59:17 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
void move_forward(t_structure_main *w, int key) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
void move_forward(t_structure_main *w, int key)
|
||||
{
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 119) { // Touche 'w' pour avancer
|
||||
future_px = w->s_player.px + cos(w->s_player.pa) * w->s_map.mapS;
|
||||
future_py = w->s_player.py + sin(w->s_player.pa) * w->s_map.mapS;
|
||||
|
||||
if (can_move_to(w, future_px, future_py)) {
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
if (key == 119)
|
||||
{
|
||||
future_px = w->s_player.px + cos(w->s_player.pa) * w->s_map.map_s;
|
||||
future_py = w->s_player.py + sin(w->s_player.pa) * w->s_map.map_s;
|
||||
if (can_move_to(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void move_backward(t_structure_main *w, int key) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
void move_backward(t_structure_main *w, int key)
|
||||
{
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 115) { // Touche 's' pour reculer
|
||||
future_px = w->s_player.px - cos(w->s_player.pa) * w->s_map.mapS;
|
||||
future_py = w->s_player.py - sin(w->s_player.pa) * w->s_map.mapS;
|
||||
|
||||
if (can_move_to(w, future_px, future_py)) {
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
if (key == 115)
|
||||
{
|
||||
future_px = w->s_player.px - cos(w->s_player.pa) * w->s_map.map_s;
|
||||
future_py = w->s_player.py - sin(w->s_player.pa) * w->s_map.map_s;
|
||||
if (can_move_to(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void move_right(t_structure_main *w, int key) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
void move_right(t_structure_main *w, int key)
|
||||
{
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 100) { // Touche 'd' pour aller à droite
|
||||
future_px = w->s_player.px + cos(w->s_player.pa + (PI/2)) * w->s_map.mapS;
|
||||
future_py = w->s_player.py + sin(w->s_player.pa + (PI/2)) * w->s_map.mapS;
|
||||
|
||||
if (can_move_to(w, future_px, future_py)) {
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
if (key == 100)
|
||||
{
|
||||
future_px = w->s_player.px + cos(w->s_player.pa
|
||||
+ (PI / 2)) * w->s_map.map_s;
|
||||
future_py = w->s_player.py + sin(w->s_player.pa
|
||||
+ (PI / 2)) * w->s_map.map_s;
|
||||
if (can_move_to(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) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
void move_left(t_structure_main *w, int key)
|
||||
{
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 97) { // Touche 'a' pour aller à gauche
|
||||
future_px = w->s_player.px + cos(w->s_player.pa - (PI/2)) * w->s_map.mapS;
|
||||
future_py = w->s_player.py + sin(w->s_player.pa - (PI/2)) * w->s_map.mapS;
|
||||
|
||||
if (can_move_to(w, future_px, future_py)) {
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
}
|
||||
if (key == 97)
|
||||
{
|
||||
future_px = w->s_player.px + cos(w->s_player.pa
|
||||
- (PI / 2)) * w->s_map.map_s;
|
||||
future_py = w->s_player.py + sin(w->s_player.pa
|
||||
- (PI / 2)) * w->s_map.map_s;
|
||||
if (can_move_to(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)
|
||||
|
||||
Reference in New Issue
Block a user