forked from l0bster2/pocket-platformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
103 lines (93 loc) · 3.96 KB
/
Game.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
class Game {
static get PLAY_MODE() {
return 'play';
}
static get BUILD_MODE() {
return 'build';
}
static staticConstructor() {
this.playMode = this.PLAY_MODE;
this.refreshRateCounter = [];
this.refreshRateHigher60 = false;
this.refreshRate = null;
this.refreshRates = [];
this.currentRetryToGetFps = 0;
this.maxRetriesToGetFps = 10;
this.averageFps = null;
this.percentOfFpsHigher70 = null;
}
static executeGameMode() {
if (this.playMode === this.PLAY_MODE) {
SoundHandler.doesSoundExist("song") && SoundHandler.song.play();
window.requestAnimationFrame(play);
}
else if (this.playMode === this.BUILD_MODE) {
SoundHandler.doesSoundExist("song") && SoundHandler.song.stop();
window.requestAnimationFrame(build);
}
}
static startAnimating(fps) {
this.fpsInterval = 1000 / fps;
this.then = 0;
this.startTime = this.then;
}
static updateFPSInterval(timestamp) {
this.now = timestamp;
this.elapsed = this.now - this.then;
if (!this.refreshRate) {
this.refreshRateCounter.unshift(this.now);
const waitFrames = 15;
if (this.refreshRateCounter.length === waitFrames) {
var t0 = this.refreshRateCounter.pop();
this.refreshRate = Math.floor(1000 * waitFrames / (this.now - t0));
if (this.refreshRate > 70) {
this.refreshRateHigher60 = true;
}
this.refreshRateCounter = [];
this.currentRetryToGetFps++;
this.refreshRates.push(this.refreshRate);
if(this.currentRetryToGetFps < this.maxRetriesToGetFps) {
this.refreshRate = null;
}
else {
this.averageFps = this.refreshRates.reduce((partialSum, a) => partialSum + a, 0) / this.refreshRates.length;
const refreshRateHigher70 = this.refreshRates.filter(refreshRate => refreshRate > 70);
if(refreshRateHigher70.length > 0) {
this.percentOfFpsHigher70 = refreshRateHigher70.length / this.refreshRates.length * 100;
this.percentOfFpsHigher70 > 80 ? this.refreshRateHigher60 = true : this.refreshRateHigher60 = false;
}
else {
this.percentOfFpsHigher70 = 0;
}
}
}
}
}
static resetFpsInterval() {
this.then = this.now - (this.elapsed % this.fpsInterval);
}
static changeGameMode(firstTime = false) {
const canvas = document.getElementById("myCanvas");
if (this.playMode === this.PLAY_MODE) {
this.playMode = this.BUILD_MODE;
this.executeGameMode();
canvas.addEventListener("mousemove", (e) => { Controller.mouseMove(e) });
ObjectsTooltipElementsRenderer.renderPlayButton(firstTime);
LevelSizeHandler.toggleSliderDisableAttr(false);
tileMapHandler?.resetDynamicObjects();
return null;
}
if (this.playMode === this.BUILD_MODE) {
/*Camera.viewport.width = Camera.viewport.width / 2;
Camera.viewport.height = Camera.viewport.height / 2;
Camera.viewport.halfWidth = Camera.viewport.halfWidth / 2;
Camera.viewport.halfHeight = Camera.viewport.halfHeight / 2;*/
this.playMode = this.PLAY_MODE;
this.executeGameMode();
canvas.removeEventListener("mousemove", (e) => { Controller.mouseMove(e) });
ObjectsTooltipElementsRenderer.renderPauseButton(firstTime);
LevelSizeHandler.toggleSliderDisableAttr(true);
return null;
}
}
}