labyrinth

view src/player.c @ 0:8ba79034e8a6

labyrinth example initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 Jan 2015 14:59:38 +0200
parents
children
line source
1 #include <stdio.h>
2 #include <math.h>
3 #include "opengl.h"
4 #include "player.h"
5 #include "level.h"
7 void player_init(struct player *p, struct level *lvl)
8 {
9 p->lvl = lvl;
10 level_cell_to_pos(lvl, lvl->start_pos[0], lvl->start_pos[1], &p->x, &p->y);
11 p->horiz_angle = 0;
12 p->vert_angle = 0;
13 }
15 int player_move(struct player *p, float dfwd, float dright)
16 {
17 int collide;
18 float angle = M_PI * p->horiz_angle / 180.0;
19 float fwd_dirx = sin(angle);
20 float fwd_diry = -cos(angle);
21 float right_dirx = sin(angle + M_PI / 2.0);
22 float right_diry = -cos(angle + M_PI / 2.0);
24 float dx = dfwd * fwd_dirx + dright * right_dirx;
25 float dy = dfwd * fwd_diry + dright * right_diry;
27 if(!dfwd && !dright) return 1;
29 /* calculate collisions */
30 collide = level_collide(p->lvl, 0.7, p->x, p->y, &dx, &dy);
32 p->x += dx;
33 p->y += dy;
35 return !collide;
36 }
38 void player_turn(struct player *p, float horiz_deg, float vert_deg)
39 {
40 float va = p->vert_angle + vert_deg;
42 if(va < -90.0) va = -90.0;
43 if(va > 90.0) va = 90.0;
45 p->horiz_angle += horiz_deg;/*fmod(p->horiz_angle + horiz_deg, 360.0);*/
46 p->vert_angle = va;
47 }
49 void player_setup_view_matrix(struct player *p)
50 {
51 glRotatef(p->vert_angle, 1, 0, 0);
52 glRotatef(p->horiz_angle, 0, 1, 0);
53 glTranslatef(-p->x, 0, -p->y);
54 }