Items can now contain items.

This commit is contained in:
Linus_Probert 2018-01-30 13:44:21 +01:00
parent 76ddc6c529
commit b3d48bd397
6 changed files with 78 additions and 18 deletions

View File

@ -10,19 +10,51 @@ item_create(void)
item->collected = false; item->collected = false;
m_strcpy(item->label, 50, ""); m_strcpy(item->label, 50, "");
item->value = 0.0; item->value = 0.0;
item->items = NULL;
item->effect = NULL; item->effect = NULL;
return item; return item;
} }
void item_render(Item *item, Camera *cam) void
item_render(Item *item, Camera *cam)
{ {
sprite_render(item->sprite, cam); sprite_render(item->sprite, cam);
} }
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;
}
void void
item_destroy(Item *item) item_destroy(Item *item)
{ {
LinkedList *items;
Item *subitem;
if (item->sprite) if (item->sprite)
sprite_destroy(item->sprite); sprite_destroy(item->sprite);
items = item->items;
while (items != NULL) {
subitem = items->data;
items->data = NULL;
items = items->next;
item_destroy(subitem);
}
linkedlist_destroy(&items);
free(item); free(item);
} }

View File

@ -7,18 +7,23 @@
#include "position.h" #include "position.h"
#include "player.h" #include "player.h"
#include "camera.h" #include "camera.h"
#include "linkedlist.h"
typedef struct Item_t { typedef struct Item_t {
Sprite *sprite; Sprite *sprite;
bool collected; bool collected;
char label[50]; char label[50];
double value; double value;
LinkedList *items;
void (*effect)(struct Item_t *, Player *); void (*effect)(struct Item_t *, Player *);
} Item; } Item;
Item * Item *
item_create(void); item_create(void);
void
item_collected(Item*, Player*);
void void
item_render(Item*, Camera*); item_render(Item*, Camera*);

View File

@ -138,11 +138,15 @@ item_builder_build_item(ItemKey key)
item = create_treasure(); item = create_treasure();
break; break;
case FLESH: case FLESH:
item = create_item(path_flesh, (SDL_Rect) { 0, 0, 16, 16 }, &eat_flesh); item = create_item(path_flesh,
(SDL_Rect) { 0, 0, 16, 16 },
&eat_flesh);
item->value = 1; item->value = 1;
break; break;
case HEALTH: case HEALTH:
item = create_item(path_potion, (SDL_Rect) { 0, 0, 16, 16 }, &drink_health); item = create_item(path_potion,
(SDL_Rect) { 0, 0, 16, 16 },
&drink_health);
item->value = 1 + (rand() % 2); item->value = 1 + (rand() % 2);
break; break;
default: default:
@ -153,6 +157,14 @@ item_builder_build_item(ItemKey key)
return item; return item;
} }
Item *
item_builder_build_sack(void)
{
return create_item("assets/Items/Chest0.png",
(SDL_Rect) { 0, 32, 16, 16 },
NULL);
}
void void
item_builder_close(void) item_builder_close(void)
{ {

View File

@ -30,6 +30,9 @@ item_builder_init(SDL_Renderer *);
Item * Item *
item_builder_build_item(ItemKey key); item_builder_build_item(ItemKey key);
Item *
item_builder_build_sack(void);
void void
item_builder_close(void); item_builder_close(void);

View File

@ -244,27 +244,37 @@ monster_drop_loot(Monster *monster, Map *map)
{ {
static unsigned int item_drop_chance = 3; static unsigned int item_drop_chance = 3;
static unsigned int treasure_drop_chance = 2; static unsigned int treasure_drop_chance = 2;
bool dropped_something = false;
Item *item; Item *item;
Item *items[2];
unsigned int item_count = 0;
// TODO(Linus): if ((rand() % treasure_drop_chance) == 0 || true) {
// Perhaps store multidrops in an item containing items?
// A pouch or bag perhaps?
if ((rand() % treasure_drop_chance) == 0) {
item = item_builder_build_item(TREASURE); item = item_builder_build_item(TREASURE);
item->sprite->pos = monster->sprite->pos; item->sprite->pos = monster->sprite->pos;
dropped_something = true; items[item_count++] = item;
linkedlist_append(&map->items, item);
} }
if ((rand() % item_drop_chance) == 0) { if ((rand() % item_drop_chance) == 0 || true) {
item = item_builder_build_item(rand() % TREASURE); item = item_builder_build_item(rand() % TREASURE);
item->sprite->pos = monster->sprite->pos; item->sprite->pos = monster->sprite->pos;
dropped_something = true; items[item_count++] = item;
linkedlist_append(&map->items, item);
} }
if (dropped_something) if (item_count == 0)
return;
gui_log("%s dropped something", monster->label); gui_log("%s dropped something", monster->label);
if (item_count == 1) {
linkedlist_append(&map->items, items[0]);
} else {
Item *container = item_builder_build_sack();
container->sprite->pos = monster->sprite->pos;
unsigned int i;
for (i = 0; i < item_count; ++i) {
linkedlist_append(&container->items, items[i]);
}
linkedlist_append(&map->items, container);
}
} }
void void

View File

@ -62,9 +62,7 @@ has_collided(Player *player, RoomMatrix *matrix)
while (items != NULL) { while (items != NULL) {
Item *item = items->data; Item *item = items->data;
items = items->next; items = items->next;
if (item->effect) item_collected(item, player);
item->effect(item, player);
item->collected = true;
} }
} }