summaryrefslogtreecommitdiff
path: root/src/player.c
blob: 2231b9e7397df86bdfb0fc36209b0810f47b17c0 (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
#include <math.h>
#include "player.h"

#define DEFAULT_STAND_HEIGHT 6.0f
#define DEFAULT_DUCK_HEIGHT  2.5f
#define DEFAULT_HEAD_MARGIN  1.0f

void player_default(player_t *player)
{
    player->pos = (struct xyz){0.0f, 0.0f, 0.0f};
    player->velocity = (struct xyz){0.0f, 0.0f, 0.0f};

    player->angle = 0.0f; player->anglesin = 0.0f; player->anglecos = 1.0f;
    player->sector = 0;

    player->stand_height = DEFAULT_STAND_HEIGHT;
    player->duck_height = DEFAULT_DUCK_HEIGHT;
    player->head_margin = DEFAULT_HEAD_MARGIN;

    player->ground = false; player->falling = true;
    player->moving = false; player->ducking = false;
}
void player_input(player_t *player, bool *input, int rmouse_x, int rmouse_y)
{
    (void)rmouse_y;
    player->angle += rmouse_x * 0.03f;

    float move_vec[2] = {0.f, 0.f};
    if(input[0]) { move_vec[0] += player->anglecos*0.2f; move_vec[1] += player->anglesin*0.2f; }
    if(input[2]) { move_vec[0] -= player->anglecos*0.2f; move_vec[1] -= player->anglesin*0.2f; }
    if(input[1]) { move_vec[0] += player->anglesin*0.2f; move_vec[1] -= player->anglecos*0.2f; }
    if(input[3]) { move_vec[0] -= player->anglesin*0.2f; move_vec[1] += player->anglecos*0.2f; }
    player->moving = input[0] || input[1] || input[2] || input[3];
    float acceleration = player->moving ? 0.4f : 0.2f;

    player->velocity.x = player->velocity.x * (1-acceleration) + move_vec[0] * acceleration;
    player->velocity.y = player->velocity.y * (1-acceleration) + move_vec[1] * acceleration;

    player->anglesin = sinf(player->angle);
    player->anglecos = cosf(player->angle);
}

void player_update(player_t *player)
{
    player->pos.x += player->velocity.x;
    player->pos.y += player->velocity.y;
    player->pos.z = player->stand_height; // temp
}