-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.c
105 lines (86 loc) · 2.29 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
struct PlayerInput
{
bool forward;
bool backward;
bool left;
bool right;
bool jump;
v3 rot_look;
};
struct PlayerState
{
v3 pos;
v3 rot;
//u8 action;
};
struct Player
{
struct PlayerInput input;
struct PlayerState state;
/* Other state */
f32 rot_z_t;
f32 rot_z_target;
v3 dir_looking;
v3 dir_walking;
bool cam_fps;
v3 eyes_pos;
mat4 transform;
};
#define FLOOR_HEIGHT 2
void
player_control(struct PlayerInput *input, struct PlayerState *state, f32 dt)
{
/* Constants */
f32 speed_walk = 1.4 * dt;
//f32 speed_run = 3.7 * dt;
f32 speed_run = 10 * dt;
f32 speed_jump = 1.4 * dt;
f32 speed_gravity = 1.4 * dt;
f32 sensitivity = 4;
/* Variables */
bool jump = false;
v3 new_pos = V3_ZERO;
v3 new_dir_walking = V3_ZERO;
v3 new_dir_looking = V3_ZERO;
v3 new_dir_input = V3_ZERO;
f32 new_rot_forward = 0.0f;
f32 new_rot_z = 0;
bool free_look = false;
bool move = true;
new_rot_forward = input->rot_look.z;
//if(player->rot.z != player->rot_z_target) player->rot_z_t += 0.1 * dt;
//else player->rot_z_t = 0;
/* Keyboard */
if(input->jump) jump = true;
if(input->forward) new_dir_input.y += 1;
if(input->left) new_dir_input.x -= 1;
if(input->backward) new_dir_input.y -= 1;
if(input->right) new_dir_input.x += 1;
/* TODO INCOMPLETE(lungu): Interpolate between rotations */
/* NOTE(lungu): Change the rotation to quaternion */
if(new_dir_input.x && !new_dir_input.y) new_rot_z = new_rot_forward + 90 * new_dir_input.x;
else if(new_dir_input.x && new_dir_input.y == 1) new_rot_z = new_rot_forward + (45 * new_dir_input.x);
else if(!new_dir_input.x && new_dir_input.y == 1) new_rot_z = new_rot_forward + 0;
else if(new_dir_input.x && new_dir_input.y == -1) new_rot_z = new_rot_forward + (135 * new_dir_input.x);
else if(!new_dir_input.x && new_dir_input.y == -1) new_rot_z = new_rot_forward + 180;
else
{
new_rot_z = state->rot.z;
move = false;
}
new_rot_z = f32_mod(new_rot_z, 360);
if(move)
{
/* Update state */
new_dir_walking = v3_dir_from_rot(V3(0, 0, new_rot_z));
new_pos = v3_mf(new_dir_walking, speed_run);
}
state->rot.z = new_rot_z;
/* Physics */
new_pos = v3_a(state->pos, new_pos);
new_pos.z -= speed_gravity;
f32 floor_height = FLOOR_HEIGHT;
if(new_pos.z < floor_height) new_pos.z = floor_height;
/* NOTE(lungu): TERM */
state->pos = new_pos;
}