-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
111 lines (109 loc) · 2.73 KB
/
menu.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
class Menu{
constructor(width,height,canvas,sprites){
this.width = width;
this.height = height;
this.canvas = canvas;
this.initCanvas(canvas);
this.sprites = sprites;
this.keys = [];
this.time = 0;
this.index = 0;
this.lastMove = 0;
this.transitioning = false;
this.transTime = 0;
this.interval = setInterval(()=>{
this.update();
this.render();
});
musicManager.menu.loop(true);
musicManager.menu.play();
}
initCanvas(canvas){
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.canvas.height = 720;
this.canvas.width = 1280;
this.canvas.style.width = '1280px';
this.canvas.style.height = '720px';
this.canvas.addEventListener('blur',()=>{
this.keys = [];
});
}
update(){
if(this.transitioning) return;
this.handleInput();
this.time++;
}
render(){
let canvas = this.canvas;
let ctx = this.ctx;
if(this.transitioning){
if(!this.transTime){
ctx.fillRect(0,0,canvas.width,canvas.height);
let img = spriteManager.sprites.credits;
let drawWidth = canvas.width;
let drawHeight = canvas.width/img.width * img.height;
ctx.drawImage(img,0,0,drawWidth,drawHeight);
}
this.transTime++;
if(this.transTime > 30*60){
this.transitioning = false;
}
return;
}
ctx.clearRect(0,0,canvas.width,canvas.height);
let img = game.sprites.menuBg;
ctx.fillRect(0,0,canvas.width,canvas.height);
let drawWidth = canvas.width;
let drawHeight = canvas.width/img.width * img.height;
ctx.drawImage(img,0,0,drawWidth,drawHeight);
ctx.drawImage(game.sprites.menuButtons,980,350,209*1.3,232*1.3);
ctx.drawImage(game.sprites.title, 0,60,game.sprites.title.width * 0.5,game.sprites.title.height * 0.5);
let offset = 360;
let distance = 100;
ctx.drawImage(game.sprites.titleMarker,930,offset + distance * this.index,game.sprites.titleMarker.width * 0.2,game.sprites.titleMarker.height * 0.2);
}
move(dir){
if(this.lastMove > this.time) return;
this.index+= dir;
if(this.index < 0) this.index = 2;
if(this.index > 2) this.index = 0;
this.lastMove = this.time + 30;
}
select(){
soundFxManager.select.play();
switch(this.index){
case 0:
clearInterval(this.interval);
musicManager.menu.stop();
game.scene = new Scene(this.width,this.height,this.canvas,game.sprites);
break;
case 1:
this.transTime = 0;
this.transitioning = true;
break;
}
}
handleInput(){
game.keySettings.up.forEach(key=>{
if(this.keys[key]){
this.move(-1);
}
});
game.keySettings.down.forEach(key=>{
if(this.keys[key]){
this.move(1);
}
});
game.keySettings.select.forEach(key=>{
if(this.keys[key]){
this.select();
}
});
game.keySettings.drop.forEach(key=>{
if(this.keys[key]){
this.select();
}
});
}
}