player, collision: split it up

This commit is contained in:
Quinten 2025-04-22 14:44:15 +02:00
parent 8cd885f6f8
commit cde704228f
4 changed files with 54 additions and 17 deletions

21
inc/collision.h Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collision.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 14:40:47 by qmennen #+# #+# */
/* Updated: 2025/04/22 14:42:35 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef COLLISION_H
# define COLLISION_H
# include "cub3d.h"
int collision_horizontal(t_map *map, t_player *player, float xa);
int collision_vertical(t_map *map, t_player *player, float ya);
#endif

View File

@ -45,6 +45,7 @@
# include "hooks.h"
# include "render.h"
# include "player.h"
# include "collision.h"
# include "parser.h"
#endif

30
src/collision.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/04/22 14:42:09 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "collision.h"
int collision_horizontal(t_map *map, t_player *player, float xa)
{
t_tile tile;
tile = get_tile(map, (int) ((player->pos.x + xa) / TILE_SIZE), (int) ((player->pos.y) / TILE_SIZE));
return (tile != TILE_WALL && tile != TILE_VOID);
}
int collision_vertical(t_map *map, t_player *player, float ya)
{
t_tile tile;
tile = get_tile(map, (int) ((player->pos.x) / TILE_SIZE), (int) ((player->pos.y + ya) / TILE_SIZE));
return (tile != TILE_WALL && tile != TILE_VOID);
}

View File

@ -30,21 +30,6 @@ int player_create(t_game **game)
return (SUCCESS);
}
static int check_horizontal(t_map *map, t_player *player, float xa)
{
t_tile tile;
tile = get_tile(map, (int) ((player->pos.x + xa) / TILE_SIZE), (int) ((player->pos.y) / TILE_SIZE));
return (tile != TILE_WALL && tile != TILE_VOID);
}
static int check_vertical(t_map *map, t_player *player, float ya)
{
t_tile tile;
tile = get_tile(map, (int) ((player->pos.x) / TILE_SIZE), (int) ((player->pos.y + ya) / TILE_SIZE));
return (tile != TILE_WALL && tile != TILE_VOID);
}
static void move(t_map *map, t_player *player, int dir, float delta)
{
@ -53,9 +38,9 @@ static void move(t_map *map, t_player *player, int dir, float delta)
xa = dir * (sin(player->angle) * player->speed * delta);
ya = dir * -1 * (cos(player->angle) * player->speed * delta);
if ( xa != 0 && check_horizontal(map, player, xa))
if ( xa != 0 && collision_horizontal(map, player, xa))
player->pos.x += xa;
if ( ya != 0 && check_vertical(map, player, ya))
if ( ya != 0 && collision_vertical(map, player, ya))
player->pos.y += ya;
}