summaryrefslogtreecommitdiffstats
path: root/src/player.c
blob: 9ababd4fe0116d64b8023371cd6ceed798cf4904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2021  P. J. McDermott
 *
 * This file is part of Maze Fight
 *
 * Maze Fight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Maze Fight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Maze Fight.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <SDL.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "maze.h"
#include "player.h"

enum _mf_player_dir {
	MF_PLAYER_DIR_N_,
	MF_PLAYER_DIR_U_,
	MF_PLAYER_DIR_D_,
	MF_PLAYER_DIR_L_,
	MF_PLAYER_DIR_R_
};

struct mf_player {
	struct mf_maze      *maze;
	int                  cell_width;
	int                  speed;
	enum _mf_player_dir  cur_dir;
	enum _mf_player_dir  new_dir;
	int                  turning;
	int                  cur_x;
	int                  cur_y;
	int                  new_x;
	int                  new_y;
	int                  travel;
};

struct mf_player *
mf_player_new(struct mf_maze *maze, int cell_width)
{
	struct mf_player *p;

	p = calloc(1, sizeof(*p));
	if (p == NULL) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't create player: %s",
				strerror(errno));
		return NULL;
	}

	p->maze       = maze;
	p->cell_width = cell_width;
	p->speed      = MF_PLAYER_SPEED;
	p->new_dir    = MF_PLAYER_DIR_N_;
	p->turning    = 0;
	p->cur_x      = 0;
	p->cur_y      = 0;
	p->travel     = 0;

	if (mf_maze_is_wall(maze, 0, 0, 1, 0)) {
		p->cur_dir = MF_PLAYER_DIR_D_;
	} else {
		p->cur_dir = MF_PLAYER_DIR_R_;
	}

	return p;
}

static void
_mf_player_move(struct mf_player *p, enum _mf_player_dir dir)
{
	p->new_dir = dir;
}

void
mf_player_key_event(struct mf_player *p, SDL_Event *e)
{
	switch (e->type) {
		case SDL_KEYDOWN:
			switch (e->key.keysym.sym) {
				case SDLK_UP:
					_mf_player_move(p, MF_PLAYER_DIR_U_);
					break;
				case SDLK_DOWN:
					_mf_player_move(p, MF_PLAYER_DIR_D_);
					break;
				case SDLK_LEFT:
					_mf_player_move(p, MF_PLAYER_DIR_L_);
					break;
				case SDLK_RIGHT:
					_mf_player_move(p, MF_PLAYER_DIR_R_);
					break;
				default:
					break;
			}
		default:
			break;
	}
}

int
mf_player_update(struct mf_player *p)
{
	int dx;
	int dy;

	if (p->travel > 0) {
		/* Currently moving */
		p->travel += p->speed;
		if (p->travel >= p->cell_width) {
			/* Reached next cell */
			p->cur_x = p->new_x;
			p->cur_y = p->new_y;
			if (p->cur_dir == p->new_dir) {
				/* Want to continue straight */
				p->travel -= p->cell_width;
			} else {
				/* Want to stop or turn */
				p->travel = 0;
			}
		} else {
			/* Farther to go */
			return 0;
		}
	}
	if (p->new_dir != MF_PLAYER_DIR_N_ && p->cur_dir != p->new_dir) {
		/* Want to turn */
		p->cur_dir = p->new_dir;
		p->new_dir = MF_PLAYER_DIR_N_;
		p->turning = MF_PLAYER_TURN_TIME + 1;
	}
	if (p->turning > 0) {
		/* Turning */
		--p->turning;
	}
	if (p->turning == 0) {
		/* Done turning */
		if (p->new_dir != MF_PLAYER_DIR_N_) {
			/* Want to move */
			switch (p->new_dir) {
				case MF_PLAYER_DIR_U_: dx =  0; dy = -1; break;
				case MF_PLAYER_DIR_D_: dx =  0; dy =  1; break;
				case MF_PLAYER_DIR_L_: dx = -1; dy =  0; break;
				case MF_PLAYER_DIR_R_: dx =  1; dy =  0; break;
				default:               dx =  0; dy =  0; break;
			}
			p->new_dir = MF_PLAYER_DIR_N_;
			if (mf_maze_is_wall(p->maze, p->cur_x, p->cur_y,
						dx, dy)) {
				return 0;
			}
			p->new_x = p->cur_x + dx;
			p->new_y = p->cur_y + dy;
			p->travel += p->speed;
		}
	}

	return 0;
}

int
mf_player_render(struct mf_player *p, SDL_Renderer *renderer)
{
	int e   = 0;
	int cx;
	int cy;
	int r;
	int x;
	int y;
	int i;

	if (SDL_SetRenderDrawColor(renderer,
				MF_COLOR_PLYR_R, MF_COLOR_PLYR_G,
				MF_COLOR_PLYR_B, MF_COLOR_PLYR_A) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't render player: %s",
				SDL_GetError());
		e = -1;
	}

	cx = p->cur_x * p->cell_width + p->cell_width / 2;
	cy = p->cur_y * p->cell_width + p->cell_width / 2;
	switch (p->cur_dir) {
		case MF_PLAYER_DIR_U_: cy -= p->travel; break;
		case MF_PLAYER_DIR_D_: cy += p->travel; break;
		case MF_PLAYER_DIR_L_: cx -= p->travel; break;
		case MF_PLAYER_DIR_R_: cx += p->travel; break;
		default: break;
	}
	r = p->cell_width / 2 - MF_PLAYER_P;

#define _mf_player_px(X, Y) \
	do { \
		if (SDL_RenderDrawPoint(renderer, cx + X, cy + Y) < 0) { \
			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
					"Couldn't render widget: %s", \
					SDL_GetError()); \
			e = -1; \
		} \
	} while (0)

	/* TODO: This is one pixel larger than it should be. */
	for (x = 0, y = r; y > x; ++x) {
		y = round(sqrt((r-0.5)*(r-0.5) - (x-0.5)*(x-0.5)) + 0.5);
		for (i = 0; i <=   y; ++i) _mf_player_px(  x,   i);
		for (i = 0; i >= 0-y; --i) _mf_player_px(  x,   i);
		for (i = 0; i <=   y; ++i) _mf_player_px(0-x,   i);
		for (i = 0; i >= 0-y; --i) _mf_player_px(0-x,   i);
		for (i = 0; i <=   y; ++i) _mf_player_px(  i,   x);
		for (i = 0; i <=   y; ++i) _mf_player_px(  i, 0-x);
		for (i = 0; i >= 0-y; --i) _mf_player_px(  i,   x);
		for (i = 0; i >= 0-y; --i) _mf_player_px(  i, 0-x);
	}

#undef _mf_player_px

	return e;
}

void
mf_player_destroy(struct mf_player **p_p)
{
	struct mf_player *p;

	if (p_p == NULL || *p_p == NULL) {
		return;
	}
	p = *p_p;

	free(p);
	p = NULL;
}