68 lines
2.3 KiB
C
68 lines
2.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* :::::::: */
|
|
/* player.c :+: :+: */
|
|
/* +:+ */
|
|
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
|
/* +#+ */
|
|
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
|
/* Updated: 2025/04/18 12:15:31 by whaffman ######## odam.nl */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "cub3d.h"
|
|
|
|
int player_create(t_game **game)
|
|
{
|
|
t_player *player;
|
|
|
|
player = malloc(sizeof(t_player));
|
|
if (!player)
|
|
return (FAILURE);
|
|
player->pos.x = 2 * TILE_SIZE;
|
|
player->pos.y = 2 * TILE_SIZE;
|
|
player->angle = 0.f;
|
|
player->speed = 80.f;
|
|
player->fov = 90.f;
|
|
(*game)->player = player;
|
|
return (SUCCESS);
|
|
}
|
|
|
|
|
|
static void move(t_map *map, t_player *player, int dir, float delta)
|
|
{
|
|
float xa;
|
|
float ya;
|
|
|
|
xa = dir * (sin(player->angle) * player->speed * delta);
|
|
ya = dir * -1 * (cos(player->angle) * 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;
|
|
}
|
|
|
|
void player_update(t_game *game)
|
|
{
|
|
if (get_key(game, MLX_KEY_W))
|
|
move(game->map, game->player, 1, game->screen->mlx->delta_time);
|
|
else if (get_key(game, MLX_KEY_S))
|
|
move(game->map, game->player, -1, game->screen->mlx->delta_time);
|
|
if (get_key(game, MLX_KEY_LEFT))
|
|
game->player->angle -= .1f;
|
|
else if (get_key(game, MLX_KEY_RIGHT))
|
|
game->player->angle += .1f;
|
|
}
|
|
|
|
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, player->pos, 4, 0x3294a8ff);
|
|
direction.x = player->pos.x + sin(player->angle) * 30;
|
|
direction.y = player->pos.y - cos(player->angle) * 30;
|
|
render_line(screen, player->pos, direction, 0xa83232ff);
|
|
}
|