92 lines
3.0 KiB
C
92 lines
3.0 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* player.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
|
/* Updated: 2025/05/06 15:20:18 by qmennen ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "cub3d.h"
|
|
|
|
int player_create(t_game **game)
|
|
{
|
|
t_player *player;
|
|
|
|
player = malloc(sizeof(t_player));
|
|
if (!player)
|
|
return (FAILURE);
|
|
player->pos.x = 1;
|
|
player->pos.y = 1;
|
|
player->dir.x = 1;
|
|
player->dir.y = 0;
|
|
player->camera.x = 0;
|
|
player->camera.y = 0.66f;
|
|
player->speed = 3.f;
|
|
player->fov = 90.f;
|
|
(*game)->player = player;
|
|
return (SUCCESS);
|
|
}
|
|
|
|
static void move(t_map *map, t_player *player, int dir, double delta)
|
|
{
|
|
double xa;
|
|
double ya;
|
|
|
|
xa = dir * player->dir.x * player->speed * delta;
|
|
ya = dir * player->dir.y * player->speed * delta;
|
|
if (xa != 0 && collision_horizontal(map, player, xa))
|
|
player->pos.x += xa;
|
|
if (ya != 0 && collision_vertical(map, player, ya))
|
|
player->pos.y += ya;
|
|
}
|
|
|
|
static void strave(t_map *map, t_player *player, int dir, double delta)
|
|
{
|
|
double xa;
|
|
double ya;
|
|
|
|
xa = dir * perp(player->dir).x * player->speed * delta;
|
|
ya = dir * perp(player->dir).y * player->speed * delta;
|
|
if (xa != 0 && collision_horizontal(map, player, xa))
|
|
player->pos.x += xa;
|
|
if (ya != 0 && collision_vertical(map, player, ya))
|
|
player->pos.y += ya;
|
|
}
|
|
|
|
static void rotate(t_player *player, double rot_speed)
|
|
{
|
|
player->dir = rot(player->dir, rot_speed);
|
|
player->camera = rot(player->camera, rot_speed);
|
|
}
|
|
|
|
void player_update(t_game *game, double delta_time)
|
|
{
|
|
if (get_key(game, MLX_KEY_W))
|
|
move(game->map, game->player, 1, delta_time);
|
|
else if (get_key(game, MLX_KEY_S))
|
|
move(game->map, game->player, -1, delta_time);
|
|
if (get_key(game, MLX_KEY_A))
|
|
strave(game->map, game->player, -1, delta_time);
|
|
else if (get_key(game, MLX_KEY_D))
|
|
strave(game->map, game->player, 1, delta_time);
|
|
if (get_key(game, MLX_KEY_LEFT))
|
|
rotate(game->player, -.05f);
|
|
else if (get_key(game, MLX_KEY_RIGHT))
|
|
rotate(game->player, .05f);
|
|
}
|
|
|
|
void player_render(t_screen *screen, t_player *player)
|
|
{
|
|
t_vec2 direction;
|
|
|
|
if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height)
|
|
return;
|
|
render_circle(screen, mul(player->pos, TILE_SIZE), 4, 0x111111ff);
|
|
direction = add(mul(player->pos, TILE_SIZE), mul(player->dir, TILE_SIZE));
|
|
render_line(screen, mul(player->pos, TILE_SIZE), direction, 0xa83232ff);
|
|
}
|