-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.ts
50 lines (37 loc) · 1.17 KB
/
init.ts
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
class Game {
public state: State
public renderer: Renderer
private ui: UI
private music: HTMLAudioElement
private lastUpdate: number
constructor() {
Input.init();
this.state = new State()
this.renderer = new Renderer(document.getElementById("canvas_main") as HTMLCanvasElement)
this.ui = new UI()
this.music = new Audio(AudioResource.BGM)
this.music.loop = true
this.music.volume = 0.25
this.music.play()
this.lastUpdate = NaN
}
mainLoop(time: DOMHighResTimeStamp) {
let deltaTime = (time - this.lastUpdate) / 1000
this.lastUpdate = time
if (!isNaN(deltaTime)) {
this.state.update(deltaTime)
this.renderer.update(deltaTime, this.state)
this.ui.update(deltaTime, this.state.player)
Input.update()
}
window.requestAnimationFrame(this.mainLoop.bind(this))
}
}
window.onload = () => {
let preloader = new Preloader();
preloader.onFinish( () => {
let game = new Game()
window.requestAnimationFrame(game.mainLoop.bind(game))
})
preloader.startLoading();
}