remove usage of extern for joker_registry. Add array size macro to util.h
This commit is contained in:
+2
-2
@@ -60,8 +60,8 @@ typedef struct {
|
|||||||
u8 base_value;
|
u8 base_value;
|
||||||
JokerEffectFunc effect;
|
JokerEffectFunc effect;
|
||||||
} JokerInfo;
|
} JokerInfo;
|
||||||
extern const JokerInfo joker_registry[];
|
const JokerInfo* get_joker_registry_entry(int joker_id);
|
||||||
extern const size_t joker_registry_size;
|
size_t get_joker_registry_size(void);
|
||||||
|
|
||||||
void joker_init();
|
void joker_init();
|
||||||
|
|
||||||
|
|||||||
@@ -35,4 +35,6 @@ static inline int get_digits_even(int n)
|
|||||||
|
|
||||||
#define UNDEFINED -1
|
#define UNDEFINED -1
|
||||||
|
|
||||||
|
#define NUM_ELEM_IN_ARR(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||||
|
|
||||||
#endif // UTIL_H
|
#endif // UTIL_H
|
||||||
+1
-1
@@ -1825,7 +1825,7 @@ static void game_shop_create_items(JokerObject *shop_jokers[], bool first_time)
|
|||||||
joker_object_destroy(&shop_jokers[i]); // Destroy the joker object if it exists
|
joker_object_destroy(&shop_jokers[i]); // Destroy the joker object if it exists
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 joker_id = random() % joker_registry_size;
|
u8 joker_id = random() % get_joker_registry_size();
|
||||||
|
|
||||||
shop_jokers[i] = joker_object_new(joker_new(joker_id));
|
shop_jokers[i] = joker_object_new(joker_new(joker_id));
|
||||||
shop_jokers[i]->sprite_object->x = int2fx(120 + i * 32);
|
shop_jokers[i]->sprite_object->x = int2fx(120 + i * 32);
|
||||||
|
|||||||
+6
-4
@@ -38,10 +38,10 @@ void joker_init()
|
|||||||
|
|
||||||
Joker *joker_new(u8 id)
|
Joker *joker_new(u8 id)
|
||||||
{
|
{
|
||||||
if (id >= joker_registry_size) return NULL;
|
if (id >= get_joker_registry_size()) return NULL;
|
||||||
|
|
||||||
Joker *joker = malloc(sizeof(Joker));
|
Joker *joker = malloc(sizeof(Joker));
|
||||||
const JokerInfo *jinfo = &joker_registry[id];
|
const JokerInfo *jinfo = get_joker_registry_entry(id);
|
||||||
|
|
||||||
joker->id = id;
|
joker->id = id;
|
||||||
joker->modifier = BASE_EDITION; // TODO: Make this random later
|
joker->modifier = BASE_EDITION; // TODO: Make this random later
|
||||||
@@ -61,8 +61,10 @@ void joker_destroy(Joker **joker)
|
|||||||
|
|
||||||
JokerEffect joker_get_score_effect(Joker *joker, Card *scored_card)
|
JokerEffect joker_get_score_effect(Joker *joker, Card *scored_card)
|
||||||
{
|
{
|
||||||
if (joker->id >= joker_registry_size) return (JokerEffect){0};
|
const JokerInfo *jinfo = get_joker_registry_entry(joker->id);
|
||||||
return joker_registry[joker->id].effect(joker, scored_card);
|
if (!jinfo) return (JokerEffect){0};
|
||||||
|
|
||||||
|
return jinfo->effect(joker, scored_card);
|
||||||
}
|
}
|
||||||
|
|
||||||
// JokerObject methods
|
// JokerObject methods
|
||||||
|
|||||||
+13
-1
@@ -1,4 +1,5 @@
|
|||||||
#include "joker.h"
|
#include "joker.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
|
||||||
static JokerEffect default_joker_effect(Joker *joker, Card *scored_card) {
|
static JokerEffect default_joker_effect(Joker *joker, Card *scored_card) {
|
||||||
@@ -38,4 +39,15 @@ const JokerInfo joker_registry[] = {
|
|||||||
{ COMMON_JOKER, 5, gluttonous_joker_effect },
|
{ COMMON_JOKER, 5, gluttonous_joker_effect },
|
||||||
};
|
};
|
||||||
|
|
||||||
const size_t joker_registry_size = sizeof(joker_registry) / sizeof(joker_registry[0]);
|
static const size_t joker_registry_size = NUM_ELEM_IN_ARR(joker_registry);
|
||||||
|
|
||||||
|
const JokerInfo* get_joker_registry_entry(int joker_id) {
|
||||||
|
if (joker_id < 0 || (size_t)joker_id >= joker_registry_size) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return &joker_registry[joker_id];
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t get_joker_registry_size(void) {
|
||||||
|
return joker_registry_size;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user