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