summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/char.h3
-rw-r--r--src/char/enemy.c36
-rw-r--r--src/game.c71
3 files changed, 54 insertions, 56 deletions
diff --git a/src/char.h b/src/char.h
index 2c31ca2..ac25c14 100644
--- a/src/char.h
+++ b/src/char.h
@@ -29,7 +29,8 @@ struct mf_char *
mf_player_new(struct mf_maze *maze, int cell_width);
struct mf_char *
-mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size);
+mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size,
+ struct mf_char *prev_enemy);
void
mf_char_get_vector(struct mf_char *c, int *x, int *y, int *travel,
diff --git a/src/char/enemy.c b/src/char/enemy.c
index fb259a2..b981da7 100644
--- a/src/char/enemy.c
+++ b/src/char/enemy.c
@@ -25,13 +25,20 @@
#include "char.h"
struct mf_enemy {
- struct mf_char parent;
+ struct mf_char parent;
+ struct mf_char *next_enemy;
};
static int
-_mf_enemy_update(struct mf_char *c __attribute__((__unused__)))
+_mf_enemy_update(struct mf_char *c)
{
- return 0;
+ struct mf_enemy *p = (struct mf_enemy *) c;
+
+ if (p->next_enemy != NULL) {
+ return mf_char_update(p->next_enemy);
+ } else {
+ return 0;
+ }
}
static int
@@ -112,19 +119,28 @@ _mf_enemy_collide(struct mf_char *c)
}
static int
-_mf_enemy_render(struct mf_char *c __attribute__((__unused__)),
- SDL_Renderer *renderer __attribute__((__unused__)))
+_mf_enemy_render(struct mf_char *c, SDL_Renderer *renderer)
{
- return 0;
+ struct mf_enemy *p = (struct mf_enemy *) c;
+
+ if (p->next_enemy != NULL) {
+ return mf_char_render(p->next_enemy, renderer);
+ } else {
+ return 0;
+ }
}
static void
-_mf_enemy_destroy(struct mf_char *c __attribute__((__unused__)))
+_mf_enemy_destroy(struct mf_char *c)
{
+ struct mf_enemy *p = (struct mf_enemy *) c;
+
+ mf_char_destroy(&p->next_enemy);
}
struct mf_char *
-mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size)
+mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size,
+ struct mf_char *prev_enemy)
{
struct mf_char *c;
struct mf_enemy *p __attribute__((__unused__));
@@ -163,6 +179,10 @@ mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size)
_mf_enemy_step(c);
c->cur_dir = c->new_dir;
+ if (prev_enemy != NULL) {
+ ((struct mf_enemy *) prev_enemy)->next_enemy = c;
+ }
+
return c;
}
diff --git a/src/game.c b/src/game.c
index 0c94202..22d6e11 100644
--- a/src/game.c
+++ b/src/game.c
@@ -144,8 +144,9 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
SDL_Renderer *renderer)
{
struct mf_maze *maze = NULL;
- struct mf_char **chars = NULL;
- int i;
+ struct mf_char *player = NULL;
+ struct mf_char *enemy_head = NULL;
+ struct mf_char *enemy_tail = NULL;
SDL_Color maze_color;
char *font_path = NULL;
TTF_Font *text_font = NULL;
@@ -156,7 +157,6 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
int fr;
int won;
Uint32 beg;
- int j;
int secs;
Uint32 end;
Uint32 delay;
@@ -172,22 +172,19 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
maze_color.b = MF_COLOR_MAZE_B;
maze_color.a = MF_COLOR_MAZE_A;
- chars = calloc(1 + enemies, sizeof(*chars));
- if (chars == NULL) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
- "Couldn't allocate characters: %s",
- strerror(errno));
- goto err;
- }
- chars[0] = mf_player_new(maze, MF_WINDOW_H / size);
- if (chars[0] == NULL) {
+ player = mf_player_new(maze, MF_WINDOW_H / size);
+ if (player == NULL) {
goto err;
}
- for (i = 1; i < 1 + enemies; ++i) {
- chars[i] = mf_enemy_new(maze, MF_WINDOW_H / size, size);
- if (chars[i] == NULL) {
+ for (; enemies > 0; --enemies) {
+ enemy_tail = mf_enemy_new(maze, MF_WINDOW_H / size, size,
+ enemy_tail);
+ if (enemy_tail == NULL) {
goto err;
}
+ if (enemy_head == NULL) {
+ enemy_head = enemy_tail;
+ }
}
font_path = mf_strcat(mf_get_fonts_dir(), "/FifteenTwenty-Regular.ttf");
@@ -233,7 +230,7 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
default:
break;
}
- mf_player_key_event(chars[0], &event);
+ mf_player_key_event(player, &event);
switch (mftk_window_event(win, &event)) {
case 0:
break;
@@ -244,16 +241,9 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
}
}
if (won == SDL_FALSE) {
- for (i = 0; i < 1 + enemies; ++i) {
- mf_char_update(chars[i]);
- }
- for (i = 1; i < 1 + enemies; ++i) {
- for (j = i + 1; j < 1 + enemies; ++j) {
- mf_char_collision(chars[i], chars[j],
- SDL_TRUE);
- }
- }
- mf_char_get_vector(chars[0], &game.player_cx,
+ mf_char_update(player);
+ mf_char_update(enemy_head);
+ mf_char_get_vector(player, &game.player_cx,
&game.player_cy, &game.player_travel,
&game.player_dx, &game.player_dy);
game.player_x = game.player_cx * MF_WINDOW_H / size;
@@ -269,9 +259,8 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
MF_COLOR_BACK_R, MF_COLOR_BACK_G,
MF_COLOR_BACK_B, MF_COLOR_BACK_A);
SDL_RenderClear(renderer);
- for (i = 0; i < 1 + enemies; ++i) {
- mf_char_render(chars[i], renderer);
- }
+ mf_char_render(player, renderer);
+ mf_char_render(enemy_head, renderer);
if (fow == SDL_TRUE && _mf_game_fow(renderer, &game, maze,
MF_WINDOW_H / size) < 0) {
goto err;
@@ -309,12 +298,8 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
TTF_CloseFont(text_font);
text_font = NULL;
mf_maze_destroy(&maze);
- if (chars != NULL) {
- for (i = 0; i < 1 + enemies; ++i) {
- mf_char_destroy(&chars[i]);
- }
- free(chars);
- }
+ mf_char_destroy(&player);
+ mf_char_destroy(&enemy_head);
return 0;
quit:
@@ -322,12 +307,8 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
TTF_CloseFont(text_font);
text_font = NULL;
mf_maze_destroy(&maze);
- if (chars != NULL) {
- for (i = 0; i < 1 + enemies; ++i) {
- mf_char_destroy(&chars[i]);
- }
- free(chars);
- }
+ mf_char_destroy(&player);
+ mf_char_destroy(&enemy_head);
return 1;
err:
@@ -339,11 +320,7 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
}
mftk_window_destroy(&win);
mf_maze_destroy(&maze);
- if (chars != NULL) {
- for (i = 0; i < 1 + enemies; ++i) {
- mf_char_destroy(&chars[i]);
- }
- free(chars);
- }
+ mf_char_destroy(&player);
+ mf_char_destroy(&enemy_head);
return -1;
}