GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
shop.c
Go to the documentation of this file.
1
7#include "game/shop.h"
8
9#include "audio_utils.h"
10#include "background_shop_gfx.h"
11#include "bitset.h"
12#include "button.h"
13#include "game.h"
14#include "game/blind_select.h"
15#include "game/joker_row.h"
16#include "game_variables.h"
17#include "joker.h"
18#include "layout.h"
19#include "list.h"
20#include "mgba_logger.h"
21#include "random.h"
22#include "save.h"
23#include "soundbank.h"
24#include "state_machine.h"
25#include "timer.h"
26#include "util.h"
27
28#include <string.h>
29
30// Timer defs
31#define TM_END_GAME_SHOP_INTRO 12
32#define TM_CREATE_SHOP_ITEMS_WAIT 1
33#define TM_SHIFT_SHOP_ICON_WAIT 7
34#define TM_SHOW_CARD_DESC_WAIT 12
35#define TM_HIDE_DECK_WAIT 5
36
37// Pixel sized
38#define ITEM_SHOP_Y 71
39#define OWNED_CARDS_HIDE_Y_OFFSET 50
40
41// Shop
42#define REROLL_BASE_COST 5 // Base cost for rerolling the shop items
43#define NEXT_ROUND_BTN_SEL_X 0
44
45// Palette IDs
46#define REROLL_BTN_PAL_IDX 3
47#define NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX 5
48#define SHOP_PANEL_SHADOW_PAL_IDX 6
49#define REROLL_BTN_SELECTED_BORDER_PAL_IDX 7
50#define SHOP_LIGHTS_1_PAL_IDX 8
51#define SHOP_LIGHTS_2_PAL_IDX 14
52#define NEXT_ROUND_BTN_PAL_IDX 16
53#define SHOP_LIGHTS_3_PAL_IDX 17
54#define SHOP_LIGHTS_4_PAL_IDX 22
55#define SHOP_BOTTOM_PANEL_BORDER_PAL_IDX 26
56#define SHOP_DESC_RARITY_MAIN_COLOR_PAL_IDX 27
57#define SHOP_DESC_RARITY_SHADOW_COLOR_PAL_IDX 28
58
59#define SHOP_LIGHTS_1_CLR 0xFFFF
60#define SHOP_LIGHTS_2_CLR 0x32BE
61#define SHOP_LIGHTS_3_CLR 0x4B5F
62#define SHOP_LIGHTS_4_CLR 0x5F9F
63
64// clang-format off
65// Positions in tiles
66static const Rect SHOP_ICON_FROM_RECT = { 0, 26, 8, 26};
67static const BG_POINT SHOP_ICON_TO_POS = { 0, 0};
68static const BG_POINT OWNED_CARDS_PANEL_3X3_SRC_POS = { 29, 21};
69static const Rect OWNED_JOKERS_PANEL_RECT = { 9, 1, 21, 5};
70static const Rect OWNED_CONSUMABLES_PANEL_RECT = { 23, 1, 28, 5};
71static const Rect OWNED_CARDS_PANEL_RECT = { 9, 1, 28, 5};
72static const Rect OWNED_CARDS_PANEL_ANIM_CLEAR = { 9, 0, 28, 1};
73static const Rect CARD_DESC_9_PTCH_TO_RECT = { 9, 6, 28, 18};
74static const NinePatchRect CARD_DESC_9_PTCH_SRC = {
75 .patch_rect = { 27, 25, 31, 31},
76 .margins = { 2, 3, 2, 3}
77};
78static const int CARD_DESC_MAX_TEXT_HEIGHT = CARD_DESC_9_PTCH_TO_RECT.bottom -
79 CARD_DESC_9_PTCH_TO_RECT.top + 1 -
80 CARD_DESC_9_PTCH_SRC.margins.top -
81 CARD_DESC_9_PTCH_SRC.margins.bottom;
82static const Rect CARD_DESC_TEXT_RECT = { 11, 9, 26, 18};
83static const Rect CARD_NAME_TEXT_RECT = { 10, 7, 27, 7};
84
85// Positions in pixels
86static const BG_POINT SHOP_JOKER_SPRITES_INIT_POS = {120, 160};
87static const BG_POINT CARD_DESCRIPTION_SPRITE_POS = {135, 9};
88static const Rect SHOP_PRICES_TEXT_RECT = { 72, 56, 192, 160 };
89static const Rect SHOP_REROLL_RECT = { 88, 96, UNDEFINED, UNDEFINED };
90// clang-format on
91
92static List s_shop_items_list = LIST_DEFAULT;
93
94enum GameShopStates
95{
96 GAME_SHOP_INTRO,
97 GAME_SHOP_ACTIVE,
98 GAME_SHOP_SHOW_CARD_DESC,
99 GAME_SHOP_HIDE_CARD_DESC,
100 GAME_SHOP_EXIT,
101 GAME_SHOP_MAX
102};
103
104static void game_shop_intro(void);
105static void game_shop_process_user_input(void);
106static void game_shop_show_card_desc(void);
107static void game_shop_hide_card_desc(void);
108static void game_shop_outro(void);
109
110static StateInfo shop_state_actions[GAME_SHOP_MAX] = {
111 STATE_INFO_UPDATE_FN_ONLY(game_shop_intro),
112 STATE_INFO_UPDATE_FN_ONLY(game_shop_process_user_input),
113 STATE_INFO_UPDATE_FN_ONLY(game_shop_show_card_desc),
114 STATE_INFO_UPDATE_FN_ONLY(game_shop_hide_card_desc),
115 STATE_INFO_UPDATE_FN_ONLY(game_shop_outro),
116};
117
118static StateMachine shop_sm = STATE_MACHINE_DEFINE(shop_state_actions, GAME_SHOP_MAX);
119
120// Shop SelectionGrid
121
122static int shop_top_row_get_size(void);
124 SelectionGrid* selection_grid,
125 int row_idx,
126 const Selection* prev_selection,
127 const Selection* new_selection
128);
129static void shop_top_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection);
130static int shop_reroll_row_get_size(void);
132 SelectionGrid* selection_grid,
133 int row_idx,
134 const Selection* prev_selection,
135 const Selection* new_selection
136);
137static void shop_reroll_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection);
138
139static SelectionGridRow shop_selection_rows[] = {
140 {0, jokers_sel_row_get_size, jokers_sel_row_on_selection_changed, jokers_sel_row_on_key_transit, {.wrap = false, .has_h_exit_idx = false, .h_exit_idx = 0}},
141 {1, shop_top_row_get_size, shop_top_row_on_selection_changed, shop_top_row_on_key_transit, {.wrap = false, .has_h_exit_idx = false, .h_exit_idx = 0}},
142 {2, shop_reroll_row_get_size, shop_reroll_row_on_selection_changed, shop_reroll_row_on_key_transit, {.wrap = false, .has_h_exit_idx = true, .h_exit_idx = 1} },
143};
144
145static const Selection SHOP_INIT_SEL = {-1, 1};
146
147static SelectionGrid shop_selection_grid = {
148 shop_selection_rows,
149 NUM_ELEM_IN_ARR(shop_selection_rows),
150 SHOP_INIT_SEL
151};
152
153// Shop Buttons
154
155static void next_round_on_pressed(void);
156static void reroll_on_pressed(void);
157static bool reroll_can_be_pressed(void);
158static Button next_round_button =
159 {NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX, NEXT_ROUND_BTN_PAL_IDX, next_round_on_pressed, NULL};
160static Button reroll_button = {
161 REROLL_BTN_SELECTED_BORDER_PAL_IDX,
162 REROLL_BTN_PAL_IDX,
163 reroll_on_pressed,
164 reroll_can_be_pressed
165};
166
167// Shop internal variables
168
169static int s_timer;
170
171static int s_reroll_cost = REROLL_BASE_COST;
172
173// Variables relative to the Card we are showing the description of
174
175// TODO: Change this to item once it has description printing API.
176static JokerObject* s_description_card = NULL;
177static FIXED s_description_card_original_x_pos = UNDEFINED;
178static FIXED s_description_card_original_y_pos = UNDEFINED;
179static List* s_description_card_original_list = NULL;
180static int s_show_description_anim_progress = 0;
181
183{
184 return s_description_card;
185}
186
188{
189 list_clear(&s_shop_items_list);
190 s_shop_items_list = list_init();
192}
193
195{
196 toggle_windows(false, true);
197
198 GRIT_CPY(pal_bg_mem, background_shop_gfxPal);
199 GRIT_CPY(&tile_mem[MAIN_BG_CBB], background_shop_gfxTiles);
200 GRIT_CPY(&se_mem[MAIN_BG_SBB], background_shop_gfxMap);
201
202 // Set the outline colors for the shop background. This is used for the alternate shop
203 // palettes when opening packs
204 pal_bg_mem[SHOP_BOTTOM_PANEL_BORDER_PAL_IDX] = 0x213D;
205 pal_bg_mem[SHOP_PANEL_SHADOW_PAL_IDX] = 0x10B4;
206
207 // Reset the shop lights to correct colors
208 pal_bg_mem[SHOP_LIGHTS_2_PAL_IDX] = SHOP_LIGHTS_2_CLR;
209 pal_bg_mem[SHOP_LIGHTS_3_PAL_IDX] = SHOP_LIGHTS_3_CLR;
210 pal_bg_mem[SHOP_LIGHTS_4_PAL_IDX] = SHOP_LIGHTS_4_CLR;
211 pal_bg_mem[SHOP_LIGHTS_1_PAL_IDX] = SHOP_LIGHTS_1_CLR;
212
213 // Disable the button highlight colors
214 pal_bg_mem[REROLL_BTN_SELECTED_BORDER_PAL_IDX] = pal_bg_mem[REROLL_BTN_PAL_IDX];
215 pal_bg_mem[NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX] = pal_bg_mem[NEXT_ROUND_BTN_PAL_IDX];
216}
217
219{
221
222 s_timer = TM_ZERO;
223
224 state_machine_register(&shop_sm);
225 state_machine_change_state(&shop_sm, GAME_SHOP_INTRO);
226
227 // The selection grid is initialized outside of bounds and moved
228 // to trigger the selection change so the initial selection is visible
229 shop_selection_grid.selection = SHOP_INIT_SEL;
230 selection_grid_move_selection_horz(&shop_selection_grid, 1);
231}
232
238{
239 // TODO: Randomize item type when consumables are implemented
240 return item_roll_new(ITEM_TYPE_JOKER, RNG_SEQ_SHOP_ITEMS);
241}
242
248{
249 tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT);
250
251 List* shop_items_list = &s_shop_items_list;
252
253 list_clear(shop_items_list);
254 *shop_items_list = list_init();
255
256 for (int i = 0; i < MAX_SHOP_ITEMS; i++)
257 {
259
260 if (item == NULL)
261 {
262 MGBA_FUNC_WARN("Could not create shop item");
263
264 // TODO: Decide how to handle this case gracefully
265 // If the issue for example is that we can't generate any more jokers,
266 // because for example the user owns all of them,
267 // maybe we need to generate consumables instead.
268 return;
269 }
270
271 item->x = int2fx(SHOP_JOKER_SPRITES_INIT_POS.x + i * CARD_SPRITE_SIZE);
272 item->y = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y);
273 item->tx = item->x;
274 item->ty = int2fx(ITEM_SHOP_Y);
275
277
278 list_push_back(shop_items_list, item);
279 }
280}
281
285static void game_shop_intro()
286{
287 main_bg_se_copy_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_UP);
288
289 if (s_timer == TM_CREATE_SHOP_ITEMS_WAIT)
290 {
292 }
293
294 if (s_timer >= TM_SHIFT_SHOP_ICON_WAIT) // Shift the shop icon
295 {
296 int timer_offset = s_timer - 6;
297
298 // TODO: Extract to generic function?
299 for (int y = 0; y < timer_offset; y++)
300 {
301 Rect from = SHOP_ICON_FROM_RECT;
302 from.top += y - timer_offset;
303 from.bottom += y - timer_offset;
304
305 BG_POINT to = SHOP_ICON_TO_POS;
306 to.y = y;
307
308 main_bg_se_copy_rect(from, to);
309 }
310 }
311
312 if (s_timer == TM_END_GAME_SHOP_INTRO)
313 {
314 state_machine_change_state(&shop_sm, GAME_SHOP_ACTIVE);
315 s_timer = TM_ZERO; // Reset the timer
316
317 // print initial reroll cost only when the panel is in place
318 tte_printf(
319 "#{P:%d,%d; cx:0x%X000}$%d",
320 SHOP_REROLL_RECT.left,
321 SHOP_REROLL_RECT.top,
322 TTE_WHITE_PB,
323 s_reroll_cost
324 );
325 }
326}
327
332static int shop_top_row_get_size(void)
333{
334 // + 1 to account for next round button
335 return list_get_len(&s_shop_items_list) + 1;
336}
337
341static inline void game_shop_buy_item(int shop_item_idx)
342{
343 List* shop_items_list = &s_shop_items_list;
344 Item* item = (Item*)list_get_at_idx(shop_items_list, shop_item_idx);
345
346 g_game_vars.money -= item_get_buy_price(item);
347 display_money();
350 item_acquire(item);
351 list_remove_at_idx(shop_items_list, shop_item_idx); // Remove the joker from the shop
352}
353
357static void shop_top_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection)
358{
359 if (!key_hit(SELECT_CARD))
360 return;
361
362 if (selection->x == NEXT_ROUND_BTN_SEL_X)
363 {
364 button_press(&next_round_button);
365 }
366 else
367 {
368 int shop_item_idx = selection->x - 1; // - 1 to account for next round button
369 Item* item = (Item*)list_get_at_idx(&s_shop_items_list, shop_item_idx);
370 if (!item_can_acquire(item) || g_game_vars.money < item_get_buy_price(item))
371 {
372 return;
373 }
374
375 game_shop_buy_item(shop_item_idx);
376 selection_grid_move_selection_horz(selection_grid, -1);
377 }
378}
379
384 SelectionGrid* selection_grid,
385 int row_idx,
386 const Selection* prev_selection,
387 const Selection* new_selection
388)
389{
390 List* shop_items_list = &s_shop_items_list;
391 // Guard if we move down while on jokers
392 if (new_selection->y > row_idx && prev_selection->x > 0)
393 return false;
394
395 // The selection grid system only guarantees that the new selection is within bounds
396 // but not the previous one...
397 // This allows using INIT_SEL = {-1, 1} and move to set the initial selection in a hacky way...
398 if (prev_selection->y == row_idx && prev_selection->x >= 0 &&
399 prev_selection->x < shop_top_row_get_size())
400 {
401 if (prev_selection->x == NEXT_ROUND_BTN_SEL_X)
402 {
403 button_set_highlight(&next_round_button, false);
404 }
405 else
406 {
407 int idx = prev_selection->x - 1; // -1 to account for next round button
408 SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(shop_items_list, idx);
409 sprite_object_set_focus(sprite_object, false);
410 }
411 }
412
413 if (new_selection->y == row_idx)
414 {
415 if (new_selection->x == NEXT_ROUND_BTN_SEL_X)
416 {
417 button_set_highlight(&next_round_button, true);
418 }
419 else
420 {
421 int idx = new_selection->x - 1; // -1 to account for next round button
422 SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(shop_items_list, idx);
423 sprite_object_set_focus(sprite_object, true);
424 }
425 }
426
427 return true;
428}
429
436{
437 return 1;
438}
439
444 SelectionGrid* selection_grid,
445 int row_idx,
446 const Selection* prev_selection,
447 const Selection* new_selection
448)
449{
450 if (row_idx == prev_selection->y)
451 {
452 button_set_highlight(&reroll_button, false);
453
454 if (new_selection->x != NEXT_ROUND_BTN_SEL_X)
455 {
456 int idx = new_selection->x - 1;
457 SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(&s_shop_items_list, idx);
458 sprite_object_set_focus(sprite_object, true);
459 }
460 }
461 else if (row_idx == new_selection->y)
462 {
463 button_set_highlight(&reroll_button, true);
464 }
465
466 return true;
467}
468
473static inline void game_shop_reroll(void)
474{
475 g_game_vars.money -= s_reroll_cost;
476 display_money(); // Update the money display
477
478 List* shop_items_list = &s_shop_items_list;
479 ListItr itr = list_itr_create(shop_items_list);
480 Item* item;
481
482 while ((item = list_itr_next(&itr)))
483 {
484 if (item != NULL && item->type == ITEM_TYPE_JOKER)
485 {
486 item_dispose(&item);
487 }
488 }
489
490 list_clear(shop_items_list);
491 *shop_items_list = list_init();
492
494
495 itr = list_itr_create(shop_items_list);
496
497 SpriteObject* item_sprite_object;
498 while ((item_sprite_object = list_itr_next(&itr)))
499 {
500 if (item_sprite_object != NULL)
501 {
502 item_sprite_object->y = item_sprite_object->ty;
503
504 sprite_object_shake(item_sprite_object, UNDEFINED);
505 }
506 }
507
508 s_reroll_cost++;
509 tte_printf(
510 "#{P:%d,%d; cx:0x%X000}$%d",
511 SHOP_REROLL_RECT.left,
512 SHOP_REROLL_RECT.top,
513 TTE_WHITE_PB,
514 s_reroll_cost
515 );
516}
517
521static void shop_reroll_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection)
522{
523 if (!key_hit(SELECT_CARD))
524 {
525 return;
526 }
527
528 button_press(&reroll_button);
529}
530
531static void next_round_on_pressed(void)
532{
533 // Go to next blind selection game state
534 state_machine_change_state(&shop_sm, GAME_SHOP_EXIT);
535 s_timer = TM_ZERO;
536 s_reroll_cost = REROLL_BASE_COST;
537
538 pal_bg_mem[NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX] = pal_bg_mem[SHOP_PANEL_SHADOW_PAL_IDX];
539}
540
541static void reroll_on_pressed(void)
542{
543 // TODO: Add money sound effect
545}
546
547static bool reroll_can_be_pressed(void)
548{
549 return g_game_vars.money >= s_reroll_cost;
550}
551
556{
557 selection_grid_process_input(&shop_selection_grid);
558
559 static JokerObject* tmp_card = NULL;
560
561 // Determine the Joker we would show the description of
562 switch (shop_selection_grid.selection.y)
563 {
564 // Owned Joker
565 case 0:
566 {
567 s_description_card_original_list = get_jokers_list();
568 tmp_card = list_get_at_idx(get_jokers_list(), shop_selection_grid.selection.x);
569 break;
570 }
571
572 // Jokers for sale
573 case 1:
574 {
575 s_description_card_original_list = &s_shop_items_list;
576 tmp_card = (shop_selection_grid.selection.x > 0)
577 ? list_get_at_idx(&s_shop_items_list, shop_selection_grid.selection.x - 1)
578 : NULL;
579 break;
580 }
581
582 // TODO: handle Consumables and Vouchers when implemented
583
584 default:
585 {
586 s_description_card_original_list = NULL;
587 tmp_card = NULL;
588 break;
589 }
590 }
591
592 // Show description of selected card when pressing B.
593 // Always wait for the card in question to be immobile to avoid accumulating
594 // errors when pressing and releasing B in quick succession.
595 if (tmp_card != NULL && tmp_card->vx == 0 && tmp_card->vy == 0 && key_held(DESELECT_CARDS))
596 {
597 s_description_card = tmp_card;
598 s_description_card_original_x_pos = s_description_card->x;
599 s_description_card_original_y_pos = s_description_card->y;
600
601 s_timer = TM_ZERO;
602 state_machine_change_state(&shop_sm, GAME_SHOP_SHOW_CARD_DESC);
603 }
604}
605
606static void game_shop_show_card_desc(void)
607{
608 // Anim start
609 if (s_timer == 1)
610 {
611 // This starts at 0, then gets incremented up to TM_SHOW_CARD_DESC_WAIT. Will be used to
612 // revert the animation if the B button is released midway through it
613 s_show_description_anim_progress = 0;
614
615 // Erase shop text and disable transparency window
616
617 tte_erase_rect_wrapper(PLAYING_SCREEN_RECT);
618 toggle_windows(false, true);
619
620 // Move all other Jokers offscreen
621
622 JokerObject* joker_object = NULL;
623
624 // Owned Jokers
625 ListItr itr = list_itr_create(get_jokers_list());
626 while ((joker_object = list_itr_next(&itr)))
627 {
628 if (joker_object != s_description_card)
629 joker_object->ty -= int2fx(OWNED_CARDS_HIDE_Y_OFFSET);
630 }
631
632 // Shop Jokers
633 itr = list_itr_create(&s_shop_items_list);
634 while ((joker_object = list_itr_next(&itr)))
635 {
636 if (joker_object != s_description_card)
637 joker_object->ty = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y + TILE_SIZE);
638 }
639
640 // Set description_card new target position
641
642 s_description_card->tx = int2fx(CARD_DESCRIPTION_SPRITE_POS.x);
643 s_description_card->ty = int2fx(CARD_DESCRIPTION_SPRITE_POS.y);
644 }
645
646 if (s_timer <= TM_SHOW_CARD_DESC_WAIT)
647 {
648 s_show_description_anim_progress++;
649
650 // Hide Deck (last frames only)
651 if (TM_SHOW_CARD_DESC_WAIT - s_timer < TM_HIDE_DECK_WAIT)
652 main_bg_se_move_rect_1_tile_vert(DECK_ANIM_RECT, SCREEN_DOWN);
653 // Hide shop panel
654 main_bg_se_move_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_DOWN);
655 // Hide Owned Cards panels
656 main_bg_se_move_rect_1_tile_vert(OWNED_CARDS_PANEL_RECT, SCREEN_UP);
657 }
658
659 // Anim end
660 else if (s_timer == TM_SHOW_CARD_DESC_WAIT + 1)
661 {
662 // Compute needed space for the description
663 const JokerInfo* info = get_joker_registry_entry(s_description_card->joker->id);
664 int desc_bottom_offset =
665 CARD_DESC_MAX_TEXT_HEIGHT -
666 info->joker_print_desc(s_description_card->joker, CARD_DESC_TEXT_RECT);
667
668 // Print Rarity and change color or the panel
669 // Do it before drawing the panel so the color is already set
670 const char* rarity_str = joker_get_rarity_string(info->rarity);
671 tte_printf(
672 TTE_WHITE_TAG "#{P:%d,%d}%*s%s",
673 CARD_DESC_TEXT_RECT.left * TILE_SIZE,
674 (CARD_DESC_TEXT_RECT.bottom - desc_bottom_offset - 1) * TILE_SIZE,
675 (rect_width(&CARD_DESC_TEXT_RECT) - strlen(rarity_str)) / 2,
676 "",
677 rarity_str
678 );
679 pal_bg_mem[SHOP_DESC_RARITY_MAIN_COLOR_PAL_IDX] =
680 joker_get_rarity_color(info->rarity, true);
681 pal_bg_mem[SHOP_DESC_RARITY_SHADOW_COLOR_PAL_IDX] =
682 joker_get_rarity_color(info->rarity, false);
683
684 // Draw description panel
685 Rect actual_dest_rect = CARD_DESC_9_PTCH_TO_RECT;
686 actual_dest_rect.bottom -= desc_bottom_offset;
687 main_bg_se_copy_expand_9_patch(actual_dest_rect, &CARD_DESC_9_PTCH_SRC);
688
689 // Print joker name
690 tte_printf(
691 TTE_WHITE_TAG "#{P:%d,%d}%*s%s",
692 CARD_NAME_TEXT_RECT.left * TILE_SIZE,
693 CARD_NAME_TEXT_RECT.top * TILE_SIZE,
694 (rect_width(&CARD_NAME_TEXT_RECT) - strlen(info->name)) / 2,
695 "",
696 info->name
697 );
698 }
699
700 // Actively wait for the B button to be released
701 if (!key_held(DESELECT_CARDS))
702 {
703 s_timer = TM_ZERO;
704 state_machine_change_state(&shop_sm, GAME_SHOP_HIDE_CARD_DESC);
705 }
706}
707
708static void game_shop_hide_card_desc(void)
709{
710 // just so we don't print the price of an owned Joker too many times
711 static bool owned_joker_price_printed = false;
712
713 // Anim start
714 if (s_timer == 1)
715 {
716 // Erase shop text and Joker Description frame if we had time to draw them
717 if (s_show_description_anim_progress >= TM_SHOW_CARD_DESC_WAIT)
718 {
719 main_bg_se_clear_rect(CARD_DESC_9_PTCH_TO_RECT);
720 }
721 // Or clear the owned cards' panel that haven't finished moving up
722 else
723 {
724 main_bg_se_clear_rect(OWNED_CARDS_PANEL_ANIM_CLEAR);
725 }
726
727 tte_erase_rect_wrapper(PLAYING_SCREEN_RECT);
728
729 // Enable transparency window
730 toggle_windows(false, true);
731
732 // Redraw Jokers/Consumables frames
733 main_bg_se_copy_expand_3x3_rect(OWNED_JOKERS_PANEL_RECT, OWNED_CARDS_PANEL_3X3_SRC_POS);
735 OWNED_CONSUMABLES_PANEL_RECT,
736 OWNED_CARDS_PANEL_3X3_SRC_POS
737 );
738
739 // Move Jokers back to their positions
740 JokerObject* joker_object = NULL;
741
742 // Owned Jokers
743 ListItr itr = list_itr_create(get_jokers_list());
744 while ((joker_object = list_itr_next(&itr)))
745 {
746 if (joker_object != s_description_card)
747 joker_object->ty = int2fx(HELD_JOKERS_POS.y);
748 }
749
750 // Shop Jokers
751 itr = list_itr_create(&s_shop_items_list);
752 while ((joker_object = list_itr_next(&itr)))
753 {
754 if (joker_object != s_description_card)
755 joker_object->ty = int2fx(ITEM_SHOP_Y);
756 }
757
758 s_description_card->tx = s_description_card_original_x_pos;
759 s_description_card->ty = s_description_card_original_y_pos;
760 }
761
762 if (s_timer <= s_show_description_anim_progress)
763 {
764 // Show Deck (last frames only)
765 if (s_show_description_anim_progress > TM_HIDE_DECK_WAIT &&
766 s_timer < (s_show_description_anim_progress - (TM_HIDE_DECK_WAIT + 1)))
767 {
768 main_bg_se_move_rect_1_tile_vert(DECK_ANIM_RECT, SCREEN_UP);
769 }
770 // Show shop panel
771 main_bg_se_move_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_UP);
772 }
773
774 // Last anim frame (no need to wait for the Joker to have stopped for this):
775 else if (s_timer == s_show_description_anim_progress + 1)
776 {
777 // Need to account for the description_card being selected if it came from the shop.
778 if (s_description_card_original_list == &s_shop_items_list)
779 s_description_card->ty += int2fx(TILE_SIZE);
780
781 // Print price under shop Jokers
782 Item* item = NULL;
783 ListItr itr = list_itr_create(&s_shop_items_list);
784 while ((item = list_itr_next(&itr)))
785 {
787 }
788
789 if (s_description_card_original_list == &s_shop_items_list)
790 s_description_card->ty -= int2fx(TILE_SIZE);
791
792 // Print Reroll prince
793 tte_printf(
794 "#{P:%d,%d; cx:0x%X000}$%d",
795 SHOP_REROLL_RECT.left,
796 SHOP_REROLL_RECT.top,
797 TTE_WHITE_PB,
798 s_reroll_cost
799 );
800
801 // Print Deck size that was erased
802 display_deck_size_max();
803 }
804
805 // Cleanup and change state
806 else if (s_description_card->vx == 0 && s_description_card->vy == 0)
807 {
808 owned_joker_price_printed = false;
809 s_description_card = NULL;
810 s_timer = TM_ZERO;
811 state_machine_change_state(&shop_sm, GAME_SHOP_ACTIVE);
812 }
813
814 // At any point after the other prices have been printed, and while the card is still moving,
815 // if we are NOT pressing A, print the price under it.
816 else if (!owned_joker_price_printed && !key_held(SELECT_CARD) &&
817 s_description_card_original_list == get_jokers_list())
818 {
819 owned_joker_price_printed = true;
821 (SpriteObject*)s_description_card,
822 joker_get_sell_value(s_description_card->joker)
823 );
824 }
825}
826
831static void game_shop_outro(void)
832{
833 // Shift the shop panel
834 main_bg_se_move_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_DOWN);
835
836 main_bg_se_copy_rect_1_tile_vert(TOP_LEFT_PANEL_ANIM_RECT, SCREEN_UP);
837
838 if (s_timer == 1)
839 {
840 tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT); // Erase the shop prices text
841
842 ListItr itr = list_itr_create(&s_shop_items_list);
843 SpriteObject* shop_item;
844 while ((shop_item = list_itr_next(&itr)))
845 {
846 if (shop_item != NULL)
847 {
848 shop_item->ty = int2fx(160);
849 }
850 }
851
853 }
854 else if (s_timer == 2)
855 {
856 // TODO: make heads or tails of what's going on here and replace
857 // magic numbers.
858 int y = 5;
859 memset16(&se_mat[MAIN_BG_SBB][y - 1][0], 0x0001, 1);
860 memset16(&se_mat[MAIN_BG_SBB][y - 1][1], 0x0002, 7);
861 memset16(&se_mat[MAIN_BG_SBB][y - 1][8], SE_HFLIP | 0x0001, 1);
862 }
863
864 if (s_timer >= MENU_POP_OUT_ANIM_FRAMES)
865 {
866 game_change_state(GAME_STATE_BLIND_SELECT);
867 }
868}
869
873static inline void game_shop_lights_anim_frame(void)
874{
875 // Shift palette around the border of the shop icon
876 COLOR shifted_palette[4];
877 shifted_palette[0] = pal_bg_mem[SHOP_LIGHTS_2_PAL_IDX];
878 shifted_palette[1] = pal_bg_mem[SHOP_LIGHTS_3_PAL_IDX];
879 shifted_palette[2] = pal_bg_mem[SHOP_LIGHTS_4_PAL_IDX];
880 shifted_palette[3] = pal_bg_mem[SHOP_LIGHTS_1_PAL_IDX];
881
882 // Circularly shift the palette
883 int last = shifted_palette[3];
884
885 for (int i = 3; i > 0; --i)
886 {
887 shifted_palette[i] = shifted_palette[i - 1];
888 }
889
890 shifted_palette[0] = last;
891
892 // Copy the shifted palette to the next 4 slots
893 pal_bg_mem[SHOP_LIGHTS_2_PAL_IDX] = shifted_palette[0];
894 pal_bg_mem[SHOP_LIGHTS_3_PAL_IDX] = shifted_palette[1];
895 pal_bg_mem[SHOP_LIGHTS_4_PAL_IDX] = shifted_palette[2];
896 pal_bg_mem[SHOP_LIGHTS_1_PAL_IDX] = shifted_palette[3];
897}
898
900{
901 s_timer++;
902
903 if (s_timer % 20 == 0)
904 {
906 }
907}
908
910{
911 List* shop_items_list = &s_shop_items_list;
912 ListItr itr = list_itr_create(shop_items_list);
913 Item* item;
914
915 while ((item = list_itr_next(&itr)))
916 {
917 item_dispose(&item);
918 }
919
920 list_clear(shop_items_list);
921
922 increment_blind(BLIND_STATE_DEFEATED); // TODO: Move to game_round_end()?
923
924 state_machine_remove(&shop_sm);
925
926 save_game();
927}
Utilities for using maxmod to play sound effects.
A bitset for operating on flags.
Blind select state functions.
A button structure containing the common button functionalities Including highlight and pressing func...
void button_press(Button *button)
Execute a button press, by calling the button's on_pressed function. This checks the button's can_be_...
Definition button.c:18
void button_set_highlight(Button *button, bool highlight)
Set the button highlight on or off.
Definition button.c:8
void reset_top_left_panel_bottom_row(void)
Restores the bottom row of the top-left panel from the background map.
Definition common_ui.c:48
Game global game variables struct definition.
void main_bg_se_move_rect_1_tile_vert(Rect se_rect, enum ScreenVertDir direction)
Moves a rect in the main background vertically in direction by a single tile.
void main_bg_se_copy_rect_1_tile_vert(Rect se_rect, enum ScreenVertDir direction)
Copies a rect in the main background vertically in direction by a single tile.
void main_bg_se_clear_rect(Rect se_rect)
Clears a rect in the main background.
void main_bg_se_copy_rect(Rect se_rect, BG_POINT dest_pos)
Copies a rect in the main background from se_rect to the position (x, y).
void toggle_windows(bool win0, bool win1)
Toggles the visibility of the window layers.
#define TILE_SIZE
Tile size in pixels, both height and width as tiles are square.
void main_bg_se_copy_expand_3x3_rect(Rect se_rect_dest, BG_POINT se_rect_src_3x3_top_left)
Copies a 3x3 rect and expands it to fit a passed rect. Special case of the 9-patch.
void tte_erase_rect_wrapper(Rect rect)
A wrapper for tte_erase_rect that would use the rect struct.
void main_bg_se_copy_expand_9_patch(Rect se_dest_rect, const NinePatchRect *src_9_ptch)
Copies a 9-patch and expands it to fit a passed rect.
INLINE int rect_width(const Rect *rect)
Get the width of a rectangle.
void item_dispose(Item **item)
Destroys an item, freeing underlying resources, and manages rollable items sets if needed....
Definition item.c:56
void item_print_buy_price_under(Item *item)
Prints the buy price under the item Relies on the fact item is a SpriteObject.
Definition item.c:67
Item * item_roll_new(enum ItemType item_type, enum RngSequence key)
Rolls a random item of type item_type and returns a newly created one. Manages rollable items set if ...
Definition item.c:9
bool item_can_acquire(Item *item)
Returns true if the item can be acquired, i.e. added to inventory. Does not check if the player has e...
Definition item.c:46
void item_acquire(Item *item)
Acquires the item, adding to inventory if applicable. Called when it is purchased from the shop,...
Definition item.c:35
int item_get_buy_price(Item *item)
Returns the buy price of the item.
Definition item.c:24
Functions relative to the handling of Jokers.
u16 joker_get_rarity_color(u8 rarity, bool main_color)
Get Joker rarity panel color.
Definition joker.c:179
void joker_reset_rollable_jokers(void)
Reset rollable jokers to include all jokers in the registry.
Definition joker.c:338
All the functions used specifically to interact with Jokers. This includes browsing,...
bool jokers_sel_row_on_selection_changed(SelectionGrid *selection_grid, int row_idx, const Selection *prev_selection, const Selection *new_selection)
Handle directional inputs on the Jokers' row at the top of the game screen.
Definition joker_row.c:16
void jokers_sel_row_on_key_transit(SelectionGrid *selection_grid, Selection *selection)
Handle button inputs on the Jokers' row at the top of the game screen.
Definition joker_row.c:100
int jokers_sel_row_get_size(void)
Returns the size of the Jokers' row at the top of the game screen. Is equal to the number of Jokers o...
Definition joker_row.c:11
Header of shared rects and points needed for ui.
A doubly-linked list.
List list_init(void)
Definition list.c:38
void * list_get_at_idx(List *list, unsigned int idx)
Definition list.c:219
int list_get_len(const List *list)
Definition list.c:214
void list_clear(List *list)
Definition list.c:44
void * list_itr_next(ListItr *itr)
Definition list.c:281
bool list_remove_at_idx(List *list, unsigned int idx)
Definition list.c:237
#define LIST_DEFAULT
Default list declaration for empty lists.
Definition list.h:30
void list_push_back(List *list, void *data)
Definition list.c:89
ListItr list_itr_create(List *list)
Definition list.c:257
Interface to interact with the mgba logger.
Common functions to handle RNG manipulation. Using this interface has three goals:
Utils functions to save/load data structures from/to the SRAM.
void save_game(void)
Save current run data to SRAM.
Definition save.c:341
void selection_grid_move_selection_horz(SelectionGrid *selection_grid, int direction_tribool)
Moves the selection horizontally within the selection grid.
void selection_grid_process_input(SelectionGrid *selection_grid)
Processes user input for the selection grid.
static bool shop_reroll_row_on_selection_changed(SelectionGrid *selection_grid, int row_idx, const Selection *prev_selection, const Selection *new_selection)
Handle d-pad inputs for the "Reroll" button's row.
Definition shop.c:443
static void shop_top_row_on_key_transit(SelectionGrid *selection_grid, Selection *selection)
Handle button inputs for the "Next Round" button and shop Jokers.
Definition shop.c:357
JokerObject * game_shop_get_description_card(void)
Get a pointer to the Card we are currently showing the description of.
Definition shop.c:182
void game_shop_on_exit(void)
Shop cleanup.
Definition shop.c:909
static bool shop_top_row_on_selection_changed(SelectionGrid *selection_grid, int row_idx, const Selection *prev_selection, const Selection *new_selection)
Handle d-pad inputs for the "Next Round" button and shop Jokers' row.
Definition shop.c:383
static void game_shop_reroll(void)
Reroll items up for sale in the Shop. Reroll cost will go up by a rate that increases by 1 each rerol...
Definition shop.c:473
static void game_shop_process_user_input(void)
Handle user inputs logic in the Shop though a SelectionGrid.
Definition shop.c:555
static void game_shop_intro(void)
Intro sequence (menu and shop icon coming into frame)
Definition shop.c:285
static void game_shop_lights_anim_frame(void)
Cycling shop lights animation substate update.
Definition shop.c:873
void game_shop_reset(void)
Initialize the shop for a run. Resets all the shop data for the run, needs to be called once per run.
Definition shop.c:187
void game_shop_on_init(void)
Shop state initialization.
Definition shop.c:218
static int shop_reroll_row_get_size(void)
Get size of the row with the "Reroll" button.
Definition shop.c:435
static void game_shop_buy_item(int shop_item_idx)
Called when pressing A on a Shop Joker to buy it.
Definition shop.c:341
static int shop_top_row_get_size(void)
Computes the number of Jokers up for sale, aka the number of "buttons" there are on the top row of th...
Definition shop.c:332
static void shop_reroll_row_on_key_transit(SelectionGrid *selection_grid, Selection *selection)
Handle button inputs for the "Reroll" button's row.
Definition shop.c:521
static void game_shop_create_top_row_items(void)
Setup for the lists of items we can purchase in the top row of the Shop. i.e. Jokers and consumables ...
Definition shop.c:247
static void game_shop_outro(void)
Outro sequence substate update. This makes the menu and shop icon go out of frame.
Definition shop.c:831
void game_shop_on_update(void)
Shop state update.
Definition shop.c:899
void game_shop_change_background(void)
Change to the shop background.
Definition shop.c:194
static Item * game_shop_create_top_row_item(void)
Create a shop top row item - jokers, consumables, and possibly playing cards Currently only jokers ar...
Definition shop.c:237
Shop state functions.
void sprite_object_set_focus(SpriteObject *sprite_object, bool focus)
Set the focus for SpriteObject Raises the object by SPRITE_FOCUS_RAISE_PX.
Definition sprite.c:370
void sprite_object_print_price_under(SpriteObject *sprite_object, int price)
Print the price string directly beneath a SpriteObject. More specialized version of sprite_object_pri...
Definition sprite.c:447
void sprite_object_shake(SpriteObject *sprite_object, mm_word sound_id)
Shake SpriteObject on screen and play a sound.
Definition sprite.c:350
void sprite_object_erase_text_under(SpriteObject *sprite_object)
Erase the text within the Rect directly beneath a SpriteObject. This is used only for Cards for now.
Definition sprite.c:457
State Machine.
void state_machine_remove(StateMachine *state_machine)
Remove a statemachine's update function.
void state_machine_register(StateMachine *state_machine)
Register a statemachine to run it's update function once per frame.
void state_machine_change_state(StateMachine *state_machine, int new_state)
Calls the current state's on_exit, the new state's on_init, and sets the active update fn.
A button representation.
Definition button.h:31
A generic interface for all items that can appear in the shop or be in the inventory....
Definition item.h:76
enum ItemType type
The item type - used to dispatch the function implementations for inheriting types.
Definition item.h:87
An iterator into a list.
Definition list.h:91
A doubly-linked list.
Definition list.h:61
Structure to represent arbitrary-sized 9-patches.
The core selection grid struct, represents the grid itself and its state.
A sprite object is a sprite that is focusable and movable in animation.
Definition sprite.h:61
State machine callbacks.
State machine instance.
Timer constants.
Utilities relating around number string representation and protected arithmatic helper functions.
#define NUM_ELEM_IN_ARR(arr)
Get the number of elements in an array.
Definition util.h:43