diff --git a/game.c b/game.c index 2a2ba9f..7bd6a93 100644 --- a/game.c +++ b/game.c @@ -1,4 +1,5 @@ #include +#include #include #include "game.h" @@ -24,9 +25,14 @@ void initialize_game(Game *g) { g->should_close = false; g->state = GAME_STATE_INITIALIZING; g->field_multiplier = NULL; - g->kan_value = 12; g->dialog = NULL; + + g->kan_value = 12; + g->number_of_rounds = 1; + g->black_card_backs = true; g->deal_speed = 0.2; + g->options = malloc(sizeof(Options)); + load_options_from_game(g); init_dialogs(g); @@ -136,9 +142,8 @@ void initialize_game(Game *g) { break; } - g->number_of_rounds = 1; g->current_round = 0; - g->state = GAME_STATE_INITIALIZING; + g->state = GAME_STATE_OPTIONS; } Player *current_player(Game *g) { @@ -159,15 +164,19 @@ bool is_player_turn(Game *g) { } void handle_input(Game *g) { - if (IsKeyPressed(KEY_R)) { - g->state = GAME_STATE_INITIALIZING; - return; + if (g->state == GAME_STATE_OPTIONS) { + return options_handle_input(g); } if (g->dialog) { return dialog_handle_input(g->dialog); } + if (IsKeyPressed(KEY_R)) { + g->state = GAME_STATE_INITIALIZING; + return; + } + if (!is_player_turn(g)) return; switch (g->state) { @@ -821,6 +830,8 @@ void run_frame(Game *g) { case GAME_STATE_NEW_GAME: run_frame_new_game(g); break; + case GAME_STATE_OPTIONS: + break; } } @@ -853,6 +864,12 @@ void draw_frame(Game *g) { draw_cards(g); + if (g->state == GAME_STATE_OPTIONS) { + options_draw(g); + EndDrawing(); + return; + } + if (g->state == GAME_STATE_DEALING) { DrawText("Dealing....", 60, 385, 40, BLACK); } else if (g->field_multiplier) { diff --git a/game.h b/game.h index 5faee56..8e14999 100644 --- a/game.h +++ b/game.h @@ -12,6 +12,7 @@ typedef struct Game Game; #include "points.h" #include "player.h" #include "dialog.h" +#include "options.h" typedef enum GameState { GAME_STATE_INITIALIZING, @@ -33,6 +34,7 @@ typedef enum GameState { GAME_STATE_END_OF_GAME, GAME_STATE_NEW_GAME, GAME_STATE_TITLE_SCREEN, + GAME_STATE_OPTIONS, } GameState; struct Game { @@ -43,16 +45,17 @@ struct Game { Hand deck, field; FieldMultiplier *field_multiplier; Card *current_play_from_hand, *current_play_target; - int kan_value; Player player, right, left; int temp_points; int turn_number; Dialog *dialog; Player *dealer; - int number_of_rounds; - Color card_backs; - float deal_speed; int current_round; + int kan_value; + int number_of_rounds; + bool black_card_backs; + float deal_speed; + Options *options; }; void initialize_game(Game *g); diff --git a/options.c b/options.c index 67bdde2..db6a2dc 100644 --- a/options.c +++ b/options.c @@ -1,15 +1,112 @@ #include "options.h" -void load_options_from_game(Game *g, Options *o) { - o->kan_value = g->kan_value; - o->number_of_rounds = g->number_of_rounds; - o->card_backs = g->card_backs; - o->deal_speed = g->deal_speed; +int kan_value_from_index(int index) { return index == 0 ? 10 : 12; } +int index_from_kan_value(int kan_value) { return kan_value == 10 ? 0 : 1; } +int number_of_rounds_from_index(int index) { + int r[] = { 1, 3, 6, 12 }; + return r[index]; +} +int index_from_number_of_rounds(int number_of_rounds) { + int r[] = { 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 3 }; + return r[number_of_rounds]; +} +float deal_speed_from_index(int index) { return (index + 1) / 10. ; } +float index_from_deal_speed(float deal_speed) { return (int) (deal_speed * 10) - 1 ; } + +void load_options_from_game(Game *g) { + g->options->kan_value = index_from_kan_value(g->kan_value); + g->options->number_of_rounds = index_from_number_of_rounds(g->number_of_rounds); + g->options->card_backs = g->black_card_backs; + g->options->deal_speed = index_from_deal_speed(g->deal_speed); } -void save_options_to_game(Game *g, Options *o) { - g->kan_value = o->kan_value; - g->number_of_rounds = o->number_of_rounds; - g->card_backs = o->card_backs; - g->deal_speed = o->deal_speed; +void save_options_to_game(Game *g) { + g->kan_value = kan_value_from_index(g->options->kan_value); + g->number_of_rounds = number_of_rounds_from_index(g->options->number_of_rounds); + g->black_card_backs = g->options->card_backs; + g->deal_speed = deal_speed_from_index(g->options->deal_speed); +} + +void handle_options_save(Game *g) { + save_options_to_game(g); + g->state = GAME_STATE_INITIALIZING; +} + +void handle_options_cancel(Game *g) { + load_options_from_game(g); + g->state = GAME_STATE_INITIALIZING; +} + +void handle_select_kan(Game *g, int index) { + g->options->kan_value = index; +} + +void handle_select_number_of_rounds(Game *g, int index) { + g->options->number_of_rounds = index; +} + +void handle_select_card_backs(Game *g, int index) { + g->options->card_backs = index; +} + +void handle_select_deal_speed(Game *g, int index) { + g->options->deal_speed = index; +} + +OptionsChoices kan_choices = { { "Ten", "Twelve" }, 2, 250, &handle_select_kan }; +OptionsChoices number_of_rounds_choices = { { "One", "Three", "Six", "Twelve" }, 4, 400, &handle_select_number_of_rounds }; +OptionsChoices card_backs_choices = { { "Red", "Black" }, 2, 550, &handle_select_card_backs }; +OptionsChoices deal_speed_choices = { { "Very Slow", "Slow", "Medium", "Fast", "Very Fast" }, 5, 700, &handle_select_deal_speed }; +OptionsChoices *oc[4] = { &kan_choices, &number_of_rounds_choices, &card_backs_choices, &deal_speed_choices }; + +void options_handle_input(Game *g) { + if (!IsMouseButtonPressed(0)) return; + + int left = 250; + int width = 900; + + Vector2 mouse_pos = GetMousePosition(); + int x = mouse_pos.x, y = mouse_pos.y; + for (int i = 0; i < 4; i++) { + OptionsChoices *choices = oc[i]; + if (y < choices->y || y > choices->y + 30) continue; + + for (int j = 0; j < choices->count; j++) { + char *choice = choices->choice[j]; + int w = MeasureText(choice, 30); + int center = left + (width / (choices->count + 1)) * (j + 1); + int min = center - (w/2); + int max = center + (w/2); + if (x > min && x < max) { + choices->handle(g, j); + return; + } + } + } +} + +void DrawTextCentered(char *text, int center_x, int y, int point, Color color) { + int width = MeasureText(text, point); + DrawText(text, center_x - (width / 2), y, point, color); +} + +void draw_option_choices(OptionsChoices *choices, int selected_index) { + int left = 250; + int width = 900; + for (int i = 0; i < choices->count; i++) { + Color color = i == selected_index ? RED : BLACK; + DrawTextCentered(choices->choice[i], left + (width / (choices->count + 1)) * (i + 1), choices->y, 30, color); + } +} + +void options_draw(Game *g) { + DrawTextCentered("Options", 700, 50, 60, BLACK); + DrawTextCentered("Kan Value", 700, 175, 40, BLACK); + draw_option_choices(&kan_choices, g->options->kan_value); + DrawTextCentered("Number of Rounds", 700, 325, 40, BLACK); + draw_option_choices(&number_of_rounds_choices, g->options->number_of_rounds); + DrawTextCentered("Card Backs", 700, 475, 40, BLACK); + draw_option_choices(&card_backs_choices, g->options->card_backs); + DrawTextCentered("Deal Speed", 700, 625, 40, BLACK); + draw_option_choices(&deal_speed_choices, g->options->deal_speed); } diff --git a/options.h b/options.h index fc50954..52f4a79 100644 --- a/options.h +++ b/options.h @@ -2,17 +2,27 @@ #define _HF_OPTIONS_ typedef struct Options Options; +typedef struct OptionsChoices OptionsChoices; #include "game.h" struct Options { int kan_value; int number_of_rounds; - Color card_backs; - float deal_speed; + int card_backs; + int deal_speed; }; -void load_options_from_game(Game *g, Options *o); -void save_options_to_game(Game *g, Options *o); +struct OptionsChoices { + char *choice[5]; + int count; + int y; + void (*handle)(Game*, int); +}; + +void load_options_from_game(Game *g); +void save_options_to_game(Game *g); +void options_handle_input(Game *g); +void options_draw(Game *g); #endif