From 1082493e2761c3f4d6a9809b5f012776723fd937 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 10 Aug 2021 01:29:05 -0400 Subject: game: Create multiple enemies --- (limited to 'src/game.c') diff --git a/src/game.c b/src/game.c index 549088c..5daea66 100644 --- a/src/game.c +++ b/src/game.c @@ -140,25 +140,26 @@ _mf_game_fow(SDL_Renderer *renderer, struct _mf_game *game, } int -mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) +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 = NULL; - SDL_Color maze_color; - char *font_path = NULL; - TTF_Font *text_font = NULL; - SDL_Color form_color; - SDL_Color text_color; - struct _mf_game game; - struct mftk_window *win = NULL; - int fr; - int won; - Uint32 beg; - int secs; - Uint32 end; - Uint32 delay; - SDL_Event event; + struct mf_maze *maze = NULL; + struct mf_char **chars = NULL; + int i; + SDL_Color maze_color; + char *font_path = NULL; + TTF_Font *text_font = NULL; + SDL_Color form_color; + SDL_Color text_color; + struct _mf_game game; + struct mftk_window *win = NULL; + int fr; + int won; + Uint32 beg; + int secs; + Uint32 end; + Uint32 delay; + SDL_Event event; /* Create maze */ maze = mf_maze_new(seed, size, size, reveal); @@ -170,14 +171,23 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) 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; } - enemy = mf_enemy_new(maze, MF_WINDOW_H / size, size); - if (enemy == NULL) { + chars[0] = mf_player_new(maze, MF_WINDOW_H / size); + if (chars[0] == 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) { + goto err; + } + } font_path = mf_strcat(mf_get_fonts_dir(), "/FifteenTwenty-Regular.ttf"); text_font = TTF_OpenFont(font_path, MF_TEXT_FONT_S); @@ -222,7 +232,7 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) default: break; } - mf_player_key_event(player, &event); + mf_player_key_event(chars[0], &event); switch (mftk_window_event(win, &event)) { case 0: break; @@ -233,9 +243,10 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) } } if (won == SDL_FALSE) { - mf_char_update(player); - mf_char_update(enemy); - mf_char_get_vector(player, &game.player_cx, + for (i = 0; i < 1 + enemies; ++i) { + mf_char_update(chars[i]); + } + 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; @@ -251,8 +262,9 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) 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, 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; @@ -290,8 +302,12 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) TTF_CloseFont(text_font); text_font = NULL; mf_maze_destroy(&maze); - mf_char_destroy(&player); - mf_char_destroy(&enemy); + if (chars != NULL) { + for (i = 0; i < 1 + enemies; ++i) { + mf_char_destroy(&chars[i]); + } + free(chars); + } return 0; quit: @@ -299,8 +315,12 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) TTF_CloseFont(text_font); text_font = NULL; mf_maze_destroy(&maze); - mf_char_destroy(&player); - mf_char_destroy(&enemy); + if (chars != NULL) { + for (i = 0; i < 1 + enemies; ++i) { + mf_char_destroy(&chars[i]); + } + free(chars); + } return 1; err: @@ -312,7 +332,11 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) } mftk_window_destroy(&win); mf_maze_destroy(&maze); - mf_char_destroy(&player); - mf_char_destroy(&enemy); + if (chars != NULL) { + for (i = 0; i < 1 + enemies; ++i) { + mf_char_destroy(&chars[i]); + } + free(chars); + } return -1; } -- cgit v0.9.1