forked from sparticle999/SpaceCompany
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathachievements.js
123 lines (102 loc) · 3.25 KB
/
achievements.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Game.achievements = (function() {
'use strict';
var instance = {};
instance.dataVersion = 6;
instance.nextId = 0;
instance.rank = 1;
instance.xp = 0;
instance.entries = {};
instance.achievementCount = 0;
instance.achievementCountIncludingTiers = 0;
instance.initialise = function() {
for (var id in Game.achievementsData) {
var data = Game.achievementsData[id];
this.entries[id] = $.extend({}, data, {
id: id,
category: data.categoryInstance.title,
iconPath: Game.constants.iconPath,
iconExtension: Game.constants.iconExtension,
unlocked: -1,
progressDisplay: -1,
displayNeedsUpdate: true
});
if (data.brackets === undefined) {
this.entries[id].brackets = data.categoryInstance.brackets;
}
this.achievementCount++;
this.achievementCountIncludingTiers += this.entries[id].brackets.length;
}
console.debug("Loaded " + this.achievementCount + " (" + this.achievementCountIncludingTiers +") Achievements");
};
instance.getAchievementTitle = function(data, for_tooltip) {
if(data.unlocked === data.brackets.length - 1) {
var title = data.title.replace('%s', Game.settings.format(data.brackets[data.unlocked]));
if(for_tooltip === true) {
title += " (Completed)";
}
return title;
} else {
var title = data.title.replace('%s', Game.settings.format(data.brackets[data.unlocked+1]));
if(for_tooltip === true) {
title += ' (' + data.progressDisplay + '%)';
}
return title;
}
};
instance.update = function(delta) {
for(var id in this.entries) {
var data = this.entries[id];
var bracket = data.brackets[data.unlocked + 1];
if(data.unlocked < data.brackets.length - 1 && data.evaluator(bracket)) {
Game.notifySuccess("Achievement Reached", this.getAchievementTitle(data, false));
this.unlock(id, data.unlocked + 1);
newUnlock('more');
} else if(data.unlocked < data.brackets.length - 1) {
var progressDisplay = Math.floor(100 * data.progressEvaluator(bracket));
this.updateProgress(id, progressDisplay);
}
}
};
instance.unlock = function(id, tier) {
if(this.entries[id].unlocked < tier) {
this.entries[id].unlocked = tier;
this.entries[id].displayNeedsUpdate = true;
}
};
instance.updateProgress = function(id, progress) {
if(this.entries[id].progressDisplay != progress) {
this.entries[id].progressDisplay = progress;
this.entries[id].displayNeedsUpdate = true;
}
};
instance.save = function(data) {
data.achievements = {version: this.dataVersion, entries: {}};
for(var id in this.entries) {
if(this.entries[id].unlocked >= 0) {
data.achievements.entries[id] = {
unlocked: this.entries[id].unlocked
};
}
}
};
instance.load = function(data) {
if (data.achievements && data.achievements.version) {
switch (data.achievements.version) {
case 6: this.loadV6(data); break;
default: console.debug("Could not load saved achievement data from version " + data.achievements.version); break;
}
}
};
instance.loadV6 = function(data) {
if (data.achievements) {
for (var id in data.achievements.entries) {
if (this.entries[id]) {
if (data.achievements.entries[id].unlocked >= 0) {
this.unlock(id, data.achievements.entries[id].unlocked);
}
}
}
}
};
return instance;
}());