-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.js
More file actions
113 lines (102 loc) · 2.81 KB
/
menu.js
File metadata and controls
113 lines (102 loc) · 2.81 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
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
import { drawAudioControl, audioCanvasTouchHandler } from "./input.js";
import { startGame } from "./game.js";
import { drawButton, parseText } from "./utils.js";
const canvas = document.getElementById("screen");
const ctx = canvas.getContext("2d");
export function drawMenu() {
drawSeperator();
writeIntro();
drawAudioControl();
drawButtons();
}
export function menuTouchHandler({ x, y }) {
const buttons = getButtons();
const buttonPressed = buttons.find(
(btn) =>
btn.x - btn.width / 2 <= x &&
btn.x + btn.width / 2 >= x &&
btn.y - btn.height / 2 <= y &&
btn.y + btn.height / 2 >= y
);
if (!buttonPressed) {
audioCanvasTouchHandler({ x, y });
return;
}
if (buttonPressed.text === "start") {
startGame();
} else {
difficulty = buttonPressed.text;
}
}
function drawButtons() {
getButtons().forEach((btn) => drawButton(ctx, btn));
}
function drawSeperator() {
ctx.strokeStyle = "white";
ctx.beginPath();
ctx.moveTo(canvas.width / 2, 25);
ctx.lineTo(canvas.width / 2, canvas.height - 25);
ctx.stroke();
}
function writeIntro() {
const intro = getIntro();
ctx.font = `${intro.fontSize}px slkscr`;
ctx.fillStyle = "White";
ctx.textAlign = "center";
intro.lines.forEach(({ y, text }) => {
ctx.fillText(text, canvas.width / 4, y);
});
}
function getButtons() {
const buttons = [
{
x: (3 * canvas.width) / 4,
y: canvas.height - 50,
width: 100,
height: 40,
color: "grey",
text: "start",
},
{
x: (3 * canvas.width) / 4 - 110,
y: canvas.height - 110,
width: 100,
height: 40,
color: "grey",
text: "easy",
},
{
x: (3 * canvas.width) / 4,
y: canvas.height - 110,
width: 100,
height: 40,
color: "grey",
text: "medium",
},
{
x: (3 * canvas.width) / 4 + 110,
y: canvas.height - 110,
width: 100,
height: 40,
color: "grey",
text: "hard",
},
];
buttons.forEach((btn) =>
difficulty === btn.text ? (btn.color = "green") : null
);
return buttons;
}
function getIntro() {
const intro = [
"This is Noisy Gamer. A game that involves your voice.",
"",
"Click/tap on the screen or press space to jump upwards. Speak/shout to fire your laser. Any sound works, but a high-pitched 'pew' is supreme",
"",
"Walls will move in from the right, and you can eliminate the middle bits with your laser. Your goal is to survive as long as you can without hitting a wall. When not playing on easy, your firepower is limited and shown on top of the screen.",
"",
"Adjust the slider on the right to determine the therehold for firing the laser, choose your difficulty and press start.",
];
const targetWidth = canvas.width / 2 - 20;
return parseText(intro, 20, targetWidth, 40, ctx);
}