Monsters now drop both treasure and items.

This commit is contained in:
Linus_Probert 2018-01-29 13:48:44 +01:00
parent 7b66b7726b
commit 6e6a9641ae
7 changed files with 43 additions and 24 deletions

View File

@ -18,9 +18,9 @@ typedef enum {
} Treasure;
typedef enum {
TREASURE,
FLESH,
HEALTH,
TREASURE,
ITEM_COUNT
} ItemKey;

View File

@ -109,9 +109,7 @@ map_clear_dead_monsters(Map *map)
Monster *monster = current->data;
// Loot drops
Item *item = monster_drop_loot(monster);
if (item)
linkedlist_append(&map->items, item);
monster_drop_loot(monster, map);
monster_destroy(monster);
current->data = NULL;

View File

@ -9,6 +9,7 @@
#include "gui.h"
#include "item.h"
#include "item_builder.h"
#include "map.h"
static void
monster_load_texts(Monster *m, SDL_Renderer *renderer)
@ -238,21 +239,34 @@ monster_hit(Monster *monster, unsigned int dmg)
monster->state.current = monster->state.challenge;
}
Item *
monster_drop_loot(Monster *monster)
void
monster_drop_loot(Monster *monster, Map *map)
{
static unsigned int drop_chance = 3;
unsigned int drop;
static unsigned int item_drop_chance = 3;
static unsigned int treasure_drop_chance = 2;
bool dropped_something = false;
unsigned int item_drop;
Item *item;
if ((rand() % drop_chance) != 0)
return NULL;
// TODO(Linus):
// 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->sprite->pos = monster->sprite->pos;
dropped_something = true;
linkedlist_append(&map->items, item);
}
if ((rand() % item_drop_chance) == 0) {
item_drop = rand() % TREASURE;
item = item_builder_build_item(item_drop);
item->sprite->pos = monster->sprite->pos;
dropped_something = true;
linkedlist_append(&map->items, item);
}
drop = rand() % ITEM_COUNT;
Item *item = item_builder_build_item(drop);
item->sprite->pos = monster->sprite->pos;
gui_log("%s dropped something", monster->label);
return item;
if (dropped_something)
gui_log("%s dropped something", monster->label);
}
void

View File

@ -34,7 +34,7 @@ void monster_render(Monster*, Camera*);
void monster_hit(Monster*, unsigned int dmg);
Item *monster_drop_loot(Monster*);
void monster_drop_loot(Monster*, Map*);
void monster_destroy(Monster*);

View File

@ -57,10 +57,16 @@ has_collided(Player *player, RoomMatrix *matrix)
gui_log("Ouch! There is something in the way");
}
if (space->item != NULL) {
if (space->item->effect)
space->item->effect(space->item, player);
space->item->collected = true;
if (space->items != NULL) {
LinkedList *items = space->items;
Item *item;
while (items != NULL) {
item = items->data;
items = items->next;
if (item->effect)
item->effect(item, player);
item->collected = true;
}
}
return collided;

View File

@ -72,7 +72,7 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
continue;
position = position_to_matrix_coords(&item->sprite->pos);
rm->spaces[position.x][position.y].item = item;
linkedlist_push(&rm->spaces[position.x][position.y].items, item);
}
}
@ -187,7 +187,7 @@ void roommatrix_reset(RoomMatrix *m)
m->spaces[i][j].lightsource = false;
m->spaces[i][j].light = 0;
m->spaces[i][j].monster = NULL;
m->spaces[i][j].item = NULL;
m->spaces[i][j].items = NULL;
m->spaces[i][j].player = NULL;
}
}

View File

@ -11,6 +11,7 @@ typedef struct Map_t Map;
typedef struct Monster_t Monster;
typedef struct Player_t Player;
typedef struct Item_t Item;
typedef struct Node LinkedList;
typedef struct {
bool occupied;
@ -18,7 +19,7 @@ typedef struct {
unsigned int light;
Monster *monster;
Player *player;
Item *item;
LinkedList *items;
} RoomSpace;
typedef struct {