summaryrefslogtreecommitdiff
path: root/player.c
blob: 68f77ff22afddbee11be85211e594bd80bc2a526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   player.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: kdx    <kdx   @student.42angouleme.fr      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/10/09 08:29:41 by kdx               #+#    #+#             */
/*   Updated: 2022/10/14 14:36:25 by kdx              ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "player.h"
#include "sprite.h"
#include "game.h"
#include "map.h"
#include <math.h>
#include <stdbool.h>

static void	player_move(t_player *player, t_map *map)
{
	int	i;
	int	sign;

	if (player_collide(map, player->pos[0], player->pos[1], TILE_WALL))
		return ;
	i = -1;
	while (++i < 2)
	{
		sign = (player->vel[i] > 0) - (player->vel[i] < 0);
		player->pos[i] += player->vel[i];
		while (player_collide(map, player->pos[0], player->pos[1], TILE_WALL))
		{
			player->vel[i] = 0.0;
			player->pos[i] = round(player->pos[i]) - sign;
		}
	}
}

void	player_init(t_player *player, double x, double y)
{
	player->pos[0] = x;
	player->pos[1] = y;
	player->vel[0] = 0.0;
	player->vel[1] = 0.0;
}

static void	player_update_end(t_sily *sily, t_player *player)
{
	player_move(player, &sily->game->map);
	player_collect(sily->game, player, &sily->game->map);
	player_collide_exit(sily, player, &sily->game->map);
	if (player_collide_badeline(player, &sily->game->badeline))
	{
		ft_putendl_fd("oof", 1);
		sily->quit = true;
	}
}

void	player_update(t_sily *sily, t_player *player)
{
	double	dir[2];
	int		i;

	dir[0] = sily->input[K_RIGHT] - sily->input[K_LEFT];
	dir[1] = sily->input[K_DOWN] - sily->input[K_UP];
	player->invincible -= (player->invincible > 0);
	if ((dir[0] || dir[1]) && !sily->game->badeline.active)
	{
		sily->game->badeline.active = true;
		player->invincible = PLAYER_INVINCIBLE;
	}
	i = 0;
	while (i < 2)
	{
		player->old_pos[i] = player->pos[i];
		if (dir[0] && dir[1])
			dir[i] /= 1.4142135623730951;
		i++;
	}
	player->vel[0] *= 1.0 - PLAYER_FRICTION;
	player->vel[0] += PLAYER_ACCELERATION * dir[0];
	player->vel[1] += PLAYER_THRUSTER * dir[1];
	player->vel[1] += PLAYER_GRAVITY;
	player_update_end(sily, player);
}

void	player_draw(t_sily *sily, t_player *player)
{
	sprite_draw(sily, sily->game->s_player, round(player->pos[0]),
		round(player->pos[1]));
}