-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurrencies.js
85 lines (81 loc) · 3.25 KB
/
currencies.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
/*
*
* Mouse Pro
*
* Currencies.js
*/
(function(Game, Display, Currencies) {
Currencies.newCurrency = function(settings) {
let currency = {
...settings,
saveableState: {
xp: 0,
level: 0,
highestLevelAttained: 0
},
levelsLabel: settings.levelsLabel || 'levels',
xpLabel: settings.xpLabel || 'xp',
isToBeDisplayedNormally: settings.isToBeDisplayedNormally === undefined ? true : settings.isToBeDisplayedNormally,
getLevel: function() {
return this.saveableState.level;
},
getHighestLevelAttained: function() {
return this.saveableState.highestLevelAttained;
},
getXp: function() {
return this.saveableState.xp;
},
setXp: function(xp) {
this.saveableState.xp = Math.round(xp * 100) / 100;
},
xpIncreaseFactor: settings.xpIncreaseFactor || 1.2,
xpRequiredForNextLevel: function () {
return Math.round(settings.xpRequiredForNextLevel * Math.pow(this.xpIncreaseFactor, this.saveableState.level));
},
levelUp: function() {
this.saveableState.xp -= this.xpRequiredForNextLevel();
this.saveableState.level++;
if (settings.levelUp)
settings.levelUp(this);
if (this.isToBeDisplayedNormally)
Display.notifyLevelUp(this);
if (this.saveableState.highestLevelAttained == undefined
|| this.saveableState.level > this.saveableState.highestLevelAttained)
this.saveableState.highestLevelAttained = this.saveableState.level;
Game.checkUnlocks();
},
levelDown: function(amount) {
this.saveableState.level -= amount;
this.checkLevelUps();
},
canLevelUp: function() {
return this.saveableState.xp >= this.xpRequiredForNextLevel();
},
acquireXp: function(amount) {
this.saveableState.xp += amount;
if (isFinite(this.saveableState.xp)) {
this.saveableState.xp = Math.round(this.saveableState.xp * 100) / 100;
if (isNaN(this.saveableState.xp)) this.saveableState.xp = 0;
if (settings.xpGained)
settings.xpGained(this);
this.checkLevelUps();
} else {
this.levelUp();
}
},
acquireLevels: function(amount) {
this.saveableState.level += amount;
this.checkLevelUps();
},
checkLevelUps: function() {
while (this.canLevelUp()) {
this.levelUp();
}
},
xpProgressPercent: function() {
return (this.saveableState.xp / this.xpRequiredForNextLevel()) * 100.0;
}
};
Game.currencies.push(currency);
}
})(gameObjects.Game, gameObjects.Display, gameObjects.Currencies);