GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
item.c
1#include "item.h"
2
3#include "item_funcs.h"
4#include "mgba_logger.h"
5#include "util.h"
6
7#include <tonc.h>
8
9Item* item_roll_new(enum ItemType item_type, enum RngSequence key)
10{
11 if ((int)item_type < 0 || item_type >= ITEM_NUM_TYPES)
12 {
13 MGBA_FUNC_ERROR("Undefined type %d", item_type);
14 return NULL;
15 }
16
17 ItemFuncs* item_funcs = get_item_type_funcs(item_type);
18 GBAL_RETURN_IF_NULL_RET(item_funcs, NULL);
19 GBAL_RETURN_IF_NULL_RET(item_funcs->roll_new, NULL);
20
21 return item_funcs->roll_new(key);
22}
23
25{
26 GBAL_RETURN_IF_NULL_RET(item, UNDEFINED);
27
28 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
29 GBAL_RETURN_IF_NULL_RET(item_funcs, UNDEFINED);
30 GBAL_RETURN_IF_NULL_RET(item_funcs->get_buy_price, UNDEFINED);
31
32 return item_funcs->get_buy_price(item);
33}
34
35void item_acquire(Item* item)
36{
38
39 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
40 GBAL_RETURN_IF_NULL_VOID(item_funcs);
41 GBAL_RETURN_IF_NULL_VOID(item_funcs->acquire);
42
43 item_funcs->acquire(item);
44}
45
47{
48 GBAL_RETURN_IF_NULL_RET(item, false);
49 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
50 GBAL_RETURN_IF_NULL_RET(item_funcs, false);
51 GBAL_RETURN_IF_NULL_RET(item_funcs->can_acquire, false);
52
53 return item_funcs->can_acquire(item);
54}
55
56void item_dispose(Item** item)
57{
60 ItemFuncs* item_funcs = get_item_type_funcs((*item)->type);
61 GBAL_RETURN_IF_NULL_VOID(item_funcs);
62 GBAL_RETURN_IF_NULL_VOID(item_funcs->dispose);
63
64 item_funcs->dispose(item);
65}
66
The core structure for items in the shop and inventory. Provides a common API for the shop and invent...
void item_dispose(Item **item)
Destroys an item, freeing underlying resources, and manages rollable items sets if needed....
Definition item.c:56
void item_print_buy_price_under(Item *item)
Prints the buy price under the item Relies on the fact item is a SpriteObject.
Definition item.c:67
Item * item_roll_new(enum ItemType item_type, enum RngSequence key)
Rolls a random item of type item_type and returns a newly created one. Manages rollable items set if ...
Definition item.c:9
bool item_can_acquire(Item *item)
Returns true if the item can be acquired, i.e. added to inventory. Does not check if the player has e...
Definition item.c:46
void item_acquire(Item *item)
Acquires the item, adding to inventory if applicable. Called when it is purchased from the shop,...
Definition item.c:35
int item_get_buy_price(Item *item)
Returns the buy price of the item.
Definition item.c:24
An API for the item functions defined in item.h Separated module from item.c/.h so item_funcs....
ItemFuncs * get_item_type_funcs(enum ItemType type)
Returns the function table for a given item type.
Definition item_funcs.c:39
Interface to interact with the mgba logger.
RngSequence
Keys to different independent RNG sequences.
Definition random.h:19
void sprite_object_print_price_under(SpriteObject *sprite_object, int price)
Print the price string directly beneath a SpriteObject. More specialized version of sprite_object_pri...
Definition sprite.c:447
The set of functions that each item type implements.
Definition item.h:94
Item *(* roll_new)(enum RngSequence key)
Definition item.h:99
A generic interface for all items that can appear in the shop or be in the inventory....
Definition item.h:76
enum ItemType type
The item type - used to dispatch the function implementations for inheriting types.
Definition item.h:87
A sprite object is a sprite that is focusable and movable in animation.
Definition sprite.h:61
Utilities relating around number string representation and protected arithmatic helper functions.
#define GBAL_RETURN_IF_NULL_RET(param, ret_val)
Checks if param is equal to NULL and prints error message and returns in case it is....
Definition util.h:95
#define GBAL_RETURN_IF_NULL_VOID(param)
Checks if param is NULL and prints error message and returns in case it is. Useful for checking argum...
Definition util.h:76