mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
sprite
This commit is contained in:
@@ -6,45 +6,29 @@
|
||||
/* 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 */
|
||||
/* Updated: 2024/01/25 20:43:43 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 can_move_to(t_structure_main *w, double future_x, double future_y) {
|
||||
int map_x, map_y;
|
||||
|
||||
int check_collision(t_structure_main *w, int future_px, int future_py)
|
||||
{
|
||||
int future_ipx;
|
||||
int future_ipy;
|
||||
// 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};
|
||||
|
||||
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');
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
printf("Pas de collision, peut se déplacer\n");
|
||||
return 1; // Pas de collision, peut se déplacer
|
||||
}
|
||||
|
||||
@@ -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 12:41:41 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/25 18:45:14 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 = 16 * ((PI / 3) / NUMRAY);
|
||||
angle_adjustment = 4 * (PI/180);
|
||||
if (key == 65361)
|
||||
{
|
||||
w->s_player.pa -= angle_adjustment;
|
||||
@@ -80,10 +80,10 @@ int deal_key(int key, t_structure_main *w)
|
||||
else if (key == 101) //e door
|
||||
{
|
||||
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)] == '1')
|
||||
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';
|
||||
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)] == '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)] = '1';
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -6,73 +6,72 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:08:25 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/25 15:09:55 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/25 18:33:49 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;
|
||||
void move_forward(t_structure_main *w, int key) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 119 )
|
||||
{
|
||||
if (w->s_map.map[(int)((w->s_player.py)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + (cos(w->s_player.pa) * w->s_map.mapS))/w->s_map.mapS)]=='0')
|
||||
w->s_player.px += cos(w->s_player.pa) * w->s_map.mapS;
|
||||
if (w->s_map.map[(int)((w->s_player.py + (sin(w->s_player.pa) * w->s_map.mapS))/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px)/w->s_map.mapS)]=='0')
|
||||
w->s_player.py += sin(w->s_player.pa) * w->s_map.mapS;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void move_backward(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
void move_backward(t_structure_main *w, int key) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 115 )
|
||||
{
|
||||
if (w->s_map.map[(int)((w->s_player.py)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + (cos(w->s_player.pa + (PI)) * w->s_map.mapS))/w->s_map.mapS)]=='0')
|
||||
w->s_player.px += cos(w->s_player.pa + (PI)) * w->s_map.mapS;
|
||||
if (w->s_map.map[(int)((w->s_player.py + (sin(w->s_player.pa + (PI)) * w->s_map.mapS))/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px)/w->s_map.mapS)]=='0')
|
||||
w->s_player.py += sin(w->s_player.pa + (PI)) * w->s_map.mapS;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void move_right(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
void move_right(t_structure_main *w, int key) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 100 )
|
||||
{
|
||||
if (w->s_map.map[(int)((w->s_player.py)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + (cos(w->s_player.pa + (PI/2)) * w->s_map.mapS))/w->s_map.mapS)]=='0')
|
||||
w->s_player.px += cos(w->s_player.pa + (PI/2)) * w->s_map.mapS;
|
||||
if (w->s_map.map[(int)((w->s_player.py + (sin(w->s_player.pa + (PI/2)) * w->s_map.mapS))/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px)/w->s_map.mapS)]=='0')
|
||||
w->s_player.py += sin(w->s_player.pa + (PI/2)) * w->s_map.mapS;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void move_left(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
void move_left(t_structure_main *w, int key) {
|
||||
double future_px;
|
||||
double future_py;
|
||||
|
||||
if (key == 97)
|
||||
{
|
||||
if (w->s_map.map[(int)((w->s_player.py)/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px + (cos(w->s_player.pa + (3*(PI/2))) * w->s_map.mapS))/w->s_map.mapS)]=='0')
|
||||
w->s_player.px += cos(w->s_player.pa + (3*(PI/2))) * w->s_map.mapS;
|
||||
if (w->s_map.map[(int)((w->s_player.py + (sin(w->s_player.pa + (3*(PI/2))) * w->s_map.mapS))/w->s_map.mapS) * w->s_map.mapX + (int)((w->s_player.px)/w->s_map.mapS)]=='0')
|
||||
w->s_player.py += sin(w->s_player.pa + (3*(PI/2))) * w->s_map.mapS;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void move(int key, t_structure_main *w)
|
||||
{
|
||||
move_forward(w, key);
|
||||
|
||||
Reference in New Issue
Block a user