labyrinth

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/player.c	Thu Jan 15 14:59:38 2015 +0200
     1.3 @@ -0,0 +1,54 @@
     1.4 +#include <stdio.h>
     1.5 +#include <math.h>
     1.6 +#include "opengl.h"
     1.7 +#include "player.h"
     1.8 +#include "level.h"
     1.9 +
    1.10 +void player_init(struct player *p, struct level *lvl)
    1.11 +{
    1.12 +	p->lvl = lvl;
    1.13 +	level_cell_to_pos(lvl, lvl->start_pos[0], lvl->start_pos[1], &p->x, &p->y);
    1.14 +	p->horiz_angle = 0;
    1.15 +	p->vert_angle = 0;
    1.16 +}
    1.17 +
    1.18 +int player_move(struct player *p, float dfwd, float dright)
    1.19 +{
    1.20 +	int collide;
    1.21 +	float angle = M_PI * p->horiz_angle / 180.0;
    1.22 +	float fwd_dirx = sin(angle);
    1.23 +	float fwd_diry = -cos(angle);
    1.24 +	float right_dirx = sin(angle + M_PI / 2.0);
    1.25 +	float right_diry = -cos(angle + M_PI / 2.0);
    1.26 +
    1.27 +	float dx = dfwd * fwd_dirx + dright * right_dirx;
    1.28 +	float dy = dfwd * fwd_diry + dright * right_diry;
    1.29 +
    1.30 +	if(!dfwd && !dright) return 1;
    1.31 +
    1.32 +	/* calculate collisions */
    1.33 +	collide = level_collide(p->lvl, 0.7, p->x, p->y, &dx, &dy);
    1.34 +
    1.35 +	p->x += dx;
    1.36 +	p->y += dy;
    1.37 +
    1.38 +	return !collide;
    1.39 +}
    1.40 +
    1.41 +void player_turn(struct player *p, float horiz_deg, float vert_deg)
    1.42 +{
    1.43 +	float va = p->vert_angle + vert_deg;
    1.44 +
    1.45 +	if(va < -90.0) va = -90.0;
    1.46 +	if(va > 90.0) va = 90.0;
    1.47 +
    1.48 +	p->horiz_angle += horiz_deg;/*fmod(p->horiz_angle + horiz_deg, 360.0);*/
    1.49 +	p->vert_angle = va;
    1.50 +}
    1.51 +
    1.52 +void player_setup_view_matrix(struct player *p)
    1.53 +{
    1.54 +	glRotatef(p->vert_angle, 1, 0, 0);
    1.55 +	glRotatef(p->horiz_angle, 0, 1, 0);
    1.56 +	glTranslatef(-p->x, 0, -p->y);
    1.57 +}