/
resources.js
66 lines (59 loc) · 1.27 KB
/
resources.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
function Resources(amounts, population) {
if (amounts === undefined) {
amounts = {
food : 0,
wood : 0,
stone : 0,
metal : 0
};
}
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] = amounts[t] || 0;
}
if (population > 0) {
this.population = parseInt(population);
} else {
this.population = 0;
}
}
Resources.prototype.types = [ "food", "wood", "stone", "metal" ];
Resources.prototype.canAfford = function(that) {
for ( var tKey in this.types) {
var t = this.types[tKey];
if (this[t] < that[t]) {
return false;
}
}
return true;
};
Resources.prototype.add = function(that) {
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] += that[t];
}
this.population += that.population;
};
Resources.prototype.subtract = function(that) {
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] -= that[t];
}
this.population += that.population;
};
Resources.prototype.multiply = function(n) {
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] *= n;
}
this.population *= n;
};
Resources.prototype.toInt = function() {
var sum = 0;
for ( var tKey in this.types) {
var t = this.types[tKey];
sum += this[t];
}
sum += this.population * 50; // based on typical unit costs
return sum;
};