-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventoryHandler.js
More file actions
73 lines (57 loc) · 1.47 KB
/
inventoryHandler.js
File metadata and controls
73 lines (57 loc) · 1.47 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
// Create the hashmap
var inventory = {};
//Item list array (for easier selection)
var iList = [];
//Takes in a Item
//Adds it to the inventory if it doesn't exist
//increments it's count if it does exist
function addItem(i){
if (i.name in inventory){
inventory[i.name].Item.quantity += 1;
}
else {
i.quantity = 1; //Make sure quantity is 1
inventory[i.name] = { Item: i }
}
}
//Removes given Item from inventory
function removeItem(i){
if (i.name in inventory){
inventory[i.name].Item.quantity -= 1;
}
}
//Removes given Item from inventory
function updateinventory(){
for (var x in inventory){
if (inventory[x].Item.quantity <= 0){
delete inventory[x];
}
}
}
function inventoryHandler(){
//locate main canvas in document and clear
var main_canvas = document.getElementById("main_screen");
var context = main_canvas.getContext("2d");
context.clearRect(0,0,500,500);
//Draw main background
var inventory_menu1 = new Image();
inventory_menu1.src = "screens/inventory_screen.png";
context.drawImage(inventory_menu1,0,0);
context.font = "30px Papyrus";
iList = [];
for (var x in inventory) {
iList.push(x);
}
//To display all Items
var yPlace = 110
var y2Place = 110
for (var x in iList){
if (x < 7){
context.fillText(iList[x] + " x" + inventory[iList[x]].Item.quantity, 25, yPlace);
yPlace += 50;
} else {
context.fillText(iList[x] + " x" + inventory[iList[x]].Item.quantity, 275, y2Place);
y2Place += 50;
}
}
}