aboutsummaryrefslogtreecommitdiffstats
path: root/src/inventory.cpp
blob: b8905dd4d803dd1e05ac047b0f8839c171897e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <inventory.h>
#include <ui.h>

const char *itemName[]={
	"\0",
	"Dank Maymay"
};

Inventory::Inventory(unsigned int s){
	size=s;
	item=(struct item_t *)calloc(size,sizeof(struct item_t));
}

Inventory::~Inventory(void){
	free(item);
}
	
int Inventory::addItem(ITEM_ID id,unsigned char count){
	unsigned int i;
	for(i=0;i<size;i++){
		if(item[i].id==id){
			item[i].count+=count;
			return 0;
		}else if(!item[i].count){
			item[i].id=id;
			item[i].count=count;
			return 0;
		}
	}
	return -1;
}

int Inventory::takeItem(ITEM_ID id,unsigned char count){
	unsigned int i;
	for(i=0;i<size;i++){
		if(item[i].id==id){
			item[i].count-=count;
			if(item[i].count<0)
				return item[i].count*-1;
			return 0;
		}
	}
	return -1;
}

#include <entities.h>
extern Player *player;

void Inventory::draw(void){
	unsigned int i=0;
	float y=SCREEN_HEIGHT/2;
	ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"Inventory:");
	while(item[i].count){
		y-=ui::fontSize*1.15;
		ui::putText(player->loc.x-SCREEN_WIDTH/2,y,"%d x %s",item[i].count,itemName[(unsigned)item[i].id]);
		i++;
	}
}