-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
184 lines (158 loc) · 4.96 KB
/
game.js
File metadata and controls
184 lines (158 loc) · 4.96 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
const carousel = document.querySelectorAll(".carousel")
const button = document.querySelector("#button")
// set spin variables
i = 0
let increment = 0
const min = 20
const max = 80
// prepare variables for final slot images
var finalImages = []
var imageTitles = []
// set bonus variables
const bonusImages = ["sign.png", "sign.png", "sign.png", "jellybean.png",]
const bonusElemArray = document.querySelectorAll(".bonus-images")
var clicks = 3
// set scoring
var score = 0
const ROB_LAURA = 30
const SALLY_BUDDY = 15
const LOGO = 100
const TWO_LOGOS = 50
const ONE_LOGO = 5
/******************* START GAME LOGIC *************************/
button.addEventListener("click", function() {
button.style.display="none";
var buttonText = this.textContent;
if (buttonText == "SPIN") {
carousel.forEach(function(item){
spin(item);
})
} else {
resumeMainGame();
}
})
function resumeMainGame(){
// hide bonus images
bonusElemArray.forEach(function(item){
item.style.display = "none";
item.classList.remove("revealed-images");
});
document.getElementById("bonus-text").style.display="none";
document.getElementById("bonus-container").style.display="none";
document.getElementById("spin-text").style.display="block";
document.getElementsByClassName("container")[0].style.display="block";
document.getElementById("button").style.display="inline-block"
document.getElementById("button").innerText="SPIN";
}
/******************* SPIN LOGIC *************************/
function spin(item) {
var intervalId = setInterval(function(){
if(i > spinAmount()){
clearInterval(intervalId);
// console.log(i)
i = 0;
} else {
i++;
increment++;
$(item).css({
"-webkit-transform": "rotateX(" + (increment * -60) + "deg)"
})
// keep only front image in focus
$(item).attr("data-state", (increment % 6) + 1);
}
}, 50);
// wait for image positions to finalize
setTimeout(function(){
finalImages.push(item);
if (finalImages.length === 3){
getImageTitles(finalImages);
}
}, 3500);
}
// choose random number for each wheel spin
function spinAmount(){
// randomNumber = Math.round(Math.random() * 10 + 5)
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/******************* SCORING LOGIC *************************/
function getImageTitles(resultingImages){
for (i = 0; i < 3; i++){
var imageObject = Object.values(resultingImages)[i];
var dataState = imageObject.getAttribute('data-state') - 1;
var image = imageObject.children[dataState];
var imageTitle = image.innerHTML.split('-')[1].split('.')[0];
imageTitles.push(imageTitle);
}
getScore();
}
function getScore(){
var robTotal = countResults(imageTitles, "rob");
var lauraTotal = countResults(imageTitles, "laura");
var sallyTotal = countResults(imageTitles, "sally");
var buddyTotal = countResults(imageTitles, "buddy");
var logoTotal = countResults(imageTitles, "logo");
if (robTotal == 3 || lauraTotal == 3){
score += ROB_LAURA;
} else if (sallyTotal == 3 || buddyTotal == 3){
score += SALLY_BUDDY;
} else if (logoTotal == 3){
score += LOGO;
setTimeout(function(){
bonusGame();
}, 1500);
} else if (logoTotal == 2){
score += TWO_LOGOS;
} else if (logoTotal == 1){
score += ONE_LOGO;
} else {
;
}
document.getElementById("score").innerText = "Score: " + score;
finalImages = [];
imageTitles = [];
button.style.display="inline-block";
}
function countResults(array, value) {
var count = 0;
array.forEach((v) => (v === value && count++));
return count;
}
/******************* BONUS GAME *************************/
function bonusGame(){
document.getElementsByClassName("container")[0].style.display="none"
document.getElementById("spin-text").style.display="none";
document.getElementById("bonus-text").style.display="block";
document.getElementById("bonus-container").style.display="block";
bonusElemArray.forEach(function(item){
item.src = "images/jellybean-cover.png";
item.style.display = "inline-block";
item.addEventListener("click", clickHandler);
});
}
function clickHandler(){
if (clicks > 1) {
revealBonusImage(this);
displayRemainingChances();
} else {
revealBonusImage(this);
clicks = 3;
document.getElementById("button").innerText="RETURN";
document.getElementById("button").style.display="inline-block";
}
}
function revealBonusImage(elem) {
var randomBonusImage = bonusImages[Math.floor(Math.random() * bonusImages.length)];
elem.src = "images/" + randomBonusImage;
elem.classList.add("revealed-images");
if (elem.src.indexOf("jellybean") != -1) {
score += 500;
document.getElementById("score").innerText = "Score: " + score;
}
clicks--;
elem.removeEventListener("click", clickHandler);
return
}
function displayRemainingChances(){
document.getElementById("bonus-text").innerText = clicks + " chances left!";
return
}