-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinventory.js
84 lines (63 loc) · 1.82 KB
/
inventory.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
var inherits = require('inherits');
module.exports = Inventory;
function Inventory(game){
this.game = game;
this.game.inventory = {};
this.defaultMaxWeight = 100;
this.maxWeight = this.defaultMaxWeight;
this.currentWeight = 0;
this.createHTML();
}
Inventory.prototype.createHTML = function(){
this.el = document.createElement('ul');
this.el.id = 'inventory';
var h3 = document.createElement('h3');
h3.innerHTML = 'inventory';
this.el.appendChild(h3);
this.weightEl = document.createElement('span');
this.weightEl.id = 'weight';
this.weightEl.innerHTML = this.currentWeight + '/' + this.maxWeight;
h3.appendChild(this.weightEl);
document.body.appendChild(this.el);
};
Inventory.prototype.add = function(item){
var self = this;
var items = this.game.inventory;
if (!items[item.id]){
items[item.id] = {
item: item,
quantity: 1
}
this.addWeight(item.weight);
var li = document.createElement('li');
li.innerHTML = item.name;
li.id = item.id;
self.el.appendChild(li);
} else {
items[item.id].quantity += 1;
}
};
Inventory.prototype.remove = function(item){
var items = this.game.inventory;
if (items[item.id]){
if (items[item.id].quantity > 1){
items[item.id].quantity -= 1;
} else {
delete items[item.id];
var itemEl = document.getElementById(item.id);
this.el.removeChild(itemEl);
}
}
this.removeWeight(item.weight);
};
Inventory.prototype.list = function(){
return this.game.inventory.join(', ');
};
Inventory.prototype.addWeight = function(weight){
this.currentWeight += weight;
this.weightEl.innerHTML = this.currentWeight + '/' + this.maxWeight;
}
Inventory.prototype.removeWeight = function(weight){
this.currentWeight -= weight;
this.weightEl.innerHTML = this.currentWeight + '/' + this.maxWeight;
}