blob: d81cc523b1534a46f429a8ffc00c19742717da73 (
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
|
#include <math.h>
#include "player.h"
#define DEFAULT_STAND_HEIGHT 6.0f
#define DEFAULT_DUCK_HEIGHT 2.5f
#define DEFAULT_HEAD_MARGIN 1.0f
#define DEFAULT_KNEE_HEIGHT 2.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->knee_height = DEFAULT_KNEE_HEIGHT;
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; }
if(input[4]) { if(player->velocity.z == 0.0f) player->velocity.z += 0.5f; }
player->ducking = input[5];
bool pressing = input[0] || input[1] || input[2] || input[3] || input[4];
float acceleration = pressing ? 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->velocity.z;
}
|