2018-01-24 21:20:24 +01:00
|
|
|
#include <stdlib.h>
|
2018-01-24 21:14:34 +01:00
|
|
|
#include "item.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
Item *
|
|
|
|
item_create(void)
|
|
|
|
{
|
|
|
|
Item *item = ec_malloc(sizeof(Item));
|
|
|
|
item->sprite = NULL;
|
|
|
|
item->collected = false;
|
2018-01-25 10:45:05 +01:00
|
|
|
m_strcpy(item->label, 50, "");
|
|
|
|
item->value = 0.0;
|
2018-01-30 13:44:21 +01:00
|
|
|
item->items = NULL;
|
2018-01-24 21:14:34 +01:00
|
|
|
item->effect = NULL;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:44:21 +01:00
|
|
|
void
|
|
|
|
item_render(Item *item, Camera *cam)
|
2018-01-24 21:14:34 +01:00
|
|
|
{
|
|
|
|
sprite_render(item->sprite, cam);
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:44:21 +01:00
|
|
|
void
|
|
|
|
item_collected(Item *item, Player *player)
|
|
|
|
{
|
|
|
|
LinkedList *items = item->items;
|
|
|
|
|
|
|
|
while (items != NULL) {
|
|
|
|
Item *subitem = items->data;
|
|
|
|
items = items->next;
|
|
|
|
item_collected(subitem, player);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->effect != NULL)
|
|
|
|
item->effect(item, player);
|
|
|
|
|
|
|
|
item->collected = true;
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
void
|
|
|
|
item_destroy(Item *item)
|
|
|
|
{
|
2018-01-30 13:44:21 +01:00
|
|
|
LinkedList *items;
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
if (item->sprite)
|
|
|
|
sprite_destroy(item->sprite);
|
2018-01-30 13:44:21 +01:00
|
|
|
|
|
|
|
items = item->items;
|
|
|
|
while (items != NULL) {
|
2018-01-30 14:00:52 +01:00
|
|
|
Item *subitem = items->data;
|
2018-01-30 13:44:21 +01:00
|
|
|
items->data = NULL;
|
|
|
|
items = items->next;
|
|
|
|
item_destroy(subitem);
|
|
|
|
}
|
|
|
|
linkedlist_destroy(&items);
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
free(item);
|
2018-01-25 10:45:05 +01:00
|
|
|
}
|