forked from l0bster2/pocket-platformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameStatistics.js
85 lines (74 loc) · 2.59 KB
/
GameStatistics.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
class GameStatistics {
static staticConstructor() {
this.resetPlayerStatistics();
this.alreadyStopped = false;
}
static resetPlayerStatistics() {
this.resetTimer();
this.deathCounter = 0;
}
static resetPermanentObjects() {
WorldDataHandler.levels.forEach(level => {
level.levelObjects.forEach(levelObject => {
if (levelObject.type === ObjectTypes.COLLECTIBLE) {
levelObject.extraAttributes.collected = false;
}
});
})
}
static resetTimer() {
this.startTime = null;
this.endTime = null;
this.timeBetweenPauses = 0;
this.timesPauseWasPressed = 0;
}
static startTimer() {
if (!this.startTime) {
this.startTime = new Date();
}
}
static getTimeDifference() {
const endTime = this.endTime.getTime();
const startTime = this.startTime.getTime();
if (startTime > endTime) {
return null;
}
return endTime - startTime + this.timeBetweenPauses;
}
static updateTimeBetweenPauses() {
if (!this.endTime || !this.startTime) {
return null;
}
/*
The cost of stopping and resuming the game is approximatley 17 milliseconds. Just to initialize date
Therefore we substract 17 milliseconds the first 3 times the player presses pause.
If he does it more often, it's "his fault", which is not realistic
*/
this.timesPauseWasPressed++;
this.timeBetweenPauses = this.timesPauseWasPressed <= 3 ? this.getTimeDifference() - 17 : this.getTimeDifference();
this.startTime = null;
this.endTime = null;
}
static getFinalTime() {
if (!this.endTime || !this.startTime) {
return null;
}
var diff = this.getTimeDifference();
var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
return hh > 0 ? `${this.leadingZero(hh)}:${this.leadingZero(mm)}:${this.leadingZero(ss)}:${msec}` :
`${this.leadingZero(mm)}:${this.leadingZero(ss)}:${msec}`;
}
static leadingZero(num) {
return `0${num}`.slice(-2);
}
static stopTimer() {
this.endTime = new Date();
this.alreadyStopped = true;
}
}