player, collision: split it up
This commit is contained in:
parent
8cd885f6f8
commit
cde704228f
21
inc/collision.h
Normal file
21
inc/collision.h
Normal 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
|
||||||
@ -45,6 +45,7 @@
|
|||||||
# include "hooks.h"
|
# include "hooks.h"
|
||||||
# include "render.h"
|
# include "render.h"
|
||||||
# include "player.h"
|
# include "player.h"
|
||||||
|
# include "collision.h"
|
||||||
# include "parser.h"
|
# include "parser.h"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
30
src/collision.c
Normal file
30
src/collision.c
Normal 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);
|
||||||
|
}
|
||||||
19
src/player.c
19
src/player.c
@ -30,21 +30,6 @@ int player_create(t_game **game)
|
|||||||
return (SUCCESS);
|
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)
|
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);
|
xa = dir * (sin(player->angle) * player->speed * delta);
|
||||||
ya = dir * -1 * (cos(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;
|
player->pos.x += xa;
|
||||||
if ( ya != 0 && check_vertical(map, player, ya))
|
if ( ya != 0 && collision_vertical(map, player, ya))
|
||||||
player->pos.y += ya;
|
player->pos.y += ya;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user