-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
65 lines (57 loc) · 1.92 KB
/
system.js
File metadata and controls
65 lines (57 loc) · 1.92 KB
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
import { Timer, Sound } from "./inc.js"
export default class System{
static GAMESTATE = { PLAY: "play", STOP: "stop", PAUSE: "pause", GAMEOVER: "gameover" }
constructor({timerStamp = 50, isUpdate = true, isDev = false}){
this.gamestate = System.GAMESTATE.PLAY
this.timerStamp = timerStamp
this.isDev = isDev
this.#_(isUpdate)
}
async #_(isUpdate = true){
await this.Awake()
await this.Start()
this.timer = (isUpdate) ? new Timer(() => { this.Update() }, this.timerStamp) : null
}
getState(){ return this.gamestate }
setState(state){ return this.gamestate = state }
getTimerStamp(){ return this.timerStamp }
setTimer(timer){
this.timerStamp = timer
this.timer?.reset(timer)
}
Awake(){}
Start(){}
Update(){
switch(this.gamestate){
case System.GAMESTATE.PLAY: this.Play(); break;
case System.GAMESTATE.STOP: this.Stop(); break;
case System.GAMESTATE.PAUSE: this.Pause(); break;
case System.GAMESTATE.GAMEOVER: this.GameOver(); break;
}
}
Play(){
this.gamestate = System.GAMESTATE.PLAY
this.timer?.start()
}
Stop(){
this.gamestate = System.GAMESTATE.STOP
this.timer?.stop()
}
Pause(){
this.gamestate = System.GAMESTATE.PAUSE
this.timer?.stop()
}
GameOver(){
this.gamestate = System.GAMESTATE.GAMEOVER
this.timer?.stop()
}
PlaySound({src, volume = 1}){ Sound.play(src, volume) }
/** @return {boolean} */
isPlay(){ return this.gamestate === System.GAMESTATE.PLAY; }
/** @return {boolean} */
isStop(){ return this.gamestate === System.GAMESTATE.STOP; }
/** @return {boolean} */
isPause(){ return this.gamestate === System.GAMESTATE.PAUSE; }
/** @return {boolean} */
isGameOver(){ return this.gamestate === System.GAMESTATE.GAMEOVER; }
}