refactor - extracted functions into smaller sub-functions
This commit is contained in:
+91
-52
@@ -96,9 +96,7 @@ static inline Card *discard_pop()
|
||||
}
|
||||
|
||||
// General functions
|
||||
void sort_cards()
|
||||
{
|
||||
if (sort_by_suit)
|
||||
void sort_hand_by_suit()
|
||||
{
|
||||
for (int a = 0; a < hand_top; a++)
|
||||
{
|
||||
@@ -113,7 +111,8 @@ void sort_cards()
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
void sort_hand_by_rank()
|
||||
{
|
||||
for (int a = 0; a < hand_top; a++)
|
||||
{
|
||||
@@ -129,6 +128,18 @@ void sort_cards()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void sort_cards()
|
||||
{
|
||||
if (sort_by_suit)
|
||||
{
|
||||
sort_hand_by_suit();
|
||||
}
|
||||
else
|
||||
{
|
||||
sort_hand_by_rank();
|
||||
}
|
||||
|
||||
// Update the sprites in the hand by destroying them and creating new ones in the correct order
|
||||
// (This is feels like a diabolical solution but like literally how else would you do this)
|
||||
for (int i = 0; i <= hand_top; i++)
|
||||
@@ -578,19 +589,8 @@ void game_init()
|
||||
tte_printf("#{P:8,144; cx:0xC000}%d#{cx:0xF000}/%d", 1, 8); // Ante
|
||||
}
|
||||
|
||||
void game_playing()
|
||||
static void game_playing_process_input_and_state()
|
||||
{
|
||||
// Background logic (thissss might be moved to the card'ssss logic later. I'm a sssssnake)
|
||||
if (hand_state == HAND_DRAW || hand_state == HAND_DISCARD || hand_state == HAND_SELECT)
|
||||
{
|
||||
change_background(BG_ID_CARD_SELECTING);
|
||||
}
|
||||
else
|
||||
{
|
||||
change_background(BG_ID_CARD_PLAYING);
|
||||
}
|
||||
|
||||
// Input and state related logic
|
||||
if (hand_state == HAND_SELECT)
|
||||
{
|
||||
if (key_hit(KEY_LEFT))
|
||||
@@ -673,8 +673,10 @@ void game_playing()
|
||||
tte_printf("#{P:32,48; cx:0xF000}%d", score); // Score
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Card logic
|
||||
static void game_playing_process_card_draw()
|
||||
{
|
||||
if (hand_state == HAND_DRAW && cards_drawn < hand_size)
|
||||
{
|
||||
if (timer % 10 == 0) // Draw a card every 10 frames
|
||||
@@ -689,12 +691,10 @@ void game_playing()
|
||||
cards_drawn = 0;
|
||||
timer = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int played_selections = 0;
|
||||
static bool sound_played = false;
|
||||
bool discarded_card = false;
|
||||
|
||||
// Discarded cards loop (mainly for shuffling)
|
||||
static void game_playing_discarded_cards_loop()
|
||||
{
|
||||
if (hand_get_size() == 0 && hand_state == HAND_SHUFFLING && discard_top >= -1 && timer > 10)
|
||||
{
|
||||
// We take each discarded card and put it back into the deck with a short animation
|
||||
@@ -740,8 +740,10 @@ void game_playing()
|
||||
timer = 1; // Reset the timer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cards in hand update loop
|
||||
static void cards_in_hand_update_loop(bool* discarded_card, int* played_selections, bool* sound_played)
|
||||
{
|
||||
for (int i = hand_top + 1; i >= 0; i--) // Start from the end of the hand and work backwards because that's how Balatro does it
|
||||
{
|
||||
if (hand[i] != NULL)
|
||||
@@ -783,17 +785,17 @@ void game_playing()
|
||||
case HAND_DISCARD: // TODO: Add sound
|
||||
if (hand[i]->selected || hand_state == HAND_SHUFFLING)
|
||||
{
|
||||
if (!discarded_card)
|
||||
if (!*discarded_card)
|
||||
{
|
||||
hand_x = int2fx(240);
|
||||
hand_y = int2fx(70);
|
||||
|
||||
if (!sound_played)
|
||||
if (!*sound_played)
|
||||
{
|
||||
const int pitch_lut[MAX_SELECTION_SIZE] = { 1024, 960, 896, 832, 768 };
|
||||
mm_sound_effect sfx_draw = { {SFX_CARD_DRAW}, pitch_lut[cards_drawn], 0, 255, 128, };
|
||||
mmEffectEx(&sfx_draw);
|
||||
sound_played = true;
|
||||
*sound_played = true;
|
||||
}
|
||||
|
||||
if (hand[i]->x >= hand_x)
|
||||
@@ -804,14 +806,14 @@ void game_playing()
|
||||
|
||||
hand_top--;
|
||||
cards_drawn++; // This technically isn't drawing cards, I'm just reusing the variable
|
||||
sound_played = false;
|
||||
*sound_played = false;
|
||||
timer = 1;
|
||||
|
||||
hand_y = hand[i]->y;
|
||||
hand_x = hand[i]->x;
|
||||
}
|
||||
|
||||
discarded_card = true;
|
||||
*discarded_card = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -831,10 +833,10 @@ void game_playing()
|
||||
hand_x = hand_x + (int2fx(i) - int2fx(hand_top) / 2) * -spacing_lut[hand_top];
|
||||
}
|
||||
|
||||
if (i == 0 && discarded_card == false && timer % 10 == 0)
|
||||
if (i == 0 && *discarded_card == false && timer % 10 == 0)
|
||||
{
|
||||
hand_state = HAND_DRAW;
|
||||
sound_played = false;
|
||||
*sound_played = false;
|
||||
cards_drawn = 0;
|
||||
hand_selections = 0;
|
||||
timer = 1;
|
||||
@@ -871,7 +873,7 @@ void game_playing()
|
||||
cards_drawn = 0;
|
||||
hand_selections = 0;
|
||||
timer = 1;
|
||||
played_selections = played_top + 1;
|
||||
*played_selections = played_top + 1;
|
||||
|
||||
switch (hand_type) // select the cards that apply to the hand type
|
||||
{
|
||||
@@ -1029,9 +1031,13 @@ void game_playing()
|
||||
card_object_update(hand[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Played cards update loop
|
||||
for (int i = 0; i <= played_top; i++) // So this one is a bit fucking weird because I have to work kinda backwards for everything because of the order of the pushed cards from the hand to the play stack (also crazy that the company that published Balatro is called "Playstack" and this is a play stack, but I digress)
|
||||
static void played_cards_update_loop(bool* discarded_card, int* played_selections, bool* sound_played)
|
||||
{
|
||||
// So this one is a bit fucking weird because I have to work kinda backwards for everything because of the order of the pushed cards from the hand to the play stack
|
||||
// (also crazy that the company that published Balatro is called "Playstack" and this is a play stack, but I digress)
|
||||
for (int i = 0; i <= played_top; i++)
|
||||
{
|
||||
if (played[i] != NULL)
|
||||
{
|
||||
@@ -1050,18 +1056,18 @@ void game_playing()
|
||||
switch (play_state)
|
||||
{
|
||||
case PLAY_PLAYING:
|
||||
if (i == 0 && (timer % 10 == 0 || played[played_top - played_selections]->selected == false) && timer > 40)
|
||||
if (i == 0 && (timer % 10 == 0 || played[played_top - *played_selections]->selected == false) && timer > 40)
|
||||
{
|
||||
played_selections--;
|
||||
(*played_selections)--;
|
||||
|
||||
if (played_selections == 0)
|
||||
if (*played_selections == 0)
|
||||
{
|
||||
play_state = PLAY_SCORING;
|
||||
timer = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (played[i]->selected && played_top - i >= played_selections)
|
||||
if (played[i]->selected && played_top - i >= *played_selections)
|
||||
{
|
||||
played_y -= (10 << FIX_SHIFT);
|
||||
}
|
||||
@@ -1076,7 +1082,7 @@ void game_playing()
|
||||
if (played[played_top - j]->selected)
|
||||
{
|
||||
scored_cards++;
|
||||
if (scored_cards > played_selections)
|
||||
if (scored_cards > *played_selections)
|
||||
{
|
||||
tte_erase_rect(72, 48, 240, 56);
|
||||
tte_set_pos(fx2int(played[played_top - j]->x) + 8, 48); // Offset of 16 pixels to center the text on the card
|
||||
@@ -1087,7 +1093,7 @@ void game_playing()
|
||||
snprintf(score_buffer, sizeof(score_buffer), "+%d", card_get_value(played[played_top - j]->card));
|
||||
tte_write(score_buffer);
|
||||
|
||||
played_selections = scored_cards;
|
||||
*played_selections = scored_cards;
|
||||
//played[j]->vy += (3 << FIX_SHIFT);
|
||||
played[played_top - j]->vscale = float2fx(0.3f); // Scale down the card when it's scored
|
||||
played[played_top - j]->vrotation = float2fx(8.0f); // Rotate the card when it's scored
|
||||
@@ -1102,12 +1108,12 @@ void game_playing()
|
||||
}
|
||||
}
|
||||
|
||||
if (j == 0 && scored_cards == played_selections) // Check if it's the last card
|
||||
if (j == 0 && scored_cards == *played_selections) // Check if it's the last card
|
||||
{
|
||||
tte_erase_rect(72, 48, 240, 56);
|
||||
play_state = PLAY_ENDING;
|
||||
timer = 1;
|
||||
played_selections = played_top + 1; // Reset the played selections to the top of the played stack
|
||||
*played_selections = played_top + 1; // Reset the played selections to the top of the played stack
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1119,34 +1125,34 @@ void game_playing()
|
||||
}
|
||||
break;
|
||||
case PLAY_ENDING: // This is the reverse of PLAY_PLAYING. The cards get reset back to their neutral position sequentially
|
||||
if (i == 0 && (timer % 10 == 0 || played[played_top - played_selections]->selected == false) && timer > 40)
|
||||
if (i == 0 && (timer % 10 == 0 || played[played_top - *played_selections]->selected == false) && timer > 40)
|
||||
{
|
||||
played_selections--;
|
||||
(*played_selections)--;
|
||||
|
||||
if (played_selections == 0)
|
||||
if (*played_selections == 0)
|
||||
{
|
||||
play_state = PLAY_ENDED;
|
||||
timer = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (played[i]->selected && played_top - i <= played_selections - 1)
|
||||
if (played[i]->selected && played_top - i <= *played_selections - 1)
|
||||
{
|
||||
played_y -= (10 << FIX_SHIFT);
|
||||
}
|
||||
break;
|
||||
case PLAY_ENDED: // Basically a copy of HAND_DISCARD
|
||||
if (!discarded_card && played[i] != NULL && timer > 40)
|
||||
if (!*discarded_card && played[i] != NULL && timer > 40)
|
||||
{
|
||||
played_x = int2fx(240);
|
||||
played_y = int2fx(70);
|
||||
|
||||
if (!sound_played)
|
||||
if (!*sound_played)
|
||||
{
|
||||
const int pitch_lut[MAX_SELECTION_SIZE] = {1024, 960, 896, 832, 768};
|
||||
mm_sound_effect sfx_draw = {{SFX_CARD_DRAW}, pitch_lut[cards_drawn], 0, 255, 128,};
|
||||
mmEffectEx(&sfx_draw);
|
||||
sound_played = true;
|
||||
*sound_played = true;
|
||||
}
|
||||
|
||||
if (played[i]->x >= played_x)
|
||||
@@ -1156,7 +1162,7 @@ void game_playing()
|
||||
|
||||
//played_top--;
|
||||
cards_drawn++; // This technically isn't drawing cards, I'm just reusing the variable
|
||||
sound_played = false;
|
||||
*sound_played = false;
|
||||
|
||||
if (i == played_top)
|
||||
{
|
||||
@@ -1172,14 +1178,14 @@ void game_playing()
|
||||
play_state = PLAY_PLAYING;
|
||||
cards_drawn = 0;
|
||||
hand_selections = 0;
|
||||
played_selections = 0;
|
||||
*played_selections = 0;
|
||||
played_top = -1; // Reset the played stack
|
||||
timer = 1;
|
||||
break; // Break out of the loop to avoid accessing an invalid index
|
||||
}
|
||||
}
|
||||
|
||||
discarded_card = true;
|
||||
*discarded_card = true;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -1191,8 +1197,10 @@ void game_playing()
|
||||
card_object_update(played[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UI Text update
|
||||
static void game_playing_ui_text_update()
|
||||
{
|
||||
static int last_hand_size = 0;
|
||||
static int last_deck_size = 0;
|
||||
|
||||
@@ -1214,6 +1222,37 @@ void game_playing()
|
||||
}
|
||||
}
|
||||
|
||||
void game_playing()
|
||||
{
|
||||
// Background logic (thissss might be moved to the card'ssss logic later. I'm a sssssnake)
|
||||
if (hand_state == HAND_DRAW || hand_state == HAND_DISCARD || hand_state == HAND_SELECT)
|
||||
{
|
||||
change_background(BG_ID_CARD_SELECTING);
|
||||
}
|
||||
else
|
||||
{
|
||||
change_background(BG_ID_CARD_PLAYING);
|
||||
}
|
||||
|
||||
game_playing_process_input_and_state();
|
||||
|
||||
// Card logic
|
||||
|
||||
game_playing_process_card_draw();
|
||||
|
||||
// Discarded cards loop (mainly for shuffling)
|
||||
game_playing_discarded_cards_loop();
|
||||
|
||||
static int played_selections = 0;
|
||||
static bool sound_played = false;
|
||||
bool discarded_card = false;
|
||||
|
||||
cards_in_hand_update_loop(&discarded_card, &played_selections, &sound_played);
|
||||
played_cards_update_loop(&discarded_card, &played_selections, &sound_played);
|
||||
|
||||
game_playing_ui_text_update();
|
||||
}
|
||||
|
||||
void game_round_end()
|
||||
{
|
||||
const int timer_delay = 30; // 30 frames = 500ms
|
||||
|
||||
Reference in New Issue
Block a user