A start to "priced" items
This commit is contained in:
parent
645a02c99e
commit
ad227f1bed
22
src/item.c
22
src/item.c
|
@ -21,16 +21,19 @@
|
|||
#include "util.h"
|
||||
#include "mixer.h"
|
||||
#include "gui.h"
|
||||
#include "actiontextbuilder.h"
|
||||
|
||||
Item *
|
||||
item_create(void)
|
||||
{
|
||||
Item *item = ec_malloc(sizeof(Item));
|
||||
item->sprite = NULL;
|
||||
item->subsprites = NULL;
|
||||
item->collected = false;
|
||||
item->openable = false;
|
||||
item->opened = false;
|
||||
m_strcpy(item->label, 50, "");
|
||||
item->price = 0.0;
|
||||
item->value = 0.0;
|
||||
item->items = NULL;
|
||||
item->effect = NULL;
|
||||
|
@ -41,11 +44,20 @@ void
|
|||
item_render(Item *item, Camera *cam)
|
||||
{
|
||||
sprite_render(item->sprite, cam);
|
||||
|
||||
LinkedList *subsprites = item->subsprites;
|
||||
while (subsprites)
|
||||
sprite_render(linkedlist_pop(&subsprites), cam);
|
||||
}
|
||||
|
||||
void
|
||||
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)
|
||||
return;
|
||||
|
||||
|
@ -60,6 +72,13 @@ item_collected(Item *item, Player *player)
|
|||
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)
|
||||
item->effect(item, player);
|
||||
|
||||
|
@ -76,6 +95,9 @@ item_destroy(Item *item)
|
|||
if (item->sprite)
|
||||
sprite_destroy(item->sprite);
|
||||
|
||||
while (item->subsprites != NULL)
|
||||
sprite_destroy(linkedlist_pop(&item->subsprites));
|
||||
|
||||
while (item->items != NULL)
|
||||
item_destroy(linkedlist_pop(&item->items));
|
||||
|
||||
|
|
|
@ -29,10 +29,12 @@
|
|||
|
||||
typedef struct Item_t {
|
||||
Sprite *sprite;
|
||||
LinkedList *subsprites;
|
||||
bool collected;
|
||||
bool openable;
|
||||
bool opened;
|
||||
char label[50];
|
||||
double price;
|
||||
double value;
|
||||
LinkedList *items;
|
||||
void (*effect)(struct Item_t *, Player *);
|
||||
|
|
|
@ -100,6 +100,26 @@ create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(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
|
||||
pickup_gold(Item *item, Player *player)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue