fixed mixing of turning with mouse and keys
[vrlugburz] / src / player.c
1 #include "player.h"
2
3 void init_player(struct player *p)
4 {
5         memset(p, 0, sizeof *p);
6         cgm_qcons(&p->vrot, 0, 0, 0, 1);
7
8         p->height = 1.75;
9         p->hp = p->hp_max = 10;
10         p->mp = p->mp_max = 10;
11 }
12
13 #define TWO_PI          ((float)M_PI * 2.0f)
14 #define HALF_PI         ((float)M_PI / 2.0f)
15
16 void update_player_dir(struct player *p)
17 {
18         int dir;
19         float angle;
20
21         /* TODO: take vrot into account */
22         angle = fmod(p->theta, TWO_PI);
23         if(angle < 0) angle += TWO_PI;
24
25         p->theta = angle;       /* renormalize theta */
26         p->dir = (int)(4.0f * angle / TWO_PI + 0.5) & 3;
27 }
28
29 void move_player(struct player *p, int right, int fwd)
30 {
31         static const int step[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
32         int fdir, rdir;
33         float angle;
34
35         update_player_dir(p);
36
37         fdir = p->dir & 3;
38         rdir = (fdir + 1) & 3;
39         p->cx += step[fdir][0] * fwd + step[rdir][0] * right;
40         p->cy += step[fdir][1] * fwd + step[rdir][1] * right;
41 }
42
43 void turn_player(struct player *p, int turn)
44 {
45         if(!turn) return;
46
47         p->theta += turn > 0 ? HALF_PI : -HALF_PI;
48
49         update_player_dir(p);
50         p->theta = (float)p->dir * HALF_PI;     /* snap theta */
51 }
52
53 void upd_player_xform(struct player *p)
54 {
55         cgm_vec3 pos;
56         float celld = p->lvl ? p->lvl->cell_size : DEF_CELL_SIZE;
57
58         cgm_vcons(&pos, p->cx * celld, p->height, -p->cy * celld);
59         cgm_vadd(&pos, &p->cpos);
60
61         cgm_midentity(p->view_xform);
62         cgm_mprerotate_x(p->view_xform, -p->phi);
63         cgm_mprerotate_y(p->view_xform, p->theta);
64         cgm_mrotate_quat(p->view_xform, &p->vrot);
65         cgm_mpretranslate(p->view_xform, -pos.x, -pos.y, -pos.z);
66 }
67
68 void upd_player_vis(struct player *p)
69 {
70         p->vis = 0;
71
72
73 }