pt2game/inventory.cpp

62 lines
1.8 KiB
C++

#include "engine.h"
#include "objids.h"
#include "inventory.h"
#include <assert.h>
extern const char _binary_sprite_menubar_raw_start[];
extern const char _binary_sprite_item_popcorn_raw_start[];
extern const char _binary_sprite_item_pager_raw_start[];
static const char *const inventory_sprites[] = {
_binary_sprite_item_popcorn_raw_start,
_binary_sprite_item_pager_raw_start,
};
int inventory[INVENTORY_SIZE] = {
// no default items
};
static void create_inventory_object(scene *s, int index) {
int type = inventory[index];
if(type == INVITEM_BLANK) return;
int x = 1066 + 14 + (index & 1) * 100;
int y = 200 + (index >> 1) * 100;
struct object *obj = scene_add_object(s, OBJID_INVENTORY_SLOTS + index, x, y, 87, 87, inventory_sprites[type-1]);
}
void create_standard_inventory(struct scene *s) {
scene_add_object(s, OBJID_MENUBAR, 1066, 0, 234, 800, _binary_sprite_menubar_raw_start);
for(int i = 0; i < INVENTORY_SIZE; i++) {
create_inventory_object(s, i);
}
}
void standard_inventory_onclick(struct object *obj) {
int slot = obj->id - OBJID_INVENTORY_SLOTS;
assert(inventory[slot] != INVITEM_BLANK);
// does nothing for now
}
int count_item_in_inventory(int item) {
int count = 0;
for(int i = 0; i < INVENTORY_SIZE; i++)
if(inventory[i] == item)
count++;
return count;
}
int add_to_inventory(int item) {
// returns slot number or -1 if no space available
for(int i = 0; i < INVENTORY_SIZE; i++) {
if(!inventory[i]) {
inventory[i] = item;
// TODO: inventory should refresh when a scene is activated when a different scene is activated before.
// If not a fully custom rendering. Otherwise, popping a scene may revert inventory objects on screen to an earlier state.
create_inventory_object(&top_scene, i);
return i;
}
}
return -1;
}