A start to "priced" items

This commit is contained in:
Linus Probert 2019-02-28 13:49:15 +01:00
parent 645a02c99e
commit ad227f1bed
3 changed files with 44 additions and 0 deletions

View File

@ -21,16 +21,19 @@
#include "util.h" #include "util.h"
#include "mixer.h" #include "mixer.h"
#include "gui.h" #include "gui.h"
#include "actiontextbuilder.h"
Item * Item *
item_create(void) item_create(void)
{ {
Item *item = ec_malloc(sizeof(Item)); Item *item = ec_malloc(sizeof(Item));
item->sprite = NULL; item->sprite = NULL;
item->subsprites = NULL;
item->collected = false; item->collected = false;
item->openable = false; item->openable = false;
item->opened = false; item->opened = false;
m_strcpy(item->label, 50, ""); m_strcpy(item->label, 50, "");
item->price = 0.0;
item->value = 0.0; item->value = 0.0;
item->items = NULL; item->items = NULL;
item->effect = NULL; item->effect = NULL;
@ -41,11 +44,20 @@ void
item_render(Item *item, Camera *cam) item_render(Item *item, Camera *cam)
{ {
sprite_render(item->sprite, cam); sprite_render(item->sprite, cam);
LinkedList *subsprites = item->subsprites;
while (subsprites)
sprite_render(linkedlist_pop(&subsprites), cam);
} }
void void
item_collected(Item *item, Player *player) item_collected(Item *item, Player *player)
{ {
if (item->price > player->gold){
gui_log("You don't have enough gold to buy %s", item->label);
return;
}
if (item->collected || item->opened) if (item->collected || item->opened)
return; return;
@ -60,6 +72,13 @@ item_collected(Item *item, Player *player)
gui_log("You find nothing inside"); gui_log("You find nothing inside");
} }
if (item->price) {
player->gold -= item->price;
char costLabel[10];
m_sprintf(costLabel, 10, "-%d", item->price);
actiontextbuilder_create_text(costLabel, C_YELLOW, &player->sprite->pos);
}
if (item->effect != NULL) if (item->effect != NULL)
item->effect(item, player); item->effect(item, player);
@ -76,6 +95,9 @@ item_destroy(Item *item)
if (item->sprite) if (item->sprite)
sprite_destroy(item->sprite); sprite_destroy(item->sprite);
while (item->subsprites != NULL)
sprite_destroy(linkedlist_pop(&item->subsprites));
while (item->items != NULL) while (item->items != NULL)
item_destroy(linkedlist_pop(&item->items)); item_destroy(linkedlist_pop(&item->items));

View File

@ -29,10 +29,12 @@
typedef struct Item_t { typedef struct Item_t {
Sprite *sprite; Sprite *sprite;
LinkedList *subsprites;
bool collected; bool collected;
bool openable; bool openable;
bool opened; bool opened;
char label[50]; char label[50];
double price;
double value; double value;
LinkedList *items; LinkedList *items;
void (*effect)(struct Item_t *, Player *); void (*effect)(struct Item_t *, Player *);

View File

@ -100,6 +100,26 @@ create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item
return item; return item;
} }
static Item *
create_priced_item(double price, const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item *, Player*))
{
Item *item = create_item(path0, path1, clip, cb);
item->price = price;
Sprite *priceSprite = sprite_create();
sprite_load_text_texture(priceSprite, "GUI/SDS_8x8.ttf", 0, 8, 1);
char priceLabel[10];
m_sprintf(priceLabel, 10, "%d", item->price);
texture_load_from_text(priceSprite->textures[0],
priceLabel,
C_YELLOW,
C_BLACK,
builder->renderer);
priceSprite->pos = item->sprite->pos;
linkedlist_append(&item->subsprites, priceSprite);
return item;
}
static void static void
pickup_gold(Item *item, Player *player) pickup_gold(Item *item, Player *player)
{ {