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 "skip_tag.h"
24#include "soundbank.h"
25#include "state_machine.h"
26#include "timer.h"
27#include "util.h"
28
29#include <string.h>
30
31// Timer defs
32#define TM_END_GAME_SHOP_INTRO 12
33#define TM_CREATE_SHOP_ITEMS_WAIT 1
34#define TM_SHIFT_SHOP_ICON_WAIT 7
35#define TM_SHOW_ITEM_DESC_WAIT 12
36#define TM_HIDE_DECK_WAIT 5
37
38// Pixel sized
39#define ITEM_SHOP_Y 71
40#define OWNED_ITEMS_HIDE_Y_OFFSET 50
41
42// Shop
43#define REROLL_BASE_COST 5 // Base cost for rerolling the shop items
44#define NEXT_ROUND_BTN_SEL_X 0
45
46// Palette IDs
47#define REROLL_BTN_PAL_IDX 3
48#define NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX 5
49#define SHOP_PANEL_SHADOW_PAL_IDX 6
50#define REROLL_BTN_SELECTED_BORDER_PAL_IDX 7
51#define SHOP_LIGHTS_1_PAL_IDX 8
52#define SHOP_LIGHTS_2_PAL_IDX 14
53#define NEXT_ROUND_BTN_PAL_IDX 16
54#define SHOP_LIGHTS_3_PAL_IDX 17
55#define SHOP_LIGHTS_4_PAL_IDX 22
56#define SHOP_BOTTOM_PANEL_BORDER_PAL_IDX 26
57#define SHOP_DESC_RARITY_MAIN_COLOR_PAL_IDX 27
58#define SHOP_DESC_RARITY_SHADOW_COLOR_PAL_IDX 28
59
60#define SHOP_LIGHTS_1_CLR 0xFFFF
61#define SHOP_LIGHTS_2_CLR 0x32BE
62#define SHOP_LIGHTS_3_CLR 0x4B5F
63#define SHOP_LIGHTS_4_CLR 0x5F9F
64
65// clang-format off
66// Positions in tiles
67static const Rect SHOP_ICON_FROM_RECT = { 0, 26, 8, 26};
68static const BG_POINT SHOP_ICON_TO_POS = { 0, 0};
69static const BG_POINT OWNED_ITEMS_PANEL_3X3_SRC_POS = { 29, 21};
70static const Rect OWNED_JOKERS_PANEL_RECT = { 9, 1, 21, 5};
71static const Rect OWNED_CONSUMABLES_PANEL_RECT = { 23, 1, 28, 5};
72static const Rect OWNED_ITEMS_PANEL_RECT = { 9, 1, 28, 5};
73static const Rect OWNED_ITEMS_PANEL_ANIM_CLEAR = { 9, 0, 28, 1};
74static const Rect ITEM_DESC_9_PTCH_TO_RECT = { 9, 6, 28, 18};
75static const NinePatchRect ITEM_DESC_9_PTCH_SRC = {
76 .patch_rect = { 27, 25, 31, 31},
77 .margins = { 2, 3, 2, 3}
78};
79static const int ITEM_DESC_MAX_TEXT_HEIGHT = ITEM_DESC_9_PTCH_TO_RECT.bottom -
80 ITEM_DESC_9_PTCH_TO_RECT.top + 1 -
81 ITEM_DESC_9_PTCH_SRC.margins.top -
82 ITEM_DESC_9_PTCH_SRC.margins.bottom;
83static const Rect ITEM_DESC_TEXT_RECT = { 11, 9, 26, 18};
84static const Rect ITEM_NAME_TEXT_RECT = { 10, 7, 27, 7};
85
86// Positions in pixels
87static const BG_POINT SHOP_JOKER_SPRITES_INIT_POS = {120, 160};
88static const BG_POINT ITEM_DESCRIPTION_SPRITE_POS = {135, 9};
89static const Rect SHOP_PRICES_TEXT_RECT = { 72, 56, 192, 160 };
90static const Rect SHOP_REROLL_RECT = { 88, 96, UNDEFINED, UNDEFINED };
91// clang-format on
92
93static List s_shop_items_list = LIST_DEFAULT;
94
95enum GameShopStates
96{
97 GAME_SHOP_SKIP_TAGS,
98 GAME_SHOP_INTRO,
99 GAME_SHOP_ACTIVE,
100 GAME_SHOP_SHOW_ITEM_DESC,
101 GAME_SHOP_HIDE_ITEM_DESC,
102 GAME_SHOP_EXIT,
103 GAME_SHOP_MAX
104};
105
106static void shop_redeem_skip_tags(void);
107static void shop_intro(void);
108static void shop_process_user_input(void);
109static void shop_show_item_desc_on_init(void);
110static void shop_show_item_desc_on_update(void);
111static void shop_hide_item_desc_on_init(void);
112static void shop_hide_item_desc_on_update(void);
113static void shop_hide_item_desc_on_exit(void);
114static void shop_outro(void);
115
116// clang-format off
117static StateInfo shop_state_actions[GAME_SHOP_MAX] = {
118 [GAME_SHOP_SKIP_TAGS] = STATE_INFO_UPDATE_FN_ONLY(shop_redeem_skip_tags),
119 [GAME_SHOP_INTRO] = STATE_INFO_UPDATE_FN_ONLY(shop_intro),
120 [GAME_SHOP_ACTIVE] = STATE_INFO_UPDATE_FN_ONLY(shop_process_user_input),
121 [GAME_SHOP_SHOW_ITEM_DESC] = STATE_INFO_INIT_UPDATE_FN(
122 shop_show_item_desc_on_init,
123 shop_show_item_desc_on_update),
124 [GAME_SHOP_HIDE_ITEM_DESC] = {.on_init = shop_hide_item_desc_on_init,
125 .on_update = shop_hide_item_desc_on_update,
126 .on_exit = shop_hide_item_desc_on_exit},
127 [GAME_SHOP_EXIT] = STATE_INFO_UPDATE_FN_ONLY(shop_outro),
128};
129// clang-format on
130
131static StateMachine shop_sm = STATE_MACHINE_DEFINE(shop_state_actions, GAME_SHOP_MAX);
132
133// Shop SelectionGrid
134
135#define SHOP_GRID_OWNED_ROW 0
136#define SHOP_GRID_FOR_SALE_ROW 1
137
138static int shop_top_row_get_size(void);
140 SelectionGrid* selection_grid,
141 int row_idx,
142 const Selection* prev_selection,
143 const Selection* new_selection
144);
145static void shop_top_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection);
146static int shop_reroll_row_get_size(void);
148 SelectionGrid* selection_grid,
149 int row_idx,
150 const Selection* prev_selection,
151 const Selection* new_selection
152);
153static void shop_reroll_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection);
154
155static SelectionGridRow shop_selection_rows[] = {
156 {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}},
157 {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}},
158 {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} },
159};
160
161static const Selection SHOP_INIT_SEL = {-1, 1};
162
163static SelectionGrid shop_selection_grid = {
164 shop_selection_rows,
165 NUM_ELEM_IN_ARR(shop_selection_rows),
166 SHOP_INIT_SEL
167};
168
169// Shop Buttons
170
171static void next_round_on_pressed(void);
172static void reroll_on_pressed(void);
173static bool reroll_can_be_pressed(void);
174static Button next_round_button =
175 {NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX, NEXT_ROUND_BTN_PAL_IDX, next_round_on_pressed, NULL};
176static Button reroll_button = {
177 REROLL_BTN_SELECTED_BORDER_PAL_IDX,
178 REROLL_BTN_PAL_IDX,
179 reroll_on_pressed,
180 reroll_can_be_pressed
181};
182
183// Shop internal variables
184
185static int s_timer;
186
187static int s_reroll_cost = REROLL_BASE_COST;
188
189// Variables relative to the Card we are showing the description of
190
191static Item* s_description_item = NULL;
192static FIXED s_description_item_original_x_pos = UNDEFINED;
193static FIXED s_description_item_original_y_pos = UNDEFINED;
194static int s_show_description_anim_progress = 0;
195
197{
198 return s_description_item;
199}
200
202{
203 return s_reroll_cost;
204}
205
207{
208 s_reroll_cost = cost;
209}
210
211void shop_reset(void)
212{
213 list_clear(&s_shop_items_list);
214 s_shop_items_list = list_init();
216}
217
219{
220 toggle_windows(false, true);
221
222 GRIT_CPY(pal_bg_mem, background_shop_gfxPal);
223 GRIT_CPY(&tile_mem[MAIN_BG_CBB], background_shop_gfxTiles);
224 GRIT_CPY(&se_mem[MAIN_BG_SBB], background_shop_gfxMap);
225
226 // Set the outline colors for the shop background. This is used for the alternate shop
227 // palettes when opening packs
228 pal_bg_mem[SHOP_BOTTOM_PANEL_BORDER_PAL_IDX] = 0x213D;
229 pal_bg_mem[SHOP_PANEL_SHADOW_PAL_IDX] = 0x10B4;
230
231 // Reset the shop lights to correct colors
232 pal_bg_mem[SHOP_LIGHTS_2_PAL_IDX] = SHOP_LIGHTS_2_CLR;
233 pal_bg_mem[SHOP_LIGHTS_3_PAL_IDX] = SHOP_LIGHTS_3_CLR;
234 pal_bg_mem[SHOP_LIGHTS_4_PAL_IDX] = SHOP_LIGHTS_4_CLR;
235 pal_bg_mem[SHOP_LIGHTS_1_PAL_IDX] = SHOP_LIGHTS_1_CLR;
236
237 // Disable the button highlight colors
238 pal_bg_mem[REROLL_BTN_SELECTED_BORDER_PAL_IDX] = pal_bg_mem[REROLL_BTN_PAL_IDX];
239 pal_bg_mem[NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX] = pal_bg_mem[NEXT_ROUND_BTN_PAL_IDX];
240}
241
242void shop_on_init(void)
243{
245
246 s_timer = TM_ZERO;
247
248 state_machine_register(&shop_sm);
249 state_machine_change_state(&shop_sm, GAME_SHOP_SKIP_TAGS);
250
251 skip_tag_process_start(SKIP_TAG_EVENT_ON_SHOP_INIT);
252
253 // The selection grid is initialized outside of bounds and moved
254 // to trigger the selection change so the initial selection is visible
255 shop_selection_grid.selection = SHOP_INIT_SEL;
256 selection_grid_move_selection_horz(&shop_selection_grid, 1);
257}
258
259static void shop_redeem_skip_tags(void)
260{
261 if (skip_tag_process_get_proc_stage() == SKIP_TAG_PROCESS_STAGE_END)
262 {
263 s_timer = TM_ZERO;
264 state_machine_change_state(&shop_sm, GAME_SHOP_INTRO);
265 }
266}
267
273{
274 // TODO: Randomize item type when consumables are implemented
275 return item_roll_new(ITEM_TYPE_JOKER, RNG_SEQ_SHOP_ITEMS);
276}
277
283{
284 tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT);
285
286 List* shop_items_list = &s_shop_items_list;
287
288 list_clear(shop_items_list);
289 *shop_items_list = list_init();
290
291 for (int i = 0; i < MAX_SHOP_ITEMS; i++)
292 {
294
295 if (item == NULL)
296 {
297 MGBA_FUNC_WARN("Could not create shop item");
298
299 // TODO: Decide how to handle this case gracefully
300 // If the issue for example is that we can't generate any more jokers,
301 // because for example the user owns all of them,
302 // maybe we need to generate consumables instead.
303 return;
304 }
305
306 item->x = int2fx(SHOP_JOKER_SPRITES_INIT_POS.x + i * CARD_SPRITE_SIZE_PX);
307 item->y = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y);
308 item->tx = item->x;
309 item->ty = int2fx(ITEM_SHOP_Y);
310
312
313 list_push_back(shop_items_list, item);
314 }
315}
316
320static void shop_intro()
321{
322 main_bg_se_copy_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_UP);
323
324 if (s_timer == TM_CREATE_SHOP_ITEMS_WAIT)
325 {
327 }
328
329 if (s_timer >= TM_SHIFT_SHOP_ICON_WAIT) // Shift the shop icon
330 {
331 int timer_offset = s_timer - 6;
332
333 // TODO: Extract to generic function?
334 for (int y = 0; y < timer_offset; y++)
335 {
336 Rect from = SHOP_ICON_FROM_RECT;
337 from.top += y - timer_offset;
338 from.bottom += y - timer_offset;
339
340 BG_POINT to = SHOP_ICON_TO_POS;
341 to.y = y;
342
343 main_bg_se_copy_rect(from, to);
344 }
345 }
346
347 if (s_timer == TM_END_GAME_SHOP_INTRO)
348 {
349 state_machine_change_state(&shop_sm, GAME_SHOP_ACTIVE);
350 s_timer = TM_ZERO; // Reset the timer
351
352 // print initial reroll cost only when the panel is in place
353 tte_printf(
354 "#{P:%d,%d; cx:0x%X000}$%d",
355 SHOP_REROLL_RECT.left,
356 SHOP_REROLL_RECT.top,
357 TTE_WHITE_PB,
358 s_reroll_cost
359 );
360 }
361}
362
367static int shop_top_row_get_size(void)
368{
369 // + 1 to account for next round button
370 return list_get_len(&s_shop_items_list) + 1;
371}
372
376static inline void shop_buy_item(int shop_item_idx)
377{
378 List* shop_items_list = &s_shop_items_list;
379 Item* item = (Item*)list_get_at_idx(shop_items_list, shop_item_idx);
380
381 g_game_vars.money -= item_get_buy_price(item);
382 display_money();
385 item_acquire(item);
386 list_remove_at_idx(shop_items_list, shop_item_idx); // Remove the joker from the shop
387}
388
392static void shop_top_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection)
393{
394 if (!key_hit(SELECT_CARD))
395 return;
396
397 if (selection->x == NEXT_ROUND_BTN_SEL_X)
398 {
399 button_press(&next_round_button);
400 }
401 else
402 {
403 int shop_item_idx = selection->x - 1; // - 1 to account for next round button
404 Item* item = (Item*)list_get_at_idx(&s_shop_items_list, shop_item_idx);
405 if (!item_can_acquire(item) || g_game_vars.money < item_get_buy_price(item))
406 {
407 return;
408 }
409
410 shop_buy_item(shop_item_idx);
411 selection_grid_move_selection_horz(selection_grid, -1);
412 }
413}
414
419 SelectionGrid* selection_grid,
420 int row_idx,
421 const Selection* prev_selection,
422 const Selection* new_selection
423)
424{
425 List* shop_items_list = &s_shop_items_list;
426 // Guard if we move down while on jokers
427 if (new_selection->y > row_idx && prev_selection->x > 0)
428 return false;
429
430 // The selection grid system only guarantees that the new selection is within bounds
431 // but not the previous one...
432 // This allows using INIT_SEL = {-1, 1} and move to set the initial selection in a hacky way...
433 if (prev_selection->y == row_idx && prev_selection->x >= 0 &&
434 prev_selection->x < shop_top_row_get_size())
435 {
436 if (prev_selection->x == NEXT_ROUND_BTN_SEL_X)
437 {
438 button_set_highlight(&next_round_button, false);
439 }
440 else
441 {
442 int idx = prev_selection->x - 1; // -1 to account for next round button
443 SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(shop_items_list, idx);
444 sprite_object_set_focus(sprite_object, false);
445 }
446 }
447
448 if (new_selection->y == row_idx)
449 {
450 if (new_selection->x == NEXT_ROUND_BTN_SEL_X)
451 {
452 button_set_highlight(&next_round_button, true);
453 }
454 else
455 {
456 int idx = new_selection->x - 1; // -1 to account for next round button
457 SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(shop_items_list, idx);
458 sprite_object_set_focus(sprite_object, true);
459 }
460 }
461
462 return true;
463}
464
471{
472 return 1;
473}
474
479 SelectionGrid* selection_grid,
480 int row_idx,
481 const Selection* prev_selection,
482 const Selection* new_selection
483)
484{
485 if (row_idx == prev_selection->y)
486 {
487 button_set_highlight(&reroll_button, false);
488
489 if (new_selection->x != NEXT_ROUND_BTN_SEL_X)
490 {
491 int idx = new_selection->x - 1;
492 SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(&s_shop_items_list, idx);
493 sprite_object_set_focus(sprite_object, true);
494 }
495 }
496 else if (row_idx == new_selection->y)
497 {
498 button_set_highlight(&reroll_button, true);
499 }
500
501 return true;
502}
503
508static inline void shop_reroll(void)
509{
510 g_game_vars.money -= s_reroll_cost;
511 display_money(); // Update the money display
512
513 List* shop_items_list = &s_shop_items_list;
514 ListItr itr = list_itr_create(shop_items_list);
515 Item* item;
516
517 while ((item = list_itr_next(&itr)))
518 {
519 if (item != NULL && item->type == ITEM_TYPE_JOKER)
520 {
521 item_dispose(&item);
522 }
523 }
524
525 list_clear(shop_items_list);
526 *shop_items_list = list_init();
527
529
530 itr = list_itr_create(shop_items_list);
531
532 SpriteObject* item_sprite_object;
533 while ((item_sprite_object = list_itr_next(&itr)))
534 {
535 if (item_sprite_object != NULL)
536 {
537 item_sprite_object->y = item_sprite_object->ty;
538
539 sprite_object_shake(item_sprite_object, UNDEFINED);
540 }
541 }
542
543 s_reroll_cost++;
544 tte_printf(
545 "#{P:%d,%d; cx:0x%X000}$%d",
546 SHOP_REROLL_RECT.left,
547 SHOP_REROLL_RECT.top,
548 TTE_WHITE_PB,
549 s_reroll_cost
550 );
551}
552
556static void shop_reroll_row_on_key_transit(SelectionGrid* selection_grid, Selection* selection)
557{
558 if (!key_hit(SELECT_CARD))
559 {
560 return;
561 }
562
563 button_press(&reroll_button);
564}
565
566static void next_round_on_pressed(void)
567{
568 // Go to next blind selection game state
569 state_machine_change_state(&shop_sm, GAME_SHOP_EXIT);
570 s_timer = TM_ZERO;
571 s_reroll_cost = REROLL_BASE_COST;
572
573 pal_bg_mem[NEXT_ROUND_BTN_SELECTED_BORDER_PAL_IDX] = pal_bg_mem[SHOP_PANEL_SHADOW_PAL_IDX];
574}
575
576static void reroll_on_pressed(void)
577{
578 // TODO: Add money sound effect
579 shop_reroll();
580}
581
582static bool reroll_can_be_pressed(void)
583{
584 return g_game_vars.money >= s_reroll_cost;
585}
586
590static void shop_process_user_input(void)
591{
592 selection_grid_process_input(&shop_selection_grid);
593
594 static Item* new_description_item = NULL;
595
596 // Determine the Item we would show the description of
597 switch (shop_selection_grid.selection.y)
598 {
599 case SHOP_GRID_OWNED_ROW:
600 {
601 new_description_item =
602 list_get_at_idx(get_jokers_list(), shop_selection_grid.selection.x);
603 break;
604 }
605
606 case SHOP_GRID_FOR_SALE_ROW:
607 {
608 // Ignore the "Next Round" button when selecting descripted Item: get it at
609 // selection.x - 1 in the list to account for this next round button being ignored.
610 new_description_item =
611 (shop_selection_grid.selection.x != NEXT_ROUND_BTN_SEL_X)
612 ? list_get_at_idx(&s_shop_items_list, shop_selection_grid.selection.x - 1)
613 : NULL;
614 break;
615 }
616
617 // TODO: handle Consumables and Vouchers when implemented
618
619 default:
620 {
621 new_description_item = NULL;
622 break;
623 }
624 }
625
626 // Show description of selected card when pressing B
627 if (new_description_item != NULL && key_held(DESELECT_CARDS))
628 {
629 s_description_item = new_description_item;
630 s_description_item_original_x_pos = s_description_item->tx;
631 s_description_item_original_y_pos = s_description_item->ty;
632
633 s_timer = TM_ZERO;
634 state_machine_change_state(&shop_sm, GAME_SHOP_SHOW_ITEM_DESC);
635 }
636}
637
638static void shop_show_item_desc_on_init(void)
639{
640 // This starts at 0, then gets incremented up to TM_SHOW_ITEM_DESC_WAIT. Will be used to
641 // revert the animation if the B button is released midway through it
642 s_show_description_anim_progress = 0;
643
644 // Erase shop/round text
645 tte_erase_rect_wrapper(PLAYING_SCREEN_RECT);
646
647 // Move all other Sprites offscreen
648
649 Item* tmp_item = NULL;
650
651 // Owned Jokers
652 ListItr itr = list_itr_create(get_jokers_list());
653 while ((tmp_item = (Item*)list_itr_next(&itr)))
654 {
655 if (tmp_item != s_description_item)
656 tmp_item->ty -= int2fx(OWNED_ITEMS_HIDE_Y_OFFSET);
657 }
658
659 // Shop Jokers
660 itr = list_itr_create(&s_shop_items_list);
661 while ((tmp_item = (Item*)list_itr_next(&itr)))
662 {
663 if (tmp_item != s_description_item)
664 tmp_item->ty = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y + TILE_SIZE);
665 }
666
668
669 // Set description_card new target position
670
671 s_description_item->tx = int2fx(ITEM_DESCRIPTION_SPRITE_POS.x);
672 s_description_item->ty = int2fx(ITEM_DESCRIPTION_SPRITE_POS.y);
673}
674
675static void shop_show_item_desc_on_update(void)
676{
677 if (s_timer <= TM_SHOW_ITEM_DESC_WAIT)
678 {
679 s_show_description_anim_progress++;
680
681 // Hide Deck (last frames only)
682 if (TM_SHOW_ITEM_DESC_WAIT - s_timer < TM_HIDE_DECK_WAIT)
683 main_bg_se_move_rect_1_tile_vert(DECK_ANIM_RECT, SCREEN_DOWN);
684 // Hide shop panel
685 main_bg_se_move_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_DOWN);
686 // Hide Owned Cards panels
687 main_bg_se_move_rect_1_tile_vert(OWNED_ITEMS_PANEL_RECT, SCREEN_UP);
688 }
689
690 // Anim end
691 else if (s_timer == TM_SHOW_ITEM_DESC_WAIT + 1)
692 {
693 // Print the Item's name
694 const char* item_name = item_get_name(s_description_item);
695 tte_printf(
696 TTE_WHITE_TAG "#{P:%d,%d}%*s%s",
697 ITEM_NAME_TEXT_RECT.left * TILE_SIZE,
698 ITEM_NAME_TEXT_RECT.top * TILE_SIZE,
699 (rect_width(&ITEM_NAME_TEXT_RECT) - strlen(item_name)) / 2,
700 "",
701 item_name
702 );
703
704 // Compute needed space for the description and print it
705 int nb_printed_lines = item_print_description(s_description_item, ITEM_DESC_TEXT_RECT);
706 int desc_bottom_offset = ITEM_DESC_MAX_TEXT_HEIGHT - nb_printed_lines;
707
708 // Print Rarity/Type and change the panel's color before drawing it so the color is already
709 // set, in case there is any lag
710 ItemDescSubtypeInfo item_subtype_info = item_get_subtype_info(s_description_item);
711 const char* subtype_str = item_subtype_info.name_str;
712 tte_printf(
713 TTE_WHITE_TAG "#{P:%d,%d}%*s%s",
714 ITEM_DESC_TEXT_RECT.left * TILE_SIZE,
715 (ITEM_DESC_TEXT_RECT.bottom - desc_bottom_offset - 1) * TILE_SIZE,
716 (rect_width(&ITEM_DESC_TEXT_RECT) - strlen(subtype_str)) / 2,
717 "",
718 subtype_str
719 );
720
721 pal_bg_mem[SHOP_DESC_RARITY_MAIN_COLOR_PAL_IDX] = item_subtype_info.main_color;
722 pal_bg_mem[SHOP_DESC_RARITY_SHADOW_COLOR_PAL_IDX] = item_subtype_info.shadow_color;
723
724 // Draw description panel
725 Rect actual_dest_rect = ITEM_DESC_9_PTCH_TO_RECT;
726 actual_dest_rect.bottom -= desc_bottom_offset;
727 main_bg_se_copy_expand_9_patch(actual_dest_rect, &ITEM_DESC_9_PTCH_SRC);
728 }
729
730 // Actively wait for the B button to be released
731 if (!key_held(DESELECT_CARDS))
732 {
733 s_timer = TM_ZERO;
734 state_machine_change_state(&shop_sm, GAME_SHOP_HIDE_ITEM_DESC);
735 }
736}
737
738static void shop_hide_item_desc_on_init(void)
739{
740 // Erase shop text and Joker Description frame if we had time to draw them
741 if (s_show_description_anim_progress >= TM_SHOW_ITEM_DESC_WAIT)
742 {
743 main_bg_se_clear_rect(ITEM_DESC_9_PTCH_TO_RECT);
744 }
745 // Or clear the owned cards' panel that haven't finished moving up
746 else
747 {
748 main_bg_se_clear_rect(OWNED_ITEMS_PANEL_ANIM_CLEAR);
749 }
750
751 tte_erase_rect_wrapper(PLAYING_SCREEN_RECT);
752
753 // Redraw Jokers/Consumables frames
754 main_bg_se_copy_expand_3x3_rect(OWNED_JOKERS_PANEL_RECT, OWNED_ITEMS_PANEL_3X3_SRC_POS);
755 main_bg_se_copy_expand_3x3_rect(OWNED_CONSUMABLES_PANEL_RECT, OWNED_ITEMS_PANEL_3X3_SRC_POS);
756
757 // Move Sprites back to their positions
758
759 Item* tmp_item = NULL;
760
761 // Owned Items
762 ListItr itr = list_itr_create(get_jokers_list());
763 while ((tmp_item = (Item*)list_itr_next(&itr)))
764 {
765 if (tmp_item != s_description_item)
766 tmp_item->ty = int2fx(HELD_JOKERS_POS.y);
767 }
768
769 // Shop Jokers
770 itr = list_itr_create(&s_shop_items_list);
771 while ((tmp_item = (Item*)list_itr_next(&itr)))
772 {
773 if (tmp_item != s_description_item)
774 tmp_item->ty = int2fx(ITEM_SHOP_Y);
775 }
776
778
779 s_description_item->tx = s_description_item_original_x_pos;
780 s_description_item->ty = s_description_item_original_y_pos;
781}
782
783static void shop_hide_item_desc_on_update(void)
784{
785 if (s_timer <= s_show_description_anim_progress)
786 {
787 // Show Deck (last frames only)
788 if (s_show_description_anim_progress > TM_HIDE_DECK_WAIT &&
789 s_timer < (s_show_description_anim_progress - (TM_HIDE_DECK_WAIT + 1)))
790 {
791 main_bg_se_move_rect_1_tile_vert(DECK_ANIM_RECT, SCREEN_UP);
792 }
793 // Show menu panel
794 main_bg_se_move_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_UP);
795 }
796
797 // Last anim frame (no need to wait for the Joker to have stopped for this):
798 else if (s_timer == s_show_description_anim_progress + 1)
799 {
800 s_timer = TM_ZERO;
801 state_machine_change_state(&shop_sm, GAME_SHOP_ACTIVE);
802 }
803}
804
805static void shop_hide_item_desc_on_exit(void)
806{
807 // TODO: Do the following in a less hacky way
808
809 // Need to account for the description_card being selected if it came from the shop.
810 // Temporarily lower the target position of the Item so that the price is printed in the
811 // appropriate place
812 if (!item_is_owned(s_description_item))
813 s_description_item->ty += int2fx(TILE_SIZE);
814
815 // Print price under shop Jokers
816 Item* item = NULL;
817 ListItr itr = list_itr_create(&s_shop_items_list);
818 while ((item = list_itr_next(&itr)))
819 {
821 }
822
823 // Revert what we just did so the Item still appears selected
824 if (!item_is_owned(s_description_item))
825 s_description_item->ty -= int2fx(TILE_SIZE);
826
827 // Print Reroll prince
828 tte_printf(
829 "#{P:%d,%d; cx:0x%X000}$%d",
830 SHOP_REROLL_RECT.left,
831 SHOP_REROLL_RECT.top,
832 TTE_WHITE_PB,
833 s_reroll_cost
834 );
835
836 // Print Deck size that was erased
837 display_deck_size_max();
838
839 // if we are NOT pressing A, print the price under the description card if it's a card we own.
840 if (!key_held(SELECT_CARD) && item_is_owned(s_description_item))
841 {
843 (SpriteObject*)s_description_item,
844 item_get_sell_price(s_description_item)
845 );
846 }
847
848 s_description_item = NULL;
849}
850
855static void shop_outro(void)
856{
857 // Shift the shop panel
858 main_bg_se_move_rect_1_tile_vert(POP_MENU_ANIM_RECT, SCREEN_DOWN);
859
860 main_bg_se_copy_rect_1_tile_vert(TOP_LEFT_PANEL_ANIM_RECT, SCREEN_UP);
861
862 if (s_timer == 1)
863 {
864 tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT); // Erase the shop prices text
865
866 ListItr itr = list_itr_create(&s_shop_items_list);
867 SpriteObject* shop_item;
868 while ((shop_item = list_itr_next(&itr)))
869 {
870 if (shop_item != NULL)
871 {
872 shop_item->ty = int2fx(160);
873 }
874 }
875
877 }
878 else if (s_timer == 2)
879 {
880 // TODO: make heads or tails of what's going on here and replace
881 // magic numbers.
882 int y = 5;
883 memset16(&se_mat[MAIN_BG_SBB][y - 1][0], 0x0001, 1);
884 memset16(&se_mat[MAIN_BG_SBB][y - 1][1], 0x0002, 7);
885 memset16(&se_mat[MAIN_BG_SBB][y - 1][8], SE_HFLIP | 0x0001, 1);
886 }
887
888 if (s_timer >= MENU_POP_OUT_ANIM_FRAMES)
889 {
890 game_change_state(GAME_STATE_BLIND_SELECT);
891 }
892}
893
897static inline void shop_lights_anim_frame(void)
898{
899 // Shift palette around the border of the shop icon
900 COLOR shifted_palette[4];
901 shifted_palette[0] = pal_bg_mem[SHOP_LIGHTS_2_PAL_IDX];
902 shifted_palette[1] = pal_bg_mem[SHOP_LIGHTS_3_PAL_IDX];
903 shifted_palette[2] = pal_bg_mem[SHOP_LIGHTS_4_PAL_IDX];
904 shifted_palette[3] = pal_bg_mem[SHOP_LIGHTS_1_PAL_IDX];
905
906 // Circularly shift the palette
907 int last = shifted_palette[3];
908
909 for (int i = 3; i > 0; --i)
910 {
911 shifted_palette[i] = shifted_palette[i - 1];
912 }
913
914 shifted_palette[0] = last;
915
916 // Copy the shifted palette to the next 4 slots
917 pal_bg_mem[SHOP_LIGHTS_2_PAL_IDX] = shifted_palette[0];
918 pal_bg_mem[SHOP_LIGHTS_3_PAL_IDX] = shifted_palette[1];
919 pal_bg_mem[SHOP_LIGHTS_4_PAL_IDX] = shifted_palette[2];
920 pal_bg_mem[SHOP_LIGHTS_1_PAL_IDX] = shifted_palette[3];
921}
922
924{
925 s_timer++;
926
927 if (s_timer % 20 == 0)
928 {
930 }
931}
932
933void shop_on_exit(void)
934{
935 List* shop_items_list = &s_shop_items_list;
936 ListItr itr = list_itr_create(shop_items_list);
937 Item* item;
938
939 while ((item = list_itr_next(&itr)))
940 {
941 item_dispose(&item);
942 }
943
944 list_clear(shop_items_list);
945
946 increment_blind(BLIND_STATE_DEFEATED); // TODO: Move to game_round_end()?
947
948 state_machine_remove(&shop_sm);
949}
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.
bool item_is_owned(Item *item)
Returns whether or not the given Item is in the player's possession.
Definition item.c:179
int item_get_sell_price(Item *item)
Returns the sell price of the item.
Definition item.c:52
void item_dispose(Item **item)
Destroys an item, freeing underlying resources, and manages rollable items sets if needed....
Definition item.c:147
const char * item_get_name(Item *item)
Returns the name of the Item.
Definition item.c:70
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:203
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:11
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:133
ItemDescSubtypeInfo item_get_subtype_info(Item *item)
Returns the colors and name of the Item's subtype.
Definition item.c:89
void item_acquire(Item *item)
Acquires the item, adding to inventory if applicable. Called when it is purchased from the shop,...
Definition item.c:116
int item_print_description(Item *item, Rect dest_rect)
Prints the item's description inside the given rectangle.
Definition item.c:162
int item_get_buy_price(Item *item)
Returns the buy price of the item.
Definition item.c:34
Functions relative to the handling of Jokers.
void joker_reset_rollable_jokers(void)
Reset rollable jokers to include all jokers in the registry.
Definition joker.c:373
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:92
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 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:478
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:392
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:418
void shop_on_exit(void)
Shop cleanup.
Definition shop.c:933
void shop_on_update(void)
Shop state update.
Definition shop.c:923
static void 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:508
static void shop_outro(void)
Outro sequence substate update. This makes the menu and shop icon go out of frame.
Definition shop.c:855
static int shop_reroll_row_get_size(void)
Get size of the row with the "Reroll" button.
Definition shop.c:470
int shop_get_reroll_cost(void)
Get the current cost to reroll the items sold in the shop.
Definition shop.c:201
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:367
void shop_on_init(void)
Shop state initialization.
Definition shop.c:242
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:556
static void shop_intro(void)
Intro sequence (menu and shop icon coming into frame)
Definition shop.c:320
void shop_change_background(void)
Change to the shop background.
Definition shop.c:218
Item * shop_get_description_item(void)
Get a pointer to the Card we are currently showing the description of.
Definition shop.c:196
void shop_set_reroll_cost(int cost)
Set the current cost to reroll the items sold in the shop to the given value.
Definition shop.c:206
void 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:211
static void shop_buy_item(int shop_item_idx)
Called when pressing A on a Shop Joker to buy it.
Definition shop.c:376
static Item * 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:272
static void shop_lights_anim_frame(void)
Cycling shop lights animation substate update.
Definition shop.c:897
static void shop_process_user_input(void)
Handle user inputs logic in the Shop though a SelectionGrid.
Definition shop.c:590
static void 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:282
Shop state functions.
Data structures and functions related to the handling of Skip Tags.
void set_owned_skip_tags_moved_offscreen(bool move_offscreen)
Show or hide the owned SkipTag sprites by moving them offscreen to the right.
Definition skip_tag.c:141
void skip_tag_process_start(enum SkipTagEvent checked_tag_event)
Start Skip Tag processing for the specified event.
Definition skip_tag.c:344
enum SkipTagProcStage skip_tag_process_get_proc_stage(void)
Recover the current stage of the Tag processing.
Definition skip_tag.c:339
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:446
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:523
void sprite_object_shake(SpriteObject *sprite_object, mm_word sound_id)
Shake SpriteObject on screen and play a sound.
Definition sprite.c:403
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:533
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
Structure containing the main and shadow colors, and the name string associated with an Item's subtyp...
Definition item.h:65
A generic interface for all items that can appear in the shop or be in the inventory....
Definition item.h:83
enum ItemType type
The item type - used to dispatch the function implementations for inheriting types.
Definition item.h:94
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:86
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:55