diff options
| author | Sopár Adrián <adrian.sopar@protonmail.com> | 2024-06-20 09:28:14 +0200 |
|---|---|---|
| committer | Sopár Adrián <adrian.sopar@protonmail.com> | 2024-06-20 09:28:14 +0200 |
| commit | 74ea6dc86646cee9915292d73d8c7afef01ef3e0 (patch) | |
| tree | 9a58866f7765dad8ba56f1f40b1fa031e9d2687d /src/game/game.c | |
First commit. This is mostly the state of the project as I left it around the end of 2019.HEADmaster
Diffstat (limited to 'src/game/game.c')
| -rw-r--r-- | src/game/game.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/game/game.c b/src/game/game.c new file mode 100644 index 0000000..a9ac239 --- /dev/null +++ b/src/game/game.c @@ -0,0 +1,109 @@ +#include "game.h" +#include "color.h" +#include "utilities.h" +#include <string.h> + +void draw_line(int color, char *text, size_t length) +{ + int width = getmaxx(stdscr); + int height = getmaxy(stdscr) - 1; + char footer[width + 1]; + memset(footer, ' ', width); + footer[width] = 0; + //Display: + attron(COLOR_PAIR(color)); + mvprintw(height, 0, footer); + strncpy(footer, text, length); + mvprintw(height, 0, footer); +} + +void draw_line_info(int steps_taken, int steps_remaining) +{ + char buffer[getmaxx(stdscr) + 1]; + int length = sprintf(buffer, "%d/%d", steps_taken, steps_remaining); + draw_line(CI_LINE_INFO, buffer, length); +} + +void draw_line_win() +{ + char buffer[getmaxx(stdscr) + 1]; + int length = sprintf(buffer, "\u2714 Win! \u263A"); + draw_line(CI_LINE_SUCCESS, buffer, length); +} + +int get_command(char prompt, char *command) +{ + int width = getmaxx(stdscr); + int height = getmaxy(stdscr) - 1; + + char line[width + 1]; + memset(line, ' ', width); + line[width] = 0; + attron(COLOR_PAIR(CI_LINE_CMD)); + curs_set(1); + + char *current = command; + *(current++) = prompt; + *current = 0; + while (1) + { + mvaddstr(height, 0, line); + mvaddstr(height, 0, command); + refresh(); + wchar_t c = wgetch(stdscr); + if (c == KEY_BACKSPACE) + { + current--; + *current = 0; + if (current == command) + { + curs_set(0); + return 0; + } + } + else if (c == 10) + { + curs_set(0); + return 1; + } + else + { + *(current++) = c; + *current = 0; + } + } +} + +struct game_state *create_game_state(struct maze *maze) +{ + struct maze_display *display = malloc(sizeof(struct maze_display)); + + display->maze_pos.x = 0; + display->maze_pos.y = 0; + display->maze = maze; + display->player_pos = maze->starting_point; + display->display_player_path = true; + //Signs + int signs_length = sizeof_2d(maze->width, maze->height, sizeof(int)); + display->signs = malloc(signs_length); + memset(display->signs, (int)RS_NONE, signs_length); + init2d((void **)display->signs, maze->width, maze->height, sizeof(int)); + display->signs[display->player_pos.x][display->player_pos.y] = RS_PATH; + + struct game_state *state = malloc(sizeof(struct game_state)); + state->display = display; + + state->center = false; + state->win = false; + state->steps_taken = 0; + state->new_maze_pos = display->maze_pos; + state->new_player_pos = display->player_pos; + return state; +} + +struct array +{ + int width; + int height; + int *arr; +}; |
