-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinance.js
47 lines (37 loc) · 1019 Bytes
/
Finance.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
/* exported Finance */
"use strict";
var Finance = (function () {
var Finance = function(money) {
this.money = money || 0;
this.maxGrowthRate = 5000000;
// I start with max
this.growthRate = this.maxGrowthRate;
// I recover 5% of maxGrowthRate per second
this.recoveryRate = this.maxGrowthRate*0.01;
// if I take 5 damage, my growthRate goes to 0
this.damageMultiplier = this.maxGrowthRate/5;
this.resistance = 0; // percentage of resistance against damage
};
Finance.prototype.hit = function(damage) {
this.growthRate = Math.max(
0,
this.growthRate - damage*this.damageMultiplier*(1-this.resistance)
);
};
Finance.prototype.update = function(dt) {
this.growthRate = Math.min(
this.maxGrowthRate,
this.growthRate + this.recoveryRate*dt
);
this.money += this.growthRate * dt;
};
Finance.prototype.spend = function(amount) {
if (this.money >= amount) {
this.money -= amount;
return true;
} else {
return false;
}
};
return Finance;
})();