From f534c8d2c4cd8c8692cd7d831afb3041cc584c2b Mon Sep 17 00:00:00 2001 From: Jacob Janzen Date: Mon, 12 Feb 2024 12:51:08 -0600 Subject: refactor --- main.c | 111 +++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 38 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 3dcc0d7..5b4f3b0 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "cavegen.h" @@ -11,7 +12,17 @@ #define MESSAGE_PANEL_WIDTH 100 #define MESSAGE_PANEL_HEIGHT 3 +#define MAX_ENTITIES 100 + +struct entity { + char *name; + int xpos; + int ypos; + char *disp_ch; +}; + WINDOW *create_newwin(int height, int width, int starty, int startx) + { WINDOW *local_win = newwin(height, width, starty, startx); box(local_win, 0, 0); @@ -22,6 +33,7 @@ WINDOW *create_newwin(int height, int width, int starty, int startx) void initialize(void) { + srand(time(NULL)); setlocale(LC_ALL, ""); // allow extended ASCII initscr(); // initialize curses @@ -51,38 +63,47 @@ void initialize(void) refresh(); } -void display_map(WINDOW *win, bool *map, int cam_y, int cam_x) +void display_map( + WINDOW *win, enum tile_type *map, struct entity *entities, int num_entities +) { for (int i = 1; i < MAIN_PANEL_HEIGHT - 1; ++i) { for (int j = 1; j < MAIN_PANEL_WIDTH - 1; ++j) { - int i1 = i - 1 + cam_y; - int j1 = j - 1 + cam_x; + int i1 = i - 1 + entities[0].ypos; + int j1 = j - 1 + entities[0].xpos; if (i1 > HEIGHT || j1 > WIDTH || i1 < 0 || j1 < 0) { mvwaddch(win, i, j, ' '); - } else if (map[i1 * WIDTH + j1]) { + } else if (map[i1 * WIDTH + j1] == GROUND) { mvwaddch(win, i, j, '.'); - } else if (i1 > 0 && map[(i1 - 1) * WIDTH + j1]) { + } else if (map[i1 * WIDTH + j1] == UP_STAIR) { + mvwaddch(win, i, j, '<'); + } else if (map[i1 * WIDTH + j1] == DOWN_STAIR) { + mvwaddch(win, i, j, '>'); + } else if (i1 > 0 && map[(i1 - 1) * WIDTH + j1] != WALL) { mvwprintw(win, i, j, "█"); - } else if (i1 < HEIGHT - 1 && map[(i1 + 1) * WIDTH + j1]) { + } else if (i1 < HEIGHT - 1 && map[(i1 + 1) * WIDTH + j1] != WALL) { mvwprintw(win, i, j, "█"); - } else if (j1 > 0 && map[i1 * WIDTH + j1 - 1]) { + } else if (j1 > 0 && map[i1 * WIDTH + j1 - 1] != WALL) { mvwprintw(win, i, j, "█"); - } else if (j1 < WIDTH - 1 && map[i1 * WIDTH + j1 + 1]) { + } else if (j1 < WIDTH - 1 && map[i1 * WIDTH + j1 + 1] != WALL) { mvwprintw(win, i, j, "█"); } else { mvwaddch(win, i, j, ' '); } } } - mvwaddch(win, MAIN_PANEL_HEIGHT / 2, MAIN_PANEL_WIDTH / 2, '@'); + + for (int i = 1; i < num_entities; ++i) { + mvwprintw( + win, entities[i].ypos - entities[0].ypos + 1, + entities[i].xpos - entities[0].xpos + 1, entities[i].disp_ch + ); + } wrefresh(win); } int main(void) { - int starty = 12; - int startx = 40; - initialize(); // create the windows @@ -116,49 +137,63 @@ int main(void) wattron(main_win, COLOR_PAIR(1)); wrefresh(main_win); - bool *map; - struct point *open_tiles; - int num_open_tiles; - create_cave(&map, &open_tiles, &num_open_tiles); - - int xpos = rand() % num_open_tiles; - int ypos = rand() % num_open_tiles; - startx = xpos = MAIN_PANEL_WIDTH / 2 + 1; - starty = ypos = MAIN_PANEL_HEIGHT / 2 + 1; - - display_map(main_win, map, starty, startx); + enum tile_type *map; + struct point *open_tiles; + int num_open_tiles; + struct point up; + struct point down; + create_cave(&map, &open_tiles, &num_open_tiles, &up, &down); + + struct entity *entities = malloc(sizeof(struct entity) * MAX_ENTITIES); + entities[0].disp_ch = ""; + entities[0].name = "camera"; + entities[0].xpos = up.x - MAIN_PANEL_WIDTH / 2 + 1; + entities[0].ypos = up.y - MAIN_PANEL_HEIGHT / 2 + 1; + entities[1].disp_ch = "@"; + entities[1].name = "player"; + entities[1].xpos = up.x; + entities[1].ypos = up.y; + int num_entities = 2; + + display_map(main_win, map, entities, num_entities); int ch; while ((ch = getch()) != KEY_F(1)) { - xpos = startx + MAIN_PANEL_WIDTH / 2 - 1; - ypos = starty + MAIN_PANEL_HEIGHT / 2 - 1; switch (ch) { case 'k' : - if (ypos > 0) { - if (map[(ypos - 1) * WIDTH + xpos]) - --starty; + if (entities[1].ypos > 0) { + if (map[(entities[1].ypos - 1) * WIDTH + entities[1].xpos]) { + --entities[0].ypos; + --entities[1].ypos; + } } break; case 'j' : - if (ypos < HEIGHT - 1) { - if (map[(ypos + 1) * WIDTH + xpos]) - ++starty; + if (entities[1].ypos < HEIGHT - 1) { + if (map[(entities[1].ypos + 1) * WIDTH + entities[1].xpos]) { + ++entities[0].ypos; + ++entities[1].ypos; + } } break; case 'h' : - if (xpos > 0) { - if (map[ypos * WIDTH + xpos - 1]) - --startx; + if (entities[1].xpos > 0) { + if (map[entities[1].ypos * WIDTH + entities[1].xpos - 1]) { + --entities[0].xpos; + --entities[1].xpos; + } } break; case 'l' : - if (xpos < WIDTH - 1) { - if (map[ypos * WIDTH + xpos + 1]) - ++startx; + if (entities[1].xpos < WIDTH - 1) { + if (map[entities[1].ypos * WIDTH + entities[1].xpos + 1]) { + ++entities[0].xpos; + ++entities[1].xpos; + } } break; } - display_map(main_win, map, starty, startx); + display_map(main_win, map, entities, num_entities); } endwin(); -- cgit v1.2.3