diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-10 01:29:05 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-10 01:29:05 (EDT) |
commit | 1082493e2761c3f4d6a9809b5f012776723fd937 (patch) | |
tree | 15f7b2cc1acbef46e168fad8c1fac503653f471b | |
parent | 24d08e233c162578624a61b708a5e5f1aad057e5 (diff) | |
download | mazefight-1082493e2761c3f4d6a9809b5f012776723fd937.zip mazefight-1082493e2761c3f4d6a9809b5f012776723fd937.tar.gz mazefight-1082493e2761c3f4d6a9809b5f012776723fd937.tar.bz2 |
game: Create multiple enemies
-rw-r--r-- | src/game.c | 92 | ||||
-rw-r--r-- | src/game.h | 3 | ||||
-rw-r--r-- | src/menu.c | 6 |
3 files changed, 64 insertions, 37 deletions
@@ -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; } @@ -23,6 +23,7 @@ #include <SDL.h> 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); #endif /* MF_GAME_H_ */ @@ -32,7 +32,8 @@ #include "tk.h" #include "util.h" -const int MF_MENU_SIZES_[] = {15, 20, 30}; +const int MF_MENU_SIZES_[] = {15, 20, 30}; +const int MF_MENU_ENEMIES_[] = { 1, 2, 4}; struct _mf_menu { char *seed_buf; @@ -97,7 +98,8 @@ _mf_menu_play(void *user_data) int e; e = mf_game(menu->seed, MF_MENU_SIZES_[menu->size], menu->fow, - menu->reveal, menu->renderer); + menu->reveal, MF_MENU_ENEMIES_[menu->size], + menu->renderer); menu->seed = rand(); if (sprintf(menu->seed_buf, "%ld", menu->seed) < 0) { |