forked from loglot/yet-another-2d-platformer-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
149 lines (105 loc) · 3.78 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
import { Game } from "./imports/import.js"
document.getElementsByTagName("body")[0].style.cursor = "none";
const game = new Game(this)
const perfectFrameTime = 1000 / 60;
var fps = 0
let deltaTime = 0;
let lastTimestamp = 0;
let lastFrameMenu = true
let msPrev = window.performance.now()
const targetFPS = 60
const msPerFrame = 1000 / targetFPS
async function startGame() {
if(localStorage) {game.storage.accessible = false} else {game.storage.accessible = true}
game.storage.check()
requestAnimationFrame(tick);
//game.autoDebug()
//while (true) {
//game.updateGame();
//}
}
async function tick() {
requestAnimationFrame(tick);
const msNow = window.performance.now()
const msPassed = msNow - msPrev
if (msPassed < msPerFrame) return
const excessTime = msPassed % msPerFrame
msPrev = msNow - excessTime
FPSCalc()
game.audio.songMuteBrains()
//currentTime = Date.now();
if(game.debug.lag){await sleep(Math.random() * 100)}
//console.log("frame")
var now = Date.now()
deltaTime = (Date.now() - lastTimestamp) / perfectFrameTime;
lastTimestamp = Date.now();
game.gameDisplayer.drawGameFrame();
if (game.debug.playerHitbox) {
game.player.drawHitbox()
}
if (game.debug.mapBuilder) {
game.mapEdit.drawHitbox()
}
var DrawTime = Date.now() - now
var now = Date.now()
if(game.menu.check) {
updateGame();
if(lastFrameMenu){
game.audio.menuSound()
game.audio.playSound()
game.audio.playSongLoop()
lastFrameMenu = false
game.gameDisplayer.targetR = 167
game.gameDisplayer.targetG = 167
game.gameDisplayer.targetB = 167
}
} else {
game.menu.drawMenu()
if(game.keyManager.wasKeyJustPressed("KeyW") && !game.menu.checkDos) {
game.menu.fade()
}
}/**/
//game.audio.playSound()
game.keyManager.update();
//console.log("Drawing :", DrawTime , "||| Updating :", Date.now() - now, "||| Max : 16 :", DrawTime + Date.now() - now, "||| Delta Time :", deltaTime)
//lastTime = Date.now()
//await sleep(1000/60);
}
async function updateGame() {
for(let i = 0; i < game.player.orb.length; i++){
game.player.orb[i].update()
}
game.camera.update(deltaTime)
game.map.sign.update(game.player, game.keyManager)
game.player.update(deltaTime);
for(let i = 0; i < game.enemy.value.length; i++) {
game.enemy.value[i].update();
}
game.debug.update();
if(game.debug.mapBuilder){
game.mapEdit.update();
}
game.hook.update(deltaTime);
game.hookII.update(deltaTime);
game.bazooka.update()
game.shotgun.update()
game.dash.update()
game.storage.update()
DeltaTime()
if (game.keyManager.wasKeyJustPressed("KeyP") && game.menu.checkDos) {
game.menu.fade("up")
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function DeltaTime(){
}
async function FPSCalc(){
fps++
await sleep(1000)
fps--
game.debug.fpsCount = fps
}
startGame();