-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgamescript.js
More file actions
212 lines (195 loc) · 7.34 KB
/
gamescript.js
File metadata and controls
212 lines (195 loc) · 7.34 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//loading DOM
window.addEventListener("DOMContentLoaded", loadSVG);
function loadSVG() {
fetch("background1.svg")
.then(response => response.text())
.then(svgData => {
document
.querySelector("#gameplaceholder")
.insertAdjacentHTML("beforeend", svgData);
});
loadModalOne();
}
function loadModalOne() {
fetch("modals.svg")
.then(response => response.text())
.then(svgData => {
document
.querySelector("#startone")
.insertAdjacentHTML("beforeend", svgData);
//starting everything on click of the start
document
.querySelector("#startone")
.addEventListener("click", loadModalTwo);
});
}
function loadModalTwo() {
document.querySelector("#startone").style.display = "none";
document.querySelector("#starttwo").style.display = "block";
fetch("modals2.svg")
.then(response => response.text())
.then(svgData => {
document
.querySelector("#starttwo")
.insertAdjacentHTML("afterbegin", svgData);
document.querySelector("#starttwo").addEventListener("click", gameStart);
});
}
function gameStart() {
document.querySelector("#starttwo").style.display = "none";
document.querySelector("#blur2").style.display = "none";
document.querySelector("#backgroundmodal").style.display = "none";
console.log("greencup");
//hiding the modal
catching();
}
function catching() {
let GoodCup = document.querySelector("#CupForCounting");
let BadCup = document.querySelector("#badCup");
let intv;
let speed = 1000;
let killedCups = [];
let counter = 0;
let timertodeath;
//music on
document.querySelector("#music").play();
//enhancing speed and adding points on the Good Cup click
GoodCup.addEventListener("click", () => {
//points adding sound
document.querySelector("#good").play();
//here we are changing the text for pointer counter and the button and the end modal
counter++;
let pointCounter = document.querySelector("#pointstext");
pointCounter.textContent = `${counter} points`;
document.querySelector(
"#congratulations"
).innerHTML = `You earned ${counter} points!<br\> Exchange them to ${counter *
10}dkk now!`;
document.querySelector("#formbutton").innerHTML = `Get your ${counter *
10} dkk now!`;
speed = speed - 80;
//to speed up we have to clear the existing interval and turn it on again with the new speed
clearInterval(intv);
//turning the game with some timeout
clearTimeout(timertodeath);
timertodeath = setInterval(function() {
let badstrike;
killedCups.push(badstrike);
let strikes = document.querySelectorAll("[data-id]");
let properstrike = strikes[killedCups.length - 1];
properstrike.style.fill = "red";
document.querySelector("#bad").play();
document.querySelector("#nope").play();
if (killedCups.length === 3) {
endGame(counter);
}
//endGame(counter);
}, 40000);
//the point appears
document.querySelector("#onepoint").classList.add("animatingpoints");
setTimeout(function() {
document.querySelector("#onepoint").classList.remove("animatingpoints");
}, 600);
//if the counter is divisible by 3, we clone the BadCup all over and append it to the background
if (counter % 3 === 0) {
let clone = BadCup.cloneNode(true);
document.querySelector("#backgroundGame").appendChild(clone);
badCupFunctionality(killedCups, clone, counter);
}
});
//value for setting the interval with a certain speed to the moving
moveCup(GoodCup);
timertodeath = setInterval(function() {
let badstrike;
killedCups.push(badstrike);
let strikes = document.querySelectorAll("[data-id]");
let properstrike = strikes[killedCups.length - 1];
properstrike.style.fill = "red";
document.querySelector("#bad").play();
document.querySelector("#nope").play();
if (killedCups.length === 3) {
endGame(counter);
}
}, 40000);
int = setInterval(moveCup, speed, GoodCup);
//now introducing the bad cup
}
function moveCup(GoodCup) {
// let GoodCup = document.querySelector("#CupForCounting");
//so this is math random is multiplied but the width and height of the canvas
//it's aproximately the width and height through which the cup should move
//i tested it first moving the cup to the most left and most bottom in css
GoodCup.style.transform = `translate(${Math.random() *
1100}px, ${Math.random() * 400}px)`;
}
function badCupFunctionality(killedCups, clone, counter) {
clone.style.display = "block";
//I am reusing the same function for moving the cup but just with different values and one set speed
setInterval(moveCup, 1000, clone);
clone.addEventListener("click", () => {
//instead of counting clicks we have to create the array and pushed every clicked clone into that. it's because
//the counter gets reseted with every clone, because it is counting a click to this particular clone, not globally
killedCups.push(clone);
clone.querySelector("#thex").classList.add("animatingpoints");
setTimeout(function() {
clone.style.display = "none";
}, 800);
//bad points sound effect
document.querySelector("#bad").play();
document.querySelector("#nope").play();
//here I am finding all the "x" in the svg, they have data id but it's just because I was playing with that, could
//be just a class or whatever, anyway all of them are now an array of strikes and the index number of the array
//should equal to the amount of the killed cups minus one. because index numbers of arrays start from 0 and number of
//killed cups will be starting from 1
let strikes = document.querySelectorAll("[data-id]");
let properstrike = strikes[killedCups.length - 1];
properstrike.style.fill = "red";
//end the game when it's 3 clicked
if (killedCups.length === 3) {
endGame(counter);
}
});
}
function endGame(counter) {
//also enough of the got music
document.querySelector("#music").pause();
document.querySelector("#blur").style.display = "block";
document.querySelector("#endgamemodal").style.display = "block";
//replay functionality
document.querySelector("#replay").addEventListener("click", () => {
location.reload();
});
}
//ROTATE MOBILE SECTION
//Showing the rotateScreen div only on mobile devices
let mql = window.matchMedia("(max-width: 500px)");
function screenTest(e) {
if (e.matches) {
/* the viewport is 500 pixels wide or less */
//for mobile device
document.querySelector("#startone").style.display = "none";
document.querySelector("#rotateScreen").style.display = "block";
} else {
//for desktop
document.querySelector("#rotateScreen").style.display = "none";
}
}
mql.addListener(screenTest);
document.getElementById("rotateButton").addEventListener("click", start);
document
.getElementById("rotateButton")
.addEventListener("click", hideInfoAboutRotatingScreen);
function hideInfoAboutRotatingScreen() {
document.querySelector("#rotateScreen").style.display = "none";
document.querySelector("#startone").style.display = "block";
}
// Rotating the screen horizontally on mobile devices
function ready() {
const { type } = screen.orientation;
console.log(`Fullscreen and locked to ${type}. Ready!`);
}
async function start() {
await document.body.requestFullscreen();
await screen.orientation.lock("landscape");
ready();
}