diff options
-rw-r--r-- | src/char.h | 3 | ||||
-rw-r--r-- | src/char/enemy.c | 36 | ||||
-rw-r--r-- | src/game.c | 71 |
3 files changed, 56 insertions, 54 deletions
@@ -29,8 +29,7 @@ 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, - struct mf_char *prev_enemy); +mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size); 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 a7a1251..1ac5871 100644 --- a/src/char/enemy.c +++ b/src/char/enemy.c @@ -25,20 +25,13 @@ #include "char.h" struct mf_enemy { - struct mf_char parent; - struct mf_char *next_enemy; + struct mf_char parent; }; static int -_mf_enemy_update(struct mf_char *c) +_mf_enemy_update(struct mf_char *c __attribute__((__unused__))) { - struct mf_enemy *p = (struct mf_enemy *) c; - - if (p->next_enemy != NULL) { - return mf_char_update(p->next_enemy); - } else { - return 0; - } + return 0; } static int @@ -121,28 +114,19 @@ _mf_enemy_collide(struct mf_char *c) } static int -_mf_enemy_render(struct mf_char *c, SDL_Renderer *renderer) +_mf_enemy_render(struct mf_char *c __attribute__((__unused__)), + SDL_Renderer *renderer __attribute__((__unused__))) { - struct mf_enemy *p = (struct mf_enemy *) c; - - if (p->next_enemy != NULL) { - return mf_char_render(p->next_enemy, renderer); - } else { - return 0; - } + return 0; } static void -_mf_enemy_destroy(struct mf_char *c) +_mf_enemy_destroy(struct mf_char *c __attribute__((__unused__))) { - 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, - struct mf_char *prev_enemy) +mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size) { struct mf_char *c; struct mf_enemy *p __attribute__((__unused__)); @@ -181,10 +165,6 @@ 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; } @@ -144,9 +144,8 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, SDL_Renderer *renderer) { struct mf_maze *maze = NULL; - struct mf_char *player = NULL; - struct mf_char *enemy_head = NULL; - struct mf_char *enemy_tail = NULL; + struct mf_char **chars = NULL; + int i; SDL_Color maze_color; char *font_path = NULL; TTF_Font *text_font = NULL; @@ -157,6 +156,7 @@ 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,19 +172,22 @@ 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; - player = mf_player_new(maze, MF_WINDOW_H / size); - if (player == NULL) { + 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) { goto err; } - for (; enemies > 0; --enemies) { - enemy_tail = mf_enemy_new(maze, MF_WINDOW_H / size, size, - enemy_tail); - if (enemy_tail == NULL) { + for (i = 1; i < 1 + enemies; ++i) { + chars[i] = mf_enemy_new(maze, MF_WINDOW_H / size, size); + if (chars[i] == NULL) { goto err; } - if (enemy_head == NULL) { - enemy_head = enemy_tail; - } } font_path = mf_strcat(mf_get_fonts_dir(), "/FifteenTwenty-Regular.ttf"); @@ -230,7 +233,7 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, default: break; } - mf_player_key_event(player, &event); + mf_player_key_event(chars[0], &event); switch (mftk_window_event(win, &event)) { case 0: break; @@ -241,9 +244,16 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, } } if (won == SDL_FALSE) { - mf_char_update(player); - mf_char_update(enemy_head); - mf_char_get_vector(player, &game.player_cx, + 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, &game.player_cy, &game.player_travel, &game.player_dx, &game.player_dy); game.player_x = game.player_cx * MF_WINDOW_H / size; @@ -259,8 +269,9 @@ 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); - mf_char_render(player, renderer); - mf_char_render(enemy_head, renderer); + for (i = 0; i < 1 + enemies; ++i) { + mf_char_render(chars[i], renderer); + } if (fow == SDL_TRUE && _mf_game_fow(renderer, &game, maze, MF_WINDOW_H / size) < 0) { goto err; @@ -298,8 +309,12 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, TTF_CloseFont(text_font); text_font = NULL; mf_maze_destroy(&maze); - mf_char_destroy(&player); - mf_char_destroy(&enemy_head); + if (chars != NULL) { + for (i = 0; i < 1 + enemies; ++i) { + mf_char_destroy(&chars[i]); + } + free(chars); + } return 0; quit: @@ -307,8 +322,12 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, TTF_CloseFont(text_font); text_font = NULL; mf_maze_destroy(&maze); - mf_char_destroy(&player); - mf_char_destroy(&enemy_head); + if (chars != NULL) { + for (i = 0; i < 1 + enemies; ++i) { + mf_char_destroy(&chars[i]); + } + free(chars); + } return 1; err: @@ -320,7 +339,11 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, } mftk_window_destroy(&win); mf_maze_destroy(&maze); - mf_char_destroy(&player); - mf_char_destroy(&enemy_head); + if (chars != NULL) { + for (i = 0; i < 1 + enemies; ++i) { + mf_char_destroy(&chars[i]); + } + free(chars); + } return -1; } |