diff options
-rw-r--r-- | src/game.c | 32 |
1 files changed, 19 insertions, 13 deletions
@@ -144,6 +144,7 @@ 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 **chars = NULL; int i; SDL_Color maze_color; @@ -172,18 +173,18 @@ 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)); + chars = calloc(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) { + for (i = 0; i < enemies; ++i) { chars[i] = mf_enemy_new(maze, MF_WINDOW_H / size, size); if (chars[i] == NULL) { goto err; @@ -233,7 +234,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 +245,17 @@ 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(player); + for (i = 0; i < enemies; ++i) { mf_char_update(chars[i]); } - for (i = 1; i < 1 + enemies; ++i) { - for (j = i + 1; j < 1 + enemies; ++j) { + for (i = 0; i < enemies; ++i) { + for (j = i + 1; j < enemies; ++j) { mf_char_collision(chars[i], chars[j], SDL_TRUE); } } - mf_char_get_vector(chars[0], &game.player_cx, + 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,7 +271,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(player, renderer); + for (i = 0; i < enemies; ++i) { mf_char_render(chars[i], renderer); } if (fow == SDL_TRUE && _mf_game_fow(renderer, &game, maze, @@ -309,8 +312,9 @@ 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); if (chars != NULL) { - for (i = 0; i < 1 + enemies; ++i) { + for (i = 0; i < enemies; ++i) { mf_char_destroy(&chars[i]); } free(chars); @@ -322,8 +326,9 @@ 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); if (chars != NULL) { - for (i = 0; i < 1 + enemies; ++i) { + for (i = 0; i < enemies; ++i) { mf_char_destroy(&chars[i]); } free(chars); @@ -339,8 +344,9 @@ mf_game(long seed, int size, int fow, int reveal, int enemies, } mftk_window_destroy(&win); mf_maze_destroy(&maze); + mf_char_destroy(&player); if (chars != NULL) { - for (i = 0; i < 1 + enemies; ++i) { + for (i = 0; i < enemies; ++i) { mf_char_destroy(&chars[i]); } free(chars); |