2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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"
|
2018-08-10 20:09:56 +02:00
|
|
|
#include "mixer.h"
|
2018-08-10 22:31:06 +02:00
|
|
|
#include "gui.h"
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
Item *
|
|
|
|
item_create(void)
|
|
|
|
{
|
|
|
|
Item *item = ec_malloc(sizeof(Item));
|
|
|
|
item->sprite = NULL;
|
|
|
|
item->collected = false;
|
2018-08-10 14:29:45 +02:00
|
|
|
item->openable = false;
|
|
|
|
item->opened = 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)
|
|
|
|
{
|
2018-08-10 14:29:45 +02:00
|
|
|
if (item->collected || item->opened)
|
|
|
|
return;
|
2018-01-30 13:44:21 +01:00
|
|
|
|
2018-08-10 14:29:45 +02:00
|
|
|
if (!item->openable) {
|
|
|
|
item->collected = true;
|
|
|
|
} else {
|
2018-08-10 20:09:56 +02:00
|
|
|
mixer_play_effect(CHEST_OPEN);
|
2018-08-10 22:31:06 +02:00
|
|
|
gui_log("You open a container");
|
2018-08-10 14:29:45 +02:00
|
|
|
item->opened = true;
|
|
|
|
item->sprite->texture_index = 1;
|
2018-08-11 15:15:53 +02:00
|
|
|
if (!item->items)
|
|
|
|
gui_log("You find nothing inside");
|
2018-08-10 14:29:45 +02:00
|
|
|
}
|
2018-08-10 22:31:06 +02:00
|
|
|
|
|
|
|
if (item->effect != NULL)
|
|
|
|
item->effect(item, player);
|
|
|
|
|
|
|
|
while (item->items) {
|
|
|
|
Item *subitem = linkedlist_pop(&item->items);
|
|
|
|
item_collected(subitem, player);
|
|
|
|
item_destroy(subitem);
|
|
|
|
}
|
2018-01-30 13:44:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
void
|
|
|
|
item_destroy(Item *item)
|
|
|
|
{
|
|
|
|
if (item->sprite)
|
|
|
|
sprite_destroy(item->sprite);
|
2018-01-30 13:44:21 +01:00
|
|
|
|
2018-01-30 15:16:14 +01:00
|
|
|
while (item->items != NULL)
|
|
|
|
item_destroy(linkedlist_pop(&item->items));
|
2018-01-30 13:44:21 +01:00
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
free(item);
|
2018-01-25 10:45:05 +01:00
|
|
|
}
|