diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-04-01 00:20:54 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-04-01 00:20:54 +0300 |
commit | f9da494260fb1b876378bdc3eadd7bc15aec0221 (patch) | |
tree | 5d91b2d85c5fafa913cced7a445567d8ba7d40a1 | |
parent | 757b45c1ff1968f01b46cab6014686a43ecc17d6 (diff) | |
download | carcassonne-engine-c-f9da494260fb1b876378bdc3eadd7bc15aec0221.tar.gz |
feat: visually select tile placement
-rw-r--r-- | src/main.c | 68 |
1 files changed, 22 insertions, 46 deletions
@@ -25,14 +25,6 @@ void draw_structures(BoardUnit* board, WINDOW* win) { } } -void preview_tile(Tile tile, WINDOW* win) { - mvwaddch(win, 1, 1, tile.center); - mvwaddch(win, 0, 1, tile.edges[0]); - mvwaddch(win, 1, 2, tile.edges[1]); - mvwaddch(win, 2, 1, tile.edges[2]); - mvwaddch(win, 1, 0, tile.edges[3]); -} - int main() { /* initialize curses */ initscr(); @@ -50,20 +42,8 @@ int main() { box(structures_box, 0, 0); wrefresh(structures_box); - /* create input window */ - WINDOW* input_box = newwin(3, 22, BOARD_ROW_UNITS + 3, 0); - WINDOW* input_win = derwin(input_box, 1, 20, 1, 1); - box(input_box, 0, 0); - wrefresh(input_box); - - /* create tile inspector window */ - WINDOW* tile_box = newwin(5, 5, BOARD_ROW_UNITS + 3, 60); - WINDOW* tile_win = derwin(tile_box, 3, 3, 1, 1); - box(tile_box, 0, 0); - wrefresh(tile_box); - /* create messages window */ - WINDOW* messages_box = newwin(40, 60, BOARD_ROW_UNITS + 6, 0); + WINDOW* messages_box = newwin(40, 60, BOARD_ROW_UNITS + 2, 0); WINDOW* messages_win = derwin(messages_box, 40 - 2, 60 - 2, 1, 1); box(messages_box, 0, 0); wrefresh(messages_box); @@ -82,12 +62,10 @@ int main() { place_tile(tile, translate_coordinate(24), board, 1); /* main loop */ - char position[3]; - char rotation_input; + char input_key; while (1) { /* prepare */ refresh_structure_groups(board); - wclear(input_win); /* draw onto the screen */ draw_board(board, board_win); @@ -95,36 +73,34 @@ int main() { wrefresh(board_win); wrefresh(structures_win); - /* tile pickup */ + /* tile placement */ tile = tileset[rand() % 3]; + int position = 0; + BoardUnit board_preview[BOARD_UNITS]; while (1) { - preview_tile(tile, tile_win); - wrefresh(tile_win); - - rotation_input = wgetch(tile_win); - if (rotation_input == 10) break; /* enter key */ - else if (rotation_input == 'j') rotate_tile(&tile, 1); - else if (rotation_input == 'k') rotate_tile(&tile, 3); + for (int i = 0; i < BOARD_UNITS; i++) { + board_preview[i].feature = board[i].feature; + } + place_tile(tile, translate_coordinate(position), board_preview, 1); + draw_board(board_preview, board_win); + wrefresh(board_win); + + input_key = wgetch(board_win); + if (input_key == 10) break; /* enter key */ + else if (input_key == 'l') position += 1; + else if (input_key == 'h') position -= 1; + else if (input_key == 'j') position += BOARD_WIDTH; + else if (input_key == 'k') position -= BOARD_WIDTH; + else if (input_key == 'r') rotate_tile(&tile, 3); } - /* position prompt */ - echo(); - box(input_box, 0, 0); - mvwaddstr(input_box, 0, 0, "Enter position:"); - wrefresh(input_box); - wgetnstr(input_win, position, 3); - noecho(); - - if (position[0] == 'q') break; - - int result = place_tile(tile, translate_coordinate(atoi(position)), board, 0); - if (result) wprintw(messages_win, "Placed tile %s (%c) at position %i\n", tile.edges, tile.center, atoi(position)); - else wprintw(messages_win, "Could not place tile %s (%c) at position %i\n", tile.edges, tile.center, atoi(position)); + int result = place_tile(tile, translate_coordinate(position), board, 0); + if (result) wprintw(messages_win, "Placed tile %s (%c) at position %i\n", tile.edges, tile.center, position); + else wprintw(messages_win, "Could not place tile %s (%c) at position %i\n", tile.edges, tile.center, position); wrefresh(messages_win); } - endwin(); return 0; } |