-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
49 lines (42 loc) · 1.43 KB
/
player.c
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
#include <stdlib.h>
#include "ced/utils.h"
#include "ced/vector.h"
#include "player.h"
#include "config.h"
Player* player_create(V2f position, float angle){
Player* p = (Player*) malloc(sizeof(Player));
p->position = position;
p->walk_speed = PLAYER_WALK_SPEED;
p->run_speed = PLAYER_RUN_SPEED;
p->rotation_speed = PLAYER_ROTATION_SPEED;
player_set_angle(p, angle);
return p;
}
void player_free(Player* p){
free(p);
}
void player_set_angle(Player* p, float angle){
if( angle < 0 ) // Clamp angle from 0 to 2PI
angle += C_2PI;
else if ( angle > C_2PI )
angle -= C_2PI;
p->angle = angle;
p->direction = v2f_from_angle(angle);
}
void player_update(Player* p, float delta_time){
float move_speed = (p->command.run) ? p->run_speed : p->walk_speed;
float forward_speed = p->command.forward * move_speed * delta_time;
float strafe_speed = p->command.strafe * move_speed * delta_time;
float rotation_speed = p->command.rotation * p->rotation_speed * delta_time;
if (fabsf(forward_speed) > 0.0001){
p->position.x += p->direction.x * forward_speed;
p->position.y += p->direction.y * forward_speed;
}
if (fabsf(strafe_speed) > 0.0001){
p->position.x += p->direction.y * strafe_speed;
p->position.y -= p->direction.x * strafe_speed;
}
if (fabsf(rotation_speed) > 0.01){
player_set_angle(p, p->angle + rotation_speed);
}
}