-
Notifications
You must be signed in to change notification settings - Fork 172
/
building.js
100 lines (84 loc) · 3.06 KB
/
building.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Game.buildings = (function(){
var instance = {};
instance.dataVersion = 1;
instance.entries = {};
instance.updatePerSecondProduction = true;
instance.techTypeCount = 0;
instance.initialise = function() {
for (var id in Game.buildingData) {
var data = Game.buildingData[id];
this.techTypeCount++;
this.entries[id] = $.extend({}, data, {
id: id,
htmlId: 'resbld_' + id,
current: 0,
iconPath: Game.constants.iconPath,
iconName: data.icon,
iconExtension: Game.constants.iconExtension,
max: data.maxCount,
displayNeedsUpdate: true
});
}
console.debug("Loaded " + this.techTypeCount + " Building Types");
};
instance.update = function(delta) {
if (this.updatePerSecondProduction === true) {
this.updateProduction();
}
};
instance.save = function(data) {
data.buildings = { v: this.dataVersion, i: {}};
for(var key in this.entries) {
data.buildings.i[key] = this.entries[key].current;
}
};
instance.load = function(data) {
if(data.buildings) {
if(data.buildings.v && data.buildings.v === this.dataVersion) {
for(var id in data.buildings.i) {
if(this.entries[id]) {
this.constructBuildings(id, data.buildings.i[id]);
}
}
}
}
};
instance.constructBuildings = function(id, count) {
// Add the buildings and clamp to the maximum
var newValue = Math.floor(this.entries[id].current + count);
this.entries[id].current = Math.min(newValue, this.entries[id].max);
this.entries[id].displayNeedsUpdate = true;
this.updatePerSecondProduction = true;
};
instance.destroyBuildings = function(id, count) {
// Remove the buildings and ensure we can not go below 0
var newValue = Math.floor(this.entries[id].current - count);
this.entries[id].current = Math.max(newValue, 0);
this.entries[id].displayNeedsUpdate = true;
this.updatePerSecondProduction = true;
};
instance.unlock = function(id) {
this.entries[id].unlocked = true;
this.entries[id].displayNeedsUpdate = true;
};
instance.updateProduction = function() {
for(var id in this.entries) {
var data = this.entries[id];
if(data.current == 0) {
// Nothing to be done
continue;
}
var buildingData = this.entries[id];
if (!buildingData.resource) {
continue;
}
var baseValue = data.current * buildingData.perSecond;
Game.resources.setPerSecondProduction(buildingData.resource, baseValue);
}
this.updatePerSecondProduction = false;
};
instance.getBuildingData = function(id) {
return this.entries[id];
};
return instance;
}());