eobish

view src/player.c @ 4:ce0548d24918

mostly works
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 18 Jan 2015 13:30:30 +0200
parents
children
line source
1 #include <stdio.h>
2 #include "player.h"
3 #include "level.h"
5 static char *dirnames[] = {
6 "north", "east", "south", "west"
7 };
9 void player_init(struct player *p, struct level *lvl)
10 {
11 p->x = lvl->start_pos[0];
12 p->y = lvl->start_pos[1];
13 p->lvl = lvl;
14 p->dir = DIR_NORTH;
15 }
17 int player_move(struct player *p, int dfwd, int dside)
18 {
19 int nx, ny;
20 int dx, dy;
22 player_dir_vec(p, &dx, &dy);
23 nx = p->x + dx * dfwd - dy * dside;
24 ny = p->y + dy * dfwd + dx * dside;
26 if(level_cell(p->lvl, nx, ny) == '#') {
27 return -1;
28 }
29 p->x = nx;
30 p->y = ny;
31 return 0;
32 }
34 void player_turn(struct player *p, int angle)
35 {
36 int dir = (p->dir + angle) % 4;
37 if(dir < 0) dir += 4;
39 printf("turn(%d): %s -> %s\n", angle, dirnames[p->dir], dirnames[dir]);
40 p->dir = dir;
41 }
43 void player_dir_vec(struct player *p, int *dx, int *dy)
44 {
45 *dx = p->dir == DIR_EAST ? 1 : (p->dir == DIR_WEST ? -1 : 0);
46 *dy = p->dir == DIR_NORTH ? -1 : (p->dir == DIR_SOUTH ? 1 : 0);
47 }