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
parent 369c680cb7
commit d24b0b5619
23 changed files with 796 additions and 329 deletions
+1
View File
@@ -46,6 +46,7 @@ GIT_C_FLAGS := -DGIT_HASH=\"$(GIT_HASH)\" -DGIT_DIRTY=\"$(GIT_DIRTY)\"
CFLAGS := -g -O3 -Wall -Werror -std=gnu23 \
-mcpu=arm7tdmi -mtune=arm7tdmi \
-ffast-math -fomit-frame-pointer -funroll-loops \
-fms-extensions \
$(ARCH)
CFLAGS += $(GIT_C_FLAGS)
+1
View File
@@ -2,6 +2,7 @@
* @file button.h
*
* @brief A button structure containing the common button functionalities
* Including highlight and pressing functionalities.
*/
#ifndef BUTTON_H
+5 -2
View File
@@ -2,7 +2,7 @@
#define CARD_H
#include "deck_types.h"
#include "sprite.h"
#include "item.h"
#include <maxmod.h>
#include <tonc.h>
@@ -54,8 +54,8 @@ typedef struct Card
typedef struct CardObject
{
Item; // First member struct inheritance
Card* card;
SpriteObject* sprite_object;
bool selected;
} CardObject;
@@ -83,4 +83,7 @@ void card_object_set_selected(CardObject* card_object, bool selected);
bool card_object_is_selected(CardObject* card_object);
Sprite* card_object_get_sprite(CardObject* card_object);
int card_object_get_buy_price(Item* card_object);
void card_object_dispose(Item** card_object);
#endif // CARD_H
-1
View File
@@ -4,7 +4,6 @@
#include "sprite.h"
POOL_ENTRY(Sprite, MAX_SPRITES);
POOL_ENTRY(SpriteObject, MAX_SPRITE_OBJECTS);
POOL_ENTRY(Joker, MAX_ACTIVE_JOKERS);
POOL_ENTRY(JokerObject, MAX_ACTIVE_JOKERS);
POOL_ENTRY(Card, MAX_CARDS);
+3 -1
View File
@@ -4,12 +4,13 @@
#include "game/common_ui.h"
#include "game_variables.h"
#include "graphic_utils.h"
#include "item.h"
#include <tonc.h>
#define MAX_DECK_SIZE 52
#define MAX_JOKERS_HELD_SIZE 5 // This doesn't account for negatives right now.
#define MAX_SHOP_JOKERS 2 // TODO: Make this dynamic and allow for other items besides jokers
#define MAX_SHOP_ITEMS 2 // TODO: Make this dynamic
#define MAX_SELECTION_SIZE 5
#define FRAMES(x) (((x) + (g_game_vars.game_speed) - 1) / (g_game_vars.game_speed))
@@ -69,6 +70,7 @@ void game_change_state(enum GameState new_game_state);
enum GameState game_get_state(void);
bool is_joker_owned(int joker_id);
bool joker_object_can_acquire(Item* item);
bool card_is_face(Card* card);
void add_joker(JokerObject* joker_object);
void remove_owned_joker(int owned_joker_idx);
-8
View File
@@ -23,14 +23,6 @@ void game_shop_reset(void);
*/
JokerObject* game_shop_get_description_card(void);
/**
* @brief Set whether a Joker can appear in the shop.
*
* @param avail - true to make it available to the shop to appear in
* false to make it unavailable.
*/
void game_shop_set_joker_avail(int joker_id, bool avail);
/**
* @brief Change to the shop background
*/
+173
View File
@@ -0,0 +1,173 @@
/**
* @file item.h
*
* @brief The core structure for items in the shop and inventory.
* Provides a common API for the shop and inventory to handle all types of items.
* Uses struct inheritance so all inherited items can implement an is-a relationship with Item.
* This means that pointers to structs that inherit Item using first member struct inheritance
* can and should be cast to Item* so code that expects an Item* can use them.
*/
#ifndef ITEM_H
#define ITEM_H
#include "mgba_logger.h"
#include "sprite.h"
// TODO: Merge these with GBAL_RETURN_IF_NULL macros?
/**
* @brief Checks if @p item is of type @p expected_type - logs error message and returns otherwise
*
* This version is for a function that returns a value, while
* @ref ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID is for a void function.
*/
#define ITEM_RETURN_IF_UNEXPECTED_TYPE_RET(item, expected_type, ret_val) \
do \
{ \
if ((item)->type != expected_type) \
{ \
MGBA_FUNC_ERROR("Unexpected %s->type != %s", #item, #expected_type); \
return (ret_val); \
} \
} while (0)
/**
* @brief Checks if @p item is of type @p expected_type - logs error message and returns otherwise
*
* This version is for a void function, while @ref ITEM_RETURN_IF_UNEXPECTED_TYPE_RET is for one
* with a return value.
*/
#define ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(item, expected_type) \
do \
{ \
if ((item)->type != expected_type) \
{ \
MGBA_FUNC_ERROR("Unexpected %s->type != %s", #item, #expected_type); \
return; \
} \
} while (0)
enum ItemType
{
ITEM_TYPE_JOKER,
ITEM_TYPE_PLAYING_CARD,
// Future planned item types
// ITEM_TYPE_CONSUMABLE, // Expand to PLANET, TAROT, and SPECTRAL?
// ITEM_TYPE_VOUCHER,
// ITEM_TYPE_PACK
ITEM_NUM_TYPES
};
/**
* @brief A generic interface for all items that can appear in the shop or be in the inventory.
* This uses first member struct inheritance - other structs are meant to inherit it by
* making their first member field Item.
* Then casts from inheriting structs to Item* are allowed and intentional and this allows for
* generic code that uses polymorphism.
* The -fms-extensions compile flag allows for anonymous members making it behave fully
* as inheritance. It makes all member fields be fully inherited so any struct
* that inherits Item for example will have all its fields accessible directly,
* e.g. `JokerObject joker_object; joker_object.type = ITEM_TYPE_JOKER`
*/
typedef struct Item
{
/**
* @brief First member struct inheritance
* all items that can appear in the shop are SpriteObjects.
* Note that this is an anonymous member.
*/
SpriteObject;
/**
* @brief The item type - used to dispatch the function implementations for inheriting types.
*/
enum ItemType type;
} Item;
/**
* @brief The set of functions that each item type implements.
*/
typedef struct ItemFuncs
{
/**
* All items must implement the following since they are called by the shop and all items
* must be capable of appearing in the shop.
*/
Item* (*roll_new)(void);
int (*get_buy_price)(Item* item);
bool (*can_acquire)(Item* item);
void (*acquire)(Item* item);
void (*dispose)(Item** item);
// TODO: void (*print_description)(Item* item); // or something of the form
// Optional implementation functions will be added here
} ItemFuncs;
/**
* @brief Rolls a random item of type @p item_type and returns a newly created one.
* Manages rollable items set if necessary (i.e. not rolling items already in inventory)
* To be used when rolling new items for the shop or packs.
*
* Matches @ref ItemFuncs.roll_new()
*
* @param item_type The type of the item to roll
*
* @return The newly created randomly rolled item
*/
Item* item_roll_new(enum ItemType item_type);
/**
* @brief Returns the buy price of the item.
*
* Matches @ref ItemFuncs.get_buy_price()
*
* @param item The item whose price to return.
*
* @return UNDEFINED in case of error, the item's buy price otherwise.
*/
int item_get_buy_price(Item* item);
/**
* @brief Acquires the item, adding to inventory if applicable.
* Called when it is purchased from the shop, note that it does not
* perform the purchase operation of decrementing the player's money,
* that should be handled by the shop code.
* For packs this can be to just open the pack,
* for vouchers, this will apply their effect.
*
* Matches @ref ItemFuncs.acquire()
*
* @param item The item to acquire
*/
void item_acquire(Item* item);
/**
* @brief Returns true if the item can be acquired, i.e. added to inventory.
* Does not check if the player has enough money to buy the item, that is the shop's job,
* as this will be used both when purchasing and when selecting in a pack.
*
* Matches @ref ItemFuncs.can_acquire()
*
* @param item The item to check
*/
bool item_can_acquire(Item* item);
/**
* @brief Destroys an item, freeing underlying resources, and manages rollable items sets if needed.
* To be used when destroying items from the inventory, shop, or packs.
*
* @param item A pointer to an item for destruction.
*/
void item_dispose(Item** item);
/**
* @brief Prints the buy price under the item
* Relies on the fact item is a SpriteObject
*
* @param item The item to print under
*/
void item_print_buy_price_under(Item* item);
#endif // ITEM_H
+26
View File
@@ -0,0 +1,26 @@
/**
* @file item_funcs.h
*
* @brief An API for the item functions defined in item.h
* Separated module from item.c/.h so item_funcs.c can include joker.h and card.h etc.
* so the item.h doesn't depend on implementations such as jokers and cards.
*/
#ifndef ITEM_FUNCS_H
#define ITEM_FUNCS_H
#include "item.h"
/**
* @brief Returns the function table for a given item type.
*
* Looks up the @ref ItemFuncs dispatch table corresponding to @p type.
*
* @param type The item type whose function table to retrieve.
*
* @return Pointer to the @ref ItemFuncs table for @p type,
* or NULL if @p type is invalid or has no implemented functions.
*/
ItemFuncs* get_item_type_funcs(enum ItemType type);
#endif // ITEM_FUNCS_H
+47 -2
View File
@@ -9,7 +9,7 @@
#include "card.h"
#include "game.h"
#include "graphic_utils.h"
#include "sprite.h"
#include "item.h"
#include <maxmod.h>
@@ -112,8 +112,8 @@ typedef struct
typedef struct JokerObject
{
Item; // First member struct inheritance
Joker* joker;
SpriteObject* sprite_object;
} JokerObject;
typedef struct // These jokers are triggered after the played hand has finished scoring.
@@ -197,6 +197,51 @@ JokerObject* joker_object_new(Joker* joker);
void joker_object_destroy(JokerObject** joker_object);
// This doesn't actually score anything, it just performs an animation and plays a sound effect
void joker_object_shake(JokerObject* joker_object, mm_word sound_id);
/**
* @brief Returns the buy price of the joker object.
*
* @param joker_object the joker object whose price to return.
*
* @return UNDEFINED in case of error, the buy price of the joker otherwise.
*/
int joker_object_get_buy_price(Item* joker_object);
// TODO: Move to an owned_jokers.c/.h file?
/**
* @brief Add a Joker to the list of owned Jokers and place it in the joker row.
*
* @param joker_object The JokerObject to add cast to Item*
*/
void joker_object_add_to_owned(Item* joker_object);
/**
* @brief Destroy a JokerObject item, free its resources, and make it available to be rolled.
*
* @param joker_object Pointer to the JokerObject Item* to destroy; set to NULL.
*/
void joker_object_dispose(Item** joker_object);
/**
* @brief Set whether a Joker is available to be rolled for the shop, packs, etc.
*
* @param joker_id The ID of the joker whose availability to set.
* @param rollable true to make it rollable, false otherwise.
*/
void joker_set_rollable(int joker_id, bool rollable);
/**
* @brief Reset rollable jokers to include all jokers in the registry.
*/
void joker_reset_rollable_jokers(void);
/**
* @brief Roll and create a new JokerObject item.
*
* @return Newly created `Item*` (JokerObject) or NULL if none available.
*/
Item* joker_object_roll_new(void);
// This scores the joker and returns true if it was scored successfully
// card_object = NULL means the joker_event does not concern a particular Card, i.e. Independend or
// On_Blind_Selected as opposed to events that concern a particular card, i.e. On_Card_Scored or
+9 -8
View File
@@ -194,23 +194,24 @@ void sprite_init(void);
void sprite_draw(void);
/**
* @brief Allocate and retrieve a pointer to a valid SpriteObject
* @brief Initialize a SpriteObject to a default state.
* Must be called only once per SpriteObject when it is created.
*
* @return A valid pointer to an newly allocated SpriteObject
* if successful, othewise return **NULL**.
* @param sprite_object - The SpriteObject to initialize
*/
SpriteObject* sprite_object_new();
void sprite_object_init(SpriteObject* sprite_object);
/**
* @brief Destroy SpriteObject
*
* Destroy a SpriteObject by freeing it back to the pool and releasing its
* associated resources
* Destroy a SpriteObject by releasing its associated resources (e.g. the sprite).
* This invalidates the SpriteObject and it should not be used after destroyed,
* a new one should be created instead.
*
* @param sprite_object pointer to a pointer of SpriteObject to destroy.
* @param sprite_object pointer to a SpriteObject to destroy.
* Cannot be **NULL**.
*/
void sprite_object_destroy(SpriteObject** sprite_object);
void sprite_object_destroy(SpriteObject* sprite_object);
/**
* @brief Register a Sprite to an associated SpriteObject
+29 -7
View File
@@ -1,6 +1,8 @@
#include "card.h"
#include "graphic_utils.h"
#include "item.h"
#include "util.h"
#include <maxmod.h>
#include <stdlib.h>
@@ -101,9 +103,10 @@ u8 card_get_value(Card* card)
CardObject* card_object_new(Card* card)
{
CardObject* card_object = POOL_GET(CardObject);
sprite_object_init((SpriteObject*)card_object);
card_object->card = card;
card_object->sprite_object = sprite_object_new();
card_object->type = ITEM_TYPE_PLAYING_CARD;
card_object->selected = false;
return card_object;
@@ -113,7 +116,7 @@ void card_object_destroy(CardObject** card_object)
{
if (*card_object == NULL)
return;
sprite_object_destroy(&((*card_object)->sprite_object));
sprite_object_destroy((SpriteObject*)(*card_object));
POOL_FREE(CardObject, *card_object);
*card_object = NULL;
}
@@ -134,7 +137,7 @@ void card_object_set_sprite(CardObject* card_object, int layer)
CARD_PB,
layer + CARD_STARTING_LAYER
);
sprite_object_set_sprite(card_object->sprite_object, sprite);
sprite_object_set_sprite((SpriteObject*)card_object, sprite);
}
void card_object_set_sprite_face_down(CardObject* card_object, enum DeckType deck, int layer)
@@ -152,12 +155,12 @@ void card_object_set_sprite_face_down(CardObject* card_object, enum DeckType dec
DECK_SPRITES_PB,
layer + CARD_STARTING_LAYER
);
sprite_object_set_sprite(card_object->sprite_object, sprite);
sprite_object_set_sprite((SpriteObject*)card_object, sprite);
}
void card_object_shake(CardObject* card_object, mm_word sound_id)
{
sprite_object_shake(card_object->sprite_object, sound_id);
sprite_object_shake((SpriteObject*)card_object, sound_id);
}
void card_object_set_selected(CardObject* card_object, bool selected)
@@ -178,5 +181,24 @@ Sprite* card_object_get_sprite(CardObject* card_object)
{
if (card_object == NULL)
return NULL;
return sprite_object_get_sprite(card_object->sprite_object);
return sprite_object_get_sprite((SpriteObject*)card_object);
}
int card_object_get_buy_price(Item* card_object)
{
GBAL_RETURN_IF_NULL_RET(card_object, UNDEFINED);
ITEM_RETURN_IF_UNEXPECTED_TYPE_RET(card_object, ITEM_TYPE_PLAYING_CARD, UNDEFINED);
return 1;
}
void card_object_dispose(Item** card_object_item)
{
GBAL_RETURN_IF_NULL_VOID(card_object_item);
GBAL_RETURN_IF_NULL_VOID(*card_object_item);
ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(*card_object_item, ITEM_TYPE_PLAYING_CARD);
CardObject* card_object = (CardObject*)(*card_object_item);
card_object_destroy(&card_object);
*card_object_item = NULL;
}
+11 -4
View File
@@ -253,10 +253,10 @@ static inline void discarded_jokers_update_loop(void)
while ((joker_object = list_itr_next(&itr)))
{
if (joker_object->sprite_object->x == joker_object->sprite_object->tx &&
joker_object->sprite_object->y == joker_object->sprite_object->ty)
if (joker_object->x == joker_object->tx && joker_object->y == joker_object->ty)
{
list_itr_remove_current_node(&itr);
// TODO: joker_object_dispose() instead?
joker_object_destroy(&joker_object);
}
}
@@ -282,11 +282,17 @@ static inline void held_jokers_update_loop(void)
{
// Let the Shop handle the position of this Joker
if (joker != game_shop_get_description_card())
joker->sprite_object->tx = hand_x - int2fx(spacing_lut[jokers_top][i]);
joker->tx = hand_x - int2fx(spacing_lut[jokers_top][i]);
i++;
}
}
bool joker_object_can_acquire(Item* joker_object)
{
GBAL_RETURN_IF_NULL_RET(joker_object, false);
return (list_get_len(get_jokers_list()) < MAX_JOKERS_HELD_SIZE);
}
static inline void expired_jokers_update_loop(void)
{
if (list_is_empty(&_expired_jokers_list))
@@ -429,7 +435,8 @@ void remove_owned_joker(int owned_joker_idx)
shortcut_joker_count--;
}
game_shop_set_joker_avail(joker_object->joker->id, true);
// TODO: Move to site of joker_destroy()?
joker_set_rollable(joker_object->joker->id, true);
list_remove_at_idx(&_owned_jokers_list, owned_joker_idx);
}
+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()?
+3 -3
View File
@@ -258,7 +258,7 @@ void reorder_card_sprites_layers(void)
}
// card_object_get_sprite() will not work here since we need the address
sprite_destroy(&(hand.cards[i]->sprite_object->sprite));
sprite_destroy(&(hand.cards[i]->sprite));
}
// Recreate the sprites for the remaining non NULL cards, in order
@@ -270,8 +270,8 @@ void reorder_card_sprites_layers(void)
card_object_set_sprite(hand.cards[i], i);
sprite_position(
card_object_get_sprite(hand.cards[i]),
fx2int(hand.cards[i]->sprite_object->x),
fx2int(hand.cards[i]->sprite_object->y)
fx2int(hand.cards[i]->x),
fx2int(hand.cards[i]->y)
);
}
}
+71
View File
@@ -0,0 +1,71 @@
#include "item.h"
#include "item_funcs.h"
#include "mgba_logger.h"
#include "util.h"
#include <tonc.h>
Item* item_roll_new(enum ItemType item_type)
{
if ((int)item_type < 0 || item_type >= ITEM_NUM_TYPES)
{
MGBA_FUNC_ERROR("Undefined type %d", item_type);
return NULL;
}
ItemFuncs* item_funcs = get_item_type_funcs(item_type);
GBAL_RETURN_IF_NULL_RET(item_funcs, NULL);
GBAL_RETURN_IF_NULL_RET(item_funcs->roll_new, NULL);
return item_funcs->roll_new();
}
int item_get_buy_price(Item* item)
{
GBAL_RETURN_IF_NULL_RET(item, UNDEFINED);
ItemFuncs* item_funcs = get_item_type_funcs(item->type);
GBAL_RETURN_IF_NULL_RET(item_funcs, UNDEFINED);
GBAL_RETURN_IF_NULL_RET(item_funcs->get_buy_price, UNDEFINED);
return item_funcs->get_buy_price(item);
}
void item_acquire(Item* item)
{
GBAL_RETURN_IF_NULL_VOID(item);
ItemFuncs* item_funcs = get_item_type_funcs(item->type);
GBAL_RETURN_IF_NULL_VOID(item_funcs);
GBAL_RETURN_IF_NULL_VOID(item_funcs->acquire);
item_funcs->acquire(item);
}
bool item_can_acquire(Item* item)
{
GBAL_RETURN_IF_NULL_RET(item, false);
ItemFuncs* item_funcs = get_item_type_funcs(item->type);
GBAL_RETURN_IF_NULL_RET(item_funcs, false);
GBAL_RETURN_IF_NULL_RET(item_funcs->can_acquire, false);
return item_funcs->can_acquire(item);
}
void item_dispose(Item** item)
{
GBAL_RETURN_IF_NULL_VOID(item);
GBAL_RETURN_IF_NULL_VOID(*item);
ItemFuncs* item_funcs = get_item_type_funcs((*item)->type);
GBAL_RETURN_IF_NULL_VOID(item_funcs);
GBAL_RETURN_IF_NULL_VOID(item_funcs->dispose);
item_funcs->dispose(item);
}
void item_print_buy_price_under(Item* item)
{
GBAL_RETURN_IF_NULL_VOID(item);
sprite_object_print_price_under((SpriteObject*)item, item_get_buy_price(item));
}
+65
View File
@@ -0,0 +1,65 @@
#include "item_funcs.h"
#include "card.h"
#include "game.h"
#include "game/shop.h"
#include "joker.h"
#include "util.h"
static Item* item_roll_new_unimplemented();
static void item_acquire_unimplemented(Item* item);
static bool item_always_can_acquire(Item* item);
// clang-format off
ItemFuncs item_func_table[] = {
[ITEM_TYPE_JOKER] = {
.roll_new = joker_object_roll_new,
.get_buy_price = joker_object_get_buy_price,
.acquire = joker_object_add_to_owned,
.can_acquire = joker_object_can_acquire,
.dispose = joker_object_dispose
},
/* Currently playing cards have partial implementations since Magic Trick is not implemented
* and playing cards can't appear in the shop.
* If the Magic Trick voucher is implemented, these need to be completed.
* Playing cards must stay the only exception, the rest of the item types must implement
* all the basic functions to appear in the shop.
*/
[ITEM_TYPE_PLAYING_CARD] = {
.roll_new = item_roll_new_unimplemented,
.get_buy_price = card_object_get_buy_price,
.acquire = item_acquire_unimplemented,
.can_acquire = item_always_can_acquire,
.dispose = card_object_dispose
}
};
// clang-format on
ItemFuncs* get_item_type_funcs(enum ItemType type)
{
if ((int)type < 0 || type >= NUM_ELEM_IN_ARR(item_func_table))
{
MGBA_FUNC_ERROR("Invalid type %d", type);
return NULL;
}
return &item_func_table[type];
}
static Item* item_roll_new_unimplemented()
{
MGBA_FUNC_ERROR("Unimplemented roll_new function called");
return NULL;
}
static void item_acquire_unimplemented(Item* item)
{
GBAL_RETURN_IF_NULL_VOID(item);
MGBA_FUNC_ERROR("Unimplemented acquire function called for item type type %d", (item)->type);
}
static bool item_always_can_acquire(Item* item)
{
return true;
}
+170 -14
View File
@@ -4,6 +4,8 @@
#include "game/round.h"
#include "game_variables.h"
#include "graphic_utils.h"
#include "layout.h"
#include "mgba_logger.h"
#include "pool.h"
#include "random.h"
#include "soundbank.h"
@@ -11,6 +13,7 @@
// Tiles and palettes
#include "card_rarity_pal_gfx.h"
#include "item.h"
#include "joker_gfx.h"
#include <maxmod.h>
@@ -58,6 +61,8 @@ static bool used_layers[MAX_JOKER_OBJECTS] = {false}; // Track used layers for j
static int joker_spritesheet_pb_map[MAX_NUM_JOKERS_SPRITESHEETS];
static int joker_pb_num_sprite_users[JOKER_LAST_PB - JOKER_BASE_PB + 1] = {0};
BITSET_DEFINE(s_rollable_jokers_bitset, MAX_DEFINABLE_JOKERS)
// See linked issue for context of maps
// https://github.com/GBALATRO/balatro-gba/issues/274#issue-3685075538
@@ -179,12 +184,16 @@ u16 joker_get_rarity_color(u8 rarity, bool main_color)
return card_rarity_pal_gfxPal[1 + 2 * rarity + (main_color ? 0 : 1)];
}
int joker_get_buy_price(const Joker* joker)
{
GBAL_RETURN_IF_NULL_RET(joker, UNDEFINED);
return joker->value;
}
int joker_get_sell_value(const Joker* joker)
{
if (joker == NULL)
{
return UNDEFINED;
}
GBAL_RETURN_IF_NULL_RET(joker, UNDEFINED);
return joker->value / 2;
}
@@ -194,6 +203,8 @@ JokerObject* joker_object_new(Joker* joker)
{
JokerObject* joker_object = POOL_GET(JokerObject);
sprite_object_init((SpriteObject*)joker_object);
int layer = 0;
for (int i = 0; i < MAX_JOKER_OBJECTS; i++)
{
@@ -206,7 +217,8 @@ JokerObject* joker_object_new(Joker* joker)
}
joker_object->joker = joker;
joker_object->sprite_object = sprite_object_new();
joker_object->type = ITEM_TYPE_JOKER;
int tile_index = JOKER_TID + layer * JOKER_SPRITE_OFFSET;
@@ -222,7 +234,7 @@ JokerObject* joker_object_new(Joker* joker)
);
sprite_object_set_sprite(
joker_object->sprite_object,
(SpriteObject*)joker_object,
sprite_new(
ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF,
ATTR1_SIZE_32,
@@ -250,18 +262,162 @@ void joker_object_destroy(JokerObject** joker_object)
UNDEFINED;
}
sprite_object_destroy(&(*joker_object)->sprite_object); // Destroy the sprite
joker_destroy(&(*joker_object)->joker); // Destroy the joker
sprite_object_destroy((SpriteObject*)(*joker_object));
joker_destroy(&(*joker_object)->joker);
POOL_FREE(JokerObject, *joker_object);
*joker_object = NULL;
}
void joker_object_shake(JokerObject* joker_object, mm_word sound_id)
void joker_object_dispose(Item** joker_object_item)
{
sprite_object_shake(joker_object->sprite_object, sound_id);
GBAL_RETURN_IF_NULL_VOID(joker_object_item);
GBAL_RETURN_IF_NULL_VOID(*joker_object_item);
ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(*joker_object_item, ITEM_TYPE_JOKER);
JokerObject* joker_object = (JokerObject*)(*joker_object_item);
GBAL_RETURN_IF_NULL_VOID(joker_object->joker);
joker_set_rollable(joker_object->joker->id, true);
joker_object_destroy(&joker_object);
*joker_object_item = NULL;
}
void set_and_shift_text(char* str, int* cursor_pos_x, int* cursor_pos_y, int color_pb)
void joker_object_shake(JokerObject* joker_object, mm_word sound_id)
{
sprite_object_shake((SpriteObject*)joker_object, sound_id);
}
int joker_object_get_buy_price(Item* joker_object)
{
GBAL_RETURN_IF_NULL_RET(joker_object, UNDEFINED);
ITEM_RETURN_IF_UNEXPECTED_TYPE_RET(joker_object, ITEM_TYPE_JOKER, UNDEFINED);
return ((JokerObject*)joker_object)->joker->value;
}
void joker_object_add_to_owned(Item* joker_object)
{
GBAL_RETURN_IF_NULL_VOID(joker_object);
ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(joker_object, ITEM_TYPE_JOKER);
joker_object->ty = int2fx(HELD_JOKERS_POS.y);
add_joker((JokerObject*)joker_object);
}
void joker_set_rollable(int joker_id, bool rollable)
{
bitset_set_idx(&s_rollable_jokers_bitset, joker_id, rollable);
}
/**
* @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.
*/
static inline int get_num_rollable_jokers(void)
{
return bitset_num_set_bits(&s_rollable_jokers_bitset);
}
/**
* @brief Returns true if we can't roll any Joker
*/
static inline bool no_rollable_jokers(void)
{
return bitset_is_empty(&s_rollable_jokers_bitset);
}
GBAL_UNUSED
static inline bool joker_is_rollable(int joker_id)
{
return bitset_get_idx(&s_rollable_jokers_bitset, joker_id);
}
void joker_reset_rollable_jokers(void)
{
int num_jokers = get_joker_registry_size();
bitset_clear(&s_rollable_jokers_bitset);
for (int i = 0; i < num_jokers; i++)
{
bitset_set_idx(&s_rollable_jokers_bitset, i, true);
}
}
/**
* @brief Rolls a random Joker among the available ones
*/
static int joker_roll_id(void)
{
// Now determine how many jokers are available based on the rarity
int jokers_avail_size = get_num_rollable_jokers();
if (jokers_avail_size == 0)
return UNDEFINED;
// Roll for what rarity the joker will be
int joker_rarity = joker_get_random_rarity();
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_rollable_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;
}
Item* joker_object_roll_new(void)
{
if (no_rollable_jokers())
return NULL;
int joker_id = 0;
#ifdef TEST_JOKER_ID0 // Allow defining an ID for a joker to always appear in shop and be tested
if (joker_is_rollable(TEST_JOKER_ID0))
{
joker_id = TEST_JOKER_ID0;
}
else
#endif
#ifdef TEST_JOKER_ID1
if (joker_is_rollable(TEST_JOKER_ID1))
{
joker_id = TEST_JOKER_ID1;
}
else
#endif
{
joker_id = joker_roll_id();
}
// If for some reason only no joker is left, don't make another
if (joker_id == UNDEFINED)
return NULL;
joker_set_rollable(joker_id, false);
return (Item*)joker_object_new(joker_new(joker_id));
}
static void set_and_shift_text(char* str, int* cursor_pos_x, int* cursor_pos_y, int color_pb)
{
tte_set_pos(*cursor_pos_x, *cursor_pos_y);
tte_set_special(color_pb * TTE_SPECIAL_PB_MULT_OFFSET);
@@ -305,12 +461,12 @@ bool joker_object_score(
{
// display the text on top of the card instead of below the Joker for Held Cards effects
// scored_card cannot be NULL here because of the joker event
cursorPosX += fx2int(card_object->sprite_object->x);
cursorPosX += fx2int(card_object->x);
cursorPosY = HELD_CARD_SCORE_TEXT_Y;
}
else
{
cursorPosX += fx2int(joker_object->sprite_object->x);
cursorPosX += fx2int(joker_object->x);
cursorPosY = JOKER_SCORE_TEXT_Y;
}
@@ -375,7 +531,7 @@ Sprite* joker_object_get_sprite(JokerObject* joker_object)
{
if (joker_object == NULL)
return NULL;
return sprite_object_get_sprite(joker_object->sprite_object);
return sprite_object_get_sprite((SpriteObject*)joker_object);
}
int joker_get_random_rarity()
+9 -10
View File
@@ -4,6 +4,8 @@
#include "game.h"
#include "game_variables.h"
#include "graphic_utils.h"
#include "item.h"
#include "mgba_logger.h"
#include "pool.h"
#include "random.h"
#include "soundbank.h"
@@ -169,28 +171,25 @@ int sprite_get_pb(const Sprite* sprite)
}
// SpriteObject methods
SpriteObject* sprite_object_new()
void sprite_object_init(SpriteObject* sprite_object)
{
SpriteObject* sprite_object = POOL_GET(SpriteObject);
GBAL_RETURN_IF_NULL_VOID(sprite_object);
sprite_object->sprite = NULL;
sprite_object_reset_transform(sprite_object);
sprite_object->focused = false;
list_push_back(&sprite_objects_list, sprite_object);
return sprite_object;
}
void sprite_object_destroy(SpriteObject** sprite_object)
void sprite_object_destroy(SpriteObject* sprite_object)
{
if (*sprite_object == NULL)
if (sprite_object == NULL)
return;
list_remove_data(&sprite_objects_list, *sprite_object);
list_remove_data(&sprite_objects_list, sprite_object);
sprite_destroy(&(*sprite_object)->sprite);
POOL_FREE(SpriteObject, *sprite_object);
*sprite_object = NULL;
sprite_destroy(&sprite_object->sprite);
}
void sprite_object_set_sprite(SpriteObject* sprite_object, Sprite* sprite)