Refactor shop code to use generic Item (#536)

* Turned SpriteObject into a member of JokerObject and CardObject and expiremented with casting

* Extracted shop code to use generic `SpriteObject`

* Extracted shop joker generation to a function

* Renamed object -> item

* Added setters and getters for `SpriteObject` members and updated all accesses to use them with casting (AI used for tedious work and reviewed - model Raptor mini)

* Added sprite_object_set_vx() because sprite_object_set_vy() exists and reordered the function order to have x,y parameters first, then targets

* Added Item object to act as a purchasable interface for the shop - AI used (model Raptor mini)

* Added -fms-extensions and switched to anonymous inheritance

* Fixed anonymous inheritance for joker and added direct access for that too

* fix build error from previous commit + clang-format

* Fixed build errors + clang-format for real this time (I hope...)

* clang-format one last time, I don't understand it anymore

* clang-format is annoying

* Added uses of CHECK_NULL_ARG macros

* Merged item_defs.h into item.h and fixed build error from previous commit

* clang-format...?

* Changed sprite_object_new() into sprite_object_init() and made it accept a parameter instead of returning one to make it less awkward

* Updated sprite_object_init() documentation

* Updated sprite_object_destroy documentation

* More clang-format

* Renamed add_to_inventory to acquire and updated documentation

* Added function documentations

* Update documentation + swap order of consumable and playing card types

* Added documentation for new files (+ small addition to button.h)

* clang-format

* Used typedef

* A bit more documentation

* Changed sho pcode to use generic Item instead of SpriteObject

* clang-format + fixed NULL-check error

* Revert weird clang-format...?

* clang-format for real

* Fixed errors from rebase on log_function_name

* Fixed build errors from rebase on main

* Added some more documentation about first member struct inheritance

* typo fix

* typo fix for real

* Removed SpriteObject field getters/setters (AI used - model Raptor mini)

* Renamed item_acquire -> item_on_acquired

* Added item_can_acquire()

* clang-format

* Fixed some sprite issues but not all...

* Fixed some more issues

* Cleanup JokerObject some more JokerObjects in the shop

* Added `item_destroy()`

* Added item_set_available_to_shop, CHECK_ITEM_TYPE macros + small documentation updates

* Another small JokerObject->Item replacement

* Removed item_set_available_to_shop and replaced with item_roll_new() and item_dispose(), also changed "available_to_shop" semantics to "rollable"

* Re-applied changes from game.c to round.c after rebase on main

* Added partial implementations of item functions for playing cards

* Line endings fix...

* Macro renames, function documentations

* Updated documentations

* Apply fixes from Copilot review

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Fix card_object_get_buy_price type checking

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Avoid double pointer cast on dispose

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Apply suggestions from @ricfehr3's code review

Co-authored-by: Rickey <ric@rf3.xyz>

* Remove redundant comments

* Moved error checking before joker_get_random_rarity

Co-authored-by: Rickey <ric@rf3.xyz>

* Fixed build error from rebase

* clang-format

* Added doxygen documentation for Item explaining inheritance

* Removed unnecessary comment

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Rickey <ric@rf3.xyz>
This commit is contained in:
MeirGavish
2026-07-09 09:13:51 +03:00
committed by GitHub
co-authored by Copilot Autofix powered by AI Rickey
parent 369c680cb7
commit d24b0b5619
23 changed files with 796 additions and 329 deletions
+1 -1
View File
@@ -143,7 +143,7 @@ static void game_over_common_init(enum EndCondition init_condition)
ListItr itr = list_itr_create(get_jokers_list());
while ((joker_object = list_itr_next(&itr)))
{
obj_hide(joker_object->sprite_object->sprite->obj);
obj_hide(joker_object->sprite->obj);
}
// Destroy any preexisting cards in the deck, which will be present if we restart a run
+9 -9
View File
@@ -33,8 +33,8 @@ bool jokers_sel_row_on_selection_changed(
// Don't change focus from current Joker if swapping
if (joker_object != NULL && !swapping)
{
sprite_object_erase_text_under(joker_object->sprite_object);
sprite_object_set_focus(joker_object->sprite_object, false);
sprite_object_erase_text_under((SpriteObject*)joker_object);
sprite_object_set_focus((SpriteObject*)joker_object, false);
}
}
@@ -46,7 +46,7 @@ bool jokers_sel_row_on_selection_changed(
{
if (!swapping)
{
sprite_object_set_focus(joker_object->sprite_object, true);
sprite_object_set_focus((SpriteObject*)joker_object, true);
}
// If we land on this row while the A button is being held, we are in swapping mode
// This means that we need to hide the price, whether we were already
@@ -54,7 +54,7 @@ bool jokers_sel_row_on_selection_changed(
if (!key_is_down(SELECT_CARD))
{
sprite_object_print_price_under(
joker_object->sprite_object,
(SpriteObject*)joker_object,
joker_get_sell_value(joker_object->joker)
);
}
@@ -75,8 +75,8 @@ bool jokers_sel_row_on_selection_changed(
static inline void joker_start_discard_animation(JokerObject* joker_object)
{
joker_object->sprite_object->tx = int2fx(JOKER_DISCARD_TARGET.x);
joker_object->sprite_object->ty = int2fx(JOKER_DISCARD_TARGET.y);
joker_object->tx = int2fx(JOKER_DISCARD_TARGET.x);
joker_object->ty = int2fx(JOKER_DISCARD_TARGET.y);
list_push_back(get_discarded_jokers_list(), joker_object);
}
@@ -90,7 +90,7 @@ static inline void game_sell_joker(int joker_idx)
JokerObject* joker_object = (JokerObject*)list_get_at_idx(owned_jokers_list, joker_idx);
g_game_vars.money += joker_get_sell_value(joker_object->joker);
display_money();
sprite_object_erase_text_under(joker_object->sprite_object);
sprite_object_erase_text_under((SpriteObject*)joker_object);
remove_owned_joker(joker_idx);
@@ -104,12 +104,12 @@ void jokers_sel_row_on_key_transit(SelectionGrid* selection_grid, Selection* sel
{
if (key_hit(SELECT_CARD))
{
sprite_object_erase_text_under(joker_object->sprite_object);
sprite_object_erase_text_under((SpriteObject*)joker_object);
}
else if (key_released(SELECT_CARD))
{
sprite_object_print_price_under(
joker_object->sprite_object,
(SpriteObject*)joker_object,
joker_get_sell_value(joker_object->joker)
);
}
+5 -4
View File
@@ -105,9 +105,10 @@ void game_main_menu_on_init(void)
change_background(BG_MAIN_MENU, true);
main_menu_ace = card_object_new(card_new(SPADES, ACE));
card_object_set_sprite(main_menu_ace, 0);
main_menu_ace->sprite_object->sprite->obj->attr0 |= ATTR0_AFF_DBL;
main_menu_ace->sprite_object->tscale = float2fx(0.8f);
sprite_object_position(main_menu_ace->sprite_object, MAIN_MENU_ACE_T_X, MAIN_MENU_ACE_T_Y);
// TODO: NULL-check sprite
main_menu_ace->sprite->obj->attr0 |= ATTR0_AFF_DBL;
main_menu_ace->tscale = float2fx(0.8f);
sprite_object_position((SpriteObject*)main_menu_ace, MAIN_MENU_ACE_T_X, MAIN_MENU_ACE_T_Y);
// Select last highlighted button, Play button by default.
// e.g. if we return from the options menu, we want the Options button to be highlighted.
@@ -120,7 +121,7 @@ void game_main_menu_on_init(void)
void game_main_menu_on_update(void)
{
main_menu_ace->sprite_object->trotation = lu_sin((g_game_vars.timer << 8) / 2) / 3;
main_menu_ace->trotation = lu_sin((g_game_vars.timer << 8) / 2) / 3;
selection_grid_process_input(&main_menu_selection_grid);
}
+26 -29
View File
@@ -783,7 +783,7 @@ static inline void card_in_hand_loop_handle_discard_and_shuffling(
sound_played = true;
}
if (hand[card_idx]->sprite_object->x >= *hand_x)
if (hand[card_idx]->x >= *hand_x)
{
discard_push(hand[card_idx]->card);
@@ -801,8 +801,8 @@ static inline void card_in_hand_loop_handle_discard_and_shuffling(
// and we end up with an element at `card_idx` that is still NULL.
if (hand[card_idx] != NULL)
{
*hand_y = hand[card_idx]->sprite_object->y;
*hand_x = hand[card_idx]->sprite_object->x;
*hand_y = hand[card_idx]->y;
*hand_x = hand[card_idx]->x;
}
}
@@ -1138,8 +1138,8 @@ static inline void card_draw(void)
const FIXED deck_x = int2fx(CARD_DRAW_POS.x);
const FIXED deck_y = int2fx(CARD_DRAW_POS.y);
card_object->sprite_object->x = deck_x;
card_object->sprite_object->y = deck_y;
card_object->x = deck_x;
card_object->y = deck_y;
set_hand_top(get_hand_top() + 1);
get_hand_array()[get_hand_top()] = card_object;
@@ -1190,16 +1190,16 @@ static inline void game_round_discarded_cards_loop(void)
// Set the sprite for the discarded card object
card_object_set_sprite(discarded_card_object, 0);
sprite_object_reset_transform(discarded_card_object->sprite_object);
sprite_object_reset_transform((SpriteObject*)discarded_card_object);
discarded_card_object->sprite_object->tx = int2fx(204);
discarded_card_object->sprite_object->ty = int2fx(112);
discarded_card_object->sprite_object->x = int2fx(240);
discarded_card_object->sprite_object->y = int2fx(80);
discarded_card_object->tx = int2fx(204);
discarded_card_object->ty = int2fx(112);
discarded_card_object->x = int2fx(240);
discarded_card_object->y = int2fx(80);
}
else
{
if (discarded_card_object->sprite_object->y >= discarded_card_object->sprite_object->ty)
if (discarded_card_object->y >= discarded_card_object->ty)
{
deck_push(discarded_card_object->card); // Put the card back into the deck
card_object_destroy(&discarded_card_object);
@@ -1301,13 +1301,13 @@ static inline void cards_in_hand_update_loop(void)
{
hand_y -= int2fx(CARD_FOCUSED_SEL_Y);
}
if (i != selected_card_idx && hand[i]->sprite_object->y > hand_y)
if (i != selected_card_idx && hand[i]->y > hand_y)
{
hand[i]->sprite_object->y = hand_y;
hand[i]->y = hand_y;
// Set target y to match y. Ensures target is updated even when vy becomes
// 0, preventing immediate snap back.
hand[i]->sprite_object->ty = hand_y;
hand[i]->sprite_object->vy = 0;
hand[i]->ty = hand_y;
hand[i]->vy = 0;
}
hand_x = hand_x + (int2fx(i) - int2fx(get_hand_top()) / 2) *
@@ -1339,7 +1339,7 @@ static inline void cards_in_hand_update_loop(void)
{
card_object_set_selected(hand[i], false);
played_push(hand[i]);
sprite_destroy(&hand[i]->sprite_object->sprite);
sprite_destroy(&hand[i]->sprite);
hand[i] = NULL;
reorder_card_sprites_layers();
@@ -1376,8 +1376,8 @@ static inline void cards_in_hand_update_loop(void)
break;
}
hand[i]->sprite_object->tx = hand_x;
hand[i]->sprite_object->ty = hand_y;
hand[i]->tx = hand_x;
hand[i]->ty = hand_y;
}
}
}
@@ -1526,14 +1526,14 @@ static inline void play_starting_played_cards_update(int played_idx)
}
}
played[played_idx]->sprite_object->tx =
played[played_idx]->tx =
int2fx(HAND_PLAY_POS.x) + (int2fx(played_top - played_idx) - int2fx(played_top) / 2) * -27;
played[played_idx]->sprite_object->ty = int2fx(HAND_PLAY_POS.y);
played[played_idx]->ty = int2fx(HAND_PLAY_POS.y);
card_selected = card_object_is_selected(played[played_idx]);
if (card_selected && played_top - played_idx >= scored_card_index)
{
played[played_idx]->sprite_object->ty -= int2fx(10);
played[played_idx]->ty -= int2fx(10);
}
}
@@ -1593,10 +1593,7 @@ static inline bool play_scoring_cards_update(void)
if (card_object_is_selected(scored_card_object))
{
// Offset of 1 tile to keep the text on the card
tte_set_pos(
fx2int(scored_card_object->sprite_object->x) + TILE_SIZE,
SCORED_CARD_TEXT_Y
);
tte_set_pos(fx2int(scored_card_object->x) + TILE_SIZE, SCORED_CARD_TEXT_Y);
// Set text color to blue from background memory
tte_set_special(TTE_BLUE_PB * TTE_SPECIAL_PB_MULT_OFFSET);
@@ -1809,7 +1806,7 @@ static inline void play_ending_played_cards_update(int played_idx)
if (card_object_is_selected(played[played_idx]) && played_top - played_idx >= scored_card_index)
{
played[played_idx]->sprite_object->ty = int2fx(HAND_PLAY_POS.y);
played[played_idx]->ty = int2fx(HAND_PLAY_POS.y);
}
}
@@ -1837,7 +1834,7 @@ static bool play_ended_played_cards_update(int played_idx)
}
// card has exited the screen, now discard it and set it to NULL
if (played[played_idx]->sprite_object->x >= int2fx(CARD_DISCARD_PNT.x))
if (played[played_idx]->x >= int2fx(CARD_DISCARD_PNT.x))
{
discard_push(played[played_idx]->card); // Push the card to the discard pile
card_object_destroy(&played[played_idx]);
@@ -1870,7 +1867,7 @@ static bool play_ended_played_cards_update(int played_idx)
}
// put target X position off screen to the right
played[played_idx]->sprite_object->tx = int2fx(CARD_DISCARD_PNT.x);
played[played_idx]->tx = int2fx(CARD_DISCARD_PNT.x);
discarded_card = true;
}
@@ -1968,7 +1965,7 @@ static inline void played_cards_update_loop(void)
break;
}
played[played_idx]->sprite_object->tscale = FIX_ONE;
played[played_idx]->tscale = FIX_ONE;
}
}
+22 -4
View File
@@ -602,7 +602,7 @@ void game_run_setup_on_init(void)
card_object_set_sprite_face_down(run_setup_deck, g_game_vars.deck, 0);
sprite_object_position(
run_setup_deck->sprite_object,
(SpriteObject*)run_setup_deck,
RUN_SETUP_DECK_SPRITE_T_X,
RUN_SETUP_DECK_SPRITE_T_Y
);
@@ -650,7 +650,13 @@ void game_run_setup_on_exit(void)
static void choose_deck_substate_init(void)
{
// Show Deck sprite, name and TODO: description
obj_unhide(run_setup_deck->sprite_object->sprite->obj, ATTR0_AFF);
{
Sprite* deck_sprite = sprite_object_get_sprite((SpriteObject*)run_setup_deck);
if (deck_sprite != NULL)
{
obj_unhide(deck_sprite->obj, ATTR0_AFF);
}
}
print_deck_name(g_game_vars.deck, RUN_SETUP_DECK_NAME_TEXT_POS);
print_deck_description(g_game_vars.deck, RUN_SETUP_DECK_DESC_TEXT_POS);
@@ -815,7 +821,13 @@ static void seed_keyboard_substate_init(void)
tte_erase_rect_wrapper(RUN_SETUP_DECK_NAME_DESC_RECT);
// Hide Deck card sprite
obj_hide(run_setup_deck->sprite_object->sprite->obj);
{
Sprite* deck_sprite = sprite_object_get_sprite((SpriteObject*)run_setup_deck);
if (deck_sprite != NULL)
{
obj_hide(deck_sprite->obj);
}
}
// Clean deck swap screen with frame BG color
main_bg_se_copy_expand_tile(
@@ -1110,7 +1122,13 @@ static void resume_substate_init(void)
tab_set_highlight(RUN_SETUP_TAB_RESUME);
// Show Deck card sprite
obj_unhide(run_setup_deck->sprite_object->sprite->obj, ATTR0_AFF);
{
Sprite* deck_sprite = sprite_object_get_sprite((SpriteObject*)run_setup_deck);
if (deck_sprite != NULL)
{
obj_unhide(deck_sprite->obj, ATTR0_AFF);
}
}
}
// COMMON BUTTONS
+110 -222
View File
@@ -17,6 +17,7 @@
#include "joker.h"
#include "layout.h"
#include "list.h"
#include "mgba_logger.h"
#include "random.h"
#include "save.h"
#include "soundbank.h"
@@ -87,8 +88,7 @@ static const Rect SHOP_PRICES_TEXT_RECT = { 72, 56, 192, 160 };
static const Rect SHOP_REROLL_RECT = { 88, 96, UNDEFINED, UNDEFINED };
// clang-format on
static List s_shop_jokers_list = LIST_DEFAULT;
BITSET_DEFINE(s_avail_jokers_bitset, MAX_DEFINABLE_JOKERS)
static List s_shop_items_list = LIST_DEFAULT;
enum GameShopStates
{
@@ -170,6 +170,8 @@ static int timer;
static int reroll_cost = REROLL_BASE_COST;
// Variables relative to the Card we are showing the description of
// TODO: Change this to item once it has description printing API.
static JokerObject* description_card = NULL;
static FIXED description_card_original_x_pos = UNDEFINED;
static FIXED description_card_original_y_pos = UNDEFINED;
@@ -180,30 +182,11 @@ JokerObject* game_shop_get_description_card(void)
return description_card;
}
static inline void reset_shop_jokers(void)
{
int num_jokers = get_joker_registry_size();
bitset_clear(&s_avail_jokers_bitset);
for (int i = 0; i < num_jokers; i++)
{
bitset_set_idx(&s_avail_jokers_bitset, i, true);
}
}
void game_shop_reset(void)
{
list_clear(&s_shop_jokers_list);
s_shop_jokers_list = list_init();
reset_shop_jokers();
}
/**
* @brief Set whether a Joker can appear in the shop.
*/
void game_shop_set_joker_avail(int joker_id, bool avail)
{
bitset_set_idx(&s_avail_jokers_bitset, joker_id, avail);
list_clear(&s_shop_items_list);
s_shop_items_list = list_init();
joker_reset_rollable_jokers();
}
void game_shop_change_background(void)
@@ -246,123 +229,51 @@ void game_shop_on_init(void)
}
/**
* @brief Computes the number of Jokers we can currently roll in the Shop.
* The Jokers we own is taken into account and can't be rolled again.
* @brief Create a shop top row item - jokers, consumables, and possibly playing cards
* Currently only jokers are implemented.
*/
static inline int get_num_shop_jokers_avail(void)
static Item* game_shop_create_top_row_item(void)
{
return bitset_num_set_bits(&s_avail_jokers_bitset);
// TODO: Randomize item type when consumables are implemented
return item_roll_new(ITEM_TYPE_JOKER);
}
/**
* @brief Rolls a random Joker among the available ones
* @brief Setup for the lists of items we can purchase in the top row of the Shop.
* i.e. Jokers and consumables and possibly playing cards.
*/
static inline int game_shop_get_rand_available_joker_id(void)
{
// Roll for what rarity the joker will be
int joker_rarity = joker_get_random_rarity();
// Now determine how many jokers are available based on the rarity
int jokers_avail_size = get_num_shop_jokers_avail();
if (jokers_avail_size == 0)
return UNDEFINED;
int matching_joker_ids[jokers_avail_size];
int fallback_random_idx = rng_get_u32() % jokers_avail_size;
int fallback_random_joker_id = UNDEFINED;
int match_count = 0;
BitsetItr itr = bitset_itr_create(&s_avail_jokers_bitset);
int i = 0;
int joker_id = UNDEFINED;
while ((joker_id = bitset_itr_next(&itr)) != UNDEFINED)
{
if (i++ == fallback_random_idx)
fallback_random_joker_id = joker_id;
const JokerInfo* info = get_joker_registry_entry(joker_id);
if (info->rarity == joker_rarity)
{
matching_joker_ids[match_count++] = joker_id;
}
}
int selected_joker_id = (match_count > 0) ? matching_joker_ids[rng_get_u32() % match_count]
: fallback_random_joker_id;
return selected_joker_id;
}
/**
* @brief Returns true if we can't roll any Joker
*/
static inline bool no_avail_jokers(void)
{
return bitset_is_empty(&s_avail_jokers_bitset);
}
GBAL_UNUSED
static inline bool is_shop_joker_avail(int joker_id)
{
return bitset_get_idx(&s_avail_jokers_bitset, joker_id);
}
/**
* @brief Setup for the lists of items we can purchase in the Shop.
* Only Jokers are available for now, but this is where consumables and
* booster packs will be rolled when they are implemented.
*/
static void game_shop_create_items(void)
static void game_shop_create_top_row_items(void)
{
tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT);
if (no_avail_jokers())
return;
List* shop_items_list = &s_shop_items_list;
List* shop_jokers_list = &s_shop_jokers_list;
list_clear(shop_items_list);
*shop_items_list = list_init();
list_clear(shop_jokers_list);
*shop_jokers_list = list_init();
for (int i = 0; i < MAX_SHOP_JOKERS; i++)
for (int i = 0; i < MAX_SHOP_ITEMS; i++)
{
int joker_id = 0;
#ifdef TEST_JOKER_ID0 // Allow defining an ID for a joker to always appear in shop and be tested
if (is_shop_joker_avail(TEST_JOKER_ID0))
Item* item = game_shop_create_top_row_item();
if (item == NULL)
{
joker_id = TEST_JOKER_ID0;
}
else
#endif
#ifdef TEST_JOKER_ID1
if (is_shop_joker_avail(TEST_JOKER_ID1))
{
joker_id = TEST_JOKER_ID1;
}
else
#endif
{
joker_id = game_shop_get_rand_available_joker_id();
MGBA_FUNC_WARN("Could not create shop item");
// TODO: Decide how to handle this case gracefully
// If the issue for example is that we can't generate any more jokers,
// because for example the user owns all of them,
// maybe we need to generate consumables instead.
return;
}
// If for some reason only no joker is left, don't make another
if (joker_id == UNDEFINED)
break;
item->x = int2fx(SHOP_JOKER_SPRITES_INIT_POS.x + i * CARD_SPRITE_SIZE);
item->y = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y);
item->tx = item->x;
item->ty = int2fx(ITEM_SHOP_Y);
game_shop_set_joker_avail(joker_id, false);
item_print_buy_price_under(item);
JokerObject* joker_object = joker_object_new(joker_new(joker_id));
joker_object->sprite_object->x =
int2fx(SHOP_JOKER_SPRITES_INIT_POS.x + i * CARD_SPRITE_SIZE);
joker_object->sprite_object->y = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y);
joker_object->sprite_object->tx = joker_object->sprite_object->x;
joker_object->sprite_object->ty = int2fx(ITEM_SHOP_Y);
sprite_object_print_price_under(joker_object->sprite_object, joker_object->joker->value);
list_push_back(shop_jokers_list, joker_object);
list_push_back(shop_items_list, item);
}
}
@@ -375,7 +286,7 @@ static void game_shop_intro()
if (timer == TM_CREATE_SHOP_ITEMS_WAIT)
{
game_shop_create_items();
game_shop_create_top_row_items();
}
if (timer >= TM_SHIFT_SHOP_ICON_WAIT) // Shift the shop icon
@@ -419,32 +330,23 @@ static void game_shop_intro()
static int shop_top_row_get_size(void)
{
// + 1 to account for next round button
return list_get_len(&s_shop_jokers_list) + 1;
}
/**
* @brief Add a newly purchased Joker to the list of owned Jokers.
*/
static inline void add_to_held_jokers(JokerObject* joker_object)
{
joker_object->sprite_object->ty = int2fx(HELD_JOKERS_POS.y);
add_joker(joker_object);
return list_get_len(&s_shop_items_list) + 1;
}
/**
* @brief Called when pressing A on a Shop Joker to buy it.
*/
static inline void game_shop_buy_joker(int shop_joker_idx)
static inline void game_shop_buy_item(int shop_item_idx)
{
List* shop_jokers_list = &s_shop_jokers_list;
JokerObject* joker_object = (JokerObject*)list_get_at_idx(shop_jokers_list, shop_joker_idx);
List* shop_items_list = &s_shop_items_list;
Item* item = (Item*)list_get_at_idx(shop_items_list, shop_item_idx);
g_game_vars.money -= joker_object->joker->value;
g_game_vars.money -= item_get_buy_price(item);
display_money();
sprite_object_erase_text_under(joker_object->sprite_object);
sprite_object_set_focus(joker_object->sprite_object, false);
add_to_held_jokers(joker_object);
list_remove_at_idx(shop_jokers_list, shop_joker_idx); // Remove the joker from the shop
sprite_object_erase_text_under((SpriteObject*)item);
sprite_object_set_focus((SpriteObject*)item, false);
item_acquire(item);
list_remove_at_idx(shop_items_list, shop_item_idx); // Remove the joker from the shop
}
/**
@@ -461,16 +363,14 @@ static void shop_top_row_on_key_transit(SelectionGrid* selection_grid, Selection
}
else
{
int shop_joker_idx = selection->x - 1; // - 1 to account for next round button
JokerObject* joker_object =
(JokerObject*)list_get_at_idx(&s_shop_jokers_list, shop_joker_idx);
if (joker_object == NULL || list_get_len(get_jokers_list()) >= MAX_JOKERS_HELD_SIZE ||
g_game_vars.money < joker_object->joker->value)
int shop_item_idx = selection->x - 1; // - 1 to account for next round button
Item* item = (Item*)list_get_at_idx(&s_shop_items_list, shop_item_idx);
if (!item_can_acquire(item) || g_game_vars.money < item_get_buy_price(item))
{
return;
}
game_shop_buy_joker(shop_joker_idx);
game_shop_buy_item(shop_item_idx);
selection_grid_move_selection_horz(selection_grid, -1);
}
}
@@ -485,7 +385,7 @@ static bool shop_top_row_on_selection_changed(
const Selection* new_selection
)
{
List* shop_jokers_list = &s_shop_jokers_list;
List* shop_items_list = &s_shop_items_list;
// Guard if we move down while on jokers
if (new_selection->y > row_idx && prev_selection->x > 0)
return false;
@@ -503,8 +403,8 @@ static bool shop_top_row_on_selection_changed(
else
{
int idx = prev_selection->x - 1; // -1 to account for next round button
JokerObject* joker_object = (JokerObject*)list_get_at_idx(shop_jokers_list, idx);
sprite_object_set_focus(joker_object->sprite_object, false);
SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(shop_items_list, idx);
sprite_object_set_focus(sprite_object, false);
}
}
@@ -517,8 +417,8 @@ static bool shop_top_row_on_selection_changed(
else
{
int idx = new_selection->x - 1; // -1 to account for next round button
JokerObject* joker_object = (JokerObject*)list_get_at_idx(shop_jokers_list, idx);
sprite_object_set_focus(joker_object->sprite_object, true);
SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(shop_items_list, idx);
sprite_object_set_focus(sprite_object, true);
}
}
@@ -552,8 +452,8 @@ static bool shop_reroll_row_on_selection_changed(
if (new_selection->x != NEXT_ROUND_BTN_SEL_X)
{
int idx = new_selection->x - 1;
JokerObject* joker_object = (JokerObject*)list_get_at_idx(&s_shop_jokers_list, idx);
sprite_object_set_focus(joker_object->sprite_object, true);
SpriteObject* sprite_object = (SpriteObject*)list_get_at_idx(&s_shop_items_list, idx);
sprite_object_set_focus(sprite_object, true);
}
}
else if (row_idx == new_selection->y)
@@ -573,35 +473,33 @@ static inline void game_shop_reroll(void)
g_game_vars.money -= reroll_cost;
display_money(); // Update the money display
List* shop_jokers_list = &s_shop_jokers_list;
ListItr itr = list_itr_create(shop_jokers_list);
JokerObject* joker_object;
List* shop_items_list = &s_shop_items_list;
ListItr itr = list_itr_create(shop_items_list);
Item* item;
while ((joker_object = list_itr_next(&itr)))
while ((item = list_itr_next(&itr)))
{
if (joker_object != NULL)
if (item != NULL && item->type == ITEM_TYPE_JOKER)
{
game_shop_set_joker_avail(joker_object->joker->id, true);
joker_object_destroy(&joker_object); // Destroy the joker object if it exists
item_dispose(&item);
}
}
list_clear(shop_jokers_list);
*shop_jokers_list = list_init();
list_clear(shop_items_list);
*shop_items_list = list_init();
game_shop_create_items();
game_shop_create_top_row_items();
itr = list_itr_create(shop_jokers_list);
itr = list_itr_create(shop_items_list);
while ((joker_object = list_itr_next(&itr)))
SpriteObject* item_sprite_object;
while ((item_sprite_object = list_itr_next(&itr)))
{
if (joker_object != NULL)
if (item_sprite_object != NULL)
{
// Set the y position to the target position
joker_object->sprite_object->y = joker_object->sprite_object->ty;
item_sprite_object->y = item_sprite_object->ty;
// Give the joker a little wiggle animation
joker_object_shake(joker_object, UNDEFINED);
sprite_object_shake(item_sprite_object, UNDEFINED);
}
}
@@ -672,9 +570,9 @@ static void game_shop_process_user_input(void)
// Jokers for sale
case 1:
{
description_card_original_list = &s_shop_jokers_list;
description_card_original_list = &s_shop_items_list;
tmp_card = (shop_selection_grid.selection.x > 0)
? list_get_at_idx(&s_shop_jokers_list, shop_selection_grid.selection.x - 1)
? list_get_at_idx(&s_shop_items_list, shop_selection_grid.selection.x - 1)
: NULL;
break;
}
@@ -692,12 +590,11 @@ static void game_shop_process_user_input(void)
// Show description of selected card when pressing B.
// Always wait for the card in question to be immobile to avoid accumulating
// errors when pressing and releasing B in quick succession.
if (tmp_card != NULL && tmp_card->sprite_object->vx == 0 && tmp_card->sprite_object->vy == 0 &&
key_held(DESELECT_CARDS))
if (tmp_card != NULL && tmp_card->vx == 0 && tmp_card->vy == 0 && key_held(DESELECT_CARDS))
{
description_card = tmp_card;
description_card_original_x_pos = description_card->sprite_object->x;
description_card_original_y_pos = description_card->sprite_object->y;
description_card_original_x_pos = description_card->x;
description_card_original_y_pos = description_card->y;
timer = TM_ZERO;
state_machine_change_state(&shop_sm, GAME_SHOP_SHOW_CARD_DESC);
@@ -723,21 +620,21 @@ static void game_shop_show_card_desc(void)
while ((joker_object = list_itr_next(&itr)))
{
if (joker_object != description_card)
joker_object->sprite_object->ty -= int2fx(OWNED_CARDS_HIDE_Y_OFFSET);
joker_object->ty -= int2fx(OWNED_CARDS_HIDE_Y_OFFSET);
}
// Shop Jokers
itr = list_itr_create(&s_shop_jokers_list);
itr = list_itr_create(&s_shop_items_list);
while ((joker_object = list_itr_next(&itr)))
{
if (joker_object != description_card)
joker_object->sprite_object->ty = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y + TILE_SIZE);
joker_object->ty = int2fx(SHOP_JOKER_SPRITES_INIT_POS.y + TILE_SIZE);
}
// Set description_card new target position
description_card->sprite_object->tx = int2fx(CARD_DESCRIPTION_SPRITE_POS.x);
description_card->sprite_object->ty = int2fx(CARD_DESCRIPTION_SPRITE_POS.y);
description_card->tx = int2fx(CARD_DESCRIPTION_SPRITE_POS.x);
description_card->ty = int2fx(CARD_DESCRIPTION_SPRITE_POS.y);
}
// First 12 anim frames
@@ -795,8 +692,7 @@ static void game_shop_show_card_desc(void)
// Actively wait for the B button to be released, but only if the described card has stopped
// moving
else if (description_card->sprite_object->vx == 0 && description_card->sprite_object->vy == 0 &&
!key_held(DESELECT_CARDS))
else if (description_card->vx == 0 && description_card->vy == 0 && !key_held(DESELECT_CARDS))
{
timer = TM_ZERO;
state_machine_change_state(&shop_sm, GAME_SHOP_HIDE_CARD_DESC);
@@ -833,19 +729,19 @@ static void game_shop_hide_card_desc(void)
while ((joker_object = list_itr_next(&itr)))
{
if (joker_object != description_card)
joker_object->sprite_object->ty = int2fx(HELD_JOKERS_POS.y);
joker_object->ty = int2fx(HELD_JOKERS_POS.y);
}
// Shop Jokers
itr = list_itr_create(&s_shop_jokers_list);
itr = list_itr_create(&s_shop_items_list);
while ((joker_object = list_itr_next(&itr)))
{
if (joker_object != description_card)
joker_object->sprite_object->ty = int2fx(ITEM_SHOP_Y);
joker_object->ty = int2fx(ITEM_SHOP_Y);
}
description_card->sprite_object->tx = description_card_original_x_pos;
description_card->sprite_object->ty = description_card_original_y_pos;
description_card->tx = description_card_original_x_pos;
description_card->ty = description_card_original_y_pos;
}
// First 12 anim frames
@@ -862,22 +758,19 @@ static void game_shop_hide_card_desc(void)
else if (timer == TM_SHOW_CARD_DESC_WAIT + 1)
{
// Need to account for the description_card being selected if it came from the shop.
if (description_card_original_list == &s_shop_jokers_list)
description_card->sprite_object->ty += int2fx(TILE_SIZE);
if (description_card_original_list == &s_shop_items_list)
description_card->ty += int2fx(TILE_SIZE);
// Print price under shop Jokers
JokerObject* joker_object = NULL;
ListItr itr = list_itr_create(&s_shop_jokers_list);
while ((joker_object = list_itr_next(&itr)))
Item* item = NULL;
ListItr itr = list_itr_create(&s_shop_items_list);
while ((item = list_itr_next(&itr)))
{
sprite_object_print_price_under(
joker_object->sprite_object,
joker_object->joker->value
);
item_print_buy_price_under(item);
}
if (description_card_original_list == &s_shop_jokers_list)
description_card->sprite_object->ty -= int2fx(TILE_SIZE);
if (description_card_original_list == &s_shop_items_list)
description_card->ty -= int2fx(TILE_SIZE);
// Print Reroll prince
tte_printf(
@@ -893,7 +786,7 @@ static void game_shop_hide_card_desc(void)
}
// Cleanup and change state
else if (description_card->sprite_object->vx == 0 && description_card->sprite_object->vy == 0)
else if (description_card->vx == 0 && description_card->vy == 0)
{
owned_joker_price_printed = false;
description_card = NULL;
@@ -908,7 +801,7 @@ static void game_shop_hide_card_desc(void)
{
owned_joker_price_printed = true;
sprite_object_print_price_under(
description_card->sprite_object,
(SpriteObject*)description_card,
joker_get_sell_value(description_card->joker)
);
}
@@ -925,19 +818,17 @@ static void game_shop_outro(void)
main_bg_se_copy_rect_1_tile_vert(TOP_LEFT_PANEL_ANIM_RECT, SCREEN_UP);
// TODO: make heads or tails of what's going on here and replace
// magic numbers.
if (timer == 1)
{
tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT); // Erase the shop prices text
ListItr itr = list_itr_create(&s_shop_jokers_list);
JokerObject* joker_object;
while ((joker_object = list_itr_next(&itr)))
ListItr itr = list_itr_create(&s_shop_items_list);
SpriteObject* shop_item;
while ((shop_item = list_itr_next(&itr)))
{
if (joker_object != NULL)
if (shop_item != NULL)
{
joker_object->sprite_object->ty = int2fx(160);
shop_item->ty = int2fx(160);
}
}
@@ -945,6 +836,8 @@ static void game_shop_outro(void)
}
else if (timer == 2)
{
// TODO: make heads or tails of what's going on here and replace
// magic numbers.
int y = 5;
memset16(&se_mat[MAIN_BG_SBB][y - 1][0], 0x0001, 1);
memset16(&se_mat[MAIN_BG_SBB][y - 1][1], 0x0002, 7);
@@ -998,21 +891,16 @@ void game_shop_on_update(void)
void game_shop_on_exit(void)
{
List* shop_jokers_list = &s_shop_jokers_list;
ListItr itr = list_itr_create(shop_jokers_list);
JokerObject* joker_object;
List* shop_items_list = &s_shop_items_list;
ListItr itr = list_itr_create(shop_items_list);
Item* item;
while ((joker_object = list_itr_next(&itr)))
while ((item = list_itr_next(&itr)))
{
if (joker_object != NULL)
{
// Make the joker available back to shop
game_shop_set_joker_avail(joker_object->joker->id, true);
}
joker_object_destroy(&joker_object); // Destroy the joker objects
item_dispose(&item);
}
list_clear(shop_jokers_list);
list_clear(shop_items_list);
increment_blind(BLIND_STATE_DEFEATED); // TODO: Move to game_round_end()?